logo

像我这个问题咋解决呀?不知道修改哪里?

2022-04-19 18:25
3
natp
273650ce-9d1f-420c-9b21-12250bc34ec4.png
import sensor, time, image, pyb
sensor.reset()
sensor.set_contrast(3)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.HQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

rtc = pyb.RTC()
rtc.datetime((2022, 3, 29, 7, 7, 40, 0, 0))
NUM_SUBJECTS = 6 #图像库中不同人数,一共6人
NUM_SUBJECTS_IMGS = 20 #每人有20张样本图片
# 加载Haar算子
# 默认情况下,这将使用所有阶段,更低的satges更快,但不太准确。
face_cascade = image.HaarCascade("frontalface", stages=25)

while (True):
    img = sensor.snapshot()# 拍摄一张照片
    objects = img.find_features(face_cascade, threshold=0.75, scale=1.35)
    #print(objects)

    for r in objects:       #识别人脸画矩形框
        img.draw_rectangle(r)
    x=len(objects)

    if x>=1:
        print(x)
        c=0
        while c<x:
                
            z=objects[c]
            print(c)
            d0 = img.find_lbp(z)
            print(z)
            c=c+1

            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("a123/s%d/%d.pgm"%(s, i))
                    d1 = img.find_lbp((0, 0, img.width(), img.height()))
                    dist += image.match_descriptor(d0, d1)
                print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
                pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
                print(pmin)
2022-04-20 19:48
I
网红小智智 星瞳科技

第50行img尺寸改变了,所以下次28行循环时,就报错了。你可以默认只有一个人脸,分辨率比较低,分辨一个人脸就不错了。

2022-04-21 09:33
3
natp

@kidswong999 谢谢大哥,明白了。

2022-04-21 09:40
3
natp

@kidswong999 不过为啥我用if语句的时候就没报错呢?我还运行出来了。虽然识别过程有点艰难。

    if  x==2:   #如果画面中出现两个人
        z1=objects[0]
        #print(z)
        d0 = img.find_lbp(z1)
        #print(d0)
        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("a123/s%d/%d.pgm"%(s, i))
                d1 = img.find_lbp((0, 0, img.width(), img.height()))
                dist += image.match_descriptor(d0, d1)
           #print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
            pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
           #print(pmin)

z2=objects[1]
        #print(z2)
        d2 = img.find_lbp(z2)
        #print(d2)
        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("a123/s%d/%d.pgm"%(s, i))
                d1 = img.find_lbp((0, 0, img.width(), img.height()))
                dist += image.match_descriptor(d2, d1)
           #print("Average dist for subject %d: %d"%(s, dist/NUM_SUBJECTS_IMGS))
            pmin = min(pmin, dist/NUM_SUBJECTS_IMGS, s)#特征差异度越小,被检测人脸与此样本更相似更匹配。
           #print(pmin)
2022-04-21 10:22
3
natp

@kidswong999 谢谢哥,问题解决啦。不过你说我买个好点的摄像头应该提高人脸识别精度吧???

2022-04-21 19:42
I
网红小智智 星瞳科技

@natp lbp的精度就比较差。。。