Real time Shape Detection
Using OpenCV
And
Python
Real time shape detection
A PROJECT REPORT
SUBMITTED IN COMPLETE FULFILLMENT OF THE
REQUIREMENTS
FOR THE AWARD OF THE DEGREE OF
BACHELOR OF TECHNOLOGY IN
[ COMPUTER SCIENCE ENGINEERING ]
SUBMITTED BY:
LAKSHAY GUPTA
2K20/B4/57
and
LAKSHAY GUPTA
2K20/B4/58
Under the supervision of
Ms. Garima Chhikara
Ms. Gull Kaur
DELHI TECHNOLOGICAL UNIVERSITY
(Formerly Delhi College of Engineering)
Bawana Road, Delhi-110042
Certificate
I hereby certify that the project Dissertation titled “Real Time
Shape Detection” which is submitted by (Lakshay Gupta,
2K20/B4/57) [Computer Engineering] and (Lakshay Gupta,
2K20/B4/58) [Computer Engineering], Delhi Technological
University, Delhi in complete fulfilment of the requirement for
the award of the degree of the Bachelor of Technology, is a
record of the project work carried out by the students under
my supervision. To bets of my knowledge this work has not
been submitted in part or full for any Degree or Diploma to
this University or elsewhere.
Ms. Garima Chhikara
Ms. Gull Kaur
(Department of Computer Science)
Abstract
Our project is based on detecting shape of an object from the
camera feed considering the object has same hue and colour
using python and openCV
The project detects the shapes displayed on camera feed like
square , rectangle , circle, pentagon etc by using openCV
directory
Usage of hue, vibration and saturation scale is done to detect
image colour and components. Further the shapes are
analysed by counting their sides respective to defined
curvature
Numpy and openCV libraries in python are used in this project,
as python is an interpreter language that verifies every
predefined function whether it is true or not and finally
presents a constructed Graphical User Interface(GUI) .
Acknowledgement
In performing our project, we had to take help and guidelines
from respected persons, who deserve our greatest gratitude.
The completion of this project gives us much pleasure. We
would like to show our gratitude to our teachers and mentors
Ms. Garima Chhikara and Ms. Gull Kaur, giving us a good
guideline for report throughout numerous consultations. We
would also like to extend our deepest gratitude to all those
who have directly and indirectly guided us in writing this
assignment.
Many people including my classmates have made
valuable comments and suggestions on this project report
which gave me the inspiration to improve my project. We
thank all the people for their help directly and indirectly to
complete my project.
In addition, I would like to thank the Department of
Computer Science , Delhi Technological University for giving us
the opportunity to work on this topic.
Contents
● Certificate
● Abstract
● Acknowledgement
● Introduction
● Background
● Approach
● Code and flow of control
● Results
● Applications and Future-work
● Summary and conclusion
Introduction
The program focuses on analyzing the camera feed and the
images it has. Since an image is stored in form of 3D array
numpy library comes in use. Based on the characteristic colour
and saturation of a part of image the image is analyzed and
processed as a separate image. In the meantime marking
backgrounds are constructed on that part.
If the image forms a closed loop approximating simple shapes
like rectangle, circle, ellipse, etc. on the camera feed type of
shape appears as the text.
As stated earlier Opencv can be
used to analyze and process
images and also provide features
like creating trackbars and named
frame windows which helps in
constructing Graphical User
Interface which could take manual
input of variables and input scaling of image that we want to
detect in real-time camera feed.The user could just easily lie
back and rest at his chair and could change scaling variables to
detect shapes easily.
Background
The program as stated uses Python and libraries like OpenCV
and Numpy, let’s quickly have a brief on them.
Python is an interpreted, high-level and general-purpose
language. Its language uses object-oriented programming and
language constructs to help programmers write clear, logical
code for small and large scale uses.
NumPy is the library used for operations on uni and multi
dimensional arrays and matrices. It is an open source library
and serves as a core library today for python.
OpenCV or Open Source Computer
Vision Library is a library of
programming mainly aimed at
real-time computer vision. It is also
an open source library providing
users numerous useful functions to
create useful programs and GUI
helping in image analysis and
processing.
Our program can be used for real time applications and if
extended it could serve various purposes that would serve in
our daily life.
OpenCV was developed in 1999 and since then it has been
used in several ways like Egomotion estimation, Facial
Recognition system using databases, Mobile robotics,
stereocaming, motion tracking etc.
OpenCV helps even in Machine Learning by giving library
functions for decision tree making, gradient boosting trees,
deep neutral networks etc.
Our code however is a basic feature of OpenCV which
establishes the extent of diversity of functions it provide.
Approach
The code operates on real time camera feed still it analyzes
every picture frame and the image in it with the frequency of
code optimization.
As we know each image has three components Hue,
Saturation and Value. And so our program operates by taking
these components in consideration. We have created a mask
frame which analyzes the camera feed and created trackbars
using OpenCV to change
hue-saturation-value of
mask so that the desired
image(we want to analyze)
is white the rest of mask is
completely black. Then we
use another feature of
OpenCV library - Contours,
using these we draw lines
of the desired image and detect approximate image if it is a
basic polygon like circle, ellipse, rectangle, square etc. using
different constraints on the number of edges the shape has.
Once contours are made the type of image polygon has is
displayed on the window displaying camera feed dynamically
alongside analyzing the next frame.
Code and flow of control
import cv2 #importing libraries
import numpy as np
def nothing(x): #a dummy function
pass
cap = cv2.VideoCapture(0) #to capture camera feed
cv2.namedWindow("Frame")
cv2.createTrackbar("L-H", "Frame", 0, 180, nothing) #to create a
trackbar with edge values
cv2.createTrackbar("L-S", "Frame", 0, 225, nothing)
cv2.createTrackbar("L-V", "Frame", 0, 225, nothing)
cv2.createTrackbar("U-H", "Frame", 180, 180, nothing)
cv2.createTrackbar("U-S", "Frame", 255, 255, nothing)
cv2.createTrackbar("U-V", "Frame", 255, 255, nothing)
while True: #runs until camera feed is allowed to be used(before esc)
_, frame = cap.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #using a
variable by converting camera feed into hsv mode
l_h = cv2.getTrackbarPos("L-H", "Frame") #to get position of
trackbar
l_s = cv2.getTrackbarPos("L-S", "Frame")
l_v = cv2.getTrackbarPos("L-V", "Frame")
u_h = cv2.getTrackbarPos("U-H", "Frame")
u_s = cv2.getTrackbarPos("U-S", "Frame")
u_v = cv2.getTrackbarPos("U-V", "Frame")
lower_red = np.array([l_h, l_s, l_v]) #to use trackbar input in
image array manipulation
upper_red = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv_frame, lower_red, upper_red) #to manipulate
mask
kernel = np.ones((5, 5), np.uint8)
mask = cv2.erode(mask, kernel)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE,
cv2.CHAIN_APPROX_NONE) #mask is used to draw contours
for contour in contours:
approx = cv2.approxPolyDP(contour, 0.008*
cv2.arcLength(contour, True), True) #approximate contours are made
assuming arc length variable
x = approx.ravel()[0]
y = approx.ravel()[1]
cv2.drawContours( frame, [approx], 0, (0, 2, 0), 5)
print(len(approx)) #to print length of each mask frame
if len(approx) == 3: #constraints to define shape
cv2.putText(frame, "Triangle ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif len(approx) == 4:
x, y, w, h = cv2.boundingRect(approx)
aspectRatio = float(w)/h
print (aspectRatio)
if aspectRatio >=0.95 and aspectRatio <= 1.05:
cv2.putText(frame, "Square ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
else:
cv2.putText(frame, "Rectangle ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif len(approx) == 5:
cv2.putText(frame, "pentagon ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif len(approx) == 6:
cv2.putText(frame, "Hexagon ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif len(approx) == 7:
cv2.putText(frame, "Septagon ", (x, y),
cv2.cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif len(approx) == 8:
cv2.putText(frame, "Octagon ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
elif 8 < len(approx) < 15 :
cv2.putText(frame, "Ellipse ", (x, y),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0))
else:
cv2.putText(frame, "circle ", (x, y), cv2.FONT_HERSHEY_COMPLEX,
1, (0, 0, 0))
cv2.imshow("Frame", frame) #to show image feed and mask windows
cv2.imshow("Mask", mask)
key = cv2.waitKey(1)
if key == 27:
break
cap.release() #to release all data
cv2.destroyAllWindows() #to destroy all windows
At first the code goes to take camera feed after importing
libraries then considering the camera is allowed to work
camfeed is converted to hue-saturation-value mode.
Furtherly the image array is manipulated using trackbars until
the image we want only seems white in mask frame.
Then code goes to list in which contour is taken as variable
and then drawn if the condition sets true.
And after that contours are eroded and made on basis of
arc-length assuming them straight lines.
No of lines are printed and the constraints deciding shape
come in work.
Finally at initial condition of false entire memory of camfeed is
lost and deleted.
Results
Applications and future-work
Automated CCTV surveillance:
Typically CCTV is running inevitably, so we need a huge size of
the memory framework to store the recorded video. By
utilizing an object discovery framework we can mechanize
CCTV so that in the event that a few items are detected, at
that point the record is going to begin. Utilizing this we can
diminish the over and over account a similar picture outlines,
which expands memory effectiveness. We can diminish the
memory prerequisite by utilizing this object detection system.
● Person Detection
It has a conspicuous augmentation to automotive applications
because of the potential for improving security frameworks.
Person detection is undertaking Computer vision frameworks
for finding and following individuals. Person detection is the
task of finding all examples of individuals present in a picture,
and it has been most broadly achieved via looking through all
areas in the picture, at all potential scales, and contrasting a
little region at every area with known layouts or examples of
individuals.
● Vehicle Detection
By using Vehicle Detection technique we can detect the
number plate of a speeding car or accident affected car.
This also enables for security of the society and
decreasing the number of crimes done by car. By using
Vehicle Detection Technology Pixel Solutionz have
successfully detected the speed of the vehicle and we
have also detected the number plate of the car using
Optical Character Recognition (OCR)
MEDICAL IMAGING
Medical image processing tools are playing an increasingly
important role in assisting the clinicians in diagnosis, therapy
planning and image-guided interventions. Accurate, robust and
fast tracking of deformable anatomical objects such as the heart, is
a crucial task in medical image analysis.
Summary
The subtract is that the code displays the shape of the image
we kept in front of the camera if we adjust the
hue-saturation-value of the desired part by drawing and
counting the number of edges using the feature of drawing
contours in OpenCV. This code detects the images of the same
colours and shades considering absence of any sort of
distortion and the image is not crumbled or light absorbent.
This code be very useful and would serve as a kickstart to big projects
including web-development, machine learning etc.
Conclusion
The program as stated could be very useful and serve
different purposes. Further developments using this
code is already being done and changing our lives.
We sincerely are grateful to our teachers and mentors
and entire department of Computer Science and
Information Technology, DTU to provide us this
valuable opportunity.
References
● OpenCV.org for understanding about opencv and
its implementation in this program
● Ros.wiki to understand use of OpenCV in artificial
intelligence and its uses
● Wikipedia to access usage and purposes of
Opencv and Python
● Docs.python.org to understand about use of
libraries and code flow in Python