美文网首页码农的世界
这个真的好简单!Opencv & Python 实现的人

这个真的好简单!Opencv & Python 实现的人

作者: b4a0155c6514 | 来源:发表于2019-01-07 13:46 被阅读0次
    image

    拍摄代码

    功能(检测到人脸后自动拍摄放入照片库中)

    image

    <pre class="ql-align-justify">

    import sensor, time, image, sys, pyb
    RED_LED_PIN = 1
    BLUE_LED_PIN = 3
    # Reset sensor
    sensor.reset()
    sensor.set_contrast(1)
    sensor.set_gainceiling(16)
    sensor.set_framesize(sensor.HQVGA)
    sensor.set_pixformat(sensor.GRAYSCALE) 
    sensor.set_framesize(sensor.B128X128) 
    sensor.set_windowing((128,128))
    face_cascade = image.HaarCascade("frontalface", stages=25)
    print(face_cascade)
    clock = time.clock()
    num = 2 #设置被拍摄者序号,第一个人的图片保存到s1文件夹,第二个人的图片保存到s2文件夹,以此类推。每次更换拍摄者时,修改num值。
    n = 20 #设置每个人拍摄图片数量。
    m = 0
    while (True):
     clock.tick()
     img = sensor.snapshot()
     objects = img.find_features(face_cascade, threshold=0.75, scale=1.25)
     ddd = clock.fps()
     fff = int(ddd)
     for r in objects:
     img.draw_rectangle(r)
     if fff != 0:
     print(fff)
     m = m + 1
     print(m)
     pyb.LED(BLUE_LED_PIN).on()
     sensor.snapshot().save("face/s%s/%s.pgm" % (num, m) )
     print("Done! Reset the camera to see the saved image.")
     pyb.LED(BLUE_LED_PIN).off()
     if m == 20:
     m = 20
     else:
     print("wrong")
     if m is 20:
     break
    
    

    </pre>

    识别代码

    功能(自动检测到人脸后进行对比,输出匹配的人脸号,或者输出高低电平)

    <pre class="ql-align-justify">

    import sensor, time, image, sys, pyb, time
    # Reset sensor
    sensor.reset()
    sensor.set_contrast(1)
    sensor.set_gainceiling(16)
    sensor.set_framesize(sensor.HQVGA)
    sensor.set_pixformat(sensor.GRAYSCALE)
    sensor.set_framesize(sensor.B128X128)
    sensor.set_windowing((128,128))
    face_cascade = image.HaarCascade("frontalface", stages=25)
    print(face_cascade)
    clock = time.clock() #TX RX 管脚控制
    #from pyb import UART
    #uart = UART(3, 115200, timeout_char=1000) # i使用给定波特率初始化
    #uart.init(115200, bits=8, parity=None, stop=1, timeout_char=1000) # 使用给定参数初始化
    n = 1
    while (True):
     clock.tick()
     p = pyb.Pin("P0", pyb.Pin.OUT_PP)
     p.low()
     #p.LOW_POWER
     img = sensor.snapshot()
     objects = img.find_features(face_cascade, threshold=0.75, scale=1.25)
     ddd = clock.fps()
     fff = int(ddd)
     for r in objects:
     img.draw_rectangle(r)
     if fff != 0:
     #print(fff)
     n = n + 1
     #print(n)
     #SUB = "s1"
     NUM_SUBJECTS = 3 #图像库中不同人数,一共3 人
     NUM_SUBJECTS_IMGS = 20 #每人有20张样本图片
     img = sensor.snapshot()
     d0 = img.find_lbp((0, 0, img.width(), img.height()))
     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("face/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)
     i=0
     s=0
     if pmin > 100000:
     print(0)
     else:
     print(num)
     p.high()
     sensor.skip_frames(time = 3000)
     p.low()
     #uart.writechar(49) #通过uart 引脚输出1
     if n == 2:
     n=5
     else:
     print("wrong")
     if n is 5:
     break
    
    

    </pre>

    相关文章

      网友评论

        本文标题:这个真的好简单!Opencv & Python 实现的人

        本文链接:https://www.haomeiwen.com/subject/qjysrqtx.html