-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathface_recognize.py
70 lines (59 loc) · 2.65 KB
/
face_recognize.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
import cv2, face_recognition, pickle
from .db import get_db
class face_recognize:
def __init__(self):
pass
def face_recognize(self, image, repainting=True):
'''
对image进行人脸识别
:param image: 待处理的图片
:param repainting: 若为False,则采用上一次的处理方式
:return: 处理后的图片
'''
ratio = 0.25
small_image = cv2.resize(image, (0,0), fx=ratio, fy=ratio)
rgb_small_image = small_image[:, :, ::-1]
if repainting:
self.face_locations = face_recognition.face_locations(rgb_small_image)
self.face_encodings = face_recognition.face_encodings(rgb_small_image, self.face_locations)
self.face_ids = []
self.face_important = []
for face_encoding in self.face_encodings:
matches = face_recognition.compare_faces(self.known_face_encodings, face_encoding, tolerance=0.4)
id = "Unknown"
important = "C"
if True in matches:
first_match_index = matches.index(True)
id = self.known_face_id[first_match_index]
important = "A" if self.know_face_important[first_match_index] else "B"
self.face_ids.append(id)
self.face_important.append(important)
for (top, right, bottom, left), id, important in zip(self.face_locations, self.face_ids, self.face_important):
top *= int(1.0/ratio)
right *= int(1.0/ratio)
bottom *= int(1.0/ratio)
left *= int(1.0/ratio)
if important == "A":
color = (0,0,255)
elif important == "B":
color = (0,255,0)
else:
color = (255, 0, 0)
cv2.rectangle(image, (left, top), (right, bottom), color, 2)
cv2.rectangle(image, (left, bottom - 35), (right, bottom), color, cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(image, id, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
return image
def update_criminal_information(self):
'''更新罪犯数据'''
db = get_db()
criminals = db.execute(
'SELECT id, encoding, important FROM criminal ORDER BY rank'
)
self.known_face_id = []
self.known_face_encodings = []
self.know_face_important = []
for criminal in criminals:
self.known_face_id.append(criminal['id'])
self.known_face_encodings.append(pickle.loads(criminal['encoding']))
self.know_face_important.append(criminal['important'])