import sensor, time, image, lcd, math,pyb
from pyb import UART
RED_LED_PIN = 1
BLUE_LED_PIN = 3
NUM_SUBJECTS = 6 #图像库中不同人数,一共6人
NUM_SUBJECTS_IMGS = 10 #每人有3张样本图片
uart = UART(3, 115200)
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.HQVGA) # HQVGA最好,其次QVGA,QQVGA
sensor.set_pixformat(sensor.GRAYSCALE) #注意人脸识别只能用灰度图哦
sensor.skip_frames(10)
sensor.skip_frames(time = 1000) #等待1s
face_cascade = image.HaarCascade("frontalface", stages=25)
clock = time.clock()
lcd.init()
def face1():
#uart.write("a,ok\r\n")
print("a,ok")
j=0
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.HQVGA) # HQVGA最好,其次QVGA,QQVGA
sensor.set_pixformat(sensor.GRAYSCALE) #注意人脸识别只能用灰度图哦
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 1000) #等待1s
while (True):
pyb.LED(RED_LED_PIN).on()
img = sensor.snapshot() # 拍摄一张照片
lcd.display(sensor.snapshot())
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35) #thresholds越大,匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。
for r in objects:
img.draw_rectangle(r) #在找到的目标上画框,标记出来
if objects:
for object in objects:
fs=object[2]*object[3]
if fs>=14884:
j+=1
if j==10:
print(fs)
print(objects)
#uart.write("a,over\r\n")
break
if j==10:
uart.write("a")
pyb.LED(RED_LED_PIN).off() #红灯灭,蓝灯亮
pyb.LED(BLUE_LED_PIN).on()
pyb.LED(BLUE_LED_PIN).off()
break
def dis_face():
#global NUM_SUBJECTS #图像库中不同人数,一共6人
#NUM_SUBJECTS = 4
#uart.write("c,ok\r\n")
print("c,ok")
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others)
sensor.set_windowing((92,112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 3000) #等待1s
lcd.init()
#SUB = "s1"
img = sensor.snapshot() # 拍摄当前人脸。
#img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height())) #d0为当前人脸的lbp特征
img = None
pmin = 999999
num=0
pyb.LED(RED_LED_PIN).on()
lcd.display(sensor.snapshot())
for s in range(1, NUM_SUBJECTS+1):
#lcd.display(sensor.snapshot())
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
img = image.Image("sample/s%d/%d.pgm"%(s, i))
d1 = img.find_lbp((0, 0, img.width(), img.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))
if (dist/NUM_SUBJECTS_IMGS)<pmin:
pmin=(dist/NUM_SUBJECTS_IMGS)
num=s
print(pmin)
if pmin<10000:
if num==1:
uart.write("d")
if num==2:
uart.write("e")
if num==3:
uart.write("f")
if num==4:
uart.write("g")
if num==5:
uart.write("h")
if num==6:
uart.write("i")
elif pmin>10000:
uart.write("j")
print(num) # num为当前最匹配的人的编号。
#uart.write("%d\r\n" %num)
#uart.write("c,over\r\n")
pyb.LED(RED_LED_PIN).off() #红灯灭,蓝灯亮
pyb.LED(BLUE_LED_PIN).on()
pyb.LED(BLUE_LED_PIN).off()
def dis_face1():
#global NUM_SUBJECTS #图像库中不同人数,一共6人
#NUM_SUBJECTS = 4
#uart.write("c,ok\r\n")
print("m,ok")
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.B128X128) # or sensor.QQVGA (or others)
sensor.set_windowing((92,112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 1000) #等待1s
lcd.init()
#SUB = "s1"
img = sensor.snapshot() # 拍摄当前人脸。
#img = image.Image("singtown/%s/1.pgm"%(SUB))
d0 = img.find_lbp((0, 0, img.width(), img.height())) #d0为当前人脸的lbp特征
img = None
pmin = 999999
num=0
pyb.LED(RED_LED_PIN).on()
lcd.display(sensor.snapshot())
for s in range(1, NUM_SUBJECTS+1):
#lcd.display(sensor.snapshot())
dist = 0
for i in range(2, NUM_SUBJECTS_IMGS+1):
img = image.Image("sample/s%d/%d.pgm"%(s, i))
d1 = img.find_lbp((0, 0, img.width(), img.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))
if (dist/NUM_SUBJECTS_IMGS)<pmin:
pmin=(dist/NUM_SUBJECTS_IMGS)
num=s
print(pmin)
#if pmin<6500:
#if num==1:
#uart.write("d")
#if num==2:
#uart.write("e")
#if num==3:
#uart.write("f")
#if num==4:
#uart.write("g")
#if num==5:
#uart.write("h")
#if num==6:
#uart.write("i")
#elif pmin>6500:
uart.write("j")
print(num) # num为当前最匹配的人的编号。
#uart.write("%d\r\n" %num)
#uart.write("c,over\r\n")
pyb.LED(RED_LED_PIN).off() #红灯灭,蓝灯亮
pyb.LED(BLUE_LED_PIN).on()
pyb.LED(BLUE_LED_PIN).off()
def length():
#uart.write("b,ok\r\n")
print("b,ok")
K=4148#the value should be measured
length=0
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.HQVGA) # HQVGA最好,其次QVGA,QQVGA
sensor.set_pixformat(sensor.GRAYSCALE) #注意人脸识别只能用灰度图哦
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 1000) #等待1s
while (True):
pyb.LED(RED_LED_PIN).on()
img = sensor.snapshot() # 拍摄一张照片
lcd.display(sensor.snapshot())
objects = img.find_features(face_cascade, threshold=0.75, scale=1.35) #thresholds越大,匹配速度越快,错误率也会上升。scale可以缩放被匹配特征的大小。
for r in objects:
img.draw_rectangle(r) #在找到的目标上画框,标记出来
if objects:
for object in objects:
print(object[2],object[3])
cx = int(object[0]+object[2]/2)
cy = int(object[1]+object[3]/2)
Lm = (object[2]+object[3])/2
length = K/Lm
img.draw_cross(cx, cy) # cx, cy
print(length)
#uart.write("%d\r\n" %length)
#uart.write("b,over\r\n")
break
if length!=0:
uart.write("l")
pyb.LED(RED_LED_PIN).off() #红灯灭,蓝灯亮
pyb.LED(BLUE_LED_PIN).on()
pyb.LED(BLUE_LED_PIN).off()
break
def snapshot1():
#uart.write("k,ok\r\n")
print("k,ok")
print("%d\r\n" %NUM_SUBJECTS)
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.GRAYSCALE
sensor.set_framesize(sensor.HQVGA) # B128X128or sensor.QQVGA (or others)
sensor.set_windowing((92,112))
sensor.skip_frames(10) # Let new settings take affect.
sensor.skip_frames(time = 1000)
clock = time.clock()
num = 2 #设置被拍摄者序号,第一个人的图片保存到s1文件夹,第二个人的图片保存到s2文件夹,以此类推。每次更换拍摄者时,修改num值。
n = 10 #设置每个人拍摄图片数量。
while(n):
pyb.LED(RED_LED_PIN).on() #红灯亮
lcd.display(sensor.snapshot())
sensor.skip_frames(time = 3000) #等待3s,准备一下表情。
pyb.LED(RED_LED_PIN).off() #红灯灭,蓝灯亮
pyb.LED(BLUE_LED_PIN).on()
print(n)
sensor.snapshot().save("data/s%s/%s.pgm" % (num, n) ) #保存截取到的图片到SD卡
n -= 1
pyb.LED(BLUE_LED_PIN).off()
if n==1:
print("Done! Reset the camera to see the saved image.")
#uart.write("k,over\r\n")
uart.write("k\r\n")
while(True):
#clock.tick()
#img = sensor.snapshot() #1
#lcd.display(sensor.snapshot()) #2 这两行无论哪一行运行,串口都无法接收到数据
a='0'
if uart.any():
a=uart.readline().decode()
print(a)
#pyb.delay(100)
if a=='a':
#uart.write("a,ready\r\n")
face1() #人脸识别(日常模式)
print("face1")
if a=='b':
#uart.write("b,ready\r\n")
length() #人脸识别(测额头距)
print("length")
if a=='c':
#uart.write("c,ready\r\n")
dis_face() #分辨人脸
print("dis_face")
if a=='k':
#uart.write("k,ready\r\n")
snapshot1() #拍照保存(人脸样本)
print("snapshot1")
if a=='m':
#uart.write("c,ready\r\n")
dis_face1() #分辨人脸
print("dis_face1")
#else:
#print("not a ")
#print("a:",a)那你把整个图像上传一下,要不我怎么测试?
代码只要最小的,可运行的,你关注的地方。
你这个代码真是又臭又长,没用的东西一大堆,还要串口才能调试。
@kidswong999 https://pan.baidu.com/s/1BW2XXy3weS9N7czQNXiR7g
提取码:1111
@kidswong999 已经好了,不同人脸虽然差异很小但可以分辨出