一。环境准备
首先在anaconda中新建一个环境,在命令行中切换至python3.7环境。
1.安装cmake和boost
pip install cmake -i
https://pypi.tuna.tsinghua.edu.cn/simple
pip install boost -i
https://pypi.tuna.tsinghua.edu.cn/simple
2.安装opencv
pip install opencv-python==3.4.1.15 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-contrib-python==3.4.1.15 -i https://pypi.tuna.tsinghua.edu.cn/simple
3.安装dlib(容易失败)
以下方式都可以尝试一下
1.pip install dlib==19.9.0
2.pip install dlib==19.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
3.或者提前去官网下载,切换至目录,然后通过python setup.py install 编译
4.安装face_recognition
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
摄像头实时识别
import cv2
import face_recognition
#1、准备人脸库
# #读取图片
me=cv2.imread('test.jpg')
him=cv2.imread('lena.jpg')
#对图片进行编码
me_face_encoding=face_recognition.face_encodings(me)[0]
him_face_encoding=face_recognition.face_encodings(him)[0]
#准备人脸库的人脸编码列表
known_face_encodings=[me_face_encoding,him_face_encoding]
#准备人脸库中人脸编码对应姓名
known_face_names=['me','him']
#2、捕获视频中的图片
vc=cv2.VideoCapture(0)
while True:
ret,img=vc.read()
if not ret:
print('没有捕获到视频')
break
#3、发现图片中的人脸位置
locations=face_recognition.face_locations(img)
#图片中人脸进行编码
face_encodings=face_recognition.face_encodings(img,locations)
#遍历locations,face_encodings,识别图片中的人脸
for (top,right,bottom,left), face_encoding in zip(locations,face_encodings):
#4、识别视频中图片中人脸的姓名
matchs=face_recognition.compare_faces(known_face_encodings,face_encoding)
name='unknown'
for match,known_name in zip(matchs,known_face_names):
if match:
name=known_name
break
#标记人脸位置
cv2.rectangle(img,(left,top),(right,bottom),(0,0,255),2)
#标记人脸姓名
cv2.putText(img,name,(left,top-20),cv2.FONT_HERSHEY_COMPLEX,2,(0,0,255),2)
#5、展示
cv2.imshow('Video',img)
#6、释放
if cv2.waitKey(1) !=-1:
vc.release()
cv2.destroyAllWindows()
break
代码可以参考
https://blog.csdn.net/guoyunfei123/article/details/97112249
https://www.bilibili.com/video/BV1UJ411V7Q4?from=search&seid=6206820950542698972