-
Notifications
You must be signed in to change notification settings - Fork 0
/
security_camera.py
67 lines (50 loc) · 1.67 KB
/
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
import cv2
import time
import datetime
#import os
cap = cv2.VideoCapture(1)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
framesize = (int(cap.get(3)), int(cap.get(4)))
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
#start recording:
#out = cv2.VideoWriter("video.mp4", fourcc, 20.0, framesize)
#stop recording:
#out.release
#prepare camera logic
timer = 5
recording = False
recording_stopped_time = None
timer_started = False
while True:
_, frame = cap.read()
#Gesicht(er) erkennen
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 3)#Y
for (x, y, width, height) in faces:
cv2.rectangle(frame, (x, y), (x+width, y + height), (255, 0, 0), 3)
if len(faces) >0:
if recording:
timer_started = False
else:
date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
out = cv2.VideoWriter((date + ".mp4"), fourcc, 10.0, framesize)
recording = True
print ("Recording started!")
elif recording:
if timer_started:
if time.time() - recording_stopped_time >= timer:
recording = False
timer_started = False
out.release()
print("Stopped recording!")
else:
timer_started = True
recording_stopped_time = time.time()
if recording:
out.write(frame)
cv2.imshow("Securitycamera", frame)
if cv2.waitKey(1) == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
out.release()