Click here to Skip to main content
15,902,275 members
Articles / Artificial Intelligence / Machine Learning

Installing OpenCV and ImageAI for Object Detection

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
4 Jun 2020CPOL4 min read 33K   280   16   7
In this article, we'll set up everything we need to build a hardhat detector with OpenCV.
Here we'll be looking at: Setting up a computer vision development environment, loading ImageAI and OpenCV, setting up a notebook in Jupyter, and testing OpenCV.

In this series, we’ll learn how to use Python, OpenCV (an open source computer vision library), and ImageAI (a deep learning library for vision) to train AI to detect whether workers are wearing hardhats. In the process, we’ll create an end-to-end solution you can use in real life—this isn’t just an academic exercise!

This is an important use case because many companies must ensure workers have the proper safety equipment. But what we’ll learn is useful beyond just detecting hardhats. By the end of the series, you’ll be able to use AI to detect nearly any kind of object in an image or video stream.

You’re currently on article 1 of 6:

  1. Installing OpenCV and ImageAI for Object Detection
  2. Finding Training Data for OpenCV and ImageAI Object Detection
  3. Using Pre-trained Models to Detect Objects With OpenCV and ImageAI
  4. Preparing Images for Object Detection With OpenCV and ImageAI
  5. Training a Custom Model With OpenCV and ImageAI
  6. Detecting Custom Model Objects with OpenCV and ImageAI

1. Installing OpenCV and ImageAI for Object Detection

Before we start using computer vision to improve workplace safety, we’ll need to install the necessary tools: OpenCV and ImageAI.

OpenCV is an open-source computer vision library with C++, Python, Java, and MATLAB interfaces. ImageAI is a machine learning library that simplifies AI training and object detection in images. These two libraries make it extremely easy to solve a number of object detection problems in images and videos. We’re going to dive straight into our solution by setting these libraries up using Python in a Jupyter Notebook (on Windows).

Setting Up a Computer Vision Development Environment

I’m going to assume you already have the Anaconda Python data science platform installed (you can get the personal edition here) and configured using basic, default settings.

To create a Jupyter Notebook to start writing our detector, we must install specific versions of OpenCV, Tensorflow, Keras, and ImageAI using Anaconda. Find and run the Anaconda command prompt from the start menu and enter the following command:

conda create -n ImageAI -c anaconda keras=2.3.1 tensorflow=1.15.0 tensorflow-gpu=1.15.0 jupyter

Image 1

The first command installs Keras, TensorFlow (CPU and GPU versions), Jupyter, and all the prerequisites into a virtual environment. We’re choosing library versions based on the needs of ImageAI.

Using a virtual environment keeps these dependencies self-contained so they won't affect your global Python environment.

Loading ImageAI and OpenCV

Next, we’ll switch to the ImageAI environment and use pip to install OpenCV and ImageAI using the following commands:

conda activate ImageAI
pip install opencv-python== imageai

Image 2

We’re using the latest version of ImageAI, 2.1.5. If this changes, some of the prerequisites might also change.

We need to install one more item—the requests library—so we can use some specific HTML methods. Do this with the following command:

pip install requests

Image 3

Setting Up Our Notebook

Now let’s create a new notebook in Jupyter. Open the Anaconda explorer, start a new notebook —I called mine "Hard-Hat-Detector"—and add the following code block to initialize our libraries:

import cv2 as cv
from imageai.Detection import ObjectDetection as od

import numpy as np
import requests as req
import os as os

Image 4

The two key imports here are OpenCV (in the cv variable) and the detection component of ImageAI (in the od variable). The other three libraries are generic Python-specific libraries: numpy is used for large arrays and matrices; requests lets you work with HTTP requests, and os is used to work with operating system-specific functions.

Testing OpenCV

Now let's test to ensure that we can work with the OpenCV library. First, let’s grab a random image of a person with a hardhat to test getting data:

url = ''
r = req.get(url)
with open('testimage.jpg', 'wb') as outfile:

Image 5

This code uses the requests library to grab the image from and saves it as a file in the directory of the Jupyter Notebook. It uses two methods from the requests library:

  • .get(url) – retrieves the web content at a specific URL
  • .content – provides access to the raw content retrieved from the URL

We’re going to use a similar process in a moment to get training data for our detection model.

Now that we have a file downloaded, let's write some code to create a window using OpenCV, and then load the image and display it in the new window:

img = cv.imread('testimage.jpg')
window_name = 'image'
cv.imshow(window_name, img)

Image 6

Our code uses a number of basic methods associated with the OpenCV library:

  • .imread – reads an image from a file
  • .imshow – shows an image using an OpenCV window
  • .waitKey – pauses until a keypress is detected
  • .destroyAllWindows – closes any windows OpenCV created

When we run our code now, the person in a hardhat should be displayed in a new window. Pressing any key should close the window. We’re going to want to do this quite often, so let’s clear out this testing and change our code to use a display window function like this:

def showImage(img):
    window_name = 'image'
    cv.imshow(window_name, img)

Image 7

This allows us to show an image any time we want by passing it to the showImage function.

Up Next

And with that, we’ve set up everything we need to build a hardhat detector with OpenCV.

Next, we’ll see how to find a dataset to train and use for our AI model.

This article is part of the series 'Workplace Safety with OpenCV View All


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
Hi! I'm a Solution Architect, planning and designing systems based in Denver, Colorado. I also occasionally develop web applications and games, as well as write. My blog has articles, tutorials and general thoughts based on more than twenty years of misadventures in IT.

Comments and Discussions

QuestionObjectDetection Pin
Member 1495445027-Oct-20 12:48
Member 1495445027-Oct-20 12:48 
AnswerRe: ObjectDetection Pin
Ryan Peden27-Oct-20 12:52
professionalRyan Peden27-Oct-20 12:52 
Bugerror - ObjectDetection Pin
Member 1494071217-Sep-20 0:58
Member 1494071217-Sep-20 0:58 
Buggot an error Pin
Member 1016795117-Jun-20 17:47
Member 1016795117-Jun-20 17:47 
GeneralRe: got an error Pin
Sean Ewington19-Jun-20 8:57
staffSean Ewington19-Jun-20 8:57 
QuestionTypo in 2nd code block Pin
amSteve10-Jun-20 7:10
professionalamSteve10-Jun-20 7:10 
QuestionTypo in first command paste Pin
amSteve10-Jun-20 5:47
professionalamSteve10-Jun-20 5:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.