资源简介
基于face_recognition库的人脸识别代码,包含单张图片识别、实时视频识别。有详细完整的注释

代码片段和文件信息
import numpy as np
import cv2
import time
import face_recognition
# Threshold = 0.65 # 人脸置信度阈值
‘‘‘
功能:计算两张图片的相似度,范围:[01]
输入:
1)人脸A的特征向量
2)人脸B的特征向量
输出:
1)sim:AB的相似度
‘‘‘
def simcos(AB):
A=np.array(A)
B=np.array(B)
dist = np.linalg.norm(A - B) # 二范数
sim = 1.0 / (1.0 + dist) #
return sim
‘‘‘
功能:
输入:
1)x:人脸库向量(n维)
2)y:被测人脸的特征向量(1维)
输出:
1)match:与人脸库匹配列表,如[FalseTrueTrueFalse]
表示被测人脸y与人脸库x的第23张图片匹配,与14不匹配
2)max(ressim):最大相似度
‘‘‘
def compare_faces(xyThreshold):
ressim = []
match = [False]*len(x)
for fet in x:
sim = simcos(fety)
ressim.append(sim)
if max(ressim) > Threshold: #置信度阈值
match[ressim.index(max(ressim))] = True
return matchmax(ressim)
‘‘‘
注册身份
输入:
1)libpath:人脸库地址
输出:
1)known_face_encodings:人脸库特征向量
2)known_face_names:人脸库名字标签
‘‘‘
def registeredIdentity(libpath):
known_face_encodings known_face_names = [] []
with open(libpath + ‘liblist.txt‘ ‘r‘) as f:
lines = f.readlines()
for line in lines:
img_lable_name = line.split()
image = face_recognition.load_image_file(libpath + str(img_lable_name[0]))
face_locations = face_recognition.face_locations(image)
# face_locations = face_recognition.face_locations(image model=‘cnn‘)
face_encoding = face_recognition.face_encodings(image face_locations)[0]
# face_encoding = face_recognition.face_encodings(image face_locations)
known_face_encodings.append(face_encoding)
known_face_names.append(str(img_lable_name[1]))
return known_face_encodings known_face_names
‘‘‘
输入:
1)testimg:测试图片
2)known_face_encodings:人脸库特征向量
3)known_face_names:人脸库名字标签
输出:
1)retname:预测的名字
2)retscore:相似度得分
3)face_locations:人脸位置坐标
‘‘‘
def identityRecognition(testimgknown_face_encodingsknown_face_namesThreshold):
face_locations = face_recognition.face_locations(testimg)
# face_locations = face_recognition.face_locations(testimg model=“cnn“)
face_encodings = face_recognition.face_encodings(testimg face_locations)
retname retscore = “Noface“ 0
for face_encoding in face_encodings:
matches score = compare_faces(known_face_encodings face_encodingThreshold)
retname retscore = “Unknow“ 0
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
if score > retscore:
retname = name
retscore = score
return retname retscoreface_locations
‘‘‘
输入:
1)img:摄像头得到的未裁剪图片
2)face_locations:人脸位置坐标
3) name:预测的名字
输出:
img:加框加年龄备注之后的画面
‘‘‘
def age_show(img face_locationsname):
for (y0 x1 y1 x0) in face_locations:
cv2.rectangle(img (x0 y0) (x1 y1) ( 0 0255) 2)
info = str(name)
t_size = cv2.getTextSize(str(info) cv2.FONT_HERSHEY_PLAIN 1 2)[0]
x2y2 = x0 + t_size[0] + 3 y0 + t_size[1] + 4
cv2.rectangle(img (x0y0) (x2y2) (0 0 255) -1) # -1填充作为文字框底色
cv2.putText(img info (x0 y0 +t_size[1]+4) cv2.FONT_HERSHEY_PLAIN 1 (0 0 0) 1)
return img
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-08-07 15:15 .idea\
文件 455 2018-08-07 13:10 .idea\face_recognition.iml
目录 0 2018-08-07 13:10 .idea\inspectionProfiles\
文件 288 2018-08-07 13:10 .idea\misc.xm
文件 284 2018-08-07 13:10 .idea\modules.xm
文件 16408 2018-08-07 15:15 .idea\workspace.xm
目录 0 2018-08-07 14:18 __pycache__\
文件 2095 2018-08-07 14:18 __pycache__\face.cpython-36.pyc
文件 3526 2018-08-07 14:27 face.py
目录 0 2018-08-07 14:52 facelib\
文件 145405 2018-06-28 17:54 facelib\1.png
文件 31910 2018-06-28 20:31 facelib\11.jpg
文件 865334 2018-06-28 17:54 facelib\11.png
文件 18128 2018-08-07 14:52 facelib\55.jpg
文件 58 2018-08-07 14:52 facelib\liblist.txt
文件 881 2018-08-07 14:27 pic_demo.py
目录 0 2018-08-07 13:15 Test\
目录 0 2018-08-07 13:16 Test\pic_in\
文件 43544 2018-06-26 10:52 Test\pic_in\test1.png
目录 0 2018-08-07 13:27 Test\pic_out\
文件 203243 2018-08-07 14:12 Test\pic_out\test1.png
目录 0 2018-08-07 13:15 Test\video_in\
目录 0 2018-08-07 14:19 Test\video_out\
文件 202210 2018-08-07 14:26 Test\video_out\1.avi
文件 1467 2018-08-07 14:26 video_demo.py
- 上一篇:LR1语法分析器
- 下一篇:工程电磁场原理内含答案
相关资源
- 一个人脸识别程序源码
- LDA 人脸识别
- halcon简单实现人脸识别.hdev
- 人脸识别开源SDK源码
- 百度人脸识别Demo
- delphi百度人脸识别离线SDK demo
- 讯飞人脸识别eclipse版
- Delphi7调用虹软人脸识别的测试
- [b115]FPGA上运行人脸识别源代码.zip
- shape_predictor_68_face_landmarks.dat.bz2 68个标
- labview人脸识别283682
- 一种基于LBP和CNN的人脸识别算法
- 基于CAFFE的人脸识别系统
- LabVIEW的人脸识别代码
- 基于深度学习实现人脸识别包含模型
- 人脸识别必备的FERET人脸数据库
- 经典的人脸识别论文,包含中、英文
- H5人脸识别+活体检测眨眼摇头
- 人脸识别图像预处理技术
- iOS平台下人脸识别系统实现研究
- 人脸识别数据集说明及其
- 卷积神经网络的人脸识别样本采集+
- STM32人脸识别代码
- 科大讯飞 语音听写 人脸识别 sdk
- 基于PCA的人脸识别技术的研究
- 基于LBP算法的人脸识别研究
- 人脸识别SDK免费,可商用,有演示、
- cpp-TDLib一个跨平台功能齐全的Telegra
- 人脸识别三套源码含小程序源码亲测
- 基于神经网络的人脸识别(附代码)
评论
共有 条评论