← Back to list

Object Detection and Bounding Boxes with Cvlib

An example of object detection with bounding boxes in Python using the cvlib library

George Pipis in Level Up Coding · 2021-06-27 09:31 · 225 claps · 2.5 min read paywalled
#object-detection #python #cvlib #opencv #bounding-box
Open on Medium ↗
Wiki topics: 📚 · Books & Reading

Object Detection and Bounding Boxes with Cvlib

[embed]Join Medium with my referral link - George Pipis Read every story from George Pipis (and thousands of other writers on Medium). Your membership fee directly supports…jorgepit-14189.medium.com

An example of object detection with bounding boxes in Python using the cvlib library

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.

Unsplash

Unsplash

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

Originally posted at Predictive Hacks


메타데이터
post_id
6340ebefd3eb
slug
object-detection-and-bounding-boxes-with-cvlib-6340ebefd3eb
url
https://levelup.gitconnected.com/object-detection-and-bounding-boxes-with-cvlib-6340ebefd3eb
canonical_url
https://levelup.gitconnected.com/object-detection-and-bounding-boxes-with-cvlib-6340ebefd3eb
author_url
https://medium.com/@jorgepit-14189
status
ok
fetched_at
2026-07-28 04:57:20