OpenCV4.1已经发布将近一年了,其人脸识别速度和性能有了一定的提高,这里我们使用opencv来做一个实时活体面部识别的demo
首先安装一些依赖的库
pip install opencv-python
pip install opencv-contrib-python
pip install numpy
pip install pillow
需要注意一点,最好将pip设置国内的阿里云的源,否则安装会很慢
win10在用户目录下创建一个pip文件夹,然后在pip文件夹内创建一个pip.ini文件,文件内容如下
[global]
trusted-host = mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple
这样就可以用国内的源来下载安装包
一开始,我们可以简单的在摄像头中识别人的脸部和眼镜,原来就是用opencv内置的分类器,对直播影像中的每一帧进行扫描
import numpy as np
import cv2
from settings import src
# 人脸识别
faceCascade = cv2.CascadeClassifier(src+'haarcascade_frontalface_default.xml')
# 识别眼睛
eyeCascade = cv2.CascadeClassifier(src+'haarcascade_eye.xml')
# 开启摄像头
cap = cv2.VideoCapture(0)
ok = True
result = []
while ok:
# 读取摄像头中的图像,ok为是否读取成功的判断参数
ok, img = cap.read()
# 转换成灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 人脸检测
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(32, 32)
)
# 在检测人脸的基础上检测眼睛
for (x, y, w, h) in faces:
fac_gray = gray[y: (y+h), x: (x+w)]
result = []
eyes = eyeCascade.detectMultiScale(fac_gray, 1.3, 2)
# 眼睛坐标的换算,将相对位置换成绝对位置
for (ex, ey, ew, eh) in eyes:
result.append((x+ex, y+ey, ew, eh))
# 画矩形
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
for (ex, ey, ew, eh) in result:
cv2.rectangle(img, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
cv2.imshow('video', img)
k = cv2.waitKey(1)
if k == 27: #按 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
第二步,就是为模型训练收集训练数据,还是通过摄像头逐帧来收集,在脚本运行过程中,会提示输入用户id,请从0开始输入,即第一个人的脸的数据id为0,第二个人的脸的数据id为1,运行一次可收集一张人脸的数据
脚本时间可能会比较长,会将摄像头每一帧的数据进行保存,保存路径在项目目录下的Facedat目录,1200个样本后退出摄像录制
import cv2
import os
# 调用笔记本内置摄像头,所以参数为0,如果有其他的摄像头可以调整参数为1,2
from settings import src
cap = cv2.VideoCapture(0)
face_detector = cv2.CascadeClassifier(src+'haarcascade_frontalface_default.xml')
face_id = input('n enter user id:')
print('n Initializing face capture. Look at the camera and wait ...')
count = 0
while True:
# 从摄像头读取图片
sucess, img = cap.read()
# 转为灰度图片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 检测人脸
faces = face_detector.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+w), (255, 0, 0))
count += 1
# 保存图像
cv2.imwrite("./Facedata/User." + str(face_id) + '.' + str(count) + '.jpg', gray[y: y + h, x: x + w])
cv2.imshow('image', img)
# 保持画面的持续。
k = cv2.waitKey(1)
if k == 27: # 通过esc键退出摄像
break
elif count >= 1200: # 得到1000个样本后退出摄像
break
# 关闭摄像头
cap.release()
cv2.destroyAllWindows()
第三步,对收集下来的人脸数据进行模型训练,提取特征,训练后,会将特征数据保存在项目目录中的face\_trainer文件夹下面
import numpy as np
from PIL import Image
import os
import cv2
from settings import src
# 人脸数据路径
path = 'Facedata'
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier(src+"haarcascade_frontalface_default.xml")
def getImagesAndLabels(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faceSamples = []
ids = []
for imagePath in imagePaths:
PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale
img_numpy = np.array(PIL_img, 'uint8')
id = int(os.path.split(imagePath)[-1].split(".")[1])
faces = detector.detectMultiScale(img_numpy)
for (x, y, w, h) in faces:
faceSamples.append(img_numpy[y:y + h, x: x + w])
ids.append(id)
return faceSamples, ids
print('训练需要一定时间,请耐心等待....')
faces, ids = getImagesAndLabels(path)
recognizer.train(faces, np.array(ids))
recognizer.write(r'./face_trainer/trainer.yml')
print("{0} faces trained. Exiting Program".format(len(np.unique(ids))))
最后一步,人脸测试,我们将摄像头中的人脸和模型中的特征进行比对,用来判断是否为本人
import cv2
from settings import src
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('./face_trainer/trainer.yml')
cascadePath = src+"haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX
idnum = 0
names = ['andonghui', 'admin']
cam = cv2.VideoCapture(0)
minW = 0.1*cam.get(3)
minH = 0.1*cam.get(4)
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(int(minW), int(minH))
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
idnum, confidence = recognizer.predict(gray[y:y+h, x:x+w])
if confidence < 100:
idnum = names[idnum]
confidence = "{0}%".format(round(100 - confidence))
else:
idnum = "unknown"
confidence = "{0}%".format(round(100 - confidence))
cv2.putText(img, str(idnum), (x+5, y-5), font, 1, (0, 0, 255), 1)
cv2.putText(img, str(confidence), (x+5, y+h-5), font, 1, (0, 0, 0), 1)
cv2.imshow('camera', img)
k = cv2.waitKey(10)
if k == 27:
break
cam.release()
cv2.destroyAllWindows()
整个流程并不复杂,可以让opencv初学者感受一下人脸识别底层的逻辑,说明自研应用还是有一定可操作性的,并不是涉及机器学习的技术就动辄使用百度,阿里云等三方支持。
最后,送上人脸识别项目地址: