8
4

More than 1 year has passed since last update.

Py-Featで顔検出と表情認識やーる(Python 3.9、Windows 10)

Last updated at Posted at 2022-12-05

はじめに

Py-Featで顔検出と表情認識やっていきまーす

開発環境

  • Windows 10 PC
  • Python 3.9

導入

1.py-featをインストールします

pip install py-feat

2.下記プログラムを実行します。画像から一つの顔を検出します。

single_face_detection.py
from feat import Detector
import numpy as np
import cv2

detector = Detector(
    face_model="retinaface",
    landmark_model="mobilefacenet",
    au_model='jaanet', # ['svm', 'logistic', 'jaanet']
    emotion_model="resmasknet",
)

# single_face_prediction = detector.detect_image("single_face.jpg")
single_face_prediction = detector.detect_image("single_face.jpg", outputFname = "output.csv")

print(single_face_prediction.facebox)
print(single_face_prediction.aus)
print(single_face_prediction.emotions)
print(single_face_prediction.facepose) # (in degrees)
# figs = single_face_prediction.plot_detections(poses=True)
figs = single_face_prediction.plot_detections(faces='aus', muscles=True)
print(len(figs)) # 1

figs[0].canvas.draw()
image = np.array(figs[0].canvas.renderer.buffer_rgba())
# image = np.array(figs[0].canvas.renderer._renderer)
image = cv2.cvtColor(image, cv2.COLOR_RGBA2BGR)

cv2.imshow("image", image)
cv2.waitKey(0)

3.顔検出結果の矩形

FaceRectX FaceRectY FaceRectWidth FaceRectHeight FaceScore
162.010315 120.175117 137.789398 202.156555 0.998934

4.ランドマークの予測結果

AU01 AU02 AU04 AU06 AU07 AU10 AU12 AU14 AU15 AU17 AU23 AU24
0.634789 0.963166 0.001228 0.847978 0.823023 0.06328 0.995102 0.212514 0.034145 0.00111 0.00223 0.000003

5.表情認識結果

anger disgust fear happiness sadness surprise neutral
0.000574 0.000032 0.001309 0.972357 0.000069 0.025577 0.000082

6.顔向き

Pitch Roll Yaw
3.200458 -3.116152 5.953827

7.結果をプロットした画像
output.jpg

8.その他

detector.detect_imageの引数にoutputFnameを指定すると結果をcsvで保存できます。

figs = single_face_prediction.plot_detections(faces='aus', muscles=True)はエラーが出ました。
IndexError: index 15 is out of bounds for axis 0 with size 12

au_modelがsvmのときはエラーが出ました。モデルがダウンロードできないようです。
urllib.error.HTTPError: HTTP Error 404: Not Found

ここにモデルリストがあります。

お疲れ様でした

8
4
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
8
4