Security Camera using Python
Our goal is to create a program in Python that when a camera detects a human's face it will notify the owner by sending them an email. Our project covers the fundamentals of coding and design planning. In addition, our project covers the concepts of "Open CV" in Python. Understanding the concepts of coding contains variables (Hold Information) and data structures (Streamline data).
Improve security by increasing automation of security cameras using affordable materials.
How can we make affordable and reliable security cameras using Python?
Camera/Any type of webcam
Laptop/Computer
Python
Pycharm
Pictures of Materials:
You may have a potential to compromise your information if you use your personal email, so it could be better to make an alternate email. Additionally, the login information inside the program should be encrypted so people can't see your password.
Prepare Materials & Start research on Open CV
Install python – Download the latest version for your computer. www.python.org/downloads
Install PyCharm – Download the latest version for your computer www.jetbrains.com/pycharm/download
Import Open CV2
Download XML Cascade File (Link) (Originally from https://mikailalici.com/2021/06/11/face-recognition-using-python-and-opencv/)
Copy (Ctrl C) & Paste (Ctrl V) code into PyCharm
Replace Email sender and receiver to your personal email. (Have to use a gmail account to send the emails, any email can receive)
Go to Gmail Account > Security > Turn on 2 Step Verification > Go back to Security > Go to App Passwords > Generate a custom app password > Then use that as your password in the program.
Run Code (press green arrow on top right corner)
The scientific principle of our project is computer vision. Our project relates to this theory because with the use of "open cv" we can use face recognition to create the security camera. This project uses this theory to allow computers to analyze input from the cameras to produce an output. With this project ; I learned how to use the OpenCV - computer vision library that allows for the manipulation and analysis of visual data. I can use in a variety of applications, including security camera systems. With OpenCV, it is possible to detect and identify objects, people, and events in real-time video streams.
The design and implementation of a security camera system using OpenCV and Python involves several considerations. These may include the desired level of accuracy and reliability, the available resources and budget, and the ethical implications of surveillance, such as issues of privacy. It is important to carefully evaluate and balance these factors in order to create an effective and responsible security camera system.I also learned that Python is a popular programming language for creating and implementing image processing algorithms.
This project can be used for businesses, and/or schools. Currently, security cameras coded by Python are used to protect homes. Furthermore, this project benefits society because it provides an affordable alternative to expensive security cameras. Along with this, the benefit of this project is that it is highly precise in taking a picture of the individual's face and then emailing it to the owner. A future benefit of this project is that it will be accessible to a lot of people since they can use webcams that are integrated into a laptop as a camera. This project can be improved by sound being added to the recordings and videos being sent to the owner instead of pictures. With the use of OpenCV, a security camera can be trained to detect specific movements or objects, such as a person entering a room or a package being left on the doorstep. This allows the camera to send an alert to the homeowner or security company, enabling them to take action to ensure the safety of the property. Additionally, the use of Python in the security camera system allows for the integration of advanced features such as facial recognition or object classification, further improving the effectiveness of the security system. Overall, the combination of OpenCV and Python in a security camera can significantly enhance the security and protection of a home or business.
How can machine learning algorithms be used to improve the accuracy and efficiency of a security camera system?
Machine learning algorithms can be used to improve the accuracy and efficiency of a security camera system in several ways. One way is by using object detection algorithms, which can identify and classify objects within the camera's field of view. This can be used to trigger alerts or take specific actions when certain objects, such as a person or vehicle, are detected. Machine learning algorithms can also be used to analyze patterns and trends in the data collected by the security camera system, allowing it to make more informed decisions and respond more effectively to potential threats. Additionally, machine learning can be used to optimize the performance of the security camera system by adapting to changing conditions and improving over time.
How can we ensure that the security camera system respects the privacy of individuals being monitored?
Limit the scope of surveillance: Only monitor areas where there is a legitimate need for security, and avoid unnecessarily collecting data on individuals.
Obtain consent: Where possible, obtain the consent of individuals before collecting their data. This may involve providing clear notice and obtaining explicit consent for the collection and use of their data.
Use de-identification techniques: Where it is not possible to obtain consent, consider using de-identification techniques to protect the privacy of individuals. These techniques can include blurring or obscuring faces or other identifying features, or using machine learning algorithms to automatically remove such information from the data.
What are the most cost-effective hardware and software options for implementing a security camera system with OpenCV?
The most cost-effective hardware and software options for implementing a security camera system with OpenCV will depend on the specific requirements and goals of your system. Some factors to consider when selecting hardware and software include the resolution and frame rate of the cameras, the processing power required for the desired machine learning algorithms, and the type of storage and networking infrastructure needed to store and transmit the data.
How will our security camera identify faces?
We will use the haar cascade classifier in this project. Haar-cascade classifier is an Object Detection Algorithm used to identify faces in an image or a real-time video. The algorithm uses edge or line detection features proposed by Viola and Jones in their research paper “Rapid Object Detection using a Boosted Cascade of Simple Features” published in 2001. The algorithm is given a lot of positive images consisting of faces, and a lot of negative images not consisting of any face to train on them. The model created from this training is available at the OpenCV.
Why use the coding language "Python" instead of other languages?
We used the coding language Python because it is free and accessible to everyone. In addition, we used Python because it has a variety of features such as lots of libraries. Lastly, Python is very simple and easy to use.
Action Video & Photo of Procedures:
Our Code:
import cv2
import time
import datetime
import os
from email.message import EmailMessage
import ssl
import smtplib
import imghdr
email_sender = 'preston.hsadvancement22@gmail.com'
email_password = "User's Password"
email_receiver = 'preston.hsadvancement22@gmail.com'
subject = 'Face Detected'
body = """
Face Detected
"""
em = EmailMessage()
em['From'] = email_sender
em['To'] = email_receiver
em['Subject'] = subject
em.set_content(body)
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
body_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascadefullbody.xml")
frame_size = (int(cap.get(3)), int(cap.get(4)))
detection = False
detection_stopped_time = None
timer_started = False
seconds_after_detection = 5
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
bodies = face_cascade.detectMultiScale(gray, 1.3, 5)
if len(faces) + len(bodies) > 0:
if detection:
timer_started = False
else:
detection = True
current_time = datetime.datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
imagefile = f"{current_time}.jpg"
s, img = cap.read()
cv2.imwrite(imagefile, img) # save image
print("Took Picture")
elif detection:
if timer_started:
if time.time() - detection_stopped_time > seconds_after_detection:
detection = False
timer_started = False
print("stop recording")
with open(imagefile, 'rb') as f:
file_data = f.read()
file_type = imghdr.what(f.name)
file_name = f.name
em.add_attachment(file_data, maintype='png', subtype=file_type, filename=file_name)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(email_sender, email_password)
smtp.sendmail(email_sender, email_receiver, em.as_string())
print ("sent email")
else:
timer_started = True
detection_stopped_time = time.time()
for (x, y, width, height) in faces:
cv2.rectangle(frame, (x, y), (x + width, y + height), (255, 0, 0), 3)
cv2.imshow("camera", frame)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
Pictures of Our Code: