• Python 基于OpenCV+face_recognition实现人脸捕捉与人脸识别


    1.安装包依赖

    1. pip install opencv-python
    2. pip install face-recognition

    如果安装face_recognition过程中报错,提示:“CMake must be installed to build the following extensions: dlib”,则需要安装CMake

    pip install cmake

    cmake安装成功后再安装face_recognition

    可以使用国内镜像安装,否则可能比较耗时,国内镜像如下:

    • 清华:https://pypi.tuna.tsinghua.edu.cn/simple/
    • 阿里云:https://mirrors.aliyun.com/pypi/simple/
    • 中国科技大学: https://pypi.mirrors.ustc.edu.cn/simple/
    • 华中理工大学:https://pypi.hustunique.com/
    • 山东理工大学:https://pypi.sdutlinux.org/
    • 豆瓣:https://pypi.douban.com/simple/

    执行脚本的时候后面加上对应的镜像地址:

    pip install xxxxx -i https://pypi.tuna.tsinghua.edu.cn/simple/

    如果使用的是Microsoft Visual Studio ,则可以直接在项目目录下的Python环境上右键,选择Install Python Package... 安装对应的开发包

     

     在输入框里输入对应的开发包名即可,如果使用国内镜像,则在包名后面加上 -i https://xxxx即可

    例如我使用阿里云镜像安装face-recognition

    点击OK后,会给一个提示框,点击“确定”继续即可

     然后就可以在VS下面的输出中看到安装进度了

     2.代码示例

    1. import os
    2. import cv2
    3. import numpy as np
    4. import face_recognition
    5. import time
    6. #对人脸集合进行编码进行处理
    7. def findEncodeings(images):
    8. encodeList=[]
    9. for img in images:
    10. #灰度处理
    11. img=cv2.cvtColor(src=img,code=cv2.COLOR_BGR2RGB)
    12. #face_encodings对图片对象a_images进行编码并返回数组0位置编码结果
    13. encode=face_recognition.face_encodings(img)[0]
    14. encodeList.append(encode)
    15. return encodeList
    16. #获取当前存储的人脸编码集合
    17. def findExistsEncodeingList(img_path):
    18. images=[]
    19. #现有的人脸编码集合
    20. existsEncodeingList=[]
    21. #列出已经上传的所有图片
    22. imgList=os.listdir(img_path)
    23. #处理存储的图片得到其人脸编码
    24. for pic in imgList:
    25. img=cv2.imread('{}/{}'.format(img_path,pic))
    26. images.append(img)
    27. classNames.append(os.path.splitext(pic)[0])
    28. #计算findEncodeings的耗时
    29. start =time.clock()
    30. existsEncodeingList=findEncodeings(images)
    31. end = time.clock()
    32. print('Running time: %s Seconds'%(end-start))
    33. return existsEncodeingList
    34. #保存文件名,也就是图像中人物的名称
    35. classNames=[]
    36. img_path='Picture'
    37. cap=cv2.VideoCapture(0)
    38. existsEncodeingList=findExistsEncodeingList(img_path)
    39. while cap.isOpened():
    40. #读取当前摄像头的画面
    41. ret,frame=cap.read()
    42. #给摄像头画面一个尺寸大小
    43. frame=cv2.resize(src=frame,dsize=(1078,760))
    44. frameRGB=cv2.cvtColor(src=frame,code=cv2.COLOR_BGR2RGB)
    45. #对摄像头读取的检测人脸
    46. facesLocate=face_recognition.face_locations(frameRGB)
    47. #进行特征编码
    48. faceEncoded=face_recognition.face_encodings(frameRGB,facesLocate)
    49. #遍历检测的人脸和库中读取的图片进行对比,计算其相似度
    50. for (top,right, bottom,left),face_encoding in zip(facesLocate,faceEncoded):
    51. #进行匹配
    52. matchs=face_recognition.compare_faces(existsEncodeingList,face_encoding)
    53. #计算相似度
    54. distance=face_recognition.face_distance(existsEncodeingList,face_encoding)
    55. #判断是否匹配
    56. name='unknow'
    57. for index, item in enumerate(distance):
    58. if item<0.38:
    59. if matchs[index]:
    60. #得到匹配到的图片名称与相似度值
    61. name='Similar photos exist: {}; similarity value:{}'.format(classNames[index],item)
    62. break
    63. #初始化面部捕捉框显示绿色
    64. color1 =(0,255,0)
    65. if name =='unknow':
    66. #未能识别的时候显示蓝色
    67. color1 =(255,0,0)
    68. #画面部捕捉框
    69. cv2.rectangle(img=frame,pt1=(left,top),pt2=(right,bottom),color=color1,thickness=2)
    70. #在捕捉框上添加匹配到的图片信息
    71. cv2.putText(frame, name, (left,top-8),cv2.FONT_HERSHEY_SIMPLEX, 0.35, color1, 1)
    72. cv2.imshow('frame',frame)
    73. #按下回车键结束
    74. if cv2.waitKey(1)&0xFF==13:
    75. break
    76. cap.release()
    77. cv2.destroyAllWindows()

    代码里面每行基本上都有注释,所以这里不做过多解释,其中发现在启动的时候比较慢,加上了对方法执行时间的打印,最终定位到了这行代码:

    1. #face_encodings对图片对象a_images进行编码并返回数组0位置编码结果
    2. encode=face_recognition.face_encodings(img)[0]

     原因应该与图片的分辨率、大小有关。另外还发现在调用摄像头进行检测的时候,视频画面存在严重的卡顿,因为我外接的是一个工业级高清摄像头在电脑上,应该也是因为分辨率与图片的大小设置的太大导致的。

     这里的0.38是相似度,值越小代表越相似,一般设置在0.4左右即可,如果是同一个摄像头采集的人脸然后进行识别相似度在0.3左右。

  • 相关阅读:
    Linux-文件和目录权限
    艾美捷Immunochemistry MitoPT JC-1试剂盒
    迭代器模式,内部和外部迭代器举例(设计模式与开发实践 P7)
    Go的简单入门:开始使用模糊测试
    PyTorch 张量数据类型
    新掌门蔡崇信,能否“再救”阿里?
    viper读取配置文件
    C++设计模式_05_Observer 观察者模式
    Redis五大数据类型的底层设计
    Camera Hal OEM模块 ---- cmr_snapshot.c
  • 原文地址:https://blog.csdn.net/qq_17486399/article/details/126608176