赞
踩
import cv2
import numpy as np
frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth) #设置参数,10为亮度
cap.set(4, frameHeight)
cap.set(10,150)
pos_path = './resource/face_detect/video2imagem3/'
neg_path = './resource/face_detect/video2imagec1/'
test_path = './resource/face/'
images = []
labels = []
注:人脸识别器在安装opencv库的时候就一块安装了,在opencv的安装路径下可以找到。
faceCascade = cv2.CascadeClassifier("resource/haarcascade_frontalface_default.xml")
由于本数据集人脸数据是通过程序处理视频获得的,因此,便于批量导入程序中。
正样本(人脸)与负样本(环境)比例大致为1:3,据说这样比例效果较好;标签为0,1。
导入语句如下:
for i in range(60): # 正样本
images.append(cv2.imread(pos_path+str(2*(i+1))+'.jpg',cv2.IMREAD_GRAYSCALE))
labels.append(0)
for i in range(173): # 负样本
images.append(cv2.imread(neg_path+str(2*(i+1))+'.jpg',cv2.IMREAD_GRAYSCALE))
labels.append(1)
这里对于没检测到人脸区域的图片不进行处理,对于人脸区域,将其周围20像素左右部分提取出来
def processing(imageslist):
for j in range(len(imageslist)):
faces = faceCascade.detectMultiScale(imageslist[j], 1.1, 4)
for x,y,w,h in faces:
if x >= 20 and y >= 20: # 未处理没检测到人脸的情况
imageslist[j] = imageslist[j][y-20:y+h+20, x-20:x+w+20]
return imageslist
这里采用LBPH人脸识别模块,因为这种方法不需要图片数据集为统一尺寸,EigenFaces和Fisherfaces模块均需要训练集图片和测试的图片尺寸保持一致。
imagesCopy = images
# 提取图像中人脸的区域
imagesCopy = processing(imagesCopy)
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.train(imagesCopy, np.array(labels))
人脸检测中,如果未检测到人脸,则face返回的是空元组,检测到人脸后,提取出人脸区域,采用recognizer.predict进行识别,对于返回值的confidence,小于50认为结果可靠,而大于80则认为差别较大。
while True: success,img = cap.read() predict_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face = faceCascade.detectMultiScale(predict_image, 1.1, 4) # print(face) # 空的元组有可能 if face != (): for x, y, w, h in face: if x >= 20 and y >= 20: faceArea = predict_image[y-20:y+h+20, x-20:x+w+20] cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 1) label, confidence = recognizer.predict(faceArea) if confidence <= 80 and label == 0: cv2.putText(img, "LQL",(x, y-20), cv2.FONT_HERSHEY_COMPLEX, 1,(255,0,0,1)) print("YOU ARE LQL") else: cv2.putText(img, "OTHERS", (x, y-20), cv2.FONT_HERSHEY_COMPLEX, 1,(255,0,0),1) print("OTHERS") print(confidence) print(label) else: cv2.putText(img, "None", (320, 240), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 255), 1) print("None") cv2.imshow("img", img) if cv2.waitKey(1) & 0xFF == ord('q'): break
能够基本实现特定人脸识别的功能。
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.save('./resource/face_detect/MyFaceModel.xml')
recognizer.read('./resource/face_detect/MyFaceModel.xml')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。