-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_face.py
78 lines (73 loc) · 2.42 KB
/
get_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
78
"""
Module: Record Face
"""
import cv2
import time
from pathlib import Path
from yolov5_face.detect_face import draw_result
import argparse
from face_recognition.face import Face_Model
face_model = Face_Model()
cap = cv2.VideoCapture(0)
id_name = input("Enter ID: ")
name = input("Enter Name: ")
list_image = []
print("\t\t\tSTART RECORD FACE\t\t\t")
state = 'Normal'
while True:
start = time.time()
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, 1)
h, w, _ = frame.shape
key = cv2.waitKey(1) & 0xFF
# create region
thresh = 200
xmin, ymin = w//2 - thresh, h//2 - thresh
xmax, ymax = w//2 + thresh, h//2 + thresh
cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2, cv2.LINE_AA)
blur = cv2.blur(frame, (15, 15))
for i in range(5):
blur = cv2.blur(blur, (15, 15))
blur[ymin:ymax, xmin:xmax] = frame[ymin:ymax, xmin:xmax]
frame = blur
frame = cv2.putText(frame, f' MENU: Record: R -- Save Data: S -- Clear: P -- New ID: N -- Exit: E',
(10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
if key == ord('r'):
state = 'Record'
elif key == ord('s'):
state = 'Stop'
elif key == ord('p'):
state = 'Record Again'
list_image.clear()
elif key == ord('n'):
state = 'New ID'
elif key == ord('e'):
break
if state == 'Record':
if h > 1080 and w > 1920:
frame = cv2.resize(frame, (1920, 1080), interpolation=cv2.INTER_AREA)
h, w, _ = frame.shape
bbox, label, label_id, score, landmark = face_model.detect(frame)
if len(bbox) != 0:
list_image.append(frame.copy())
for idx, box in enumerate(bbox):
draw_result(frame, box, '', score[idx], landmark[idx])
elif state == 'Stop':
face_model.create_data(list_image, name, id_name)
state = 'Normal'
elif state == 'Record again':
list_image.clear()
state = 'Normal'
elif state == 'New ID':
list_image.clear()
id_name = input("Enter ID: ")
name = input("Enter Name: ")
state = 'Normal'
frame = cv2.putText(frame, f'ID: {name}', (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
frame = cv2.putText(frame, f'State: {state}', (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow('video', frame)
cap.release()
cv2.destroyAllWindows()
print('Finish!')