Predictive Hacks

Object Detection and Bounding Boxes with cvlib

object detection with bounding boxes

In this post, we will provide you an example of object detection with bounding boxes in Python using the cvlib library which is a simple, high-level, easy-to-use open-source Computer Vision library for Python.

How to install cvlib library

You can pip install cvlib provided that you have already installed the OpenCV and the Tensorflow, otherwise you can pip install as follows:

pip install opencv-python tensorflow

pip install cvlib

Object Detection

cvlib library has a function called detect_common_objects() which returns the detected labels, the bounding box co-ordinates and the confidence scores for the detected objects.

Let’s create a function that takes as input the filename of the image, the pre-trained model, where in our case will be the YOLOv3 and the confidence and will return the labels of the detected objects by generating a new image with the bounding boxes.

import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox


def object_detection_with_bounding_boxes(filename, model="yolov3", confidence=0.2):

    
    # Read the image into a numpy array
    img = cv2.imread(filename)
    
    # Perform the object detection
    bbox, label, conf = cv.detect_common_objects(img, confidence=confidence, model=model)
    
    # Print current image's filename
    print(f"========================\nImage processed: {filename}\n")
    
    # Print detected objects with confidence level
    for l, c in zip(label, conf):
        print(f"Detected object: {l} with confidence level of {c}\n")
    
    # Create a new image that includes the bounding boxes
    output_image = draw_bbox(img, bbox, label, conf)
    
    # Save the image in the directory images_with_boxes
    cv2.imwrite(f'images_with_boxes/{filename}', output_image)
    
    # Display the image with bounding boxes
    display(Image(f'images_with_boxes/{filename}'))

Let’s try the following image obtained from Unsplash where we have saved it under our working directory as my_image.jpg.

man standing in front of people sitting beside table with laptop computers
object_detection_with_bounding_boxes("my_image.jpg")

Output:

========================
Image processed: my_image.jpg

Detected object: laptop with confidence level of 0.7656973004341125

Detected object: person with confidence level of 0.6280566453933716

Detected object: person with confidence level of 0.5862714052200317

Detected object: laptop with confidence level of 0.5759404301643372

Detected object: person with confidence level of 0.5740895867347717

Detected object: person with confidence level of 0.4282369017601013

Detected object: person with confidence level of 0.3663652539253235

Detected object: laptop with confidence level of 0.36039528250694275

Detected object: chair with confidence level of 0.339595764875412

Detected object: tv with confidence level of 0.3152497410774231

Detected object: chair with confidence level of 0.3094286322593689

Detected object: laptop with confidence level of 0.26004040241241455

Detected object: keyboard with confidence level of 0.22592854499816895

And the output image:

Discussion

As we can see, we managed to get most of the objects, like laptop(s), person(s), keyboard, chair and tv. We can decrease the confidence if we want the algorithm to be less strict by returning more detected objects but with a higher probability of false-positive cases.

If you liked this article you may find interesting the other related tutorials:

References:

[1] Coursera: Introduction to Machine Learning in Production

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Image Captioning with HuggingFace

Image captioning with AI is a fascinating application of artificial intelligence (AI) that involves generating textual descriptions for images automatically.