基于Dragonboard 410c的智能魔镜设计(5)——通过人脸进行用户身份识别

根据前面的设计方案,在设计的基于dragonboard 410c的智能魔镜中,需要根据超声波检测到有人靠近的时候,通过usb摄像头采集人脸数据,然后根据数据库中的人脸数据进行人脸匹配,识别用户的身份,最终根据用户身份显示不同的消息和推送不同的内容,这期blog将重点向大家介绍智能魔镜中的人脸检测和识别处理实现的原理和过程。
需要进行基于人脸的用户身份识别,首先需要检测出人脸,然后根据检测出来的人脸与数据库中的人脸进行匹配,最终得出匹配度,根据匹配度确定该人脸的身份,这里将从这两个方面来向大家介绍如何在智能魔镜中实现人脸身份检测,其实在前面的一系列blog中已经给大家介绍了人脸识别和检测的实现方法,这里就在结合智能魔镜代码设计再向大家如何应用前面已经设计好的人脸检测和识别的代码,将其集中到智能魔镜中的应用中来。
在基于usb摄像头人脸识别的文章中已经告诉了大家如使用python和opencv来何构建人脸检测代码,我们这里构建了一个getfac.py文件,在该文件中通过getfac类来实现人脸的检测,其实也就是对前面blog方法的一次封装即可,具体代码如下:
def getface(self,device):
cap = cv2.videocapture(device)
faces=[]
num=self.maxframes
while(true):
ret,img=cap.read()
ret,img=cap.read()
ret,img=cap.read()
ret,img=cap.read()
if ret==true:
gray = cv2.cvtcolor(img,cv2.color_bgr2gray)
faces = self.face_cascade.detectmultiscale(gray,1.3,5)
i=0
for(x,y,w,h) in faces:
face=img[y:y+h,x:x+w]
print(get face sucessful)
cap.release()
cv2.imwrite('./temp.jpg',face)
return true
num=num-1
if num==0:
print(get face failed)
cap.release()
return false
else:
cap.release()
return false
有了上述类,我们就可以检测并且获得人脸图像,接下来我们需要利用检测到的人脸图像,与数据库中的人脸图像进行匹配,实现人脸的识别,其方法和前面 使用usb摄像头进行人脸识别文章--第二篇 中的方法一样,采用opencv提供的人脸识别接口来实现,但是这里为了在魔镜中调用,重写了前面blog的方法,构建了一个peopleidentification.py类来实现魔镜中的人脸身份检测,这里封装了一个peopledetected类来实现人脸的身份识别,重新封装后的代码和前面代码稍有差异,具体设计如下:
class peopledetected:
def __init__(self,trainimgpath):
self.threshold=6000
self.model = cv2.createfisherfacerecognizer()
self.trainmodel('./usrimg')
#load all user img create train table
#imgpath is user img path
def trainmodel(self,trainimgpath):
names=[]
lables=[]
userimgdir=os.listdir(trainimgpath)
#print(userimgdir)
for imgdir in userimgdir:
print(userid dir is:)
#print(imgdir)
format=%s/%s
values=(trainimgpath,imgdir)
dirpath=format % values
#find current user img
allimgpath=os.listdir(dirpath)
#print(allimgpath)
for name in allimgpath:
names.append(dirpath+'/'+name)
lables.append(int(imgdir))
print(save sucessful:)
print(names)
print(lables)
#return names,lables
trainimg=[]
for imgloadpath in names:
obj=cv2.resize(cv2.imread(imgloadpath,0),(400,500))
trainimg.append(obj)
self.model.train(trainimg,np.array(lables))
print(lables)
def getpeopleid(self,faceimgpath):
faceimg=cv2.imread(faceimgpath,0)
face=cv2.resize(faceimg,(400,500))
result = self.model.predict(face)
predictid=result[0]
predicterror=result[1]
print(predictid)
print(predicterror)
#print(result)
if predicterror>self.threshold:
return -1
else:
print(get people id)
return predictid
def getpeopleid2(self,faceimg):
face=cv2.resize(faceimg,(400,500))
result = self.model.predict(face)
predictid=result[0]
predicterror=result[1]
#print(predictid)
#print(predicterror)
#print(result)
if predicterror>self.threshold:
return -1
else:
return predictid
有了这两个类,我们就可以方便的构建魔镜的人脸身份识别子模块了,这里我们只需要通过下面的代码就可以使用人脸检测和进行人脸身份识别了:
gf=getface()
rs=gf.getface(0)
pd=peopledetected(./usrimg)
peopleid=pd.getpeopleid('./temp.jpg')
其中pd在构建的过程中,将导入./userimg中的用户信息作为训练数据。
每次检测到的人脸保存在./temp.jpg中。
后续blog将进一步向大家介绍如何利用上述两个类,并且根据超声波检测结果来完成整个魔镜的人脸身份检测和保存。

基于89C51单片机和AD590芯片实现微机通信程序的开发
51单片机数字温度报警器_DS18B20可调上下限
研华推出基于Intel N455处理器的COM-Ultra模块SOM-7562 B1
机器学习43条军规 谷歌机器学习工程最佳实践大解密
俄罗斯MTS与爱立信签署软件供应协议:网络升级
基于Dragonboard 410c的智能魔镜设计(5)——通过人脸进行用户身份识别
oled屏幕的dc调光真的有用吗
苹果不采用3D Touch后,两大台湾面板厂商获利未达预期
音圈模组助力的车载激光雷达测试联合实验室成立
工业4.0板块未来涨幅或将更明显
雷士照明拟以5亿元收购罗曼国际所持蔚蓝芯光的余下60%股权
100kvar可控硅整流器模块
如何提升手机电磁兼容的性能
小米近日再创两项充电新纪录
土壤环境综合检测分析仪器的用途都有哪些
VR设备亦将作为一种载体,大幅度应用于商业
中国动力电池登陆欧洲舞台
Nordic半导体推出物联网双核处理器无线SoC
文晔拟38亿美元收购Future 100%股权,明年上半年完成交割
美国高通技术公司宣布第四代3G/LTE多模调制解调器和射频收发芯片