-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
134 lines (114 loc) · 3.4 KB
/
demo.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# SPDX-FileCopyrightText: 2024 Idiap Research Institute <[email protected]>
#
# SPDX-FileContributor: Pierre Vuillecard <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-only
import argparse
import time
import cv2
from src.utils_demo import (
FaceDetectorCV2,
FaceDetectorYUNET,
FaceTracker,
HGPredictor,
MediapipePredictor,
TrackHandler,
Visualizer,
)
def main(args):
# Instantiate
if args.face_detector == "YUNET":
face_detector = FaceDetectorYUNET()
elif args.face_detector == "CV2":
face_detector = FaceDetectorCV2()
else:
raise ValueError("Invalid face detector")
face_tracker = FaceTracker()
face_predictor = MediapipePredictor()
hg_predictor = HGPredictor("cpu")
track_handler = TrackHandler(face_tracker)
visualizer = Visualizer(
draw_bbox=args.draw_bbox,
draw_landmarks=args.draw_landmarks,
draw_head_gesture=args.draw_head_gesture,
)
vid = cv2.VideoCapture(0)
while True:
# Capture the video frame
# by frame
start = time.time()
ret, frame = vid.read()
frame_time = int(round(time.time() * 1000))
# Detect faces
detection = face_detector.process_image(frame)
# Track the faces
face_tracker.update(detection, frame_time)
# Get the current track id
track_id = face_tracker.get_tracks()
# Detect the face landmarks in those faces
for track in track_id:
face_prediction = face_predictor.process_face(
frame, face_tracker.tracks_store[track][-1]
)
face_tracker.tracks_store[track][-1].add_prediction(face_prediction)
output_track = hg_predictor.process(face_tracker, track_id)
track_handler.add_track_prediction(output_track)
# Draw the output
frame = visualizer.process(frame, face_tracker, output_track)
# Draw real time fps
end = time.time()
fps = 1 / (end - start)
cv2.putText(
frame,
f"FPS {int(fps)} ",
(1650, 60),
cv2.FONT_HERSHEY_SIMPLEX,
2,
(0, 255, 0),
2,
cv2.LINE_AA,
)
# Display the resulting frame
cv2.imshow("frame", frame)
# the 'q' button is set as the
# quitting button you may use any
# desired button of your choice
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run demo")
parser.add_argument(
"--face_detector",
type=str,
default="CV2",
help="Face detector CV2 is faster but less accurate if face more than 2m away",
choices=["YUNET", "CV2"],
)
parser.add_argument(
"--draw_bbox",
"--db",
type=bool,
default=True,
help="Draw bounding box",
)
parser.add_argument(
"--draw_landmarks",
"--dl",
type=bool,
default=True,
help="Draw landmarks",
)
parser.add_argument(
"--draw_head_gesture",
"--dhg",
type=bool,
default=True,
help="Draw head gesture",
)
args = parser.parse_args()
# run demo head gesture
main(args)