logo

人脸识别后,读取数据小于30000的话,亮蓝灯,最后一段是自己写的,不知道哪错了,就是不亮灯

2018-11-26 13:15
8
f1pk

# Face recognition with LBP descriptors.

# See Timo Ahonen's "Face Recognition with Local Binary Patterns".

#

# Before running the example:

# 1) Download the AT&T faces database http://www.cl.cam.ac.uk/Research/DTG/attarchive/pub/data/att_faces.zip

# 2) Exract and copy the orl_faces directory to the SD card root.

import sensor, time, image, pyb

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 = 5000) #等待5s

#SUB = "s1"

NUM_SUBJECTS = 1 #图像库中不同人数,一共6人

NUM_SUBJECTS_IMGS = 6 #每人有20张样本图片

# 拍摄当前人脸。

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

def min(pmin, a, s):

global num

if a<pmin:

pmin=a

num=s

return pmin

for s in range(1, NUM_SUBJECTS+1):

dist = 0

for i in range(2, NUM_SUBJECTS_IMGS+1):

img = image.Image("singtown/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))

pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。

print(pmin)

print(num) # num为当前最匹配的人的编号。

p = pyb.Pin("P0", pyb.Pin.OUT_PP)

if s<=30000:

p.high() # or p.value(1) to make the pin high (3.3V)

pyb.LED(1).off()

pyb.LED(3).on()

else:

p.low() # or p.value(0) to make the pin low (0V)

pyb.LED(1).on()

pyb.LED(3).off()

I
网红小智智 星瞳科技

你print一下s看一下值到底是多少。

8
f1pk

s最后s换成pmin后,还让管脚P0输出高低电平,代码怎么写啊

I
网红小智智 星瞳科技
print(s)
if s<=30000:
  p.high() # or p.value(1) to make the pin high (3.3V)
  pyb.LED(1).off()
  pyb.LED(3).on()
else:
  p.low() # or p.value(0) to make the pin low (0V)
  pyb.LED(1).on()
  pyb.LED(3).off()
8
f1pk

我那个写错了,把s改成pmin后,还是不对,有没有办法在使P0输出高低电平啊

I
网红小智智 星瞳科技

不要笼统的说不对。

具体哪里不对。

8
f1pk

@kidswong9

if pmin<=30000:

pyb.LED(1).off()

pyb.LED(3).on()

else:

pyb.LED(1).on()

pyb.LED(3).off()

这样也不行

I
网红小智智 星瞳科技

你print一下pmin看一下值到底是多少。

8
f1pk

@kidswong999

不到20000,不管是多少,都应该亮灯啊,我现在运行完后出来pmin=15800 num=1;后就不在执行了,还得重新连接

I
网红小智智 星瞳科技

你的程序,最后加一个死循环。

2018-11-26 13:36
8
f1pk

@kidswong999

我加上之后就运行不了了,点开运行就断开,连pmin的值也出不来了,你能不能帮我写一下啊,非常感谢

I
网红小智智 星瞳科技

我只会帮助你解决问题,不会直接提供代码。

代码肯定要自己写。

8
f1pk

@kidswong999

那为什么加上while(1):后就执行不了呢

I
网红小智智 星瞳科技

我不知道,请你提供全部的代码。

2018-11-26 19:10
8
f1pk

@kidswong999

import sensor, time, image, pyb  



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 = 5000) #等待5s







#SUB = "s1"

NUM_SUBJECTS = 1 #图像库中不同人数,一共6人

NUM_SUBJECTS_IMGS = 6 #每人有20张样本图片



# 拍摄当前人脸。

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



def min(pmin, a, s):

    global num

    if a<pmin:

        pmin=a

        num=s

    return pmin



for s in range(1, NUM_SUBJECTS+1):

    dist = 0

    for i in range(2, NUM_SUBJECTS_IMGS+1):

        img = image.Image("singtown/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))

    pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。

    print(pmin)
    print(num) # num为当前最匹配的人的编号。
    while(1):
    p = pyb.Pin("P0", pyb.Pin.OUT_PP)
    if pmin<=30000:
      p.high() # or p.value(1) to make the pin high (3.3V)
      pyb.LED(1).off()
      pyb.LED(3).on()
    else:
      p.low() # or p.value(0) to make the pin low (0V)
      pyb.LED(1).on()
      pyb.LED(3).off()
一运行还是断开连接了,连pmin都输出不了,不加while()会输出pmin后才断开连接,但是灯没亮,也没有输出高低电平
2018-11-26 20:56
I
网红小智智 星瞳科技

你的代码很奇怪,不能有空行,缩进必须是4个空格。