-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detector.py
81 lines (53 loc) · 2.56 KB
/
face_detector.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
import mediapipe as mp
import concurrent.futures
import cv2
class FaceDetector:
def __init__(self):
self.mp_face_detector = None
self.drawing = None
def __enter__(self):
print('__enter__')
return self
def my_face_detection(self):
self.mp_face_detector = mp.solutions.face_detection
self.mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
with self.mp_face_detector.FaceDetection(
model_selection=0, min_detection_confidence=0.5
) as face_detection:
while cap.isOpened():
success, camera = cap.read()
if not success:
raise IOError('ignoring empty camera frame')
continue
camera = cv2.cvtColor(cv2.flip(camera, 1), cv2.COLOR_BGR2RGB)
camera.flags.writeable = False
self.results = face_detection.process(camera)
camera.flags.writeable = True
camera = cv2.cvtColor(camera, cv2.COLOR_RGB2BGR)
if self.results.detections:
for detection in self.results.detections:
self.mp_drawing.draw_detection(camera, detection)
cv2.imshow('face detector', camera)
if cv2.waitKey(1) & 0xFF==ord("q"):
break
cap.release()
cv2.destroyAllWindows()
def camera_position(self, camera, face_num=0, draw=True):
land_mark_list = []
if self.results.detections:
my_camera = self.results.detections[face_num]
for id, lm in enumerate(my_camera.landmark):
height, width, channels = camera.shape
cx, cy = int(lm.x*width), int(lm.y*height)
land_mark_list.append([id, cx, cy])
if draw:
cv2.circle(camera, (cx, cy), 15, (255, 0, 255),cv2.FILLED)
return land_mark_list
def __exit__(self, type, value, traceback):
return isinstance(value, TypeError)
if __name__ == "__main__":
with concurrent.futures.ThreadPoolExecutor() as executor:
with FaceDetector() as fd:
executor.map(fd.my_face_detection())
executor.map(fd.camera_position())