老样子了先看视频
K210人脸追踪
你需要准备一个人脸识别模型
然后一个云台就行了
'''
人脸追踪
'''
from machine import Timer,PWM
import sensor,lcd,time
import KPU as kpu
class Pid:
def __init__(self,Kp,Kd,Ki):
self.Kp = Kp
self.Ki = Kd
self.Kd = Kd
self.Bias = 0
self.angle = 0
self.Last_bias = 0
self.Integral_bias = 0
def Out(self,target,actullay):
self.Bias = actullay-target
self.Integral_bias+=self.Bias
self.angle = self.Ki*self.Integral_bias+self.Kp*self.Bias+self.Kd*(self.Bias-self.Last_bias)
self.Last_bias=self.Bias
return self.angle
print(self.angle)
class Servo:
def __init__(self, timer_id):
self.pin = None
self.freq = 50
self.duty = 0
self.servo = None
self.interval = 500 # ms
'''all timer default use channel0'''
if timer_id == 0:
self.tim = Timer(Timer.TIMER0, Timer.CHANNEL0, mode=Timer.MODE_PWM)
elif timer_id == 1:
self.tim = Timer(Timer.TIMER1, Timer.CHANNEL0, mode=Timer.MODE_PWM)
elif timer_id == 2:
self.tim = Timer(Timer.TIMER2, Timer.CHANNEL0, mode=Timer.MODE_PWM)
else:
print("erorr timer_id is [0,2]!")
sys.exit(retval=0)
def set_pin(self,pin_num):
self.pin = pin_num
self.servo = PWM(self.tim, freq=self.freq, duty=self.duty, pin=self.pin)
def position(self,angle):
time.sleep_us(self.interval)
self.servo.duty(angle*1.0/180*10+2.5)
def set_speed(self,_interval):
self.interval = _interval
servo1 = Servo(1) #创建对象舵机1
servo2 = Servo(2) #创建对象舵机2
servo1.set_pin(15) #水平方向接15
servo2.set_pin(33) #垂直方向接33
Servoy= Pid(Kp=0.07,Ki=0,Kd=0) #设定PID参数
Servox= Pid(Kp=0.07,Ki=0,Kd=0)
angle1 = 55 #水平方向的舵机初始角度
angle2 = 90 #垂直方向的舵机初始角度,可更改
flag_x=0 #水平方向舵机的标志位
flag_y=0 #垂直方向舵机的标志位
x=0 #人脸中心的横坐标
y=0 #人脸中心的纵坐标
#摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.set_auto_whitebal(False) # turn this off.关闭白平衡
sensor.set_vflip(0) #后置模式,所见即所得
#lcd初始化
lcd.init()
clock=time.clock()
#将模型放在 SD 卡中。
task = kpu.load("/sd/facedetect.kmodel") #模型 SD 卡上
#模型描参数
anchor = (1.889, 2.5245, 2.9465, 3.94056, 3.99987, 5.3658, 5.155437,
6.92275, 6.718375, 9.01025)
#初始化 yolo2 网络
a = kpu.init_yolo2(task, 0.5, 0.3, 5, anchor)
while True:
clock.tick()
img = sensor.snapshot()
code = kpu.run_yolo2(task, img) #运行 yolo2 网络
#识别到人脸就画矩形表示
if code:
for i in code:
print(i)
b = img.draw_rectangle(i.rect())
x=i.x()+i.w()/2
y=i.y()+i.h()/2
Servox.Out(target=160,actullay=x)
Servoy.Out(target=120,actullay=y)
angle1=angle1+Servox.angle
angle2=angle2-Servoy.angle
servo1.position(angle1)
servo2.position(angle2)
#如果找不到人脸,水平舵机自动旋转()
else:
pass
if(flag_x==0):
angle1=angle1+1
if angle1>=180:
flag_x = 1
else:
angle1=angle1-1
if angle1<=0:
flag_x = 0
#这里将垂直方向的舵机关掉,感觉有水平方向的就可以了,可以根据实际情况修改
# if(flag_y==0):
# angle2=angle2+1
# if angle2>=180:
# flag_y = 1
# else:
# angle2=angle2-1
# if angle2<=0:
# flag_y = 0
# angle2=angle2+0.1
print(angle1)
print(angle2)
servo1.position(angle1)
servo2.position(angle2)
lcd.display(img) #LCD显示图片
print(clock.fps()) #打印FPS
代码附上了,模型直接去01科技官网或者sipeed的官网上下载就行。