DLib完成人脸识别项目详细说明

Dlib介绍

       Dlib是一个现代C++框架,解决包含机器学习算法以及开发复杂软件的现实问题,它被广泛应用在工业和学术研究领域,包括机器人、嵌入式设备、移动手机以及大规模高性能计算环境中,DLib的开源使得在使用过程中方便,自由。它的主要特点有:文档说明全,高质量的代码,机器学习算法,科学计算算法,图模型推理算法,图像处理,线程,网络编程,图形用户接口,数据压缩与整合算法等,可以参考官网说明:http://dlib.net

安装dlib模块

sudo apt-get install libboost-all-dev
sudo apt-get install cmake
sudo pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple dlib

如何使用在PC环境下使用安卓手机摄像头?

在百度搜索 【IP摄像头】,下载并安装在安卓手机上,保证PC机与手机连接在同一个无线网中,PC机IP最好不好自动获取,可以先设置自动获取,然后在CMD下敲入ipconfig /all 查看分配的ip,然后修改静态ip,在IP摄像头软件的主界面中点击打开【IP摄像头服务器】按钮,摄像头就启动了,可以选择前置和后置摄像头。IP摄像头软件具体使用可以查看说明:http://app.mi.com/details?id=com.shenyaocn.android.WebCam

首先安装opencv模块

sudo pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python

python操作摄像头代码示例:

cv2.namedWindow('camera',1)
video = 'http://admin:[email protected]:8081'
cap = cv2.VideoCapture(video)

if False == cap.isOpened():
	cap.open(video)

while (True):
	ret, frame = cap.read()
	if False == ret:
		break
	cv2.imshow('camera', frame)

	key = cv2.waitKey(10)
	if key == 27:
		print ('esc break...')
		break
cap.release()
cv2.destroyAllWindows('camera')

如何使用dlib进行人脸检测与识别?

    这里需要下载两个已经训练好的人脸识别模型以及人脸关键点检测器,两个.dat文件(shape_predictor_68_face_landmarks.dat和dlib_face_recognition_resnet_model_v1.dat),新建一个文件夹candidate-faces文件夹,把库中的图片以姓名命名,如下图所示:

接下来的过程如下:

  1、加载正脸检测器,使用dlib

detector = dlib.get_frontal_face_detector()

  2、加载人脸关键点检测器

sp = dlib.shape_predictor(predictor_path)

3、加载人脸识别模型

facerec = dlib.face_recognition_model_v1(face_rec_model_path)

4、保存候选人的人脸描述子

扫描二维码关注公众号,回复: 2903167 查看本文章

   4.1、先通过候选人图像检测到候选人脸矩形框

   	image = cv2.imread(faces_folder_path+'/'+filename)
	gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	dets = detector(gray_image, 1)

dets保存的是图像中人脸的矩形框,可以有多个

 4.2、通过人脸矩形框获取人脸的关键点(68个关键点)

shape = sp(image, d)

4.3、人脸识别模型通过关键点获取人脸描述子

face_descriptor = facerec.compute_face_descriptor(image, shape)

4.4、把人脸描述子与人脸姓名分别一一对应保存在两个列表list中

candidate.append(filename.split('.')[0])

 5、被检测图像人脸识别,这里是从摄像头获取视频帧,识别没帧图像中的人脸

  5.1、被检测图像人脸矩形框获取

 	cv2.namedWindow('camera',1)

	video = 'http://admin:[email protected]:8081'
	cap = cv2.VideoCapture(video)

	if False == cap.isOpened():
		cap.open(video)
	while (True):
		ret, frame = cap.read()

		#img = cv2.resize(frame, (600,800))
		img = frame
		gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
		dets = detector(gray_image, 1)

   5.2、被检测图像人脸关键点获取

shape = sp(img, d)

   5.3、被检测图像人脸描述子获取

 face_descriptor = facerec.compute_face_descriptor(img, shape)
