import sensor, image, time,pyb
from pid import PID
from pyb import Servo
pan_servo=Servo(1)#p7
tilt_servo=Servo(2)#tilt倾斜p8
red_threshold = (13, 49, 18, 61, 6, 47)#threshold yuzhi
#pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
sensor.reset()
# Sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# HQVGA and GRAYSCALE are the best for face tracking.
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_vflip(True)#镜像翻转
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock() # Tracks FPS.
face_cascade = image.HaarCascade("frontalface", stages=25)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob[2]*blob[3] > max_size:
max_blob=blob
max_size = blob[2]*blob[3]
return max_blob
while(True):
clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image.
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35)
if objects:
max_blob = find_max( objects)
pan_error = max_blob[0]+max_blob[2]/2-img.width()/2
tilt_error = max_blob[1]+max_blob[3]/2-img.height()/2
print("pan_error: ", pan_error)
img.draw_rectangle(max_blob) # rect
img.draw_cross(int(max_blob[0]+max_blob[2]/2),int(max_blob[1]+max_blob[3]/2))
pan_output=pan_pid.get_pid(pan_error,1)/2
tilt_output=tilt_pid.get_pid(tilt_error,1)
print("pan_output",pan_output)
pan_servo.angle(pan_servo.angle()+pan_output)
tilt_servo.angle(tilt_servo.angle()-tilt_output)
#sensor.skip_frames(time = 5000)
NUM_SUBJECTS = 2#图像库中不同人数,一共6人
NUM_SUBJECTS_IMGS = 50 #每人有20张样本图片
# 拍摄当前人脸。image.save(path[, roi[, quality=50]])
img = sensor.snapshot()#.save("singtown/snapshot-%d.pgm" % pyb.rng())
img = img.draw_rectangle(max_blob).save("singtown/snapshot-%d.pgm" % pyb.rng(),[max_blob[0],max_blob[1],max_blob[2],max_blob[3]])
#img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height()))
#d0为当前人脸的lbp特征
imge = None
pmin = 999999
num=0
def min(pmin, a, x):
global num
if a<pmin:
pmin=a
num=x
return pmin
for s in range(1, NUM_SUBJECTS+1):
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
imge = image.Image("singtown/s%d/%d.pgm"%(s, i))
d1 = imge.find_lbp((0, 0, imge.width(), imge.height()))
#d1为第s文件夹中的第i张图片的lbp特征
dist += image.match_descriptor(d0, d1)#计算d0 d1即样本图像与被检测人脸的特征差异度。
print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
if pmin>31062:
img.draw_string(int(max_blob[0]+max_blob[2]/2),int(max_blob[1]+max_blob[3]/2),"stranger",[0,0,0],scale=2, mono_space=False).save("singtown/stranger-%d.jpg" % pyb.rng())
# break
elif num ==1:
img.draw_string(int(max_blob[0]+max_blob[2]/2),int(max_blob[1]+max_blob[3]/2),"man",[0,0,0],scale=2, mono_space=False).save("singtown/1-%d.jpg" % pyb.rng())
elif num==2:
img.draw_string(int(max_blob[0]+max_blob[2]/2),int(max_blob[1]+max_blob[3]/2),"woman",[0,0,0],scale=2, mono_space=False).save("singtown/2-%d.jpg" % pyb.rng())
# elif num==3:
# img.draw_string(int(max_blob[0]+max_blob[2]/2),int(max_blob[1]+max_blob[3]/2),"ghx",[0,0,0],scale=2, mono_space=False).save("singtown/mb-%d.jpg" % pyb.rng())
# print(pmin)
print(num) # num为当前最匹配的人的编号。
# image.draw_string(x, y, text[, color[, scale=1[, x_spacing=0[, y_spacing=0[, mono_space=True]]]]])