第四十三章 人臉屬性分析實驗
在上一章節中,介紹了利用maix.KPU模塊實現了人臉口罩佩戴檢測,本章將繼續介紹利用maix.KPU模塊實現的人臉屬性分析。通過本章的學習,讀者將學習到人臉屬性分析應用在CanMV上的實現。
本章分為如下幾個小節:
43.1 maix.KPU模塊介紹
43.2 硬件設計
43.3 程序設計
43.4 運行驗證
43.1 maix.KPU模塊介紹
有關maix.KPU模塊的介紹,請見第39.1小節《maix.KPU模塊介紹》。
43.2 硬件設計
43.2.1 例程功能
1. 獲取攝像頭輸出的圖像,并送入KPU進行人臉檢測,接著對檢測到的人臉分別進行人臉五關鍵點檢測和屬性分析,最后將所有的檢測結果和原始圖像一同在LCD上進行顯示。
43.2.2 硬件資源
本章實驗內容,主要講解maix.KPU模塊的使用,無需關注硬件資源。
43.2.3 原理圖
本章實驗內容,主要講解maix.KPU模塊的使用,無需關注原理圖。
43.3 程序設計
43.3.1 maix.KPU模塊介紹
有關maix.KPU模塊的介紹,請見第43.1小節《maix.KPU模塊介紹》。
43.3.2 程序流程圖? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
43.3.3 main.py代碼
main.py中的腳本代碼如下所示:
import lcd
import sensor
import gc
from maix import KPU
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))
# 構造并初始化人臉五關鍵點檢測KPU對象
ld5_kpu = KPU()
ld5_kpu.load_kmodel("/sd/KPU/ld5.kmodel")
pos_face_attr = ["Male ", "MouthOpen ", "Smiling ", "Glasses"]
neg_face_attr = ["Female ", "MouthClosed", "No Smile", "NoGlasses"]
# 構造并初始化人臉屬性分析KPU對象
fac_kpu = KPU()
fac_kpu.load_kmodel("/sd/KPU/fac.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))
# 計算人臉五關鍵點
face_img = img.cut(x, y, w, h)
resize_img = face_img.resize(128, 128)
resize_img.pix_to_ai()
output = ld5_kpu.run_with_output(resize_img, getlist=True)
for i in range(len(output) // 2):
point_x = int(KPU.sigmoid(output[2 * i]) * w + x)
point_y = int(KPU.sigmoid(output[2 * i + 1]) * h + y)
img.draw_cross(point_x, point_y, size=5, color=(0, 0, 255))
# 計算人臉屬性
output = fac_kpu.run_with_output(resize_img, getlist=True)
for i in range(len(output)):
if KPU.sigmoid(output) > 0.5:
img.draw_string(x + w + 2, y + i * 16 + 2, "%s" % (pos_face_attr), color=(255, 0, 0), scale=1.5)
else:
img.draw_string(x + w + 2, y + i * 16 + 2, "%s" % (neg_face_attr), color=(0, 0, 255), scale=1.5)
del face_img
del resize_img
lcd.display(img)
gc.collect()
可以看到一開始是先初始化了LCD和攝像頭,并分別構造并初始化了用于人臉檢測、人臉五關鍵點、人臉屬性分析的KPU對象。
然后便是在一個循環中不斷地獲取攝像頭輸出的圖像,首先將圖像進行人臉檢測,檢測圖像中存在的人臉,接著對人臉圖像進行五關鍵點檢測,分析出人臉五關鍵點的位置,接著是對人臉圖像進行屬性分析,分析人臉的性別、嘴巴開合、是否微笑、是否佩戴眼鏡等屬性,最后將以上所有的分析檢測結果在圖像上進行繪制,然后在LCD上顯示圖像。
43.4 運行驗證
將DNK210開發板連接CanMV IDE,點擊CanMV IDE上的“開始(運行腳本)”按鈕后,將攝像頭對準人臉,讓其采集到人臉圖像,隨后便能在LCD上看到攝像頭輸出的圖像,同時能看到圖像上標注了人臉位置、人臉五關鍵點位置、人臉屬性等信息,如下圖所示:? ? ?