自己打包kfpkg,試著整了好幾次,都是無(wú)法燒錄,只好不做第七步了,直接把前面獲得的人臉識(shí)別模型燒錄了
燒錄完成后,打開(kāi)IDE串口,確認(rèn)開(kāi)發(fā)板Maixpy固件的版本,好像是前期的穩(wěn)定版本V0.4.0
第九步:使用 MaixPy IDE 運(yùn)行 MaixPy 人臉識(shí)別腳本
Run MaixPy Face Recognition Script with MaixPy IDE
#MicroPython動(dòng)手做(09)——零基礎(chǔ)學(xué)MaixPy之人臉識(shí)別
#實(shí)驗(yàn)程序:簡(jiǎn)單的人臉識(shí)別之一
#MicroPython動(dòng)手做(09)——零基礎(chǔ)學(xué)MaixPy之人臉識(shí)別
#實(shí)驗(yàn)程序:簡(jiǎn)單的人臉識(shí)別之一
import sensor,image,lcd
import KPU as kpu
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(0)
sensor.run(1)
task = kpu.load(0x300000)
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
img_lcd=image.Image()
while(True):
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
if code:
for i in code:
a = img.draw_rectangle(i.rect())
a = lcd.display(img)
a = kpu.deinit(task)
運(yùn)行出錯(cuò),串口輸出
init i2c2
[MAIXPY]: find gc3028
True
encryp[MAIXPY]kpu: img w=320,h=240, but model w=128,h=128
Traceback (most recent call last):
File “”, line 21, in
ValueError: [MAIXPY]kpu: check img format err!
MicroPython v0.4.0-103-g913682433 on 2019-11-29; Sipeed_M1 with kendryte-k210
繼續(xù)實(shí)驗(yàn),調(diào)整更換程序
#MicroPython動(dòng)手做(09)——零基礎(chǔ)學(xué)MaixPy之人臉識(shí)別
#實(shí)驗(yàn)程序:簡(jiǎn)單的人臉識(shí)別之二
#MicroPython動(dòng)手做(09)——零基礎(chǔ)學(xué)MaixPy之人臉識(shí)別
#實(shí)驗(yàn)程序:簡(jiǎn)單的人臉識(shí)別之二
import sensor,image,lcd
import KPU as kpu
import time
from Maix import FPIOA,GPIO
task_fd = kpu.load(0x200000)
task_ld = kpu.load(0x300000)
task_fe = kpu.load(0x400000)
clock = time.clock()
key_pin=16
fpioa = FPIOA()
fpioa.set_function(key_pin,FPIOA.GPIO7)
key_gpio=GPIO(GPIO.GPIO7,GPIO.IN)
last_key_state=1
key_pressed=0
def check_key():
global last_key_state
global key_pressed
val=key_gpio.value()
key_pressed=0
if last_key_state == 1 and val == 0:
time.sleep(0.02) # debouncing
val=key_gpio.value() # read again
if val == 0:
key_pressed=1
last_key_state = val
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_hmirror(1)
sensor.set_vflip(1)
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
dst_point = [(44,59),(84,59),(64,82),(47,105),(81,105)] #standard face key point position
a = kpu.init_yolo2(task_fd, 0.5, 0.3, 5, anchor)
img_lcd=image.Image()
img_face=image.Image(size=(128,128))
a=img_face.pix_to_ai()
record_ftr=[]
record_ftrs=[]
names = ['Mr.1', 'Mr.2', 'Mr.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9' , 'Mr.10']
while(1):
check_key()
img = sensor.snapshot()
clock.tick()
code = kpu.run_yolo2(task_fd, img)
if code:
for i in code:
# 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_128=face_cut.resize(128,128)
a=face_cut_128.pix_to_ai()
#a = img.draw_image(face_cut_128, (0,0))
# Landmark for face 5 points
fmap = kpu.forward(task_ld, face_cut_128)
plist=fmap[:]
le=(i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h()))
re=(i.x()+int(plist[2]*i.w()), i.y()+int(plist[3]*i.h()))
nose=(i.x()+int(plist[4]*i.w()), i.y()+int(plist[5]*i.h()))
lm=(i.x()+int(plist[6]*i.w()), i.y()+int(plist[7]*i.h()))
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)
# align face to standard position
src_point = [le, re, nose, lm, rm]
T=image.get_affine_transform(src_point, dst_point)
a=image.warp_affine_ai(img, img_face, T)
a=img_face.ai_to_pix()
#a = img.draw_image(img_face, (128,0))
del(face_cut_128)
# calculate face feature vector
fmap = kpu.forward(task_fe, img_face)
feature=kpu.face_encode(fmap[:])
reg_flag = False
scores = []
for j in range(len(record_ftrs)):
score = kpu.face_compare(record_ftrs[j], feature)
scores.append(score)
max_score = 0
index = 0
for k in range(len(scores)):
if max_score < scores[k]:
max_score = scores[k]
index = k
if max_score > 85:
a = img.draw_string(i.x(),i.y(), ("%s :%2.1f" % (names[index], max_score)), color=(0,255,0),scale=2)
else:
a = img.draw_string(i.x(),i.y(), ("X :%2.1f" % (max_score)), color=(255,0,0),scale=2)
if key_pressed == 1:
key_pressed = 0
if len(record_ftrs) < len(names): # prevent appending more than the number of names.
record_ftr = feature
record_ftrs.append(record_ftr)
break
fps =clock.fps()
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)
運(yùn)行程序,可以識(shí)別,陰天情況下大約9-12fps
視頻記錄:人臉識(shí)別的小實(shí)驗(yàn)
https://v.youku.com/v_show/id_XNDYyMjA0NjcxMg==.html?spm=a2h0k.11417342.soresults.dtitle
實(shí)驗(yàn)場(chǎng)景圖
人臉識(shí)別腳本解讀
Interpretation of face recognition script
import sensor,image,lcd # import 相關(guān)庫(kù)
import KPU as kpu
import time
from Maix import FPIOA,GPIO
task_fd = kpu.load(0x200000) # 從flash 0x200000 加載人臉檢測(cè)模型
task_ld = kpu.load(0x300000) # 從flash 0x300000 加載人臉五點(diǎn)關(guān)鍵點(diǎn)檢測(cè)模型
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(): # 按鍵檢測(cè)函數(shù),用于在循環(huán)中檢測(cè)按鍵是否按下,下降沿有效
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 用于人臉檢測(cè)的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) #初始化人臉檢測(cè)模型
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=[] #空列表 用于存儲(chǔ)當(dāng)前196維特征
record_ftrs=[] #空列表 用于存儲(chǔ)按鍵記錄下人臉特征, 可以將特征以txt等文件形式保存到sd卡后,讀取到此列表,即可實(shí)現(xiàn)人臉斷電存儲(chǔ)。
names = ['Mr.1', 'Mr.2', 'Mr.3', 'Mr.4', 'Mr.5', 'Mr.6', 'Mr.7', 'Mr.8', 'Mr.9' , 'Mr.10'] # 人名標(biāo)簽,與上面列表特征值一一對(duì)應(yīng)。
while(1): # 主循環(huán)
check_key() #按鍵檢測(cè)
img = sensor.snapshot() #從攝像頭獲取一張圖片
clock.tick() #記錄時(shí)刻,用于計(jì)算幀率
code = kpu.run_yolo2(task_fd, img) # 運(yùn)行人臉檢測(cè)模型,獲取人臉坐標(biāo)位置
if code: # 如果檢測(cè)到人臉
for i in code: # 迭代坐標(biāo)框
# Cut face and resize to 128x128
a = img.draw_rectangle(i.rect()) # 在屏幕顯示人臉?lè)娇?/span>
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)檢測(cè)模型
plist=fmap[:] # 獲取關(guān)鍵點(diǎn)預(yù)測(cè)結(jié)果
le=(i.x()+int(plist[0]*i.w() - 10), i.y()+int(plist[1]*i.h())) # 計(jì)算左眼位置, 這里在w方向-10 用來(lái)補(bǔ)償模型轉(zhuǎn)換帶來(lái)的精度損失
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)位置處畫(huà)小圓圈
# 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) #對(duì)原始圖片人臉圖片進(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 = [] # 存儲(chǔ)特征比對(duì)分?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)): #迭代所有比對(duì)分?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: #如果檢測(cè)到按鍵
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)
橫過(guò)來(lái)效果好多了文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-611297.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-611297.html
到了這里,關(guān)于【雕爺學(xué)編程】MicroPython動(dòng)手做(09)——零基礎(chǔ)學(xué)MaixPy之人臉識(shí)別2的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!