How to Create a Simple Object Detection System with Python and ImageAI | by Ruben Winastwan | Towards Data Science

We all can agree that object detection is one of the most popular topics in Computer Vision. The problem is, creating an object detection system from scratch requires deep technical knowledge in Deep Learning and a huge amount of resources on your hardware.
If you look at the common object detection models like ResNet, YOLO, NASNet, or RCNN, they all have a deep stack of layers. It requires technical knowledge and also a huge amount of time to make them work. All of these factors would be a challenge for people who just want to implement object detection algorithms to solve their problems.
Fortunately, nowadays we can implement object detection algorithms in an easy way, even with just few lines of code. With ImageAI library, we can create our image or video object detection system in a quick and easy way. I’ll show you how in this article.
Below are the steps on what we’re going to do in this article:
Introduction
Creating image object detection
Creating video object detection
So, let’s get started right away.
As previously mentioned, we’re going to create an image and a video object detection system with the help of ImageAI. ImageAI is a Python library to enable ML practitioners to build an object detection system with only a few lines of code.
Before we start, we need to install some of the dependencies that we will need to run ImageAI properly. These dependencies are:
Numpy
pip install numpy==1.16.1
TensorFlow
pip install tensorflow==1.14.0
TensorFlow GPU
pip install tensorflow-gpu==1.14.0
Keras
pip install keras==2.2.4
OpenCV
pip install opencv-python
After installing all of those libraries, then we can start to install ImageAI library by typing the following command in your prompt:
pip install imageai --upgrade
Next, we are ready to build our object detection system for image and for video.
To start creating an image object detection system, first let’s import the libraries that we’re going to use and also set our current working directory.
from imageai.Detection import ObjectDetectionimport oscurrent_directory = os.getcwd()
As we want to implement an object detection in an easy and quick way, we will use a pretrained model specific for object detection that has been trained on COCO dataset.
There are three different pretrained models that you can choose with ImageAI: RetinaNet, YOLOv3, and tinyYOLOv3. You can download the model of your choice here.
After you’ve downloaded the model, place the h5 file in the same directory as your Python script. Your working directory should now has the following structure.
root_folder/   python_script.py   pretrain_model.h5
Back to our Python script, we now can instantiate the ObjectDetection class that we have imported before. Depending on the model that you have downloaded before, we need to call a proper method from ObjectDetection class. Below is the code implementation for that.
detector = ObjectDetection()# if you use RetinaNet model, call the following methoddetector.setModelTypeAsRetinaNet()# if you use YOLOv3 model, call the following methoddetector.setModelTypeAsYOLOv3()# if you use tinyYOLOv3 model, call the following methoddetector.setModelTypeAsTinyYOLOv3()
Next, we can start to load the model by first specifying the path to our model. Since our model is in the same directory as our Python script, here I show you how to load the RetinaNet model. If you use YOLOv3 or tinyYOLOv3, you need to change the file name of your h5 file accordingly.
detector.setModelPath(os.path.join(current_directory , "resnet50_coco_best_v2.0.1.h5"))detector.loadModel()
Finally, we can start to create an image object detection system. To do this, we need to specify two things: First, the directory and the filename of our input image and second, the directory and the filename of the output image.
In the following code implementation, the input image will be an image called ‘traffic.jpg’ that is located in the same directory as the Python script. Meanwhile, the detection result will be saved in a file called ‘traffic_detected.jpg’ in the same directory.
detections = detector.detectObjectsFromImage(input_image = os.path.join(current_directory, "traffic.jpg"), output_image_path = os.path.join(current_directory , "traffic_detected.jpg"))for eachObject in detections:    print(         eachObject["name"] , " : ",         eachObject["percentage_probability"], " : ",         eachObject["box_points"] )    print("--------------------------------")
And that’s the code that we need to instantiate our image object detection system. Below is the full code implementation of the steps that we have covered above:
If you run the complete code above, you’ll get more or less similar result on the image of your choice.
If you use the default value that we have seen in the implementation above, you might think that somehow the result of the object detection is overcrowded, with several bounding boxes overlapping one another.
In order to reduce the clutter in the prediction result, you can tune the object detector such that it only shows the object that really matters for you.
Let’s use the image above as an example. Let’s say I want the object detector to predict only the people and the bicycle. To do this, we need to instantiate CustomObjects method. Then, we pass the name of the objects that we want the system to detect as the argument.
custom = detector.CustomObjects(person=True, bicycle=True)
Next, we can start to build the object detection system with detectCustomObjectsFromImage method. We pass our custom variable, the path and name of our input image, as well as the path and name of our output image.
detections = detector.detectCustomObjectsFromImage(custom_objects = custom,input_image = os.path.join(current_directory, "traffic.jpg"), output_image_path = os.path.join(current_directory , "traffic_detected.jpg"))
Moreover, we can also further remove the clutter by ignoring the predictions that have probability values below a certain threshold value. Let’s say that you want to ignore the predictions with probability value below 70%. You can do that by adding the minimum_percentage_probability argument in the detectCustomObjectsFromImage method.
detections = detector.detectCustomObjectsFromImage(custom_objects = custom,input_image = os.path.join(current_directory, "traffic.jpg"), output_image_path = os.path.join(current_directory , "traffic_detected.jpg"),minimum_percentage_probability = 70)
Below is the complete code implementation when we want to detect only people and bicycle from our image, and we also only want to show the detection where the probability value is above 70%.
If you run the code above, you’ll get more or less the result like this:
And that’s it! With just a few lines of code now you can implement an object detection system for your own image. However, what if you want to detect objects in a video or even from your webcam instead of an image? Let’s find out how to create a similar object detection for video with ImageAI in the next section.
Believe it or not, the code to create a video object detection system with ImageAI is pretty much similar with the image object detection system we’ve built before. All we need to do is changing 3 lines of code.
The first one is the library that we should import. So instead of ObjectDetection , we need to import VideoObjectDetection .
from imageai.Detection import VideoObjectDetection
The second change that we should apply is the step where we instantiate object detection class. Instead of using ObjectDetection() , we should use VideoObjectDetection() .
detector = VideoObjectDetection()
Next, the third or the final change that we should apply is when we create the object detection system. In our previous code, we use detectObjectsFromImage method. If we want to detect objects from a video, we need to use detectObjectsFromVideo instead.
detections = detector.detectObjectsFromVideo( input_file_path=os.path.join(current_directory, "footage.mp4"), output_file_path=os.path.join(current_directory,"footage_detected"))
As you can see, the argument that we need to pass into this method is still the same as before. First, we need to specify the path to our video directory and the filename of our video. Second, we also need to specify the path and the filename of the output video.
Below is the entire code implementation to create a video object detection system.
Now if you run the code implementation above, you’ll get more or less similar result as below.
Note: if you somehow don’t get the same color format in your output video, i.e you get the output video that is in BGR format instead of RGB format, you can use the code below to convert the output video back to RGB format.
All you need to do is specifying the path and the filename of the video that you want to convert as well as the converted video.
Same as our image object detection system, we can also fine tune our video object detection system a little bit. We have the option to pick which objects that we want to detect and to select the threshold for the probability value that should be displayed.
Let’s say we want to only detect people and bicycle for our video object detection system. Also, we only want to show the detections that have the probability value above 70%. We can do so by first instantiating a variable to store the objects that we want to observe.
custom = detector.CustomObjects(person=True, bicycle=True)
Next, we can create our video object detection system with our custom objects. To do so, we call the detectCustomObjectsFromVideo method. For the arguments of this method, we pass our custom variable, path and filename of our input and output video, frames per second, as well as the minimum threshold for probability value.
detections = detector.detectCustomObjectsFromVideo( custom_objects = custom,  input_file_path=os.path.join(execution_path, "footage.mp4"), output_file_path=os.path.join(execution_path, "footage_detected"), frames_per_second=20, log_progress=True, minimum_percentage_probability = 70)
Below is the complete code implementation if you want to only detect people and bicycle which has a probability value above 70%.
Now what if you want to create an object detection system with your camera feeds as your input? Creating this system with ImageAI is also very straightforward. The code itself is very much similar with our video object detection system. You only need to add one line of code and also change one line of code to do this.
First, we need to create a variable to instantiate the OpenCV library to capture the frame directly from our webcam.
camera = cv2.VideoCapture(0)
Finally, we need to change the argument in detectObjectsFromVideo method. Instead of specifying the input file path, we need to specify our camera input, which is the camera variable that we’ve created above.
detections = detector.detectObjectsFromVideo(             camera_input = camera,             output_file_path =        os.path.join(current_directory,"camera_detected_video"),             frames_per_second=20, log_progress=True)
Below is the complete code implementation to create an object detection system directly from your webcam.
Again, if you somehow get the a different color format in your output video, you can use the aforementioned code to convert the output from BGR to RGB format.
Now you already know how to create a quick and easy object detection system with ImageAI. As you have seen, ImageAI library enables us to build an object detection system without having to deal with the complexity behind object detection model like ResNet or YOLO.
Note that with the pretrained model supported by ImageAI, the object detector can detect 80 different objects. These objects are:
person, bicycle, car, motorcycle, airplane, bus, train, truck, boat, traffic light, fire hydrant, stop_sign, parking meter, bench,   bird, cat, dog, horse, sheep, cow, elephant, bear,zebra, giraffe,   backpack, umbrella, handbag, tie, suitcase, frisbee, skis,   snowboard, sports ball, kite, baseball bat, baseball glove,   skateboard, surfboard, tennis racket, bottle, wine glass, cup,   fork, knife, spoon, bowl, banana, apple, sandwich, orange, broccoli,   carrot, hot dog, pizza, donot, cake, chair, couch, potted plant,   bed, dining table, toilet, tv, laptop, mouse, remote, keyboard,   cell phone, microwave, oven, toaster, sink, refrigerator, book,   clock, vase, scissors, teddy bear, hair dryer, toothbrush.
Now the question is, what if you want to create a custom object detection system with your own label that is not included in the list above?
You can create your own custom object detection with ImageAI as well. I will show you how to do so in another post.
That’s all for now and I hope that this article is somehow useful for you.