Detect an object with
OpenCV-Python
What is • OpenCV is the huge open-source library for
computer vision, machine learning, and image
OpenCV? processing and now it plays a major role in real-
time operation which is very important in
today’s systems.
• By using it, one can process images and videos to
identify objects, faces, or even the handwriting
of a human.
Object • Object Detection is a computer technology related
to computer vision, image processing, and deep
Detection learning that deals with detecting instances of
objects in images and videos.
& Haar • Haar Cascade classifiers are an effective way for
Cascades object detection.
• Haar Cascade is a machine learning-based approach
where a lot of positive and negative images are used
to train the classifier.
• Positive images – These images contain the images
which we want our classifier to identify.
• Negative Images – Images of everything else, which
do not contain the object we want to detect.
Program to # Importing the OpenCV library
import cv2
Read an # Reading the image using imread() function
image using image = cv2.imread('image.png')
CV2
# Extracting the height and width of an image
h, w = image.shape[:2]
# Displaying the height and width
print("Height = {}, Width = {}".format(h, w))
** Important note: imread function is capable of reading
only .png and .jpg images
import cv2
from matplotlib import pyplot as plt
# Opening image
img = cv2.imread("image.jpg")
Program to open
an Image # Creates the environment of the picture and
shows it
plt.subplot(1, 1 , 1) # Explained in next slide
plt.imshow(img_rgb)
plt.show()
• The subplot() function takes three
arguments that describes the layout of
the figure.
subplot() Function • The layout is organized in rows and
columns, which are represented by
the first and second argument.
• The third argument represents the index
of the current plot.
Haar files for image detection
can be downloaded from
following link:
opencv/data/haarcascades at m
aster · opencv/opencv · GitHub
Program to recognize wall clock(s) from a
picture
• Pre-requites:
Haar file to identify wall clock- .xml file (can be downloaded from Kaggle
– link pinned in slide slide 7.)
Any JPG or PNG image for testing (having image of wall clock (s))
import cv2
from matplotlib import pyplot as plt
# Opening image
img = cv2.imread("OIP1.jpg")
stop_data = cv2.CascadeClassifier('haarcascade_wallclock.xml')
#We will use the detectMultiScale() function of OpenCV to recognize big wall clocks as well as small ones:
# Use minSize because for not bothering with extra-small dots that would look like wall clocks
found = stop_data.detectMultiScale(img,minSize =(20, 20)) #found is <class 'numpy.ndarray'>
# Don't do anything if there's no wall clock
amount_found = len(found)
if amount_found != 0:
# There may be more than one wall clock in the image
for (x, y, width, height) in found:
# We draw a green rectangle around every recognized sign
cv2.rectangle(img, (x, y), (x + height, y + width),
(0, 255, 0), 5)
# Creates the environment of the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img)
plt.show()
• Github link to download the code and related files :
https://github.com/DeeVa1/DetectWallClock.git
Note : All files to be kept in same folder
• THANK YOU