-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkthrough_docs.py
65 lines (42 loc) · 1.81 KB
/
walkthrough_docs.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
import mediapipe as mp
import cv2
mp_face_detection = mp.solutions.face_detection
mp_drawing = mp.solutions.drawing_utils
image_files = []
with mp_face_detection.FaceDetection(
model_selection=1, min_detection_confidence=0.5) as face_detection:
for idx, file in enumerate(image_files):
image = cv2.imread(file)
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)) # converting BGR to RGB
if not results.detections:
continue
annotated_image = image.copy()
for detection in results.detections:
print('Nose tip')
print(mp_face_detection.get_key_point(
detection, mp_face_detection.FaceKeyPoint.NOSE_TIP
))
mp_drawing.draw_detection(annotated_image, detection)
cv2.imwrite('tmp/annotated_image' + str(idx) + '.png', annotated_image)
# webcam input
cap = cv2.VideoCapture(0)
with mp_face_detection.FaceDetection(
model_selection=0, min_detection_confidence=0.5
) as face_detection:
while cap.isOpened():
success, image = cap.read()
if not success:
print('Ignoring empty camera frame')
continue
image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)
image.flags.writeable = False
results = face_detection.process(image)
image.flags.writeable = True
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if results.detections:
for detection in results.detections:
mp_drawing.draw_detection(image, detection)
cv2.imshow('Face Detector', image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()