Count Number of Object from an image using OpenCV in Python
In this tutorial, we learn how to count the number of objects in an image or video using OpenCV in Python.
What is OpenCV
OpenCV is a Python library that allows you to perform computer vision tasks. it provides the most popular features like Face recognition, Object Detection,etc.
What Is Object Detection
Object detection is a computer technology in image processing or computer vision to detect the object in an image or video.
Requirements library for Counting the Number of objects using OpenCV Python
Step 1. Installation of OpenCV library
pip install opencv-python
Step 2. Installation of matplotlib library
pip install matplotlib
Input

Code
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
image = cv2.imread('image/download.jpeg')
box,label,c_score=cv.detect_common_objects(image)
output=draw_bbox(image,box,label,c_score)
plt.imshow(output)
plt.show()
print('Number of object in this image:',label.count('car'))
Output

Number of object in this image: 9
Explanation
box,label,c_score=cv.detect_common_objects(image)
cv.detect_common_objects functions are used to detect common objects in the image.
image function is used to locate input variables that are collected in the cv2.imread function.
box,label,c_score are used to store the data.
output=draw_bbox(image,box,label,c_score)
draw_bbox to draw a box around the detected objects in an image.
Box coordinates (box), object labels (label), and confidence scores (c_score) These functions are takes the image as input and return an output image.
plt.imshow(output) plt.show()
plt function is used to image visualization in matplotlib library and plt.imshow(output) can display the image with a box.
plt.show()  displays the output in another window.
print('Number of object in this image:',label.count('car'))
Thia coad is used to print the number of objects in the image or video in the terminal.
Leave a Reply