|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
直接上代码:- import cv2
- import sys
- import face_recognition #人脸识别库 #dlib
- #读取图片
- face_image=face_recognition.load_image_file(r'C:\Users\YY\Desktop\7.png')
- face_encodings=face_recognition.face_encodings(face_image) #特征提取
- face_locations=face_recognition.face_locations(face_image)
- #判断图片中出现的人数
- n=len(face_encodings)
- print(n)
- #只对比两个人的情况
- if n>2:
- print("超过2个人") #人数大于2个人
- sys.exit()
- try:
- face1=face_encodings[0]
- face2=face_encodings[1]
- except:
- print('2')
- sys.exit()
- #特征对比
- result=face_recognition.compare_faces([face1],face2,tolerance=0.5)
- print(result)
- if result==[True]:
- print('0')
- name='PASS'
- else:
- print('1')
- name='DENIED'
- #绘图
- for i in range(len(face_encodings)):
- #一个一个人脸框图
- #特征
- face_encodings=face_encodings[(i-1)]
- #特征的位置
- face_location=face_locations[(i-1)]
- #用四个变量来接收locatiion的返回值
- #用来画框
- top,right,bottom,left=face_location
- #画框
- cv2.rectangle(face_image,(left,top),(right,bottom),(0,255,0),2)
- #写内容
- cv2.putText(face_image,name,(left-10,top-10),cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,0,0),2)
- #让颜色生效
- face_image_rgb=cv2.cvtColor(face_image,cv2.COLOR_BGR2RGB)
- #展示图像
- cv2.imshow('Output',face_image_rgb)
- #关闭状态
- cv2.waitKey(0)
- #人脸识别的基本步骤
- #1.读取图片 face_recognition.load_image_file(r'')
- #2.特征提取 face_recognition.encodings()
- #3.特征点位置提取 face_recognition.face_locations()
- #4.判断图片中有多少个人(看有多少个矩阵)len(face_encodings)
- #将每一个人脸提取出来 face1=face_encodings[0] face2=face_encodings[1] .....
- #对比两个人脸: face_recognition.compare_faces([face1],face2,tolerance=0.5)
- #画图:利用for循环,遍历两个人脸
复制代码
|
|