d_test = numpy.array(face_descriptor)

   5.4、被检测图像人脸描述子与候选人脸描述子之间计算欧氏距离d

 	for i in descriptors:
		dist_ = numpy.linalg.norm(i - d_test)
		dist.append(dist_)

   5.5、保存欧式距离d,将d与候选人姓名组合成字典,对字典进行排序

 	c_d = dict(zip(candidate, dist))
	cd_sorted = sorted(c_d.items(), key=lambda d:d[1])

   5.6、对距离进行阈值判断,并取距离最小的那个候选人姓名

 	if cd_sorted[0][1] < 0.38:
		cv2.rectangle(img, (d.left(),d.top()),(d.right(),d.bottom()),(255,0,0),1,8,0)
		cv2.putText(img, 'unkown rate {}'.format(cd_sorted[0][1]), (d.left(),d.top()-10), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
		print('the perion is unkown rate{}'.format(cd_sorted[0][1]))
	else:
		cv2.rectangle(img, (d.left(),d.top()),(d.right(),d.bottom()),(255,0,0),1,8,0)
		cv2.putText(img, cd_sorted[0][0], (d.left(),d.top()-10), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
		print('the perion is {}'.format(cd_sorted[0][0]))

最后完整代码可以参考如下:

#-*- encoding:utf-8 -*-

import sys
import os
import dlib
import cv2
import numpy

predictor_path = './shape_predictor_68_face_landmarks.dat'

face_rec_model_path = './dlib_face_recognition_resnet_model_v1.dat'

faces_folder_path = './candidate-faecs'

if not os.path.exists(faces_folder_path):
	os.makedirs(faces_folder_path)

detector = dlib.get_frontal_face_detector()

sp = dlib.shape_predictor(predictor_path)

facerec = dlib.face_recognition_model_v1(face_rec_model_path)

descriptors = []
candidate = []
index=1
for filename in os.listdir(faces_folder_path):
	if filename.endswith('.jpg'):
		print ('start to process picture %s' %index)
	else:
		continue
	candidate.append(filename.split('.')[0])
	image = cv2.imread(faces_folder_path+'/'+filename)

	gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	dets = detector(gray_image, 1)

	print ('Number of faces detected:{}'.format(len(dets)))

	for k, d in enumerate(dets):
		shape = sp(image, d)
		face_descriptor = facerec.compute_face_descriptor(image, shape)

		v = numpy.array(face_descriptor)
		descriptors.append(v)
	index += 1
test_img_path = './xiaozhi2.jpg'

cv2.namedWindow('camera',1)

video = 'http://admin:[email protected]:8081'
cap = cv2.VideoCapture(video)

if False == cap.isOpened():
	cap.open(video)
while (True):
	ret, frame = cap.read()

	#img = cv2.resize(frame, (600,800))
	img = frame
	gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
	dets = detector(gray_image, 1)

	dist = []

	for k, d in enumerate(dets):

		print ('In test image Number of faces detected:{}'.format(len(dets)))

		shape = sp(img, d)
		face_descriptor = facerec.compute_face_descriptor(img, shape)
		d_test = numpy.array(face_descriptor)

		for i in descriptors:
			dist_ = numpy.linalg.norm(i - d_test)
			dist.append(dist_)

		c_d = dict(zip(candidate, dist))

		cd_sorted = sorted(c_d.items(), key=lambda d:d[1])

		if cd_sorted[0][1] < 0.38:
			cv2.rectangle(img, (d.left(),d.top()),(d.right(),d.bottom()),(255,0,0),1,8,0)
			cv2.putText(img, 'unkown rate {}'.format(cd_sorted[0][1]), (d.left(),d.top()-10), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
			print('the perion is unkown rate{}'.format(cd_sorted[0][1]))
		else:
			cv2.rectangle(img, (d.left(),d.top()),(d.right(),d.bottom()),(255,0,0),1,8,0)
			cv2.putText(img, cd_sorted[0][0], (d.left(),d.top()-10), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
			print('the perion is {}'.format(cd_sorted[0][0]))
	
		dist = []
	if len(dets) == 0:
		cv2.putText(img, 'not detect face', (100,100), cv2.FONT_HERSHEY_COMPLEX, 1, (255,0,0), 1)
	else:
		pass
	cv2.imshow('rectangle', img)
	
	key = cv2.waitKey(10)
	if key == 27:
		print ('esc break...')
		break
cap.release()
cv2.destroyAllWindows('camera')
dlib.hit_enter_to_continue()

猜你喜欢

转载自blog.csdn.net/jiangyingfeng/article/details/82021553