- 在安装Python的一些库时,遇到的错误及解决办法:
# OpenCV的介绍、安装和使用
# OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,OpenCV用C++语言编写,
# 支持Python、Ruby、Java等语言的接口
# OpenCV安装:前提是已经安装有Python3环境、在命令行输入pip3 install opencv-python
# 使用清华镜像源快速安装:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
# 配置Pycharm的环境:1、如果新建的项目中包含venv文件,则需将文件下scripts的路径添加到环境配置中
# 2、可通过勾选在新建项目时勾选Existing interpreter,找到scripts的路径添加环境变量,完成整体的环境配置
# 同时需要将python.exe文件的路径添加环境变量
# 错误解决:
# 此时如果输入python -m pip install --upgrade pip没反应,输入python打开WindowsApps
# 需要删除%USERPROFILE%\AppData\Local\Microsoft\WindowsApps这个用户变量,即可解决
# 若在IDE中找不到import cv2,则设置settings中--python interpreter找到Path路径下的解释器
# 项目打包
# 安装第三方模块
# 在线安装方式(针对没安装过得情况)
# 1、Win+R 输入:amd -> 回车
# 2、输入:pip install PyInstaller -> 回车(等待安装完成)
# 注意:如果出现Successfully installed则表示安装成功
# 错误解决:
# 如果出现黄色的字体,一般是因为pip库不是最新的,需要对pip库进行更新,更新指令为:
# python -m pip install --upgrade pip
# 如果更新时又反复出现黄色字体,则不要直接win+R+cmd,要选择Windows系统--命令提示符--以管理员身份运行
# 如果安装后提示错误Preparing wheel metadata ... error
# 则需要先安装wheel,安装指令为:
# pip install wheel -i https://pypi.tuna.tsinghua.edu.cn/simple
# 安装完成后再次安装pyinstaller,安装指令为:
# pip install Pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple
# 项目打包
# 1、Win+R 输入:amd -> 回车
# 2、输入:pyinstaller -F -> 项目路径 -> 回车
# 3、找到Appending archive to EXE C:\Users\LiuYongHao\dist\Demo.exe
# 即为打包后的文件路径
- 在Windows系统下安装face_recognition库
# face_recognition
# 世界上最简洁的人脸识别库和装饰库,你可以使用Python和命令行工具提取、识别、装饰人脸
# face_recognition在Windows环境下的安装:
# 1、先安装cmake,pip install cmake,测试是否安装成功:先输入python+回车,再输入import cmake
# 2、再安装boost,pip install boost
# 3、再安装dlib,pip install dlib
# 4、最后安装face_recognition,pip install face_recognition
# 镜像源安装为:pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
- 源代码如下:
# 项目名称:利用笔记本自带摄像头实现人脸识别
# 程序实现目的:
# 开发时间: 2020/12/29 15:03
import cv2
import face_recognition
def main():
# 1、准备人脸库
# 1、1读取图片
me = cv2.imread('dataset/wo.jpg')
wei = cv2.imread('dataset/wei.jpg')
xiu = cv2.imread('dataset/xiu.jpg')
you = cv2.imread('dataset/youwei.jpg')
zhi = cv2.imread('dataset/zhi.jpg')
yuan = cv2.imread('dataset/yuan.jpg')
# 1、2对图片中人脸进行编码
me_face_encoding = face_recognition.face_encodings(me)[0]
wei_face_encoding = face_recognition.face_encodings(wei)[0]
xiu_face_encoding = face_recognition.face_encodings(xiu)[0]
you_face_encoding = face_recognition.face_encodings(you)[0]
zhi_face_encoding = face_recognition.face_encodings(zhi)[0]
yuan_face_encoding = face_recognition.face_encodings(yuan)[0]
# 1、3准备人脸库的人脸编码列表
known_face_encodings = [yuan_face_encoding,me_face_encoding, wei_face_encoding, xiu_face_encoding, you_face_encoding]
# 1、4准备人脸库中人脸编码对应姓名
known_face_names = ['Yuan','Me', 'Wei', 'Xiu', 'YouWei', 'ZhiGuang']
# 2、捕获视频中的图片
vc = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
# 获取视频中每一帧的图片
ret, img = vc.read()
if not ret:
print('没有捕获到视频!')
break
# 2、1发现图片中人脸的位置
locations = face_recognition.face_locations(img)
# 2、2对图片中人脸进行编码
face_encodings = face_recognition.face_encodings(img, locations)
# 遍历locations,face_encodings,识别图片中人脸
# location包括:top、right、bottom、left
for (top, right, bottom, left), face_encoding in zip(locations, face_encodings):
# 比较人脸
matchs = face_recognition.compare_faces(known_face_encodings, face_encoding, 0.4)
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, 255, 0), 2)
# 标记人脸的姓名
cv2.putText(img, name, (left, top - 10), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 2, (0, 0, 255))
# 3、发现视频图片中的人脸位置
# 4、识别人脸
# 5、展示
cv2.imshow('Video', img)
# 6、释放资源
if cv2.waitKey(1) != -1:
# 关闭摄像头
vc.release()
# 销毁窗口
cv2.destroyAllWindows()
# 结束循环
break
# 设定主程序
if __name__ == '__main__':
main()
- 两个重要函数的说明:
# face_encodings(face_image, known_face_locations=None, num_jitters=1, model="small")
# 传入带有人脸的图片,为每一张人脸返回128维的人脸向量
# known_face_locations已知图片中每一个人脸的位置
# num_jitters重新采样的次数,值越大,越精确,但也会更慢
# compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)
# 检查未知人脸编码是否与已知人脸编码匹配
# tolerance两者距离多大才算匹配,越小越严格,0.6是典型的最佳性能
# 返回True、False的列表,标识哪些已知人脸与未知人脸匹配