使用python第三方模块face_recognition实现人脸识别,并根据已命名的图片把名字显示在屏幕上。
- 安装模块
需要安装opencv,face_recognition,face_recognition模块需要先安装dlib,而dlib需要先安装cmake和boost
所以按顺序安装
pip install cmake
pip install boost
pip install dlib
pip install face_recognition
pip install opencv-python
如果未安装pip等需要工具,请自行百度。
如果速度慢,可以在命令后加 -i [国内源]
如
pip install opencv-python -i https://mirrors.aliyun.com/pypi/simple
我用的三个国内源:
阿里云 https://mirrors.aliyun.com/pypi/simple
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple
- 代码实现
import face_recognition
import cv2
import os
import numpy
from PIL import Image, ImageDraw, ImageFont
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, numpy.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle