我是前几天帖子的帖主,这次是进行人脸识别,为了提高人脸抓取的准确性,在人脸识别的基础上进行eyes的判断,每次进行人脸计算的时候就会在min函数的调用时出现TypeError: 'int' object isn't callable的报错,属实是有些莫名其妙。希望得到官方的解答,另:论坛无法上传20张人脸库,如若测试代码,在base文件中创建s1、s2两个文件夹存储20张图片即可。
import sensor, image, time, pyb
from pyb import Pin
red = pyb.LED(1) #初始化led
green = pyb.LED(2)
blue = pyb.LED(3)
yellow= pyb.LED(4)
sensor.reset() #传感器重置
sensor.set_contrast(3) #感光元件设置
sensor.set_gainceiling(16)
sensor.set_pixformat(sensor.GRAYSCALE) #图像格式为灰度值
sensor.set_framesize(sensor.QQVGA) #图像大小为HQVGA,用于人脸识别
sensor.set_vflip(True)
sensor.skip_frames(time = 3000) #等待sensor稳定
face_cascade = image.HaarCascade("frontalface", stages=25) #载入人脸识别模型
eyes_cascade = image.HaarCascade("eye", stages=24) #载入人眼识别模型
clock = time.clock() #初始化系统时钟
p = pyb.Pin("P8", pyb.Pin.OUT_PP)
def tick(timer):
p.high()
pyb.delay(1)
p.low()
tim=pyb.Timer(4)
tim.init(freq=1)
tim.callback(tick)
count=0 #每个人拍50张照片
person=1 #不同编号的人
person_base=1 #数据库人数
person_base_total=2 #数据库总人数
person_base_total_count=0 #数据库总人数(变量)
dist_once=0 #lbp差异值
num_final=0 #最终匹配编号
num_once=0 #单次匹配编号
pmin = 999999 #最小值初始化
detect_1=0
detect_2=0
def min(pmin, a, s):
global num_once
if a<pmin:
pmin=a
num_once=s
return pmin
while(True): #系统主循环
clock.tick()
img = sensor.snapshot()
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35)
if objects:
face_find=objects[0]
if face_find[2]>=81 and face_find[3]>=81:
red.on()
pmin=999999
sensor.set_windowing(objects[0])
eyes = img.find_features(eyes_cascade, threshold=0.5, scale_factor=1.2, roi=objects[0])
if eyes:
for snapshot_save in range(1,31):
sensor.snapshot().save("now/s%s/%s.pgm" % (1,snapshot_save ) )
print(snapshot_save,':',face_find[2],face_find[3])
num_final=0
detect_1=0
detect_2=0
for s_now in range(1,31):
pmin=99999
num_once=0
img_now=image.Image("now/s%d/%d.pgm"%(1, s_now))
d_now=img_now.find_lbp((0, 0, 81, 81))
for person_base_total_count in range(1,person_base_total+1):
dist_once=0
for s_base in range(1,21):
img_base=image.Image("base/s%d/%d.pgm"%(person_base_total_count, s_base))
d_base=img_base.find_lbp((0, 0, 81, 81))
dist_once += image.match_descriptor(d_now, d_base)
dist_ave=dist_once/20
print(s_now,':',person_base_total_count,':',dist_ave)
pmin=min(pmin,dist_ave,person_base_total_count)
if num_once==1:
detect_1+=1
if num_once==2:
detect_2+=1
print('the number of photo is:',s_now,':',num_once)
print('the number of person 1:',detect_1,'the number of person 2:',detect_2)
if detect_1>=detect_2:
num_final=1
if detect_1<detect_2:
num_final=2
print('this person is number:',num_final)
if num_final==1:
green.on()
elif num_final!=1:
blue.on()
else:
red.off()
sensor.set_windowing((0,0,160,120))
else:
red.off()
sensor.set_windowing((0,0,160,120))
green.toggle()