さまざまな顔を区別する

動画チュートリアル 19 - さまざまな顔を区別する: https://singtown.com/learn/50033/

このセクションでは、LBP 機能を使用してさまざまな顔を区別する方法を説明します。

まず独自の画像ライブラリを構築する必要があります。まず、OpenMV U ディスクに新しいフォルダーを作成し (SD カードを挿入する必要があることに注意してください)、それに singtown という名前を付けます。次に、singtown フォルダー内に s1、s2、s3...sn という名前の n 個のサブフォルダーを作成します (n は )。画像ライブラリ全体の人数。以下に示すように:

次に、次のコードを実行して、さまざまな顔のサンプルを収集します。収集する際は背景をできるだけ均一にし、顔が画面全体に収まるように注意してください。人間の顔には、笑っている、笑っていない、真顔、傾いている、メガネをかけている、メガネをかけていないなどがあります。お一人様10~20枚までお選びいただけます。

# Snapshot Example
#
# Note: You will need an SD card to run this example.
#
# You can use your OpenMV Cam to save image files.

import sensor, image, machine
#import pyb

RED_LED_PIN = 1
BLUE_LED_PIN = 3

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others)
sensor.set_windowing((92,112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 2000)

num = 1 #设置被拍摄者序号,第一个人的图片保存到s1文件夹,第二个人的图片保存到s2文件夹,以此类推。每次更换拍摄者时,修改num值。

n = 20 #设置每个人拍摄图片数量。

#连续拍摄n张照片,每间隔3s拍摄一次。
while(n):
    #红灯亮
    machine.LED("LED_RED").on()
    #pyb.LED(RED_LED_PIN).on()
    sensor.skip_frames(time = 3000) # Give the user time to get ready.等待3s,准备一下表情。

    #红灯灭,蓝灯亮
    machine.LED("LED_RED").off()
    machine.LED("LED_BLUE").on()
    #pyb.LED(RED_LED_PIN).off()
    #pyb.LED(BLUE_LED_PIN).on()

    #保存截取到的图片到SD卡
    print(n)
    sensor.snapshot().save("singtown/s%s/%s.pgm" % (num, n) ) # or "example.bmp" (or others)

    n -= 1

    #pyb.LED(BLUE_LED_PIN).off()
    machine.LED("LED_BLUE").off()
    print("Done! Reset the camera to see the saved image.")

撮影後、OpenMV USB フラッシュ ドライブ内の画像は次のようになります (OpenMV USB フラッシュ ドライブに新しい画像/ファイルが書き込まれるたびに、表示する前に OpenMV をリセットする必要があることに注意してください)。

最後に、次のコードを実行して、現在のカメラの視野内で顔を識別し、現在のオブジェクトに最もよく一致する人物を出力します。

# Face recognition with LBP descriptors.
# See Timo Ahonen's "Face Recognition with Local Binary Patterns".
#
# Before running the example:
# 1) Download the AT&T faces database http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip
# 2) Exract and copy the orl_faces directory to the SD card root.


import sensor, time, image  

sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others)
sensor.set_windowing((92,112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 5000) #等待5s



#SUB = "s1"
NUM_SUBJECTS = 6 #图像库中不同人数,一共6人
NUM_SUBJECTS_IMGS = 20 #每人有20张样本图片

# 拍摄当前人脸。
img = sensor.snapshot()
#img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height()))
#d0为当前人脸的lbp特征
img = None
pmin = 999999
num=0

def min(pmin, a, s):
    global num
    if a<pmin:
        pmin=a
        num=s
    return pmin

for s in range(1, NUM_SUBJECTS+1):
    dist = 0
    for i in range(2, NUM_SUBJECTS_IMGS+1):
        img = image.Image("singtown/s%d/%d.pgm"%(s, i))
        d1 = img.find_lbp((0, 0, img.width(), img.height()))
        #d1为第s文件夹中的第i张图片的lbp特征
        dist += image.match_descriptor(d0, d1)#计算d0 d1即样本图像与被检测人脸的特征差异度。
    print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
    pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
    print(pmin)

print(num) # num为当前最匹配的人的编号。

実行結果は次の図に示すようになります。

results matching ""

    No results matching ""