Object detection with ImageAI

--

Written by Richard

ImageAI object detection using Yolo3

In a previous blog about AutoKeras, I compared the results of object detection using AutoKeras with that of ImageAI. ImageAI demonstrated that it is more capable than AutoKeras.

The documentation for ImageAI is found at https://imageai.readthedocs.io/en/latest/. The source code can be downloaded from github.

I had some issues installing the required packages. I had the same issues experienced with other projects using Anaconda. Either opencv found no module name ‘cv2’ or some other issue causing the examples not to run.

In the end, creating a conda environment with python 3.6.7 from conda-forge then installing the packages in a single conda statement made things work.

conda create -n imageai -c conda-forge python=3.6conda activate imageaiconda install -c conda-forge tensorflow keras mkl-service opencv numpy scipy pillow matplotlib h5py

If you are using pip to install, the documentation shows how to install the necessary packages. Note that Python 3.5.1 is the minimum Python version. My installation used Python 3.6.7.

ImageAI can be installed without downloading the source code project. The project README.md describes the ways to do so. In my case, the source code project was used.

A few further steps were necessary to allow the examples to work:

  • Download and run the keras-yolov3 project from github as well as yolov3.weights file. This converts the Yolov3 weights from Darknet to Keras. Then the resulting file is copied to the source code project. Otherwise download the models needed at https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0/.
  • Edit the example source code to point to the appropriate directories where the images, videos and yolov3.h5 files are located in the project. These examples source code expect these files to be located in the examples subdirectory but the images and videos are not located there.
  • Configure Keras with the .keras/keras.json to use tensorflow.

ImageAI is an easy to use wrapper for deep learning image recognition frameworks SqueezeNet, ResNet, InceptionV3 and DenseNet. The documentation describes how to configure ImageAI to use these frameworks.

Likewise, ImageAI supports video object detection using pre-trained models trained on the Coco dataset. RetinaNet, YOLOv3, and TinyYOLOv3 can be downloaded and configured to be used by ImageNet.

ImageAI appears to do a good job detecting up to 80 different everyday objects using YOLOv3. However, it takes about 1 second to process a single image using YOLOv3 on my old MacBook with only CPU processing.

https://youtu.be/NjneiRRCHJw

As can be seen in the example video above, sometimes detected objects fall out in some frames. In images, a shadow of a person can fool the detector that another person exists where one does not. Likewise, the dog is also identified as a cat albeat with a lower probability. I find it interesting that the tricycle was identified with a low probability that it is a bicycle.

The same time constraints apply to video processing. It takes about one second for each frame of a video to be processed at normal speed, depending on the video frame size, and about 5 frames per second at fastest speed using YOLOv3. On my MacBook, it is not performant enough to perform video detection real-time. The example video detection processes each frame, producing a copy of the video with the bounding boxes. Two of the examples show how analysis of the processed frames can be included at different analysis intervals.

Although video detection on my MacBook is not fast enough for real-time processing, using TinyYOLOv3 using the fastest speed setting comes very close to 10 frames per second. However, using the fastest speed setting results in less accurate detection. A more powerful computer with GPUs available for processing would certainly be able to perform real-time detection.

Here is the example code for image object detection:

from imageai.Detection import ObjectDetection
import os
from time import time
models_path = "".join((os.getcwd().rstrip("examples"), "models"))
image_path = "".join((os.getcwd().rstrip("examples"), "images"))
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(models_path , "yolo.h5"))
detector.loadModel()
our_time = time()
detections = detector.detectObjectsFromImage(input_image=os.path.join(image_path , "image3.jpg"), output_image_path=os.path.join(image_path , "image3new.jpg"), minimum_percentage_probability=30)
print("IT TOOK : ", time() - our_time)for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("--------------------------------")

Of course processing a video takes a bit more code. Download ImageAI and take a look at the example code that includes video analysis.

I am quite impressed with ImageAI for object detection. A great deal of work can be done with only a little bit of code.

--

--

Emerging technologies & challenges
Emerging technologies & challenges

Written by Emerging technologies & challenges

Thoughts about emerging technologies and some of the challenges related to them. The technology itself usually is not the problem (or the solution).

No responses yet