Object detection using deep learning library python — Part-I

Implementing Object Detection with cv2 and CVlib in Python

Jimmy
2 min readFeb 23, 2023

Deep learning is a rapidly evolving field that has shown tremendous progress in computer vision applications such as object detection, recognition, and segmentation. In this article, we will explore how to implement object detection using Python, OpenCV, and cvlib, a simple library for deep learning-based image processing tasks. Object detection has various applications, from detecting and tracking moving objects in videos to detecting faces or vehicles in images. With the help of cvlib, we can implement object detection easily and efficiently. So, let’s dive into the details of how to implement object detection using cvlib in Python.

CVlib is a powerful and user-friendly object detection library that uses OpenCV and Tensorflow at its core. By using the ‘detect_common_objects’ function, you can detect objects in an image, which is in numpy array format, and retrieve useful information like bounding box coordinates, labels, and confidence scores. The output of the function includes:

bbox: list of list containing bounding box coordinates for detected objects.

Example: [[32, 76, 128, 192], [130, 83, 220, 185]]

label: list of labels for detected objects.

Example: [‘apple’, ‘apple’]

conf: list of confidence scores for detected objects.

Example: [0.6187325716018677, 0.42835739254951477]”

Let’s write a very simple code here:

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

img = cv2.imread(f"images/apple.jpg")

bbox, lable, conf = cv.detect_common_objects(img)

img1 = draw_bbox(img, bbox, lable, conf)

cv2.imwrite('apple_with_boundary.jpg', img1)

First, the code imports the necessary libraries — cv2, cv, and draw_bbox from cvlib.object_detection.

Next, the code loads an image of an apple (which is stored in folder named ‘images’) using the cv2.imread() function and stores it in the img variable.

The cv.detect_common_objects() function is then called with img as its argument to detect the common objects in the image. The function returns three values - bbox, lable, and conf as mentioned above.

The draw_bbox() function from cvlib.object_detection is then called with the img, bbox, lable, and conf variables as arguments to draw bounding boxes around the detected objects in the image. The resulting image is stored in the img1 variable.

Finally, the cv2.imwrite() function is used to save the resulting image as a file named 'apple_with_boundary.jpg'.

That’s it… The object detection process for the given image has been completed and the results have been stored in the local file system.

Stay tuned for our upcoming post where we will dive deeper into this topic and explore it further…

--

--