視頻演示
燒錄key_gen獲取機(jī)器碼
Burn key_gen to get machine code
1. 首先下載 key_gen 固件 并解壓的到 key_gen_v1.2.bin
Download at The First key_gen Firmware and IT to the unzip key_gen_v1.2.bin
使用kflash_gui燒錄key_gen_v1.2.bin
The Use kflash_gui to Flashkey_gen_v1.2.bin
打開串口終端可以看到如下輸出
Open the serial terminal and you can see the following output
Please Send Bellow Data to Sipeed --> support@:
844871aac6843da7a3******591d27bcd77d7b
Generate key end
其中第二行 844871aac6843da7a3******591d27bcd77d7b
即為我們所需要的機(jī)器碼。
Line at The SECOND, the WHERE 844871aac6843da7a3******591d27bcd77d7b
IS WE need at The Machine code.
注意: 機(jī)器碼信息會在開發(fā)板上電第一時(shí)間顯示,如果沒有看到輸出的機(jī)器碼信息,請保持串口終端打開并按reset鍵重啟開發(fā)板。
Note: The machine code information will be displayed on the development board immediately after power on If you do not see the output machine code information, please keep the serial port terminal open and press the reset key to restart the development board..
從Maixhub模型平臺下載人臉識別模型
Download face recognition model from Maixhub model platform
1. 訪問Maixhub 模型平臺首頁, 注冊并登錄。
At The from Visit Maixhub Model Platform Homepage, the Register and login.
選擇[MaixPy 人臉識別模型]
(https://www./index.php/index/index/detail/id/235) 點(diǎn)擊下載按鈕。
The SELECT MaixPy Face Recognition Model the Click the Button at The download.
在跳轉(zhuǎn)的頁面中輸入上一步獲得的機(jī)器碼
Enter the machine code obtained in the previous step in the page that is jumped to.
點(diǎn)擊提交即可獲得人臉識別模型
Click Submit to get a face recognition model.
注意:此步點(diǎn)擊提交后需耐心等待約30秒,未開始下載前請不要關(guān)閉頁面。
Note: Please wait patiently for about 30 seconds after clicking Submit in this step Please do not close the page before downloading..
(可選步驟)可以將獲得的kfpkg后綴改為zip, 解壓獲得三個(gè)模型文件以及Maixpy精簡版固件。用戶可以自行將模型拷貝到SD卡中加載模型。kfpkg中打包的Maixpy固件可能會版本過時(shí), 用戶可以自行替換為最新的精簡版固件。
(Optional the STEP) Obtained by You CAN Change at The kfpkg suffix to ZIP, and Decompress IT to Obtain Three Model Files and Maixpy Lite Firmware. The Users CAN Copy to SD Card at The Model to the Load at The Model by Themselves. At The Version of Maixpy Packaged in kfpkg May BE Firmware outdated, and the replace the Users CAN IT at The Latest with the Streamlined Firmware.
注意:目前不支持在完整版Maixpy固件中使用人臉識別模型
Note: Face recognition models are not currently supported in the full version of Maixpy firmware
燒錄人臉識別模型
Burning face recognition model
1. 使用 kflash_gui 燒錄上一步獲取的kfpkg模型
Use kflash_gui to flash the kfpkg model obtained in the previous step.
燒錄完成后,開發(fā)板顯示 MaixPy 歡迎界面。
After the programming is complete, the development board displays the MaixPy welcome interface.
使用 MaixPy IDE 運(yùn)行 MaixPy 人臉識別腳本
Run MaixPy Face Recognition Script with MaixPy IDE
安裝 MaixPy IDE, 具體見MiaxPy IDE.
Install the MaixPy IDE, See MiaxPy IDE for the Details .
從 Github 倉庫獲取人臉識別腳本 demo_face_recognition.py
Recognition Script at The face GET demo_face_recognition.py from Github at The Repository
使用 MaixPy IDE 打開人臉識別腳本, 連接開發(fā)板, 上傳腳本到開發(fā)板, 可以看到到人臉識別已經(jīng)在MaixPy 上成功運(yùn)行起來了。 用戶可以修改我們的識別腳本, 來創(chuàng)造更有趣的應(yīng)用。
The Use MaixPy IDE to Open at The face Recognition Script, Connect at The Development Board, the Upload at The Script to at The Development Board, and you CAN See that at The face Recognition has been successfully RUN ON MaixPy. The Users CAN the Modify Our Recognition Script to the Create More interesting Applications.
人臉識別腳本解讀
Interpretation of face recognition script
import sensor,image,lcd # import 相關(guān)庫
import KPU as kpu
import time
from Maix import FPIOA,GPIO
task_fd = kpu.load(0x200000) # 從flash 0x200000 加載人臉檢測模型
task_ld = kpu.load(0x300000) # 從flash 0x300000 加載人臉五點(diǎn)關(guān)鍵點(diǎn)檢測模型
task_fe = kpu.load(0x400000) # 從flash 0x400000 加載人臉196維特征值模型
clock = time.clock() # 初始化系統(tǒng)時(shí)鐘,計(jì)算幀率
key_pin=16 # 設(shè)置按鍵引腳 FPIO16
fpioa = FPIOA()
fpioa.set_function(key_pin,FPIOA.GPIO7)
key_gpio=GPIO(GPIO.GPIO7,GPIO.IN)
last_key_state=1
key_pressed=0 # 初始化按鍵引腳 分配GPIO7 到 FPIO16
def check_key(): # 按鍵檢測函數(shù),用于在循環(huán)中檢測按鍵是否按下,下降沿有效
global last_key_state
global key_pressed
val=key_gpio.value()
if last_key_state == 1 and val == 0:
key_pressed=1
else:
key_pressed=0
last_key_state = val
lcd.init() # 初始化lcd
sensor.reset() #初始化sensor 攝像頭
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1) #設(shè)置攝像頭鏡像
sensor.set_vflip(1) #設(shè)置攝像頭翻轉(zhuǎn)
sensor.run(1) #使能攝像頭
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025) #anchor for face detect 用于人臉檢測的Anchor
dst_point = [(44,59),(84,59),(64,82),(47,105),(81,105)] #standard face key point position 標(biāo)準(zhǔn)正臉的5關(guān)鍵點(diǎn)坐標(biāo) 分別為 左眼 右眼 鼻子 左嘴角 右嘴角
a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor) #初始化人臉檢測模型
img_lcd=image.Image() # 設(shè)置顯示buf
img_face=image.Image(size=(128,128)) #設(shè)置 128 * 128 人臉圖片buf
a=img_face.pix_to_ai() # 將圖片轉(zhuǎn)為kpu接受的格式
record_ftr=[] #空列表 用于存儲當(dāng)前196維特征
record_ftrs=[] #空列表 用于存儲按鍵記錄下人臉特征, 可以將特征以txt等文件形式保存到sd卡后,讀取到此列表,即可實(shí)現(xiàn)人臉斷電存儲。
names = ['Mr.1', 'Mr.2', 'Mr.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9' , 'Mr.10'] # 人名標(biāo)簽,與上面列表特征值一一對應(yīng)。
while(1): # 主循環(huán)
check_key() #按鍵檢測
img = sensor.snapshot() #從攝像頭獲取一張圖片
clock.tick() #記錄時(shí)刻,用于計(jì)算幀率
code = kpu.run_yolo2(task_fd, img) # 運(yùn)行人臉檢測模型,獲取人臉坐標(biāo)位置
if code: # 如果檢測到人臉
for i in code: # 迭代坐標(biāo)框
# Cut face and resize to 128x128
a = img.draw_rectangle(i.rect()) # 在屏幕顯示人臉方框
face_cut=img.cut(i.x(),i.y(),i.w(),i.h()) # 裁剪人臉部分圖片到 face_cut
face_cut_128=face_cut.resize(128,128) # 將裁出的人臉圖片 縮放到128 * 128像素
a=face_cut_128.pix_to_ai() # 將猜出圖片轉(zhuǎn)換為kpu接受的格式
#a = img.draw_image(face_cut_128, (0,0))
# Landmark for face 5 points
fmap = kpu.forward(task_ld, face_cut_128) # 運(yùn)行人臉5點(diǎn)關(guān)鍵點(diǎn)檢測模型
plist=fmap[:] # 獲取關(guān)鍵點(diǎn)預(yù)測結(jié)果
le=(i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h())) # 計(jì)算左眼位置, 這里在w方向-10 用來補(bǔ)償模型轉(zhuǎn)換帶來的精度損失
re=(i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h())) # 計(jì)算右眼位置
nose=(i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h())) #計(jì)算鼻子位置
lm=(i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h())) #計(jì)算左嘴角位置
rm=(i.x()+int(plist[8]*i.w()), i.y()+int(plist[9]*i.h())) #右嘴角位置
a = img.draw_circle(le[0], le[1], 4)
a = img.draw_circle(re[0], re[1], 4)
a = img.draw_circle(nose[0], nose[1], 4)
a = img.draw_circle(lm[0], lm[1], 4)
a = img.draw_circle(rm[0], rm[1], 4) # 在相應(yīng)位置處畫小圓圈
# align face to standard position
src_point = [le, re, nose, lm, rm] # 圖片中 5 坐標(biāo)的位置
T=image.get_affine_transform(src_point, dst_point) # 根據(jù)獲得的5點(diǎn)坐標(biāo)與標(biāo)準(zhǔn)正臉坐標(biāo)獲取仿射變換矩陣
a=image.warp_affine_ai(img, img_face, T) #對原始圖片人臉圖片進(jìn)行仿射變換,變換為正臉圖像
a=img_face.ai_to_pix() # 將正臉圖像轉(zhuǎn)為kpu格式
#a = img.draw_image(img_face, (128,0))
del(face_cut_128) # 釋放裁剪人臉部分圖片
# calculate face feature vector
fmap = kpu.forward(task_fe, img_face) # 計(jì)算正臉圖片的196維特征值
feature=kpu.face_encode(fmap[:]) #獲取計(jì)算結(jié)果
reg_flag = False
scores = [] # 存儲特征比對分?jǐn)?shù)
for j in range(len(record_ftrs)): #迭代已存特征值
score = kpu.face_compare(record_ftrs[j], feature) #計(jì)算當(dāng)前人臉特征值與已存特征值的分?jǐn)?shù)
scores.append(score) #添加分?jǐn)?shù)總表
max_score = 0
index = 0
for k in range(len(scores)): #迭代所有比對分?jǐn)?shù),找到最大分?jǐn)?shù)和索引值
if max_score < scores[k]:
max_score = scores[k]
index = k
if max_score > 85: # 如果最大分?jǐn)?shù)大于85, 可以被認(rèn)定為同一個(gè)人
a = img.draw_string(i.x(),i.y(), ("%s :%2.1f" % (names[index], max_score)), color=(0,255,0),scale=2) # 顯示人名 與 分?jǐn)?shù)
else:
a = img.draw_string(i.x(),i.y(), ("X :%2.1f" % (max_score)), color=(255,0,0),scale=2) #顯示未知 與 分?jǐn)?shù)
if key_pressed == 1: #如果檢測到按鍵
key_pressed = 0 #重置按鍵狀態(tài)
record_ftr = feature
record_ftrs.append(record_ftr) #將當(dāng)前特征添加到已知特征列表
break
fps =clock.fps() #計(jì)算幀率
print("%2.1f fps"%fps) #打印幀率
a = lcd.display(img) #刷屏顯示
#kpu.memtest()
#a = kpu.deinit(task_fe)
#a = kpu.deinit(task_ld)
#a = kpu.deinit(task_fd)