Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence / machine-learning

Installing OpenCV and ImageAI for Object Detection

5.00/5 (9 votes)
4 Jun 2020CPOL4 min read 35.8K   293  
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:

Python
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:

Python
conda activate ImageAI
pip install opencv-python==4.1.2.30 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:

Python
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:

Python
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:

Python
url = 'https://p7.hiclipart.com/preview/124/937/193/architectural-engineering-engineer.jpg'
r = req.get(url)
with open('testimage.jpg', 'wb') as outfile:
    outfile.write(r.content)

Image 5

This code uses the requests library to grab the image from hiclipart.com 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)
cv.waitKey(0)
cv.destroyAllWindows()

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)
    cv.waitKey(0)
    cv.destroyAllWindows()

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.

License

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