0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ランドマークの表示

0
Posted at

導入と顔検出編

顔ランドマーク

顔のそれぞれの部位(目、口、鼻など)の位置を座標点のあつまりとして表したものです。

プログラム

import cv2
import glob
import os
import onnxruntime as ort
from insightface.app import FaceAnalysis

ort.preload_dlls(
    directory=r"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.0\bin\x64"
)

app = FaceAnalysis(name="buffalo_l", providers=["CUDAExecutionProvider", "CPUExecutionProvider"])
app.prepare(ctx_id=0, det_size=(640, 640))

img = cv2.imread("sample.png")

if img is None:
    print("画像を読み込めませんでした")
    exit()

faces = app.get(img)
print(f"検出した顔の数: {len(faces)}")

for filename in glob.glob("landmark_detect_*.png"):
    os.remove(filename)

for i, face in enumerate(faces):
    x1, y1, x2, y2 = face.bbox.astype(int)
    margin = 50
    x1 = max(0, x1 - margin)
    y1 = max(0, y1 - margin)
    x2 = min(img.shape[1], x2 + margin)
    y2 = min(img.shape[0], y2 + margin)

    face_crop = img[y1:y2, x1:x2]
    output = cv2.resize(face_crop, None, fx=4, fy=4, interpolation=cv2.INTER_NEAREST)

    landmark = face.landmark_2d_106
    for j, point in enumerate(landmark):
        x, y = point.astype(int)
        x = (x - x1) * 4
        y = (y - y1) * 4
        cv2.circle(output, (x, y), 2, (0, 0, 255), -1)
        cv2.putText(output, str(j), (x + 3, y + 12), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 127, 0), 2, cv2.LINE_AA)

    filename = f"landmark_detect_{i}.png"
    cv2.imwrite(filename, output)
    print(f"保存しました: {filename}")

検出した顔それぞれにランドマークを 106 個表示します。検出した顔をそれぞれクリップし、4 倍に拡大したのち、ランドマークの点と番号を打ちます。

サンプル

画像処理などでよく使われるフリー素材です。
sample.png

結果

landmark_large_0.png
ランドマークの番号は以下のようになっています。各点を調べることにより、顔の各部位の詳細な位置や形などがわかります。

番号 部位
0〜32 顔の輪郭
33〜42 画像左側の目
43〜51 画像左側の眉
52〜71 口・唇
72〜86 鼻
87〜96 画像右側の目
97〜105 画像右側の眉

主要な代表番号は以下のとおりです。代表番号の点の位置関係を調べる事により、おおまかな顔の傾きやどちらを向いているかなどを知ることができます。

番号 代表する部位の中心
38 画像左側の目付近
88 画像右側の目付近
86 鼻先付近
60 口の中心付近
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?