In this article, you will see how to use the YOLO (You only look once) algorithm to detect objects within images in the following ways:
- Detecting Objects with Names and Percentage Probabilities
- Detecting Objects Without Names and Percentage Probabilities
- Detecting Objects with Minimum Percentage Probability
- Returning NumPy Arrays for Images
- Save Detected Objects as Separate Images
The YOLO is a deep learning-based algorithm that learns to identify objects within images using a single training pass of a neural network. The algorithm first learns to break the image into multiple smaller regions and then identify objects within those regions.
You can implement the YOLO algorithm from scratch. However, it takes a considerable effort. A simple alternative is to use the YOLO implementation from the ImageAI library, which allows you to implement the YOLO algorithm in a few lines of code.
The process to install the ImageAI library is available at the official documentation link.
https://imageai.readthedocs.io/en/latest/index.html
Let’s start detecting objects within images without further ado.
Detecting Objects with Names and Percentage Probabilities
By default, the YOLO algorithm from the ImageAI library detects objects within images with object names and percentage probabilities of the objects.
To do so, you first need to create an object of the ObjectDetection class from the image.ai.Detection module.
The ImageAI library contains several object-detection models. To use the YOLO model, you need to call the setModelTypeAsYOLOv3() method which tells the ObjectDetection class to use the YOLO model for object detection.
The script below performs these steps:
from imageai.Detection import ObjectDetection object_detector = ObjectDetection() object_detector.setModelTypeAsYOLOv3()
The next step is to pass the pre-trained YOLO model for object detection to the setModelPath() method of the ObjectDetection class object. You can download the pre-trained YOLO model from this Github link.
Download the file “yolo.h5” model for the complete YOLO model. You can also use the tiny YOLO model, for which you need to download the “yolo-tiny.h5” file.
Finally, you can load the model by calling the loadModel() method from the ObjectDetection class object.
object_detector.setModelPath(r"/home/manimalik/Datasets/yolo.h5") object_detector.loadModel()
Finally, to detect objects from images, you need to call the detectObjectsFromImage() method from the ObjectDetection class object. You need to pass the path to the input image to the input_image parameter, and the path to the labeled output image containing detected objects to the output_image_path parameter. Here is the script to do that.
detected_objects = object_detector.detectObjectsFromImage( input_image =r"/home/manimalik/Datasets/source_image.jpg", output_image_path =r"/home/manimalik/Datasets/labelled_image.jpg" )
For the example in this article, I used the following image:

