舵机人脸跟随
舵机人脸跟随
这个项目的目标是使用摄像头捕捉人脸,然后使用2个舵机控制摄像头的方向,使摄像头始终对准人脸。
需要注意的是,下面的代码设置了初识角度,但是这个角度是根据我的舵机和我的摄像头的特性设置的。如果你使用不同的舵机和摄像头,你可能需要调整这些角度。
人脸识别模型下载facedetect.kmodel
import sensor
import lcd
import image
import board
import KPU as kpu
from machine import Timer
from machine import PWM
tim0 = Timer(Timer.TIMER0,Timer.CHANNEL0, mode=Timer.MODE_PWM)
pse6=PWM(tim0, freq=50, duty=2.5, pin=board.pin_D[6])
tim1 = Timer(Timer.TIMER1,Timer.CHANNEL0, mode=Timer.MODE_PWM)
pse7=PWM(tim1, freq=50, duty=2.5, pin=board.pin_D[7])
pse6.duty(90/18.0+2.5)
pse7.duty(135/18.0+2.5)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
sensor.skip_frames(10)
sensor.set_vflip(1)
sensor.set_hmirror(0)
lcd.init(freq=15000000,color=0x0000)
anchor= (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437, 6.92275, 6.718375, 9.01025)
KPU = kpu.load("/sd/facedetect.kmodel")
kpu.init_yolo2(KPU,0.5,0.3,5,anchor)
X1 = 90
Y1 = 135
while True:
img = sensor.snapshot()
code = kpu.run_yolo2(KPU,img)
if code:
for i in code:
img = img.draw_rectangle(i.rect(),(255,0,0),2,0)
x = i.x() + i.w() // 2
y = i.y() + i.h() // 2
img = img.draw_cross([x,y],(255,0,0),30,1)
if x < 150:
X1 = X1 - 1
if X1 < 20:
X1 = 20
if x > 170:
X1 = X1 + 1
if X1 > 160:
X1 = 160
pse6.duty(X1/18.0+2.5)
if y < 110:
Y1 = Y1 - 1
if Y1 < 95:
Y1 = 95
if y > 130:
Y1 = Y1 + 1
if Y1 > 175:
Y1 = 175
pse7.duty(Y1/18.0+2.5)
lcd.display(img)