-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.py
99 lines (83 loc) · 2.75 KB
/
recorder.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
import os
import cv2
from detector import Detector
from dotenv import load_dotenv
load_dotenv()
detection_model = os.getenv("detection_model")
detector = Detector(detection_model)
# Get the candidate name and create a directory with that name in Database
candidate_name = input("Enter the candidate's name :")
candidate_name = candidate_name.replace(" ", "_")
database_location = os.path.join("Database", candidate_name)
os.mkdir(database_location)
# Start the recording
video = cv2.VideoCapture(0)
cv2.namedWindow("Recorder")
# Creating Scale Factor Trackbar for Face Detector
# Default Value : 7
cv2.createTrackbar(
"(scaleFactor - 1) * 10",
"Recorder",
0, 20,
lambda scaleFactor_x10:
detector.update_face_rectangle_parameteres(scaleFactor = ((scaleFactor_x10 / 10) + 1))
)
# Creating Minimum Neighbor Trackbar for Face Detector
# Default Value : 3
cv2.createTrackbar(
"minNeighbors - 1",
"Recorder",
0, 20,
lambda minNeighbors:
detector.update_face_rectangle_parameteres(minNeighbors = (minNeighbors + 1))
)
sample_count = 0
recording = False
while True:
ret, raw = video.read()
ret, frame = video.read()
label_frame = detector.make_box_indicator(frame, candidate_name)
if recording:
# Recording Circle
center = (30, 30)
radius = 3
border_color = (0, 0, 255)
thickness = 5
cv2.circle(frame, center, radius, border_color, thickness)
# Instructions
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
font_color = (0, 0, 255)
font_thickness = 1
cv2.putText(
frame,
"Slowly turn your face around",
(45, 35), font, font_scale, font_color, font_thickness, cv2.LINE_AA
)
# Storing the sample if face is detected
face_rectangle = detector.get_face_rectangle(raw)
if detector.is_face_rectangle_detected(face_rectangle):
sample_location = os.path.join(database_location, str(sample_count + 1) + ".png")
cv2.imwrite(sample_location, raw)
sample_count += 1
else:
# Instructions
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.5
font_color = (0, 255, 0)
font_thickness = 1
cv2.putText(
frame,
"Press R to start the recording",
(50, 50), font, font_scale, font_color, font_thickness, cv2.LINE_AA
)
cv2.putText(
frame,
"Close the terminal to QUIT",
(50, 70), font, font_scale, font_color, font_thickness, cv2.LINE_AA
)
cv2.imshow("Recorder", label_frame)
if cv2.waitKey(1) & 0xFF == ord("r"):
recording = True if recording is False else False
video.release()
cv2.destroyAllWindows()