-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (61 loc) · 2.1 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
import cv2
from deepface import DeepFace
from uuid import getnode as get_mac
import requests
import base64
import time
from datetime import datetime, timezone
import json
mac = get_mac()
def convert(obj, image):
x = obj["region"]["x"]
y = obj["region"]["y"]
x2 = x + obj["region"]["w"]
y2 = y + obj["region"]["h"]
# cv2_imshow(frame4[y:y2,x:x2])
img = image[y:y2,x:x2]
obj["image"] = base64.b64encode(cv2.imencode('.jpg', img)[1]).decode()
obj["mac"] = mac
obj["device"] = device_name
obj["camera"] = "/".join(rtsp_video.split("/")[-2:])
obj["datetime_utc"] = str(datetime.now(timezone.utc))
obj["full_image"] = base64.b64encode(cv2.imencode('.jpg', show_img(image, obj))[1]).decode()
return obj
def save_obj(url, data):
requests.post(url, json=data)
def show_img(img, obj):
cv2.rectangle(img, (obj['region']['x'],obj['region']['y']), (obj['region']['x'] + obj['region']['w'],obj['region']['y'] + obj['region']['h']), (0,255,0), 3)
return cv2.resize(img, (0, 0), fx = 0.3, fy = 0.3)
loaded_config = {}
while(True):
print("Checking for " + str(datetime.now(timezone.utc)))
try:
config = json.load(open('config.json'))
if(loaded_config != config):
loaded_config = config
print("loaded config:", config)
rtsp_video = config["rtsp_video"]
send_url = config["send_url"]
device_name = config["device_name"]
try:
cap = cv2.VideoCapture(rtsp_video)
ret, frame = cap.read()
try:
objs = DeepFace.analyze(img_path = frame,
actions = ['age', 'gender', 'race', 'emotion'], detector_backend = 'retinaface'
)
print("Face(s) detected")
print(objs)
objs_with_metadata = [convert(obj, frame) for obj in objs]
# print(objs_with_metadata)
save_obj(send_url, objs)
print("save completed")
except Exception as e:
print(e)
except Exception as e2:
print("could not read video")
print(e2)
except Exception as e3:
print("could not load config, it should be a file named config.json with rtsp_video, send_url and device_name")
print(e3)
time.sleep(2)