はじめに
MediaPipeやっていきます。
Pythonは以下に対応している
- Face Detection
- Face Mesh
- Hands
- Pose
- Holistic
- Objectron
開発環境
- Windows 10
- Python 3.6
- anaconda
導入
pip install mediapipe
FaceDetection
import cv2
import numpy as np
import mediapipe as mp
import time
def draw(image, detection):
h, w = image.shape[:2]
left = int(detection.location_data.relative_bounding_box.xmin * w)
top = int(detection.location_data.relative_bounding_box.ymin * h)
right = int(left + detection.location_data.relative_bounding_box.width * w)
bottom = int(top + detection.location_data.relative_bounding_box.height * h)
cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 1)
cv2.putText(image, str(detection.label_id[0]) + ":" + str(round(detection.score[0], 3)), (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 1, cv2.LINE_AA)
# 右目, 左目, 鼻, 口, 右耳, 左耳
for keypoint in detection.location_data.relative_keypoints:
cv2.circle(image, (int(keypoint.x * w), int(keypoint.y * h)), 2, (0, 255, 0), -1)
return image
cap = cv2.VideoCapture(0)
min_detection_confidence = 0.7
face_detection = mp.solutions.face_detection.FaceDetection(min_detection_confidence=min_detection_confidence)
while True:
start = time.time()
ret, image = cap.read()
if not ret:
break
draw_image = image.copy()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
results = face_detection.process(image)
if results.detections is not None:
for detection in results.detections:
draw_image = draw(draw_image, detection)
fps = round(1.0 / (time.time() - start))
cv2.putText(draw_image, str(fps), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 1, cv2.LINE_AA)
cv2.imshow("draw_image", draw_image)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
実行動画
FaceDetection #MediaPipe #Python pic.twitter.com/of2ZbffDi6
— がちもとさん@熊本 (@sotongshi) May 30, 2021
お疲れ様でした