-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (63 loc) · 2.49 KB
/
main.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
74
75
76
77
78
import errno
import os
import sys
import threading
from datetime import datetime as dt, timedelta
import cvlib as cv
import cv2
from config import BASE_VIDEO_PATH, FPS, HORIZONTAL_PIXEL_COUNT, VERTICAL_PIXEL_COUNT, \
VIDEO_CAPTURE_SOURCE, IMAGE_SCANS_PER_SECOND
from yolo_email import send_email
cap = cv2.VideoCapture(VIDEO_CAPTURE_SOURCE)
cap.set(3, HORIZONTAL_PIXEL_COUNT)
cap.set(4, VERTICAL_PIXEL_COUNT)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MPEG')
cur_second = dt.now().second
remaining_scans = IMAGE_SCANS_PER_SECOND
while True:
file_path = '{}/{}/{}/{}/{}/{}_output.avi'.format(BASE_VIDEO_PATH,
dt.now().year, dt.now().month, dt.now().day, dt.now().hour,
dt.now().strftime("%Y_%m_%d__%H_%M"))
if not os.path.exists(os.path.dirname(file_path)):
try:
os.makedirs(os.path.dirname(file_path))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
stop_at = dt.now() + timedelta(minutes=1)
out = cv2.VideoWriter(file_path, fourcc, float(FPS), (int(cap.get(3)), int(cap.get(4))))
sending_email = False
detected_objects = dict()
detected_objects['person'] = 0
while dt.now() < stop_at:
ret, frame = cap.read()
if dt.now().second != cur_second:
cur_second = dt.now().second
remaining_scans = IMAGE_SCANS_PER_SECOND
if detected_objects['person'] >= 3 and not sending_email:
sending_email = True
status = cv2.imwrite('detected_person.jpg', frame)
email_thread = threading.Thread(target=send_email, args=(), kwargs={})
email_thread.start()
if detected_objects['person'] <= 3 and remaining_scans > 0:
remaining_scans -= 1
bbox, label, conf = cv.detect_common_objects(frame)
if 'person' in label:
detected_objects['person'] += 1
if ret:
out.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
cv2.destroyAllWindows()
out.release()
sys.exit(0)
else:
cap.release()
cv2.destroyAllWindows()
break
# Release the capture
out.release()
if sending_email:
email_thread.join()