Detect objects using Python – Machine Learning

 

Python program to Detect and Count objects – Machine Learning Project

Write a python program to Detect and Count common objects in a given image – it’s a simple machine learning project.

Video Tutorial

Installing Necessary Libraries to Detect and Count common objects

Following libraries are required to Detect and Count common objects in a given image.

  1. cvlib
  2. opencv-contrib-python==3.4.13.47

Use the following commands to install the above libraries:

pip install cvlib

pip install opencv-contrib-python==3.4.13.47 –force-reinstall

Steps

I am using google collaboratory to execute this project. First, check the OpenCV version using the following command.

import cv2
cv2.version

Next, upload the image into google collaborator sung the following snippet of code.

from google.colab import files
data_to_load = files.upload()

Import necessary libraries, following libraries, are required,

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly

First read the image using imread and display imshow functions of OpenCV.

image = cv2.imread(“image.jpeg”)

plt.imshow(image) plt.show()

Detect common objects using detect_common_objects function of cv2

box, label, count = cv.detect_common_objects(image)

Draw the box around the detected objects and assign labels

output = draw_bbox(image, box, label, count)

Dispay the image and Display the common objects like cars and truck detected

plt.imshow(output)

plt.show()

print(“Number of cars in this image are ” +str(label.count(‘car’)))

print(“Number of trucks in this image are ” +str(label.count(‘truck’)))

Source Code to Detect and Count common objects in python

import cv2
import numpy as np
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
from numpy.lib.polynomial import poly

#Read the image using imread and display imshow functions
image = cv2.imread("image.jpeg")
plt.imshow(image)
plt.show()

#Detect common objects using detect_common_objects function of cv2
box, label, count = cv.detect_common_objects(image)

#Draw the box around the detected objects and assign labels
output = draw_bbox(image, box, label, count)

#Dispay the image 
plt.imshow(output)
plt.show()

#Display the common objects detected 
print("Number of cars in this image are " +str(label.count('car')))
print("Number of trucks in this image are " +str(label.count('truck')))

Output

Oiginal Image

Oiginal Image

Processed image with label and boxes

Processed image with label and boxes

Number of cars in this image are 11

Number of trucks in this image are 3

Summary:

This tutorial discusses how to write a Python program to Detect and Count objects – Machine Learning Project. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Welcome to VTUPulse.com


Computer Graphics and Image Processing Mini Projects -> Click Here

Download Final Year Project -> Click Here

This will close in 12 seconds