Security Camera Report
Security Camera Report
Bachelor of Technology
(Computer Science Engineering)
Submitted By
Sia Saini
A70405220049
CERTIFICATE
year 2020-24.
Examiner
Acknowledgements
I would like to express my special thanks of gratitude to my teacher ___ as well as our
principal ___ who gave me the golden opportunity to do this wonderful project on the topic
of Building a Security Camera through Python using Computer Vision, which also helped me
in doing a lot of Research and piqued my interest in this programming language.
List of Figures
Figure 1 5
Figure 2 7
Figure 3 8
Figure 4 8
Abbreviations
Page
Contents No.
Acknowledgements iii
Abstract iv
List of figures v
Abbreviations vi
Chapter 1 Introduction 1
Chapter 5 Conclusions 9
Recommendations 9
References 9
1 Introduction
OpenCV is the huge open-source library for the computer vision, machine learning,
and image 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 handwriting of a human. When it is integrated with
various libraries, such as NumPy, python can process the OpenCV array structure for
analysis.
For making this security camera we make use of motion detection and tracking using
OpenCV contours. To detect and track this motion we use live sample video, record
it, and then store the recording in a file. Contours are retrieved from the binary image
and are useful for shape analysis, object detection and recognition for doing so we use
these functions: cv2.findContours(), cv2.drawContours().
We perform morphological operations and practically learn how they work on images,
through contrast enhancement by using equalization and contrast limitation and
finally subtracting the background from the video and implementing them using
OpenCV. The motion is detected by computing the difference between the previous
and current position which tells us a movement has been detected.
We can use this project for advanced burglar detection techniques, with the help of an
API we can use an actual IP camera stream and we can also enhance this program to
send out an email or a notification once someone is detected.
2 Literature Review
After reviewing various research papers, I realized that in some research papers there were
some drawbacks which were covered in other papers. Rather than focusing on one paper I took the
ideologies from the papers I reviewed and took the relevant points of those papers to constitute in my
project. The reason is very simple: OpenCV is used as an image processing library in many computer
vision real-time applications. There are thousands of functions available in OpenCV. These simple
techniques are used to shape our images in our required format. So, I decided to implement my project
using OpenCV library. I implemented OpenCV because while reviewing journal papers I noticed that
in some papers they used programming language as Java for creating their application which is quite
difficult to understand and it’s time consuming which is why I decided and concluded to add some
features in my project to make it more secure and easier to use.
3 Description of Project
Here, we use two libraries cv2 and winsound. cv2 is the module import name for opencv-
python. Winsound module provides access to the basic sound-playing machinery provided by
windows platforms.
To install cv2,
Run pip install opencv-python in your Python terminal.
Simply import this by typing import cv2 in your console.
To install winsound,
Run pip install winsound in your Python terminal.
Simply import this by typing import winsound in your console.
3.2 Software Required
For implementing my code, I have used PyCharm Community Edition 2021.3.2 and for an
external camera I have installed the app known as Iriun Webcam on my PC and my phone.
1. import cv2
2. import winsound
From 1-2, we are simply importing the Computer Vision and Winsound Library.
3.
4. cam = cv2.VideoCapture(0)
5. fourcc = cv2.VideoWriter_fourcc(*'XVID')
line 4 helps us to capture the video and ‘0’is basically the index number of the camera
that the cv2 has to read. In my system cv2.VideoCapture(0) is for recording through
my phone camera and cv2.VideoCapture(1) is for recording through my laptop.
In line 5, fourcc (Four Character Code) is a sequence of four bytes used to uniquely
identify data formats. fourcc encodes the avi video. XVID is a code for compressing
video and storing audio files without causing visible quality loss. This makes
transmitting the video files faster and also helps users to save significant space on
their computer.
7.
8. while cam.isOpened():
9. ret, frame = cam.read()
10. ret, frame1 = cam.read()
11. diff = cv2.absdiff(frame,frame1)
from 8 – 11, In a real time video monitoring system, to ensure that the camera is open
and is working properly or to simply say to read the camera we use isOpened()
function. ‘ret’ is for retrieving the video that is recorded. ‘Frame’ is for the previous
position and ‘frame1’ is for the current position. To detect the movement, we
calculate the difference between the previous frame and the current frame and then we
retrieve it. cv2.absdiff() calculates the per-element absolute difference between these
two frames.
line 12, we perform grayscaling here because we are more interested in shape
characteristics of the image because the colour images might not give us much
information, to do this we convert it to gray image using cv2.cvtColor().
cv2.cvtColor() helps us convert an image from one color space to another.
Syntax is, cv2.cvtColor(src,code)
src is the image whose color space is to be changed.
code is the color space conversion code.
Line 13-14, blurring and smoothing is one of the most pre-processing steps in all of
computer vision and image processing. By smoothing an image prior to applying
threshold we can reduce the amount of high frequency content such as noise.
Furthermore, this allows us to focus on lager objects in the image.
Guassian blurring makes use of the Gaussian Filter which is a low pass filter that
smoothens our video by removing the high frequency components to reduce noise.
Syntax is, cv2.GaussianBlur(src,(image size),border type)
src is the image where the guassian blurring is to applied
(image size) width and height, should be positive and odd
Border_type, can be constant, reflect, default etc. we have chosen default which is set
to 0.
In thresholding, we change the pixels of an image to make the image easier to
analyse. Here, we convert the image from grayscale to binary image (simply black
and white image).
Syntax, cv2.threshold(source, thresholdValue, maxVal, thresholding technique)
source : input image (must be Grayscale)
thresholdValue: value of threshold below and above which pixel values will
accordingly
maxVal: maximum value that can be assigned to a pixel
thresholding technique: the type of thresholding to be applied.
Line 15-16, noise removal causes our object to shrink so we dilate our object because,
since the noise is gone, they won’t come back, but our object area increases. It is also
useful in joining broken parts of an object. Syntax is, cv2.dilate(src,dst,iterations)
src: input image
dst: output image
iterations: number of times you want to dilate the image
findContour() function helps in extracting the contours from the image. We are using
this to have borders for our movement. It works best with binary images so we should
apply threshold first before doing this step. Syntax is, cv2.findContours(src,
cv2.(mode), cv2.(method))
src : input image
cv2.mode : contour-retrival mode
cv2.method: contour-approximation method
22. From line 17-21, we create a condition for focusing on the bigger contours rather than
the smaller ones only if the condition is satisfied, we will move on to creating our
frame. contourArea calculates the area of the contour. Then for creating a rectangle
we use the function cv2.boundingRect(). x, y, w, h are x-axis, y-axis, width and height
respectively. In line 21, cv2.rectangle() is used to draw rectangle, its syntax is:
cv2.rectangle(image, start point, end point, colour, thickness)
start point consists of x and y axis
end point consists of x axis + width, y axis + height
Y axis + height
X axis + width
Figure 1.
23. winsound.Beep(500, 200)
From line 24-30, waitKey() allows user to display a window for given milliseconds or
until any key is pressed in this case, the key is q.
cv2.imshow() displays the output.
out.write() writes the frame into the file which in this case is ‘recording.mp4v’
cam.release() releases the camera
cam.destroyAllWindows() shuts down the open window.
4 Result
You will see the contour lines and the sound alert from your laptop.
On the detection of the movement, you will see a green colored rectangle box on the object
with an alert sound being created. After pressing ‘q’ the window gets closed and the recording
is stored in your PC.
Figure 2.
When the object is moved,
Figure 3.
Figure 4.
5 Conclusions
This project helps in automating our life within just 30 lines of code. By using computer vision
this project is very simple and efficient. This security camera creates a beep sound when a
movement is detected to alert the user and for them to act on it.
Recommendations
If you want to start with basic yet simple automation this project is the best for you. You can
start from this and further ahead you can make it more advanced by exploring different
Computer Vision functions, APIs, etc.
References
https://medium.com/artificialis/build-a-security-camera-with-python-and-opencv-
83e69f676216#:~:text=In%20this%20tutorial%20we'll,save%20them%20into%20a%20folde
r.
https://towardsdatascience.com/computer-vision-for-beginners-part-4-64a8d9856208
www.javatpoint.com
www.geeksforgeeks.com
www.tutorialspoint.com
https://docs.opencv.org/3.4/d4/d73/tutorial_py_contours_begin.html