forked from avinashk20/student-surveillance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_utils.py
63 lines (50 loc) · 1.85 KB
/
model_utils.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
import cv2
from deepface import DeepFace
import mongo_utils
MODEL = 'Facenet'
DETECTOR = 'dlib'
def getRepresentations(img): # img = numpy array (BGR) or base64 encoded
try:
obj = DeepFace.represent(
img_path=img,
model_name=MODEL,
detector_backend=DETECTOR,
)
return obj
except Exception:
print('no face detected')
return Exception
def getEmbedding(img):
try:
obj = getRepresentations(img)
return obj[0]['embedding']
except Exception:
return Exception
def drawRectangle(img, facial_area):
x = facial_area['x']
y = facial_area['y']
w = facial_area['w']
h = facial_area['h']
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
return img
def findSuspects(input_img):
try:
input_representations = getRepresentations(input_img)
print('Detected', len(input_representations), 'face(s)')
found_suspect_ids = [] # stores ids of matched suspects
matched_rep_ids = [] # stores corresponding indexes of matched representations in input
rep_index = 0
for rep in input_representations:
res = mongo_utils.findMatch(rep['embedding'])
if(len(res) > 0):
matched_rep_ids.append(rep_index)
found_suspect_ids.append(res[0]['_id'])
rep_index += 1
# drawing a bounding box around found suspects
suspects_img = input_img
for id in matched_rep_ids:
facial_area = input_representations[id]['facial_area']
suspects_img = drawRectangle(suspects_img, facial_area)
return {'found_suspect_ids': found_suspect_ids, 'suspects_img': suspects_img}
except Exception:
return Exception