基于Python的face_recognition库实现人脸识别

一、face_recognition库简介

face_recognition是Python的一个开源人脸识别库,支持Python 3.3+和Python 2.7。引用官网介绍:

Recognize and manipulate faces from Python or from the command line with the world's simplest face recognition library.

之所以选用这个库,是因为

1、用这个库来实现一个人脸识别程序非常简单,环境配置也很容易;

2、可以直接使用已经训练好的模型,不需要在本地重新训练。一般普通的电脑都可以直接运行识别程序,硬件环境要求不高。

二、环境安装

我自己的环境如下:

硬件:08年的笔记本电脑,奔腾双核,算是比较低端的笔记本了

系统:win7x64

python:3.6 (注意:建议用3.6版本配置环境。我自己用3.7配置环境失败了,dlib安装总是失败。)

用3.6安装的过程比较简单,可以参考https://www.jianshu.com/p/8296f2aac1aa

用pip安装之前,注意首先修改pip为阿里的源,这样速度就快多了。

三、代码实现

本文采用了cnn模型进行人脸检测,然后使用余弦相似度的方法来进行人脸识别。


import face_recognition
import cv2
import os
import numpy as np
from PIL import Image,ImageDraw,ImageFont

#路径参数配置
basefacefilespath = "0s"   # faces文件夹中放待识别任务正面图,文件名为人名,将显示于结果中
destfacefilepath = "0d" #用于识别的目标图片目录

def cos_sim(vector_a, vector_b):
    """
    计算两个向量之间的余弦相似度
    :param vector_a: 向量 a
    :param vector_b: 向量 b
    :return: sim
    """
    vector_a = np.mat(vector_a)
    vector_b = np.mat(vector_b)
    num = float(vector_a * vector_b.T)
    denom = np.linalg.norm(vector_a) * np.linalg.norm(vector_b)
    cos = num / denom
    sim = 0.5 + 0.5 * cos
    return sim

#余弦相似度识别阈值
tolerance = 0.96

#写入中文字符支持
def paint_chinese_opencv(im, chinese, pos, color):
	img_PIL = Image.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
	font = ImageFont.truetype('simsun.ttc', 14)
	fillColor = color  # (255,0,0)
	position = pos  # (100,100)
	#chinese = chinese.decode('utf-8')
	draw = ImageDraw.Draw(img_PIL)
	draw.text(position, chinese, font=font, fill=fillColor)

	img = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR)
	return img


# 加载待识别人脸图像并识别。
baseface_titles = []  # 图片名字列表
baseface_face_encodings = []  # 识别所需人脸编码结构集
#读取人脸资源
for fn in os.listdir(basefacefilespath): #fn 人脸文件名
	baseface_face_encodings.append(
		face_recognition.face_encodings(face_recognition.load_image_file(basefacefilespath+"/"+fn))[0])
	#fn = fn.split("_")[1]
	fn = fn[:(len(fn) - 4)]
	baseface_titles.append(fn)
	print(fn)

#从识别库中读取一张图片并识别
for fd in os.listdir(destfacefilepath):
	# 获取一张图片
	faceData = face_recognition.load_image_file(destfacefilepath + "/" + fd)
	print(fd)

	# 人脸检测,并获取帧中所有人脸编码
	# 参数说明,number_of_times_to_upsample可以理解为迭代次数,次数越大,人脸检测越准确,但是运行耗时越长。
	# model默认为hog,运行速度快,但是识别率不高。后面第2张图分辨率低,所以必须用cnn并且迭代次数为2
	face_locations = face_recognition.face_locations(faceData, number_of_times_to_upsample=2, model="cnn")
	face_encodings = face_recognition.face_encodings(faceData, face_locations)
	# 遍历图片中所有人脸编码
	for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
		# 与baseface_face_encodings匹配否?
		name = "?"
		for i,v in enumerate(baseface_face_encodings):
			#match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)
			match = cos_sim([v], face_encoding)
			name = "?"
			if match>=tolerance:
				name = baseface_titles[i]
				print("识别出:" + name)
				break

		# 围绕脸的框
		cv2.rectangle(faceData, (left, top), (right, bottom), (0, 0, 255), 2)

		#如果遇到没有识别出的人脸,则跳过
		if name == "?":
			continue


		# 框下的名字(即,匹配的图片文件名)
		cv2.rectangle(faceData, (left, bottom), (right, bottom+25), (0, 0, 255), cv2.FILLED)
		#faceData = cv2.putText(faceData, name,(left + 2, bottom + 12), cv2.FONT_HERSHEY_SIMPLEX,0.5, (255, 255, 255),1)
		faceData = paint_chinese_opencv(faceData, name, (left + 2, bottom + 4), (255, 255, 255))
		# frame = ft.draw_text(frame, (left + 2, bottom + 12), name, 16,  (255, 255, 255))

	# show结果图像
	cv2.imshow(fd, cv2.cvtColor(faceData, cv2.COLOR_BGR2RGB))

cv2.waitKey()
cv2.destroyAllWindows()

图片分为两个文件夹:0s和0d

0s中存放的是基础人脸数据

0d文件夹中存放的是接下来要进行识别的图片,我这里放了2张:

 

四、运行效果

运行结果:

五、问题总结

1、cv2.imshow,这个最开始显示的图片颜色不对,后来修改如下:

cv2.imshow(fd, cv2.cvtColor(faceData, cv2.COLOR_BGR2RGB))

将图片的颜色模式调整为RGB,然后色彩就正常了。

2、中文字符支持,最开始用 cv2.putText,但是这个函数只支持英文字符,中文字符会显示问号乱码。

后加入中文字符支持函数,用 PIL 库中的 ImageDraw 来写入中文字符。

  • 23
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,下面是一个简单的基于face_recognition人脸识别方案代码示例: ```python import face_recognition import cv2 # 加载已知人脸的图像和名称 known_face_encodings = [] known_face_names = [] image1 = face_recognition.load_image_file("person1.jpg") image2 = face_recognition.load_image_file("person2.jpg") image3 = face_recognition.load_image_file("person3.jpg") known_face_encodings.append(face_recognition.face_encodings(image1)[0]) known_face_encodings.append(face_recognition.face_encodings(image2)[0]) known_face_encodings.append(face_recognition.face_encodings(image3)[0]) known_face_names = ["person1", "person2", "person3"] # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧图像 ret, frame = cap.read() # 将图像转换为RGB格式 rgb_frame = frame[:, :, ::-1] # 检测人脸 face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # 遍历所有检测到的人脸 for face_encoding, face_location in zip(face_encodings, face_locations): # 尝试匹配人脸 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) # 如果有匹配到的人脸,则取出其名称 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] else: name = "unknown" # 绘制人脸矩形框和名称 top, right, bottom, left = face_location cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示图像 cv2.imshow('Face Recognition', frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和窗口 cap.release() cv2.destroyAllWindows() ``` 在这个代码示例中,我们使用face_recognition加载了已知人脸的图像和名称,并使用cv2.VideoCapture()方法打开了摄像头。在每一帧图像中,我们使用face_recognition检测人脸,并使用face_recognition.compare_faces()方法尝试匹配人脸。如果有匹配到的人脸,则取出其名称,并使用cv2.rectangle()和cv2.putText()方法绘制人脸矩形框和名称。最后,我们使用cv2.imshow()方法显示图像,并使用cv2.waitKey()方法等待用户按下q键退出程序。 值得注意的是,face_recognition是一个基于dlib人脸识别,其检测速度较慢,但是识别精度较高。在实际应用中,我们需要根据具体情况选择不同的人脸识别方法以达到更好的效果。 希望这个代码对您有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值