Python-based face recognition library face_recognition

A, face_recognition library Introduction

face_recognition is an open source Python library face recognition, support for Python 3.3+ and Python 2.7. References official website:

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

This library was selected, because

1, use this library to implement a face recognition program is very simple, and very easy to configure the environment;

2, can be used as the model has been trained, you do not need to retrain locally. An ordinary computer can run recognition programs, hardware environment do not ask directly.

Second, the installation environment

I own environment as follows:

Hardware: 2008 laptop, Pentium Dual Core, as a relatively low-end notebook

System: win7x64

python: 3.6 (Note: The recommended configuration environment with version 3.6 . I use 3.7 configuration environment failed, dlib installation always fails)

3.6 with the installation process is relatively simple, can refer https://www.jianshu.com/p/8296f2aac1aa

Before installing with pip, pip Ali pay attention to first modify the source, so speed is much faster.

Third, code implementation

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

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

#写入中文字符支持
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)

	# 人脸检测,并获取帧中所有人脸编码
	face_locations = face_recognition.face_locations(faceData)
	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)
			name = "?"
			if match[0]:
				name = baseface_titles[i]
				print("识别出:" + name)
				break

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

		# 围绕脸的框
		cv2.rectangle(faceData, (left, top), (right, bottom), (0, 0, 255), 2)
		# 框下的名字(即,匹配的图片文件名)
		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()

Picture is divided into two folders: 0s and 0d

0s stored face data is the basis for

0d folder is stored in the next to identify the picture, I've put a 3:

Fourth, the operating results

operation result:

V. Summary of issues

1, cv2.imshow, this picture does not display color initially, and later amended as follows:

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

Adjust the picture color mode is RGB, and color to normal.

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

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

发布了3 篇原创文章 · 获赞 9 · 访问量 911

Guess you like

Origin blog.csdn.net/qaswzh/article/details/104333203