opencv / dlibを使用して、ライブストリームビデオで額(バウンディングボックス)の領域を取得する方法はありますか?

男性

私はライブストリーミングビデオから額の領域を取得するプロジェクトに取り組んでおり、この例のように額を使用して画像化し、トリミングするだけではありません。opencvとdlibを使用して額の領域を検出するにはどうすればよいですか?

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predict_path)


while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = detector(gray) #detects number of faces present

    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()
        
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)
        
        landmarks = predictor(gray, face)

        for n in range(68, 81):
            x = landmarks.part(n).x
            y = landmarks.part(n).y

            cv2.circle(frame, (x, y), 4, (0, 255, 0), -1) 
            

https://github.com/codeniko/shape_predictor_81_face_landmarks/blob/master/shape_predictor_81_face_landmarks.datを使用したランドマークを使用して額の領域を取得することができました

しかし、私が必要としているのは、ランドマークが額の領域を検出する場所にある長方形の境界ボックスです。これを取得することは可能ですか?そうでない場合は、額の領域を取得するにはどうすればよいですか。前もって感謝します。

Ahx

あなたはすでに次の方法で目的の座標を見つけています:

for face in faces:
    x1 = face.left()
    y1 = face.top()
    x2 = face.right()
    y2 = face.bottom()

    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

しかし、私が必要としているのは、ランドマークが額の領域を検出する場所にある長方形の境界ボックスです。

次に、y座標を変更します。

cv2.rectangle(frame, (x1, y1-100), (x2, y2-100), (0, 0, 255), 3)

更新

額のポイントに固執するには、最小landmark座標と最大座標を取得してから、長方形を描画する必要があります。

ステップ1:座標を取得する:


    1. 初期化x_ptsしてy_pts
    1. 店舗landmark(n)配列にポイント。
for n in range(68, 81):
    x = landmarks.part(n).x
    y = landmarks.part(n).y

    x_pts.append(x)
    y_pts.append(y)

    cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)

ステップ2:検出されたポイントの周りに長方形を描く


    1. 最小ポイントと最大ポイントを取得する
x1 = min(x_pts)
x2 = max(x_pts)
y1 = min(y_pts)
y2 = max(y_pts)

cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

結果:

ウェブカメラにズームすると:

ここに画像の説明を入力してください

私が遠くにいるとき:

ここに画像の説明を入力してください

コード:

import cv2
import dlib

cap = cv2.VideoCapture(0)

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_81_face_landmarks.dat")

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = detector(gray)  # detects number of faces present

    for face in faces:
        x1 = face.left()
        y1 = face.top()
        x2 = face.right()
        y2 = face.bottom()

        landmarks = predictor(gray, face)

        x_pts = []
        y_pts = []

        for n in range(68, 81):
            x = landmarks.part(n).x
            y = landmarks.part(n).y

            x_pts.append(x)
            y_pts.append(y)

            cv2.circle(frame, (x, y), 4, (0, 255, 0), -1)

        x1 = min(x_pts)
        x2 = max(x_pts)
        y1 = min(y_pts)
        y2 = max(y_pts)

        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 3)

    cv2.imshow("out", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