face_tracking.py

# この作品はMITライセンスの下で提供されています。
# Copyright (c) 2013-2023 OpenMV LLC. 全著作権所有。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 顔追跡の例
#
# この例では、Haar Cascadeによって顔が検出された後、OpenMV Camのキーポイント機能を使って
# その顔を追跡する方法を示します。このスクリプトの前半では、frontalface Haar Cascadeを
# 使って画像内の顔を検出します。
# その後、キーポイント機能を使って自動的にその顔を学習し、
# 追跡します。キーポイントは、任意の対象を自動追跡するために使用できます。
import csi
import time
import image

csi0 = csi.CSI()
csi0.reset()
csi0.contrast(3)
csi0.gainceiling(16)
csi0.framesize(csi.VGA)
csi0.window((320, 240))
csi0.pixformat(csi.GRAYSCALE)

# センサーが安定するまで数フレームスキップします
csi0.snapshot(time=2000)

# Haar カスケードを読み込む
# デフォルトでは全ステージを使用します。ステージ数を減らすと高速になりますが精度は低下します。
face_cascade = image.HaarCascade("/rom/haarcascade_frontalface.cascade", stages=25)
print(face_cascade)

# 最初のキーポイント集合
kpts1 = None

# 顔を検出します!
while kpts1 is None:
    img = csi0.snapshot()
    img.draw_string((0, 0), "Looking for a face...")
    # 顔を検出します
    objects = img.find_features(face_cascade, threshold=0.5, scale=1.25)
    if objects:
        # ROIを全方向に31ピクセル拡張します
        face = (
            objects[0][0] - 31,
            objects[0][1] - 31,
            objects[0][2] + 31 * 2,
            objects[0][3] + 31 * 2,
        )
        # 検出した顔サイズをROIとしてキーポイントを抽出します
        kpts1 = img.find_keypoints(
            threshold=10, scale_factor=1.1, max_keypoints=100, roi=face
        )
        # 最初の顔の周りに矩形を描画します
        img.draw_rectangle(objects[0])

# キーポイントを描画します
print(kpts1)
img.draw_keypoints(kpts1, size=24)
img = csi0.snapshot()
time.sleep_ms(2000)

# FPSクロック
clock = time.clock()

while True:
    clock.tick()
    img = csi0.snapshot()
    # フレーム全体からキーポイントを抽出します
    kpts2 = img.find_keypoints(
        threshold=10, scale_factor=1.1, max_keypoints=100, normalized=True
    )

    if kpts2:
        # 最初のキーポイント集合と2番目のものを照合します
        c = image.match_descriptor(kpts1, kpts2, threshold=85)
        match = c[6]  # C[6]にはマッチした数が格納されています。
        if match > 5:
            img.draw_rectangle(c[2:6])
            img.draw_cross((c[0], c[1]), size=10)
            print(kpts2, "matched:%d dt:%d" % (match, c[7]))

    # FPS を描画
    img.draw_string((0, 0), "FPS:%.2f" % (clock.fps()))

results matching ""

    No results matching ""