-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds.py
231 lines (161 loc) · 6.67 KB
/
ds.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
import queue, threading, time
import requests, json, random, logging
import numpy as np
import cv2
import enum
from datetime import datetime
import itertools
import math
from time import sleep
# from videocapture import VideoCapture
from ha import HAConnect
from dahua import Dahua
from ipc import *
from gk import *
gt_pad, gb_pad, gs_pad = 30, 20, 10
class QueuedFrame:
def __init__(self, frame, area):
self._frame = frame
self._area = area
@property
def frame(self):
return self._frame
@property
def area(self):
return self._area
class DeepStack:
def __init__(self, address, mode, handler):
self._FR_request = "http://"+address+"/v1/vision/face/recognize"
self._FD_request = "http://"+address+"/v1/vision/face"
self._OD_request = "http://"+address+"/v1/vision/detection"
self._mode = mode
self._handler = handler
self._fps = 0
self._queue = queue.Queue()
self._thread = threading.Thread(target=self.loop)
self._thread.daemon = True
self._thread.start()
def loop(self):
begin = time.time()
i = 0
while (True):
i += 1
try:
qf = self._queue.get(True, 10)
except:
qf = None
pass
if qf:
_, buf = cv2.imencode(".jpg", qf.frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
if self._mode == Mode.FR:
response = requests.post(self._FR_request, files={"image":buf.tobytes()}).json()
self.FR_parse(response, qf.frame, qf.area)
elif self._mode == Mode.OD:
response = requests.post(self._OD_request, files={"image":buf.tobytes()}).json()
self.OD_parse(response, qf.frame, qf.area)
elif self._mode == Mode.FD:
response = requests.post(self._FD_request, files={"image":buf.tobytes()}).json()
self.FD_parse(response, qf.frame, qf.area)
if time.time() - begin >= 10:
self._fps = float(i/10)
i = 0
begin = time.time()
@property
def fps(self):
return self._fps
@property
def processed(self):
return 0
def process_frame(self, frame, area):
# print (area)
qf = QueuedFrame(frame, area)
if not self._queue.empty():
try:
self._queue.get_nowait()
except queue.Empty:
pass
self._queue.put(qf)
def FR_parse(self, response, frame, area):
try:
if not bool(response["success"]):
return False
if not len(response["predictions"]) > 0:
return False
h, w, c = frame.shape
for face in response["predictions"]:
y_max = int(face["y_max"])
y_min = int(face["y_min"])
x_max = int(face["x_max"])
x_min = int(face["x_min"])
confidence = float(face["confidence"])
user = face["userid"]
if float(y_min/h) > 0.7:
return True
t_pad, b_pad, s_pad = gt_pad, gb_pad, gs_pad
if t_pad > y_min: t_pad = y_min
if (y_max - y_min < 90) or (x_max - x_min < 90):
continue
self._handler(Recognition(Mode.FR, frame[y_min - t_pad:y_max + b_pad, x_min - s_pad:x_max + s_pad], user, confidence, area))
return True
except Exception as e:
print(datetime.now().strftime("%H:%M:%S")+" "+e.__str__())
return False
def FD_parse(self, response, frame, area):
try:
if not bool(response["success"]):
return
if not len(response["predictions"]) > 0:
return
h, w, c = frame.shape
for face in response["predictions"]:
y_max = int(face["y_max"])
y_min = int(face["y_min"])
x_max = int(face["x_max"])
x_min = int(face["x_min"])
confidence = float(face["confidence"])
if float(y_min/h) > 0.7:
return True
if confidence < FACE_THRESHOLD or confidence > 1.0:
continue
t_pad, b_pad, s_pad = gt_pad, gb_pad, gs_pad
if t_pad > y_min: t_pad = y_min
self._handler(Recognition(Mode.FD, frame[y_min - t_pad:y_max + b_pad, x_min - s_pad:x_max + s_pad], "face", confidence, area))
except Exception as e:
print(datetime.now().strftime("%H:%M:%S")+" "+e.__str__())
return
def OD_parse(self, response, frame, area):
try:
if not bool(response["success"]):
return
if not len(response["predictions"]) > 0:
return
h, w, c = frame.shape
for prediction in response["predictions"]:
label = prediction["label"]
confidence = float(prediction["confidence"])
y_max = int(prediction["y_max"])
y_min = int(prediction["y_min"])
x_max = int(prediction["x_max"])
x_min = int(prediction["x_min"])
if label == "person":
if (float(y_max/h) < 0.2):
continue
if area == Areas.Chodnik:
if float(y_min/h) > 0.87:
continue
elif area == Areas.Tiguan:
if float(y_min/h) > 0.50:
continue
elif area == Areas.Touareg:
if (float(y_min/h) > 0.60):
continue
self._handler(Recognition(Mode.OD, frame[y_min:y_max, x_min:x_max], label, confidence, area))
elif label in VEHICLES:
if (float(y_max/h) < 0.4) or (float(x_min/w) > 0.52):
continue
self._handler(Recognition(Mode.OD, frame[y_min:y_max, x_min:x_max], label, confidence, area))
except Exception as e:
print(datetime.now().strftime("%H:%M:%S")+" "+e.__str__())
# sleep(1)
return