图45.3.2.1 人脸识别实验流程图
45.3.3 main.py代码
main.py中的脚本代码如下所示:
from board import board_info
from fpioa_manager import fm
from maix import GPIO
import lcd
import sensor
import gc
from maix import KPU
fm.register(board_info.KEY0, fm.fpioa.GPIOHS0)
key0 = GPIO(GPIO.GPIOHS0, GPIO.IN, GPIO.PULL_UP)
save_feature = False
def key_irq_handler(key):
global key0
global save_feature
time.sleep_ms(20)
if key is key0 and key.value() == 0:
save_feature = True
key0.irq(key_irq_handler, GPIO.IRQ_FALLING, GPIO.WAKEUP_NOT_SUPPORT, 7)
lcd.init()
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.set_hmirror(False)
anchor = (0.1075, 0.126875, 0.126875, 0.175, 0.1465625, 0.2246875, 0.1953125, 0.25375, 0.2440625, 0.351875, 0.341875, 0.4721875, 0.5078125, 0.6696875, 0.8984375, 1.099687, 2.129062, 2.425937)
names = ['face']
# 构造并初始化人脸检测KPU对象
face_detecter = KPU()
face_detecter.load_kmodel("/sd/KPU/face_detect_320x240.kmodel")
face_detecter.init_yolo2(anchor, anchor_num=len(anchor) // 2, img_w=320, img_h=240, net_w=320, net_h=240, layer_w=10, layer_h=8, threshold=0.5, nms_value=0.2, classes=len(names))
features = []
score_threshold = 80
# 构造并初始化人脸特征提取KPU对象
feature_extractor = KPU()
feature_extractor.load_kmodel('/sd/KPU/feature_extraction.kmodel')
# 按指定比例扩展矩形框
def extend_box(x, y, w, h, scale):
x1 = int(x - scale * w)
x2 = int(x + w - 1 + scale * w)
y1 = int(y - scale * h)
y2 = int(y + h - 1 + scale * h)
x1 = x1 if x1 > 0 else 0
x2 = x2 if x2 < (320 - 1) else (320 - 1)
y1 = y1 if y1 > 0 else 0
y2 = y2 if y2 < (240 - 1) else (240 - 1)
return x1, y1, x2 - x1 + 1, y2 - y1 + 1
while True:
img = sensor.snapshot()
face_detecter.run_with_output(img)
faces = face_detecter.regionlayer_yolo2()
for face in faces:
# 框出人脸位置
x, y, w, h = extend_box(face[0], face[1], face[2], face[3], 0.08)
img.draw_rectangle(x, y, w, h, color=(0, 255, 0))
# 计算人脸特征并于保存的人脸特征进行比对获取相似度得分
scores = []
max_score = 0
face_img = img.cut(x, y, w, h)
resize_img = face_img.resize(64, 64)
resize_img.pix_to_ai()
feature = feature_extractor.run_with_output(resize_img, get_feature=True)
for i in range(len(features)):
score = KPU.feature_compare(features[i], feature)
scores.append(score)
# 计算得分的最大值
if len(scores) is not 0:
max_score = max(scores)
# 根据阈进行人脸识别
if max_score > score_threshold:
img.draw_rectangle(x, y, w, h, color=(0, 255, 0))
img.draw_string(x + 2, y + 2, str(scores.index(max(scores))), color=(0, 255, 0), scale=1.5)
# 根据中断按键进行人脸特征录入
if save_feature is True:
save_feature = False
features.append(feature)
del scores
del max_score
del face_img
del resize_img
del feature
lcd.display(img)
gc.collect()
可以看到一开始是先初始化了LCD和摄像头,并分别构造并初始化了用于人脸检测和人脸特征提取的KPU对象。
然后便是在一个循环中不断地获取摄像头输出的图像,首先将图像进行人脸检测,检测图像中存在的人脸,接着对人脸图像进行人脸特征提取,然后将提取到的人脸特征与先前保存的人脸特征进行对比,若对比得到高于指定的阈值,则表示能够识别出人脸,通过在获取到人脸特征后可以根据需要进行人脸特征的录入,最后将以上所有的分析检测结果在图像上进行绘制,然后在LCD上显示图像。
45.4 运行验证
将DNK210开发板连接CanMV IDE,点击CanMV IDE上的“开始(运行脚本)”按钮后,将摄像头对准人脸,让其采集到人脸图像,接着按下KEY0按键来录入人脸的特征,录入多张人脸特征后,可以看到LCD上显示了人脸识别的结果,每张人脸根据其特征比对得分,得到一个ID号,ID号与人脸录入的顺序有关,如下图所示: