-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
79 lines (49 loc) · 1.91 KB
/
detect.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
import time
import cv2
from face_detection.scrfd.detector import SCRFD
from face_detection.yolov5_face.detector import Yolov5Face
detector = Yolov5Face(model_file="face_detection/yolov5_face/weights/yolov5m-face.pt")
# detector = SCRFD(model_file="face_detection/scrfd/weights/scrfd_2.5g_bnkps.onnx")
def main():
cap = cv2.VideoCapture(0)
start = time.time_ns()
frame_count = 0
fps = -1
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
size = (frame_width, frame_height)
video = cv2.VideoWriter(
"results/face-detection.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, size
)
while True:
_, frame = cap.read()
bboxes, landmarks = detector.detect(image=frame)
h, w, c = frame.shape
tl = 1 or round(0.002 * (h + w) / 2) + 1 # Line and font thickness
clors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255)]
for i in range(len(bboxes)):
x1, y1, x2, y2, score = bboxes[i]
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 146, 230), 2)
for id, key_point in enumerate(landmarks[i]):
cv2.circle(frame, tuple(key_point), tl + 1, clors[id], -1)
frame_count += 1
if frame_count >= 30:
end = time.time_ns()
fps = 1e9 * frame_count / (end - start)
frame_count = 0
start = time.time_ns()
if fps > 0:
fps_label = "FPS: %.2f" % fps
cv2.putText(
frame, fps_label, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2
)
video.write(frame)
cv2.imshow("Face Detection", frame)
if cv2.waitKey(25) & 0xFF == ord("q"):
break
video.release()
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(0)
if __name__ == "__main__":
main()