object detection project and count objects in image using python

Suriya Ravichandran

 


About this project:

The code you provided is a simple example of object detection using the cvlib library in Python. It aims to detect common objects in an image, specifically zebras, and count the number of zebras present.

Here's a breakdown of the code:

1. Importing the necessary libraries:

   - cv2: The OpenCV library for image processing.
   - cvlib: A high-level computer vision library built on top of OpenCV.
   - draw_bbox function from cvlib.object_detection: A utility function to draw bounding boxes around detected objects.
   - matplotlib.pyplot as plt: A library for visualizing data.

2. Reading the image:

   - The image file zebras.jpg is read using cv2.imread() and stored in the image variable.


3. Performing object detection:

   - The cv.detect_common_objects() function from cvlib is used to detect common objects in the image. It returns the bounding box coordinates, labels, and confidence scores for each detected object. This information is stored in the box, label, and c_score variables, respectively.

4. Drawing bounding boxes:

   - The draw_bbox() function from cvlib.object_detection is used to draw bounding boxes around the detected objects on the original image. The function takes the image, bounding box coordinates, labels, and confidence scores as input and returns the image with the bounding boxes drawn. The result is stored in the output variable.

5. Displaying the image:

   - The plt.imshow() function is used to display the output image with bounding boxes.
   - The plt.show() function is called to show the image in a separate window.


6. Counting the number of zebras:

   - The label.count('zebras') statement is used to count the number of occurrences of the label 'zebras' in the label list, which contains the labels of the detected objects. The result is printed as the number of zebras in the image.

Regarding the project itself, based on the provided code snippet, it appears to be a basic implementation of object detection using cvlib for detecting zebras in an image. It demonstrates how to use the library's functions to detect objects and draw bounding boxes around them. However, it's important to note that the success of the object detection depends on the accuracy and performance of the underlying model used by cvlib. Additionally, for this specific project, it relies on the availability of the necessary files and the successful download of those files, as mentioned in our previous discussions.

Using Package of this project:

  1. opencv
  2. cvlib
  3. matplotlib
  4. tensorflow

How to install packages

1. Install command to tensorflow
   "pip install tensorflow"


2.Install command to opencv
  "pip install opencv-python"


3.Install command to cvlib
  "pip install cvlib"



4.Install command to matplotlib
  "pip install matplotlib"


Import package to object detection project


import cv2
import cvlib as cv
from cvlib.object_detection import draw_bbox
import matplotlib.pyplot as plt


Write a program to this project:

#Add image 
image = cv2.imread('zebras.jpg')
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 zebras in this image:',label.count('zebra'))

To run this program:

1.Open terminal in vscode or cmd
2.type command "python objectdetection.py"

I will face some error to run this project: 

PS D:\object detection project and count objects in image using python> python objectdetection.py
Downloading yolov4.cfg from https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4.cfg
Could not establish connection. Download failed
Downloading yolov4.weights from https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights
Downloading yolov3_classes.txt from https://github.com/arunponnusamy/object-detection-opencv/raw/master/yolov3.txt
Could not establish connection. Download failed
Traceback (most recent call last):
  File "D:\object detection project and count objects in image using python\objectdetection.py", line 8, in <module>
    box,label,c_score = cv.detect_common_objects(image)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
124, in detect_common_objects
    classes = populate_class_labels()
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Suriya Ravichandran\AppData\Local\Programs\Python\Python311\Lib\site-packages\cvlib\object_detection.py", line 
19, in populate_class_labels
    f = open(class_file_abs_path, 'r')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Suriya Ravichandran\\.cvlib\\object_detection\\yolo\\yolov3\\yolov3_classes.txt'

How to fix this error:

To fix the issue, you can download the required files manually and place them in the appropriate directories. Here's what you need to do:

1. Download yolov4.cfg from this URL:

Download link:click here

2. Download yolov4.weights from this URL: 

Download link:click here

3. Download yolov3_classes.txt from this URL:

Download link:click here

4. Create the following directory structure:

 C:\Users\Suriya Ravichandran\.cvlib\object_detection\yolo\yolov3\.

5. Place the downloaded files in the appropriate directories:

   - Place yolov4.cfg in C:\Users\Suriya Ravichandran\.cvlib\object_detection\yolo\yolov3\.
   - Place yolov4.weights in C:\Users\Suriya Ravichandran\.cvlib\object_detection\yolo\yolov3\.
   - Place yolov3_classes.txt in C:\Users\Suriya Ravichandran\.cvlib\object_detection\yolo\yolov3\.

After completing these steps, the script should be able to find the necessary files and perform object detection without any errors.

After complete this step again run this program

"python objectdetection.py"

Output:


Then you will close detect window

Output:

number of zebras in this image: 4



Buy this project to our site:Buynow👈
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send