-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpy-security-camera.py
73 lines (59 loc) · 2.29 KB
/
py-security-camera.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cv2
import time
import datetime
import smtplib, os
from email.message import EmailMessage
def send_email():
msg = EmailMessage()
msg.set_content('A face was detected in your security camera!')
msg['Subject'] = f'Py-Security-Camera - Face detected'
msg['From'] = '[email protected]' # enter sender email address here
msg['To'] = '[email protected]' # enter destinatary email address here
email_user = os.environ.get('EMAIL_USER') # set EMAIL_USER as a system variable
email_password = os.environ.get('EMAIL_PASSWORD') # set EMAIL_PASSWORD as a system variable
server = smtplib.SMTP_SSL('smtp.email.com', 465) # secured connection - enter your smtp server here
server.ehlo()
server.login(email_user, email_password)
server.send_message(msg)
print("E-mail sent!")
server.quit()
if __name__ == "__main__":
cap = cv2.VideoCapture(0) # opens webcam
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') # face classifier
frame_size = (int(cap.get(3)), int(cap.get(4))) # captures webcam frame size
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # video format
detection = False
detection_stopped_time = None
timer_started = False
SECONDS_TO_RECORD_AFTER_DETECTION = 5
while True:
_, frame = cap.read() # reads frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # new image with gray scale
faces = face_cascade.detectMultiScale(gray, 1.3, 5) # scale factor = 1.3 (1.1, 1.5), number of faces = 5
if len(faces) > 0:
if detection:
timer_started = False
else:
detection = True
current_time = datetime.datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
out = cv2.VideoWriter(f"{current_time}.mp4", fourcc, 20.0, frame_size)
print("Started recording!")
send_email()
elif detection:
if timer_started:
if time.time() - detection_stopped_time >= SECONDS_TO_RECORD_AFTER_DETECTION:
detection = False
timer_started = False
out.release()
print("Stopped recording!")
else:
timer_started = True
detection_stopped_time = time.time()
if detection:
out.write(frame) # writes frame
cv2.imshow("Camera", frame) # displays frame
if cv2.waitKey(1) == ord('q'): # exit key
break
out.release() # releases video capture
cap.release() # release camera resources
cv2.destroyAllWindows() # closes windows