鱼C论坛

 找回密码
 立即注册
查看: 2899|回复: 0

一个简单的人脸检测程序

[复制链接]
发表于 2019-8-1 16:53:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
直接上代码:
  1. import cv2
  2. import sys

  3. import face_recognition #人脸识别库 #dlib


  4. #读取图片
  5. face_image=face_recognition.load_image_file(r'C:\Users\YY\Desktop\7.png')
  6. face_encodings=face_recognition.face_encodings(face_image)   #特征提取
  7. face_locations=face_recognition.face_locations(face_image)

  8. #判断图片中出现的人数
  9. n=len(face_encodings)
  10. print(n)
  11. #只对比两个人的情况
  12. if n>2:
  13.     print("超过2个人")   #人数大于2个人
  14.     sys.exit()

  15. try:
  16.     face1=face_encodings[0]
  17.     face2=face_encodings[1]
  18. except:
  19.     print('2')
  20.     sys.exit()

  21. #特征对比
  22. result=face_recognition.compare_faces([face1],face2,tolerance=0.5)
  23. print(result)

  24. if result==[True]:
  25.     print('0')
  26.     name='PASS'
  27. else:
  28.     print('1')
  29.     name='DENIED'

  30. #绘图
  31. for i in range(len(face_encodings)):
  32.     #一个一个人脸框图
  33.     #特征
  34.     face_encodings=face_encodings[(i-1)]
  35.     #特征的位置
  36.     face_location=face_locations[(i-1)]
  37.     #用四个变量来接收locatiion的返回值
  38.     #用来画框
  39.     top,right,bottom,left=face_location
  40.     #画框
  41.     cv2.rectangle(face_image,(left,top),(right,bottom),(0,255,0),2)
  42.     #写内容
  43.     cv2.putText(face_image,name,(left-10,top-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,0,0),2)

  44. #让颜色生效
  45. face_image_rgb=cv2.cvtColor(face_image,cv2.COLOR_BGR2RGB)

  46. #展示图像
  47. cv2.imshow('Output',face_image_rgb)

  48. #关闭状态
  49. cv2.waitKey(0)


  50. #人脸识别的基本步骤
  51. #1.读取图片 face_recognition.load_image_file(r'')
  52. #2.特征提取 face_recognition.encodings()
  53. #3.特征点位置提取 face_recognition.face_locations()
  54. #4.判断图片中有多少个人(看有多少个矩阵)len(face_encodings)
  55. #将每一个人脸提取出来   face1=face_encodings[0]   face2=face_encodings[1]  .....
  56. #对比两个人脸: face_recognition.compare_faces([face1],face2,tolerance=0.5)
  57. #画图:利用for循环,遍历两个人脸
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-28 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表