Skip to content

[Machine Learning][Python] The simplest object recognition package: ImageAI

Last Updated on 2021-08-01 by Clay

It needs to be stated in advance that the teaching of this article is learning from: https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606. You can also go directly to that website to learn.

In addition, here is the developer's Github: https://github.com/OlafenwaMoses/ImageAI?source=post_page-----d6cb4d86f606---------------------- , and I have offered my star in admiration =)

Since this tool is really useful, I wrote this article to promote it.

Basically as advertised in the original text, as long as the basic related packages are installed, the code to call ImageAI for image recognition is almost less than 10 lines.

And the best thing about this kit is that its computing speed is very, very fast. In the past, I was impressed by the classic models such as Yolov3 and SSD in the object recognition part of image processing. Of course, these models must use the GPU, and only use the speed of the CPU. Oh my God, it is simply unimaginable.

However, ImageAI only uses CPU computing but it is fast and very fast. (Of course, it may be because we directly use their trained models, and there is another chapter in it later that tells us that if we want to retrain their models for object recognition, we still need to use GPU, and I seem to have seen Yolov3 XD)

So, the following begins to explain how I use this convenient package.


Install Dependencies

It should be noted in advance that the version of Tensorflow installed with 1.4 (including later) is better and does not need to be upgraded to 2.0, but the developer has mentioned that 2.0 supports will coming soon, which is worth looking forward to.

sudo pip3 install opencv-python
sudo pip3 install pillow
sudo pip3 install numpy
sudo pip3 install scipy
sudo pip3 install matplotlib
sudo pip3 install h5py
sudo pip3 install tensorflow==1.14
sudo pip3 install keras

Finally, of course, install the most important:

sudo pip3 install imageai

If there is no way to run the ImageAI suite, please go to the developer's Github or the teaching article on Medium to confirm the version, I think it should be able to solve the problem smoothly.

In addition, we have to download the pre-trained model provided by the developer. Save the model where the program will run.

In order to test the effect of the model, I do not plan to use the image provided by the developer. I just found this image on Google.

I also saved this picture in the same folder as the code, named test.jpg


Program

# -*- coding: utf-8 -*-
import os
from imageai.Detection import ObjectDetection

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath('resnet50_coco_best_v2.0.1.h5')
detector.loadModel()

detections = detector.detectObjectsFromImage(input_image='test.jpg',
                                             output_image_path='test_new.jpg')

for eachObject in detections:
    print('{}:{}'.format(eachObject['name'], eachObject['percentage_probability']))

​​

Output:

 car:54.42100763320923
 car:61.10377311706543
 person:58.36105942726135
 motorcycle:54.96629476547241
 motorcycle:50.44354200363159
 person:57.037585973739624
 bus:66.54394865036011
 car:65.67930579185486
 car:54.16930913925171
 car:56.64970874786377
 car:72.12932705879211
 car:68.234783411026
 car:64.10300731658936
 car:58.10954570770264
 car:63.097649812698364
 car:53.60064506530762
 car:64.27150964736938
 car:66.74740314483643
 car:58.199405670166016
 car:58.271121978759766
 person:63.879597187042236
 person:59.48329567909241
 car:76.3867735862732
 car:80.26036024093628
 car:84.88558530807495
 car:81.44459128379822
 car:86.51140332221985
 person:54.11458611488342
 car:96.40277624130249
 person:50.599128007888794
 motorcycle:54.92902398109436
 person:66.21726155281067
 car:92.68029928207397
 car:95.23538947105408
 car:67.5695538520813
 car:89.36827182769775
 car:97.36456871032715
 car:99.09504652023315

Before:

After:

the speed is very fast! The code is also quite streamlined. I strongly recommend this RetinaNet encapsulated Python Package to developers who have such needs.

Leave a Reply