Artificial Intelihence

Object Detection with 10 lines of code

Object Detection with 10 lines of code

To perform object detection using ImageAI, all you need to do is

  1. Install Python on your computer system
  2. Install ImageAI and its dependencies

3. Download the Object Detection model file

4. Run the sample codes (which is as few as 10 lines)

Now let’s get started.

1) Download and install Python 3 from official Python Language website

2) Install the following dependencies via pip:
i. TensorFlow

pip3 install tensorflow

ii. OpenCV

pip3 install opencv-python

iii. Keras

pip3 install keras

iv. ImageAI

pip3 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,, 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.

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelPath( os.path.join(execution_path , “resnet50_coco_best_v2.0.1.h5”))
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”] )

Open chat
Need Help ?
Can I help You