-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
412 lines (335 loc) · 14.7 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from multiprocessing import Process, Array, Value
from flask import Flask, render_template, Response, request, send_from_directory
from datetime import datetime, timedelta
import time
import threading
import imutils
from imutils.video import FPS
import ctypes
import cv2
import re
import base64
import asyncio
import io
import glob
import os
import sys
import uuid
import requests
from urllib.parse import urlparse
from io import BytesIO
from PIL import Image, ImageDraw
from azure.cognitiveservices.vision.face import FaceClient
from msrest.authentication import CognitiveServicesCredentials
import numpy as np
import json
from multiprocessing import Process, Array
import threading
from dotenv import load_dotenv
load_dotenv()
GPU_AVAILABLE = False
MAX_FPS = 25
CONFIDENCE_ = 0.5
NMS_THRESH_ = 0.3
VERBOSE = False
app = Flask(__name__)
class FaceRecognition():
CAMARA_SOURCE = 0
AZURE_KEY = os.getenv("AZURE_KEY1")
AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT")
BACK_ENDPOINT = os.getenv("BACK_ENDPOINT")
BACK_ENABLE = False if os.getenv("BACK_ENABLE") == "False" else True
def __init__(self, inputFrame, frameShape):
self.inputFrame = inputFrame
self.camaraSrc = FaceRecognition.CAMARA_SOURCE
self.frameShape = frameShape
# Create an authenticated FaceClient.
self.face_client = FaceClient(FaceRecognition.AZURE_ENDPOINT, CognitiveServicesCredentials(FaceRecognition.AZURE_KEY))
self.face_list_id = 'camara0'
self.face_list_count = 0
self.deleteFaceList()
self.initDeployment()
def deleteFaceList(self):
self.face_client.face_list.delete(self.face_list_id, custom_headers=None, raw=False)
def initDeployment(self):
self.listCheck()
self.runModel()
def listCheck(self):
currentLists = self.face_client.face_list.list()
for currentList in currentLists:
if currentList.face_list_id == self.face_list_id:
currentList = self.face_client.face_list.get(self.face_list_id)
if currentList.persisted_faces == None:
self.face_list_count = 0
else:
self.face_list_count = len(currentList.persisted_faces)
return
self.face_client.face_list.create(self.face_list_id, name = self.face_list_id)
def runModel(self):
# Free Trail 20 callsPerMinute
callsPerMinute = 6
callsInterval = 60 / callsPerMinute
timeStart = time.time()
while True:
if time.time() - timeStart >= callsInterval:
detatecionThread = threading.Thread(target=self.callDetection, args=())
detatecionThread.start()
timeStart = time.time()
def callDetection(self):
sharedFrame = np.frombuffer(self.inputFrame, dtype=np.uint8)
sharedFrame = sharedFrame.reshape(self.frameShape)
img_data = cv2.imencode('.jpg', sharedFrame)[1].tobytes()
detected_faces = self.face_client.face.detect_with_stream(image=io.BytesIO(img_data), detectionModel='detection_02')
if not detected_faces:
return
def getRectangleList(detected_face):
rect = detected_face.face_rectangle
left = rect.left
top = rect.top
width = rect.width
height = rect.height
return [left, top, width, height]
def getRectangle(detected_face):
rect = detected_face.face_rectangle
left = rect.left
top = rect.top
right = left + rect.width
bottom = top + rect.height
return ((left, top), (right, bottom))
def getCropRectangle(detected_face, frameShape):
rect = detected_face.face_rectangle
left = rect.left
top = rect.top
right = left + rect.width
bottom = top + rect.height
oldLeft = left
oldTop = top
oldRight = right
oldBottom = bottom
left = max(left - ((oldRight - oldLeft) / 2), 0)
right = min(right + ((oldRight - oldLeft) / 2), frameShape[1])
top = max(top - ((oldBottom - oldTop) / 2), 0)
bottom = min(bottom + ((oldBottom - oldTop) / 2), frameShape[0])
return (left, top, right, bottom)
for face in detected_faces:
face_id = face.face_id
target_face = getRectangleList(face)
persisted_face_id = None
if self.face_list_count > 0 :
candidates = self.face_client.face.find_similar(face_id, self.face_list_id, max_num_of_candidates_returned=1)
for candidate in candidates:
persisted_face_id = candidate.persisted_face_id
if persisted_face_id == None:
response = self.face_client.face_list.add_face_from_stream(self.face_list_id, image = io.BytesIO(img_data), target_face = target_face)
self.face_list_count += 1
persisted_face_id = response.persisted_face_id
img = Image.open(BytesIO(img_data))
img = img.crop(getCropRectangle(face, self.frameShape))
img.save("faces/" + persisted_face_id + ".jpg")
img.show()
print("New Face:", persisted_face_id)
self.postPerson(persisted_face_id)
else:
print("Exisiting Face:", persisted_face_id)
self.postRecord(persisted_face_id)
def postRecord(self, persisted_face_id):
if not FaceRecognition.BACK_ENABLE:
return
url = FaceRecognition.BACK_ENDPOINT + "/api/v1/records/"
data = {'id': persisted_face_id}
response = requests.post(url, json = data)
if (response.status_code != 200):
print("Request Records StatusCode", response.status_code)
response = response.json()
if response['status'] != 1:
print(response['msg'])
def postPerson(self, persisted_face_id):
if not FaceRecognition.BACK_ENABLE:
return
url = FaceRecognition.BACK_ENDPOINT + "/api/v1/persons/"
data = {'id': persisted_face_id}
response = requests.post(url, json = data)
if (response.status_code != 200):
print("Request Records StatusCode", response.status_code)
response = response.json()
if response['status'] != 1:
print(response['msg'])
class CamaraRead:
def __init__(self, source, originalFrame, originalFrameShape, stampedFrame, stampedFrameShape):
self.source = source
self.originalFrame = originalFrame
self.originalFrameShape = originalFrameShape
self.stampedFrame = stampedFrame
self.stampedFrameShape = stampedFrameShape
# Frame FileName Variables
self.lastRequest = None
self.secondId = None
self.lastPath = None
self.mainLoop()
@staticmethod
def addText(frame, size, now):
font = cv2.FONT_HERSHEY_SIMPLEX
position = (int(size[1]-100),int(size[0]-50))
fontScale = 0.5
fontColor = (255,255,255)
lineType = 2
cv2.rectangle(frame, (position[0], position[1] - 15), (position[0] + 80, position[1] + 5), (0,0,0), -1)
cv2.putText(frame,
"Camara 1",
position,
font,
fontScale,
fontColor,
lineType)
position = (20,20)
cv2.rectangle(frame, (position[0], position[1] - 15), (position[0] + 210, position[1] + 5), (0,0,0), -1)
date = now.strftime("%B %d, %Y %H:%M:%S")
cv2.putText(frame,
date,
position,
font,
fontScale,
fontColor,
lineType)
def getFilename(self):
now = datetime.now()
date = now.strftime("%m_%d_%Y")
hour = now.strftime("%H")
minute = now.strftime("%M")
if (self.lastRequest == now.strftime("%m_%d_%Y_%H_%M_%S")):
self.secondId += 1
else:
self.secondId = 1
self.lastRequest = now.strftime("%m_%d_%Y_%H_%M_%S")
path = 'images/' + date + '/' + hour + '/' + minute + '/'
if not os.path.exists(path):
os.makedirs(path)
if self.lastPath is not None and self.lastPath != path:
joinThread = threading.Thread(target=self.joinMinute, args=(self.lastPath,))
joinThread.start()
if self.lastPath is None or self.lastPath != path:
self.lastPath = path
return path + now.strftime("%S") + "_" + str(self.secondId)
def joinMinute(self, path):
os.system('joinFiles.sh ' + path)
def mainLoop(self):
source = self.source
originalFrame = self.originalFrame
originalFrameShape = self.originalFrameShape
stampedFrame = self.stampedFrame
stampedFrameShape = self.stampedFrameShape
print("[INFO] opening video file...", source)
vs = cv2.VideoCapture(source)
while vs.read()[0] == False:
vs = cv2.VideoCapture(self.source)
# vs.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'H264'))
originalFrame_ = np.frombuffer(originalFrame, dtype=np.uint8)
originalFrame_ = originalFrame_.reshape(originalFrameShape)
stampedFrame_ = np.frombuffer(stampedFrame, dtype=np.uint8)
stampedFrame_ = stampedFrame_.reshape(stampedFrameShape)
while True:
lastTime = time.time()
status, frame = vs.read()
if not status:
vs = cv2.VideoCapture(source)
vs.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'H264'))
continue
frameStamped = frame.copy()
CamaraRead.addText(frameStamped, stampedFrameShape, datetime.now())
# Display the resulting frame
if VERBOSE:
cv2.imshow('Stream', frameStamped)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 75]
cv2.imencode('.jpg', frameStamped, encode_param)[1].tofile(self.getFilename() + '.jpg')
# cv2.imwrite(self.getFilename() + '.jpg', frameStamped)
# cv2.imwrite(self.getFilename() + '.jpg', frameStamped, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
frame = imutils.resize(frame, width=500)
originalFrame_[:] = frame
stampedFrame_[:] = frameStamped
while time.time() - lastTime < 1 / MAX_FPS:
pass
@app.route('/camara')
def camaraStream():
deltaDays = int(request.args.get("deltaDays"))
deltaHours = int(request.args.get("deltaHours"))
deltaMinutes = int(request.args.get("deltaMinutes"))
deltaSeconds = int(request.args.get("deltaSeconds"))
deltaFPS = int(request.args.get("deltaFPS"))
#Video streaming route. Put this in the src attribute of an img tag
return Response(showFrame(deltaDays, deltaHours, deltaMinutes, deltaSeconds, deltaFPS), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/faces/<path:filename>')
def image(filename):
return send_from_directory('faces/', filename)
def showFrame(deltaDays, deltaHours, deltaMinutes, deltaSeconds, deltaFPS):
outputFrame = np.frombuffer(stampedFrame, dtype=np.uint8)
outputFrame = outputFrame.reshape(stampedFrameShape)
lastPath = None
frameStatus = None
lastSecond = None
lastSecondId = 0
secondCache = []
lastTime = None
while True:
lastTime = time.time()
if deltaDays != 0 or deltaHours != 0 or deltaMinutes != 0 or deltaSeconds != 0:
frame = None
dateRequested = datetime.now() + timedelta(days=-deltaDays, hours=-deltaHours, minutes=-deltaMinutes, seconds=-deltaSeconds)
date = dateRequested.strftime("%m_%d_%Y")
hour = dateRequested.strftime("%H")
minute = dateRequested.strftime("%M")
second = dateRequested.strftime("%S")
miliseconds = dateRequested.strftime("%f")
path = 'images/' + date + '/' + hour + '/' + minute + '/'
if os.path.exists(path):
if lastPath is None or path != lastPath or not frameStatus:
cap = cv2.VideoCapture(path + "out.mp4")
lastPath = path
totalExpectedFrames = 25 * 60
totalFrames = cap.get(7)
secondsNotRecorded = int(round((totalFrames - totalExpectedFrames) / 25))
if int(second) >= secondsNotRecorded:
lastSecondId = min((int(second) - secondsNotRecorded) * 25 + int(round(int(miliseconds) * 25 / 999999)), totalFrames)
cap.set(1, lastSecondId)
frameStatus, frame = cap.read()
if frame is None:
frame = np.zeros((stampedFrameShape[0], stampedFrameShape[1], 3), np.uint8)
CamaraRead.addText(frame, stampedFrameShape, dateRequested)
ret, buffer = cv2.imencode('.jpg', frame)
else:
ret, buffer = cv2.imencode('.jpg', outputFrame)
frame_ready = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_ready + b'\r\n') # concat frame one by one and show result
while time.time() - lastTime < 1 / max(MAX_FPS + deltaFPS, 1):
pass
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
# Start Init
if __name__ == "__main__":
source = 0
originalFrame = None
originalFrameShape = None
stampedFrame = None
stampedFrameShape = None
cap = cv2.VideoCapture(source)
ret, frame = cap.read()
stampedFrameShape = frame.shape
frame = imutils.resize(frame, width=500)
originalFrameShape = frame.shape
cap.release()
originalFrame = Array(ctypes.c_uint8, originalFrameShape[0] * originalFrameShape[1] * originalFrameShape[2], lock=False)
stampedFrame = Array(ctypes.c_uint8, stampedFrameShape[0] * stampedFrameShape[1] * stampedFrameShape[2], lock=False)
if True:
processAReference = Process(target=CamaraRead, args=(source, originalFrame, originalFrameShape, stampedFrame, stampedFrameShape))
processAReference.start()
processBReference = Process(target=FaceRecognition, args=(originalFrame, originalFrameShape))
processBReference.start()
from waitress import serve
app.debug=True
app.use_reloader=False
serve(app, host="0.0.0.0", port=8081)
print("Server 0.0.0.0:8081")