OpenMV人脸注册和识别,验证成功后,通过arduino点亮小灯

OpenMV人脸注册:插入存储卡,卡内建文件夹data,下面建文件夹s1,运行paizhao.py,代码如下:

import time, pyb,sensor,image
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 = 2000)

num =1 #设置被拍摄者序号,第一个人的图片保存到s1文件夹,第二个人的图片保存到s2文件夹,以此类推。每次更换拍摄者时,修改num值。
n = 10 #设置每个人拍摄图片数量。
face_cascade = image.HaarCascade(“frontalface”, stages=25)
#连续拍摄n张照片,每间隔3s拍摄一次。
while(n):

     #红灯亮
    pyb.LED(1).on()
    sensor.skip_frames(time = 3000) # Give the user time to get ready.等待3s,准备一下表情。

    #红灯灭,蓝灯亮
    pyb.LED(1).off()
    pyb.LED(3).on()

    #保存截取到的图片到SD卡
    print(n)
    img =sensor.snapshot()
    objects = img.find_features(face_cascade, threshold=0.75, scale_factor=1.25)
    if objects:
        for r in objects:
            img.draw_rectangle(r)
            saveimg=img.copy(r) #仅仅保存人脸区域
        saveimg.save("data/s%s/%s.bmp" % (num, n) ) # .pgm or "example.bmp" (or others)
        n -= 1
        pyb.LED(3).off()
        print("Done! Reset the camera to see the saved image.")

人脸注册后,开始识别,运行shibie.py,代码如下(红灯亮,看到人脸后,绿灯亮,如果注册过,则蓝灯常亮,信号"zhaodaole"发给arduino):
import sensor, time, image, pyb
from pyb import UART
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

uart = UART(3,9600)
face_cascade = image.HaarCascade(“frontalface”, stages=25)
#红灯亮
pyb.LED(1).on()# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
NUM_SUBJECTS = 1 #图像库中不同人数,一共1人
NUM_SUBJECTS_IMGS = 10 #每人有10张样本图片
flag=True

while(flag):

  # 拍摄当前人脸。
  img = sensor.snapshot()
  objects = img.find_features(face_cascade, threshold=0.75, scale_factor=1.25)
  if objects:
     #红灯灭
     pyb.LED(1).off()# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
     #绿灯亮
     pyb.LED(2).on()# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
     for r in objects:
         img.draw_rectangle(r)
         ROIimg=img.copy(r)
     flag=False

#img = image.Image(“singtown/%s/1.pgm”%(SUB))
d0 = ROIimg.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): # s=1

dist = 0
for i in range(1, NUM_SUBJECTS_IMGS+1):
    img = image.Image("data/s%d/%d.bmp"%(s, i)) #.pgm or "example.bmp" (or others)
    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)#特征差异度越小,被检测人脸与此样本更相似更匹配。
if pmin<50000:
   message = "zhaodaole"
   uart.write(message)
   #绿灯灭
   pyb.LED(2).off()# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
   #蓝灯亮
   pyb.LED(3).on()# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
   print(message)
print(pmin)

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

arduino端代码如下(如果收到识别成功信号,则7口灯亮,为了便于调试,用的软串口):
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11);//RX=10,TX=11
String str= “”;
void setup()
{
//硬件串口波特率
Serial.begin(9600);
//软件串口波特率
mySerial.begin(9600);
pinMode(7, OUTPUT);
}
void loop()
{
while (mySerial.available() > 0)
{
str+= char(mySerial.read());
delay(2);
}
// Serial.write(str.length());
if(str.equals(“zhaodaole”))
{
digitalWrite(7, HIGH);
}
}

脱机运行方法:shibie.py改成main.py放到存储卡里就行,但此时paizhao.py无法运行的。接下去可以研究一下多程序联合脱机运行。

  • 16
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值