基于facenet的实时人脸检测

参考自https://github.com/shanren7/real_time_face_recognition

本人的项目代码https://github.com/zouzhen/real_time_face_recognize

虽然名字相同,但里面的内容可是有很大的不同
由于不能满足当前的tensorflow版本,以及未能满足设计要求,进行了优化与重新设计

基于facenet的实时人脸检测

工作环境

  • python 3.6
  • tensorflow==1.9.0(可运行在无gpu版)

代码结构

real_time_face_recognize
* |—— model_check_point(保存人脸识别模型)
* |—— models(储存了facenet采用的神经网络模型)
* |—— detect_face.py(主要实现人脸的检测,同时返回可能的人脸框)
* |—— facenet.py(这里存储了facenet的主要函数)
* |—— real_time_face_recognize.py(实现了实时人脸检测)

运行

  1. https://github.com/davidsandberg/facenet 中下载预训练的分类模型,放在model_check_point下
  2. 使用pip install requirements.txt安装需要的包,建议在virtualenv环境安装
  3. 在目录下新建picture文件,将需要识别的人的图片放入其中,每人放入一张清晰的图片即可
  4. 执行python real_time_face_recognize.py

注意

除可在facenet作者的github中下载模型外,我自己基于lfw训练集训练了一个模型,点击

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
以下是基于Python的基于facenet的人脸识别代码示例: ```python import tensorflow as tf import numpy as np import cv2 import os # 加载facenet模型 model_path = 'model/facenet.pb' sess = tf.Session() with tf.gfile.FastGFile(model_path, 'rb') as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) sess.graph.as_default() tf.import_graph_def(graph_def, name='') # 加载人脸检测face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml") # 加载人脸数据库 database = {} for file in os.listdir('database'): if file.endswith('.jpg'): name = file.replace('.jpg', '') img_path = os.path.join('database', file) img = cv2.imread(img_path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) for (x, y, w, h) in faces: face_img = img[y:y+h, x:x+w] face_img = cv2.resize(face_img, (160, 160)) # 将人脸图像转换为128维特征向量 embeddings = sess.run('embeddings:0', feed_dict={'input:0': face_img.reshape(-1, 160, 160, 3)}) database[name] = embeddings cap = cv2.VideoCapture(0) # 打开摄像头 while True: ret, frame = cap.read() # 读取摄像头数据 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 转换为灰度图像 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # 检测人脸 for (x, y, w, h) in faces: face_img = frame[y:y+h, x:x+w] face_img = cv2.resize(face_img, (160, 160)) # 将人脸图像转换为128维特征向量 embeddings = sess.run('embeddings:0', feed_dict={'input:0': face_img.reshape(-1, 160, 160, 3)}) # 在人脸数据库中查找最相似的人脸 min_dist = 100 identity = None for name, emb in database.items(): dist = np.linalg.norm(embeddings - emb) if dist < min_dist: min_dist = dist identity = name # 绘制矩形框和名字 cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.putText(frame, identity, (x+5, y-5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow('frame', frame) # 显示图像 if cv2.waitKey(1) & 0xFF == ord('q'): # 按下q键退出 break cap.release() # 释放摄像头 cv2.destroyAllWindows() # 关闭所有窗口 ``` 这段代码中,我们使用了基于facenet的人脸识别算法。首先,我们加载了一个名为“facenet.pb”的模型,这是一个预训练好的人脸识别模型,用于将人脸图像转换为128维特征向量。然后,我们加载了一个名为“haarcascade_frontalface_default.xml”的分类器,用于检测人脸。接着,我们加载了一个人脸数据库,其中包含了多个人脸图像和对应的人名。对于每个人脸图像,我们使用人脸检测器检测人脸,并将人脸图像转换为128维特征向量,然后将其存储到数据库中。在识别阶段,我们首先使用人脸检测器检测人脸,然后将人脸图像转换为128维特征向量,并在人脸数据库中查找最相似的人脸。最后,我们在图像上绘制矩形框和名字,并显示图像。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值