python 人脸识别demo
作者:互联网
使用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 = ImageFont.truetype(
“font/simsun.ttc”, textSize, encoding=“utf-8”)
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
def face(path):
# 存储知道人名列表
global face_locations, face_names
known_names = []
# 存储知道的特征值
known_encodings = []
for image_name in os.listdir(path):
print(path + image_name)
load_image = face_recognition.load_image_file(path + image_name) # 加载图片
image_face_encoding = face_recognition.face_encodings(load_image)[0] # 获得128维特征值
known_names.append(image_name.split(".")[0])
known_encodings.append(image_face_encoding)
print(known_encodings)
<span class="token comment"># 打开摄像头,0表示内置摄像头</span>
video_capture <span class="token operator">=</span> cv2<span class="token punctuation">.</span>VideoCapture<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
process_this_frame <span class="token operator">=</span> <span class="token boolean">True</span>
<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
ret<span class="token punctuation">,</span> frame <span class="token operator">=</span> video_capture<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。</span>
rgb_frame <span class="token operator">=</span> frame<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">,</span> <span class="token punctuation">:</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span>
<span class="token keyword">if</span> process_this_frame<span class="token punctuation">:</span>
face_locations <span class="token operator">=</span> face_recognition<span class="token punctuation">.</span>face_locations<span class="token punctuation">(</span>rgb_frame<span class="token punctuation">)</span> <span class="token comment"># 获得所有人脸位置</span>
face_encodings <span class="token operator">=</span> face_recognition<span class="token punctuation">.</span>face_encodings<span class="token punctuation">(</span>rgb_frame<span class="token punctuation">,</span> face_locations<span class="token punctuation">)</span> <span class="token comment"># 获得人脸特征值</span>
face_names <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token comment"># 存储出现在画面中人脸的名字</span>
<span class="token keyword">for</span> face_encoding <span class="token keyword">in</span> face_encodings<span class="token punctuation">:</span>
matches <span class="token operator">=</span> face_recognition<span class="token punctuation">.</span>compare_faces<span class="token punctuation">(</span>known_encodings<span class="token punctuation">,</span> face_encoding<span class="token punctuation">,</span> tolerance<span class="token operator">=</span><span class="token number">0.3</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> <span class="token boolean">True</span> <span class="token keyword">in</span> matches<span class="token punctuation">:</span>
first_match_index <span class="token operator">=</span> matches<span class="token punctuation">.</span>index<span class="token punctuation">(</span><span class="token boolean">True</span><span class="token punctuation">)</span>
name <span class="token operator">=</span> known_names<span class="token punctuation">[</span>first_match_index<span class="token punctuation">]</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
name <span class="token operator">=</span> <span class="token string">"unknown"</span>
face_names<span class="token punctuation">.</span>append<span class="token punctuation">(</span>name<span class="token punctuation">)</span>
process_this_frame <span class="token operator">=</span> <span class="token operator">not</span> process_this_frame
<span class="token comment"># 将捕捉到的人脸显示出来</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>top<span class="token punctuation">,</span> right<span class="token punctuation">,</span> bottom<span class="token punctuation">,</span> left<span class="token punctuation">)</span><span class="token punctuation">,</span> name <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>face_locations<span class="token punctuation">,</span> face_names<span class="token punctuation">)</span><span class="token punctuation">:</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>frame<span class="token punctuation">,</span> <span class="token punctuation">(</span>left<span class="token punctuation">,</span> top<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>right<span class="token punctuation">,</span> bottom<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment"># 画人脸矩形框</span>
<span class="token comment"># 加上人名标签</span>
cv2<span class="token punctuation">.</span>rectangle<span class="token punctuation">(</span>frame<span class="token punctuation">,</span> <span class="token punctuation">(</span>left<span class="token punctuation">,</span> bottom <span class="token operator">-</span> <span class="token number">35</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span>right<span class="token punctuation">,</span> bottom<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cv2<span class="token punctuation">.</span>FILLED<span class="token punctuation">)</span>
font <span class="token operator">=</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_DUPLEX
<span class="token comment"># cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)</span>
frame<span class="token operator">=</span>cv2ImgAddText<span class="token punctuation">(</span>frame<span class="token punctuation">,</span> name<span class="token punctuation">,</span> left <span class="token operator">+</span> <span class="token number">32</span><span class="token punctuation">,</span> bottom <span class="token operator">-</span> <span class="token number">32</span><span class="token punctuation">,</span> textColor<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> textSize<span class="token operator">=</span><span class="token number">32</span><span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">'frame'</span><span class="token punctuation">,</span> frame<span class="token punctuation">)</span>
<span class="token keyword">if</span> cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">&</span> <span class="token number">0xFF</span> <span class="token operator">==</span> <span class="token builtin">ord</span><span class="token punctuation">(</span><span class="token string">'q'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">break</span>
video_capture<span class="token punctuation">.</span>release<span class="token punctuation">(</span><span class="token punctuation">)</span>
cv2<span class="token punctuation">.</span>destroyAllWindows<span class="token punctuation">(</span><span class="token punctuation">)</span>
if name == ‘main’:
face("./images/") # 存放已知图像路径
其中,face()是识别的方法,cv2ImgAddText()是解决图片添加文字时的中文乱码问题。
matches = face_recognition.compare_faces(known_encodings, face_encoding, tolerance=0.3)是对数据进行比对,tolerance越小,进度越高,一般0.6是性能最好的。
- 效果
标签:人脸识别,name,python,demo,frame,cv2,face,image,recognition 来源: https://blog.csdn.net/weixin_42038955/article/details/115197802