-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.py
77 lines (52 loc) · 2.52 KB
/
face.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
import cv2
import face_recognition
import numpy as np
# Доступ к камере.
video_capture = cv2.VideoCapture(0)
image = face_recognition.load_image_file("Your_image_for_detection.jpg")
face_encoding = face_recognition.face_encodings(image)[0]
# Массив с энкодиннгами изображений, которые можно распознавать.
known_face_encodings = [
face_encoding,
]
known_face_names = [
"Here_your_name",
]
face_locations = []
process_this_frame_every_second_time = True
face_encodings = []
face_names = []
while True:
# Кадр с камеры.
ret, frame_bgr = video_capture.read()
rgb_frame = frame_bgr[:, :, ::-1]
# Берем каждый второй кадр для экономии.
if process_this_frame_every_second_time:
# Поиск всех лиц. (Для Классификатора).
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# Распознаные лица.
face_names = []
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# Смотрим на дистанции (Насколько лицо с камеры совпадает с доступным лицом в списке known_face_encodings).
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
# Чтобы обрабатывать только каждый второй кадр.
process_this_frame_every_second_time = not process_this_frame_every_second_time
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(frame_bgr, (left, top), (right, bottom), (255, 0, 0), 2)
cv2.rectangle(frame_bgr, (left, int(bottom - 35*(bottom - top)/200)), (right, bottom), (255, 0, 0), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame_bgr, name, (left + 6, bottom - 6), font, (bottom - top) / 200, (255, 255, 255), 1)
# Вывод преобразованного изображения
cv2.imshow('Video', frame_bgr)
esc = 27
if cv2.waitKey(1) == esc:
break
video_capture.release()
cv2.destroyAllWindows()