0% found this document useful (0 votes)
36 views

Security Camera Report

This document describes a student's summer internship project to build a security camera using Python and OpenCV. The student submitted the project to fulfill the requirements for their summer internship at Amity University. The project involved using a camera to detect motion and record video when motion was detected. Key steps included capturing video from the camera, comparing frames to detect differences indicating motion, processing the frames to identify contours representing moving objects, and saving the recorded video. The student acknowledged their teacher and principal for providing the opportunity and acknowledged OpenCV and other libraries used to complete the project.

Uploaded by

gntfhx9pyd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Security Camera Report

This document describes a student's summer internship project to build a security camera using Python and OpenCV. The student submitted the project to fulfill the requirements for their summer internship at Amity University. The project involved using a camera to detect motion and record video when motion was detected. Key steps included capturing video from the camera, comparing frames to detect differences indicating motion, processing the frames to identify contours representing moving objects, and saving the recorded video. The student acknowledged their teacher and principal for providing the opportunity and acknowledged OpenCV and other libraries used to complete the project.

Uploaded by

gntfhx9pyd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SUMMER INTERNSHIP I

Security Camera in Python

Bachelor of Technology
(Computer Science Engineering)

Submitted By
Sia Saini

A70405220049

AMITY SCHOOL OF ENGINEERING AND TECHNOLOGY

AMITY UNIVERSITY MUMBAI


Year 2020-24
AMITY UNIVERSITY MUMBAI
Established vide Maharashtra Act No. 13 of 2014 of Government of Maharashtra and recognized under section 2(f) of UGC Act
1956

CERTIFICATE

This is to certify that Mr./Miss Sia Saini Enrollment No. A70405220049 of

B. Tech Computer Science Engineering semester 5 has satisfactorily

completed the Summer Internship I prescribed by Amity School of

Engineering & Technology, Amity University Mumbai during academic

year 2020-24.

Stamp/seal of the Institute Name and Signature of

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 number Page number

Figure 1 5

Figure 2 7

Figure 3 8

Figure 4 8
Abbreviations

OpenCV : Open Computer Vision


API: Application Programming Interface
IP: Internet Protocol
Fourcc: Four Character Code
XVID: Express Video Digital
FPS: Frames Per Second
AVI: Audio Video Interleave
Abstract
In this project, we have made a Security Camera through Python using Open Computer
Vision with the help of an external camera which helps in monitoring the required object. The
aim will be to alert the user when the object is moved and to detect its motion. The detected
movement will be recorded and will be stored in a file.
Table of contents

Page
Contents No.

Acknowledgements iii

Abstract iv

List of figures v

Abbreviations vi

Chapter 1 Introduction 1

1.1 Purpose of selecting this project 1

1.2 Description of the project 1

1.3 Scope of assigned work 1

Chapter 2 Literature Review. 2

Chapter 3 Description of work 3

Chapter 4 Results and Interpretation 7

Chapter 5 Conclusions 9

Limitations of the project 9

Recommendations 9

References 9
1 Introduction

1.1 Purpose of selecting this project

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.

1.2 Description of the project

There are three features involved in making a security camera,

 A Camera for vision


 Motion detection
 Creating an alert after the motion is detected

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.

1.5 Scope of assigned work

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

3.1 Libraries Required

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.

3.3 Working of Code

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.

6. out = cv2.VideoWriter('recording.mp4v',fourcc, 20.0, (640,480))

line 6, stores the output we have recorded its format is:


cv2.VideoWriter(filename, fourcc, fps, (framesize))

to save a video in OpenCV cv2.VideoWriter() is used.


filename: Input video file
fourcc: 4 character code of codec used to compress the frames
fps : Frame rate of videostream
framesize: Height and width of frame

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.

12. gray = cv2.cvtColor(diff, cv2.COLOR_RGB2GRAY)


13. blur = cv2.GaussianBlur(gray, (5, 5), 0)
14. ret, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
15. dilated = cv2.dilate(thresh, None, iterations=3)
16. contours, ret =
cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

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.

Note: thresh_binary makes the object appear black on white background.

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

17. for c in contours:


18. if cv2.contourArea(c) < 5000:
19. continue
20. x, y, w, h = cv2.boundingRect(c)
21. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

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)

winsound.beep(frequency, duration) beeps the PC’s speaker when movement is


detected. Frequency is in hertz (loudness) and duration is in milliseconds.

24. if cv2.waitKey(10) == ord('q'):


25. break
26. cv2.imshow('output', frame)
27. out.write(frame)
28.
29. cam.release()
30. cv2.destroyAllWindows()

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

4.1 Set up for the Output.

For using this on your phone,

 Make sure the Iruin Webcam is installed on your phone and PC


 Open the app on your phone and laptop and make sure you are connected to the
same network
 Run the code
 Your phone acts as the camera and your PC will detect the movement and create an
alert sound when required.

You will see the contour lines and the sound alert from your laptop.

4.2 Interpretation from the Output.

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.

When the object is not moved.

Figure 2.
When the object is moved,

Figure 3.

The recording gets stored.

Figure 4.
5 Conclusions

5.1 Salient conclusions from the Project

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.

Limitations of the project

 Recording file is overwritten every time you run this project.

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

You might also like