人臉偵測&提示回歸中心點-k210

 人臉偵測&提示回歸中心點-k210

前言:

k210是能高速使用在機器學習和深度學習的微控板,為了不讓功能隱沒
所以得好好利用和研究。
因為手邊沒有適合的云台,所以我就想說先用模擬的方式來取代實際的
云台。

想法如下:

先做出人臉偵測,再提取坐[x,y,w.h]坐標,這4個要做什麼用?它可以利用
公式計算出在移動中的人臉中心值,還有LCD的中心點,那有了這些數
據之後,就可以判斷,偵測到的人臉離屏幕中心到達設定的容許點,提示
要往哪個方向回歸。

程式碼

import sensor, image, time, lcd
import KPU as kpu

#攝影鏡頭初使化
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)

#lcd初使化
lcd.init()

#紀錄時間初始化
clock=time.clock()

#載入模型
task=kpu.load("/sd/facedetect.kmodel")

#模型描参数
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987,
    5.3658, 5.155437, 6.92275, 6.718375, 9.01025)

#初始化yolo2
kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)

allow_value=15 #容許值
while True:
    clock.tick()
    img=sensor.snapshot()

    code=kpu.run_yolo2(task, img) #運行yolo2
    img.draw_line(160,0,160,240,color=(60,80,170),thickness=3)#畫面中準線
    #識別人臉並畫矩形
    maxArea=0
    max_i=0
    if code:
        for i in code:
            #print(i)
            #如果出現兩張人臉,選出最大值
            a=i.w()*i.h()
            img.draw_rectangle(i.rect())
            if a>maxArea:
                max_i=i
                maxArea=a
        faceData=max_i.rect()
        faceCenter=(faceData[0]+faceData[2]//2,
            faceData[1]+faceData[3]//2) #找出人臉中心
        facePoint=img.draw_circle(faceCenter[0],faceCenter[1],2,
                    color=(255,150,20),thickness=3) #畫出人臉中心圓點
        #中準點和人臉中心點
        moveAvg=img.draw_line(160,120,faceCenter[0],faceCenter[1],
        color=(60,80,170),thickness=2)
        print(faceCenter)
        if faceCenter[0]>sensor.width()//2+allow_value:
            print("向左回到中心值")
            img.draw_arrow((110,125,50,125),color=(2,255,255),
            size=20, thickness=7) #左
        elif faceCenter[0]<sensor.width()//2-allow_value:
            print("向右回到中心值")
            img.draw_arrow((190,125,250,125),color=(2,255,255),
            size=20, thickness=7) #右
        elif faceCenter[0]==sensor.width()//2:
            print("處於中心值")
        if faceCenter[1]<sensor.height()//2-allow_value:
            print("向下回到中心值")
            img.draw_arrow((150,160,150,210),color=(2,255,255),
            size=20, thickness=7) #下
        elif faceCenter[1]>sensor.height()//2+allow_value:
            print("向上到中心值")
            img.draw_arrow((150,90,150,40),color=(2,255,255),
            size=20, thickness=7) #上
        elif faceCenter[1]==sensor.height()//2:
            print("處於中心值")

    lcd.display(img)

解析


載入模組

import sensor, image, time, lcd
import KPU as kpu

#攝影鏡頭初使化

sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)

#lcd初使化

lcd.init()

#紀錄時間初始化

clock=time.clock()

#載入模型

task=kpu.load("/sd/facedetect.kmodel")

#模型描参数

anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987,
    5.3658, 5.155437, 6.92275, 6.718375, 9.01025)

#初始化yolo2

kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)


#偵測人臉,並框出人臉,在屏幕上畫出正中線,找出人臉中心點,和正中線做對比

allow_value=15 #容許值
while True:
    clock.tick()
    img=sensor.snapshot()

    code=kpu.run_yolo2(task, img) #運行yolo2
    img.draw_line(160,0,160,240,color=(60,80,170),thickness=3)#畫面中準線
    #識別人臉並畫矩形
    maxArea=0
    max_i=0
    if code:
        for i in code:
            #print(i)
            #如果出現兩張人臉,選出最大值
            a=i.w()*i.h()
            img.draw_rectangle(i.rect())
            if a>maxArea:
                max_i=i
                maxArea=a
        faceData=max_i.rect()
        faceCenter=(faceData[0]+faceData[2]//2,
            faceData[1]+faceData[3]//2) #找出人臉中心
        facePoint=img.draw_circle(faceCenter[0],faceCenter[1],2,
                    color=(255,150,20),thickness=3) #畫出人臉中心圓點
        #中準點和人臉中心點
        moveAvg=img.draw_line(160,120,faceCenter[0],faceCenter[1],
        color=(60,80,170),thickness=2)
        print(faceCenter)
    lcd.display(img)

#判斷要回歸中心的方向

if faceCenter[0]>sensor.width()//2+allow_value:
            print("向左回到中心值")
            img.draw_arrow((110,125,50,125),color=(2,255,255),
            size=20, thickness=7) #左
        elif faceCenter[0]<sensor.width()//2-allow_value:
            print("向右回到中心值")
            img.draw_arrow((190,125,250,125),color=(2,255,255),
            size=20, thickness=7) #右
        elif faceCenter[0]==sensor.width()//2:
            print("處於中心值")
        if faceCenter[1]<sensor.height()//2-allow_value:
            print("向下回到中心值")
            img.draw_arrow((150,160,150,210),color=(2,255,255),
            size=20, thickness=7) #下
        elif faceCenter[1]>sensor.height()//2+allow_value:
            print("向上到中心值")
            img.draw_arrow((150,90,150,40),color=(2,255,255),
            size=20, thickness=7) #上
        elif faceCenter[1]==sensor.height()//2:
            print("處於中心值")



    


留言

這個網誌中的熱門文章

k210攝像頭實時鏡頭實驗

k210外部中斷的運用-irq()