MediaPipe 0.8 : Colab : MediaPipe 顔検出 (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 03/06/2021 (0.8.3)
* 本ページは、MediaPipe の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
- Windows PC のブラウザからご参加が可能です。スマートデバイスもご利用可能です。
人工知能研究開発支援 | 人工知能研修サービス | テレワーク & オンライン授業を支援 |
PoC(概念実証)を失敗させないための支援 (本支援はセミナーに参加しアンケートに回答した方を対象としています。) |
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション |
E-Mail:sales-info@classcat.com ; WebSite: https://www.classcat.com/ |
Facebook: https://www.facebook.com/ClassCatJP/ |
MediaPipe 0.8 : Colab : MediaPipe 顔検出
Python の MediaPipe 顔検出ソリューションの使用サンプルです (http://solutions.mediapipe.dev/face_detection もまた見てください)。
!pip install mediapipe
顔を含む任意の画像を Colab にアップロードします。web: https://unsplash.com/photos/JyVcAIUAcPM と https://unsplash.com/photos/auTAb39ImXg から 2 つのサンプル画像を取ります。
from google.colab import files
uploaded = files.upload()
import cv2
from google.colab.patches import cv2_imshow
# Read images with OpenCV.
images = {name: cv2.imread(name) for name in uploaded.keys()}
# Preview the images.
for name, image in images.items():
print(name)
cv2_imshow(image)
![]() |
![]() |
総ての MediaPipe ソリューション Python API サンプルは mp.solutions 下にあります。
MediaPipe Face Mesh ソリューションについては、mp_face_detection = mp.solutions.face_detection としてモジュールにアクセスできます。
初期化の間にパラメータ min_detection_confidence を変更しても良いです。パラメータについてのより多くの情報を得るためには help(mp_face_detection.FaceDetection) を実行します。
import mediapipe as mp
mp_face_detection = mp.solutions.face_detection
help(mp_face_detection.FaceDetection)
Help on class FaceDetection in module mediapipe.python.solutions.face_detection: class FaceDetection(mediapipe.python.solution_base.SolutionBase) | FaceDetection(min_detection_confidence=0.5) | | MediaPipe Face Detection. | | MediaPipe Face Detection processes an RGB image and returns a list of the | detected face location data. | | Please refer to | https://solutions.mediapipe.dev/face_detection#python-solution-api | for usage examples. | | Method resolution order: | FaceDetection | mediapipe.python.solution_base.SolutionBase | builtins.object | | Methods defined here: | | __init__(self, min_detection_confidence=0.5) | Initializes a MediaPipe Face Detection object. | | Args: | min_detection_confidence: Minimum confidence value ([0.0, 1.0]) for face | detection to be considered successful. See details in | https://solutions.mediapipe.dev/face_detection#min_detection_confidence. | | process(self, image: numpy.ndarray) ->| Processes an RGB image and returns a list of the detected face location data. | | Args: | image: An RGB image represented as a numpy ndarray. | | Raises: | RuntimeError: If the underlying graph throws any error. | ValueError: If the input image is not three channel RGB. | | Returns: | A NamedTuple object with a "detections" field that contains a list of the | detected face location data. | | ---------------------------------------------------------------------- | Methods inherited from mediapipe.python.solution_base.SolutionBase: | | __enter__(self) | A "with" statement support. | | __exit__(self, exc_type, exc_val, exc_tb) | Closes all the input sources and the graph. | | close(self) -> None | Closes all the input sources and the graph. | | ---------------------------------------------------------------------- | Data descriptors inherited from mediapipe.python.solution_base.SolutionBase: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
# Prepare DrawingSpec for drawing the face landmarks later.
mp_drawing = mp.solutions.drawing_utils
drawing_spec = mp_drawing.DrawingSpec(thickness=1, circle_radius=1)
with mp_face_detection.FaceDetection(
min_detection_confidence=0.5) as face_detection:
for name, image in images.items():
# Convert the BGR image to RGB and process it with MediaPipe Face Detection.
results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# Draw face detections of each face.
print(f'Face detections of {name}:')
if not results.detections:
continue
annotated_image = image.copy()
for detection in results.detections:
# print('Nose tip:')
# print(mp.python.solutions.face_detection.get_key_point(
# detection, mp_face_detection.FaceKeyPoints.NOSE_TIP))
mp_drawing.draw_detection(annotated_image, detection)
cv2_imshow(annotated_image)
以上