The output image with detected objects looks like this. You can see the detected images along with their names and the percentage probability of detection.
The detectObjectsFromImage() method returns a list of dictionaries that contain object names, along with the percentage probabilities and the bounding box coordinates.
The following script prints the list of detected objects along with their percentage probabilities and bounding box coordinates.
for obj in detected_objects: print(obj["name"], "-" , obj["percentage_probability"], "-" , obj["box_points"])
Output:
car - 99.3631899356842 - [81, 207, 338, 330] person - 99.98153448104858 - [325, 102, 521, 433] car - 99.79725480079651 - [0, 204, 276, 373] bicycle - 99.92812275886536 - [238, 253, 607, 475] backpack - 89.0583336353302 - [339, 131, 432, 204] person - 60.788047313690186 - [178, 212, 207, 240] car - 94.16099786758423 - [681, 260, 744, 301]
Detecting Objects Without Names and Percentage Probabilities
If you do not want the names and percentage probabilities of detected objects, you can set False as the value for the display_object_name and display_percentage_probability parameters, respectively.
Here is an example:
detected_objects = object_detector.detectObjectsFromImage( input_image =r"/home/manimalik/Datasets/source_image.jpg", output_image_path =r"/home/manimalik/Datasets/labelled_image.jpg", display_percentage_probability = False, display_object_name = False )
Output:
Detecting Objects with Minimum Percentage Probability
If you want to detect objects with a minimum percentage probability threshold only, you can pass the threshold value to the minimum_percentage_probability parameter of the detectObjectsFromImage class.
For example, the following script detects images with a minimum percentage probability of 90%.
The names, percentage probabilities, and the bounding boxes for the detected objects are printed in the output.
detected_objects = object_detector.detectObjectsFromImage( input_image =r"/home/manimalik/Datasets/source_image.jpg", output_image_path =r"/home/manimalik/Datasets/labelled_image.jpg", minimum_percentage_probability = 90 ) for obj in detected_objects: print(obj["name"], "-" , obj["percentage_probability"], "-" , obj["box_points"])
Output:
1/1 [==============================] - 1s 886ms/step car - 99.3631899356842 - [81, 207, 338, 330] person - 99.98153448104858 - [325, 102, 521, 433] car - 99.79725480079651 - [0, 204, 276, 373] bicycle - 99.92812275886536 - [238, 253, 607, 475] car - 94.16099786758423 - [681, 260, 744, 301]
You can see from the output that only images with a percentage probability greater than 90% are detected.
Returning NumPy Arrays for Images
The detectObjectsFromImage() method can return a NumPy array as well. You can use the NumPy array for several image processing tasks e.g. training deep learning models, image segmentation, etc.
To return the NumPy arrays, you need to pass the string type parameter value “array” to the output_type parameter of the detectObjectsFromImage() method.
detected_objects = object_detector.detectObjectsFromImage( input_image =r"/home/manimalik/Datasets/source_image.jpg", output_image_path =r"/home/manimalik/Datasets/labelled_image.jpg", output_type = "array" )
Let’s print the type of values returned by the detectObjectsFromImage() when you want it to return the NumPy array for the input image.
print(type(detected_objects)) print(type(detected_objects[0])) print(type(detected_objects[1]))
Output:
<class 'tuple'> <class 'numpy.ndarray'> <class 'list'>
From the output, you can see that the detectObjectsFromImage() method returns a tuple of two values. The first value contains the NumPy array for the image, and the second value is the list of dictionaries containing information about detected objects.
You can print the NumPy array for the image containing detected objects as follows:
print(detected_objects[0])
Here is a partial snippet for the output NumPy array:
Save Detected Objects as Separate Images
You can also separately save different objects detected in the input image. To do so, you need to pass True as the value for the extract_detected_objects attribute.
Here is an example:
detected_objects = object_detector.detectObjectsFromImage( input_image =r"/home/manimalik/Datasets/source_image.jpg", output_image_path =r"/home/manimalik/Datasets/labelled_image.jpg", extract_detected_objects = True )
The above script will create a folder “labelled_image.jpg-objects” in the path specified by output_image_path parameter of the detectObjectsFromImage() method. If you open the folder, you will see multiple images corresponding to various objects detected inside the input image. Some of the images for detected objects are displayed below.
When you pass True as the value for the extract_detected_objects parameter, the detectObjectFromImage() method returns a tuple of two values.
The first value contains the list of dictionaries containing information about the detected object. The second value corresponds to the paths to all the objects detected inside the input image.
Let’s print the values for the returned tuple, along with its first and second items to confirm this.
print(type(detected_objects)) print(type(detected_objects[0])) print(type(detected_objects[1]))
Output:
<class 'tuple'> <class 'list'> <class 'list'>
You can iterate through the list of paths for images containing detected objects using the following script.
for img_path in detected_objects[1]: print(img_path)
Output:
/home/manimalik/Datasets/labelled_image.jpg-objects/car-1.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/person-2.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/car-3.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/bicycle-4.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/backpack-5.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/person-6.jpg /home/manimalik/Datasets/labelled_image.jpg-objects/car-7.jpg
Final Thoughts
YOLO is a very powerful object detection algorithm for detected objects. The ImageAI library makes it extremely easy to implement the YOLO algorithm in a few lines of code. Though you can use the pre-trained YOLO algorithm for the detection of the majority of objects, you can also train it on your custom dataset to detect objects not supported by YOLO by default.