Object detection using ImageAI, all you need to do is
- Install Python on your computer system
- Install ImageAI and its dependencies
- Download the Object Detection model file
- Run the sample codes (which is as few as 10 lines)
Now let’s get started.
1. download and install python on your pc.
2. Install ImageAI and dependencies
– Tensorflow
pip install tensorflow==2.4.0
– Others
pip install keras==2.4.3 numpy==1.19.3 pillow==7.0.0 scipy==1.4.1 h5py==2.10.0 matplotlib==3.3.2 opencv-python keras-resnet==0.2.0
Install the ImageAI library
pip install imageai --upgrade
3. Download the RetinaNet model file that will be used for object detection via this link.
Great. Now that you have installed the dependencies, you are ready to write your first object detection code. Create a Python file and give it a name (For example, FirstDetection.py), and then write the code below into it. Copy the RetinaNet model file and the image you want to detect to the folder that contains the python file.
FirstDetection.py
from imageai.Detection import ObjectDetection | |
import os | |
execution_path = os.getcwd() | |
detector = ObjectDetection() | |
detector.setModelTypeAsRetinaNet() | |
detector.setModelPath( os.path.join(execution_path , “resnet50_coco_best_v2.1.0.h5”)) | |
detector.loadModel() | |
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path,“image.jpg”), output_image_path=os.path.join(execution_path , “imagenew.jpg”)) | |
for eachObject in detections: | |
print(eachObject[“name”] , ” : “ , eachObject[“percentage_probability”] ) |
Then run the code and wait while the results prints in the console. Once the result is printed to the console, go to the folder in which your FirstDetection.py is and you will find a new image saved. Take a look at a 2 image samples below and the new images saved after detection.
Before Detection:
After Detection:
Console result for above image:
person : 55.8402955532074
person : 53.21805477142334
person : 69.25139427185059
person : 76.41745209693909
bicycle : 80.30363917350769
person : 83.58567953109741
person : 89.06581997871399
truck : 63.10953497886658
person : 69.82483863830566
person : 77.11606621742249
bus : 98.00949096679688
truck : 84.02870297431946
car : 71.98476791381836