-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pyw
95 lines (78 loc) · 2.85 KB
/
main.pyw
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import time
import datetime
import json
import requests
from dotenv import load_dotenv
import os
load_dotenv()
#environment variables
REFRESH_TOKEN = os.getenv("REFRESH_TOKEN")
CLIENT_ID = os.getenv("CLIENT_ID")
CLIENT_SECRET = os.getenv("CLIENT_SECRET")
cap = cv2.VideoCapture(1)
#change the number depending on which camera you will use, usually it's either 0 or 1
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
body_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_fullbody.xml")
detection = False
detection_stopped_time = None
timer_started = False
SECONDS_TO_RECORD_AFTERR_DETECTION = 3
frame_size = (int(cap.get(3)), int(cap.get(4)))
fourcc = cv2.VideoWriter_fourcc(*"jpeg")
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
bodies = face_cascade.detectMultiScale(gray, 1.1, 4)
current_time = datetime.datetime.now().strftime("%d-%m-%Y-%H-%M")
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")
out = cv2.VideoWriter(f"{current_time}.png", fourcc, 15, frame_size)
print("Press 'Q' to Exit")
elif detection:
if timer_started:
if time.time() - detection_stopped_time >= SECONDS_TO_RECORD_AFTERR_DETECTION:
detection = False
timer_started = False
out.release()
else:
timer_started = True
detection_stopped_time = time.time()
if detection:
out.write(frame)
##################### gDrive Upload ##########################
authorization_url = "https://oauth2.googleapis.com/token"
params = {
"grant_type": "refresh_token",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"refresh_token": REFRESH_TOKEN
}
r = requests.post(authorization_url, data=params)
access_token = None
if r.ok:
access_token = r.json()['access_token']
headers = {"Authorization": f"Bearer {access_token}"}
para = {
"name": f"{current_time}.png",
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open(f"{current_time}.png", "rb")
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
##################### gDrive Upload ##########################
break
out.release()
cap.release()
cv2.destroyAllWindows()