0
0

More than 3 years have passed since last update.

Mediapipeを使ってみた

Last updated at Posted at 2021-09-14

Mediapipeで顔認識をする話

インストール

まずはmediapipeのインストールから。pipインストールでok。

pip install mediapipe

MediaPipeでできること

  • 顔検出
  • 顔認識
  • 虹彩の追跡
  • 手、上半身、全身の骨格推定
  • 髪のセグメンテーション
  • 物体検出
  • トラッキング
  • 立体的な物体検出
  • KNIFT

今回行うのは上記の中にある顔検出です。

コード

import mediapipe
import cv2 
import numpy 
import mediapipe as mp
import math
import matplotlib.pyplot as plt

mp_drawing = mp.solutions.drawing_utils 

img = cv2.imread('顔検出したい画像のpath')
mp_face_detection = mp.solutions.face_detection

face_detection=mp_face_detection.FaceDetection(min_detection_confidence=0.1, model_selection=1) 
results = face_detection.process(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

DESIRED_HEIGHT = 480
DESIRED_WIDTH = 480
def resize_and_show(image):
    h,w = image.shape[:2]
    if h < w:
        img = cv2.resize(image, (DESIRED_WIDTH, math.floor(h/(w/DESIRED_WIDTH))))
    else:
        img = cv2.resize(image, (math.floor(w/(h/DESIRED_HEIGHT)), DESIRED_HEIGHT))
    img=cv2.cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    
    plt.imshow(img)
annotated= img.copy()
for detection in results.detections:
    annotated_image=mp_drawing.draw_detection(annotated, detection)
    resize_and_show(annotated)

このコードの実行時間は0.2321150302886963でした。なかなかの速度だと思います。
実行結果
out.jpeg

色々試したい

pip install ではなくgit cloneをしてコードを取得して現在遊んでいます。
顔の座標が入力されて矩形を書いているのがここの関数なので、この関数をいじって顔にモザイクをかけようとしています。
簡単にできるのですが、動画で試したらエラーが出たので、うまく動いたらだします。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0