gpt4 book ai didi

python - facenet 识别不正确

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:17 25 4
gpt4 key购买 nike

我一直在研究人脸识别考勤管理系统。我从头开始构建管道,但最后,脚本在一组 10 个类中识别出错误的面孔。我使用 Tensorflow 和 Python 实现了以下管道。

  1. 捕获图像,调整大小,使用 dlib 的形状预测器对齐它们,并将它们存储在命名文件夹中,以便日后在执行识别时进行比较。
  2. 将图像腌制到 data.pickle 文件中以供以后反序列化。

  3. 使用 OpenCV 实现 MTCNN 算法检测网络摄像头捕获的帧中的人脸

  4. 将这些帧传递到 facenet 网络以创建 128 维嵌入,并相应地与 pickle 数据库中存在的嵌入进行比较。

下面是运行第 3 步和第 4 步的主文件:

from keras import backend as K
import time
from multiprocessing.dummy import Pool
K.set_image_data_format('channels_first')
import cv2
import os
import glob
import numpy as np
from numpy import genfromtxt
import tensorflow as tf
from keras.models import load_model
from fr_utils import *
from inception_blocks_v2 import *
from mtcnn.mtcnn import MTCNN
import dlib
from imutils import face_utils
import imutils
import pickle
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split


FRmodel = load_model('face-rec_Google.h5')
# detector = dlib.get_frontal_face_detector()
detector = MTCNN()
# FRmodel = faceRecoModel(input_shape=(3, 96, 96))
#
# # detector = dlib.get_frontal_face_detector()
# # predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# def triplet_loss(y_true, y_pred, alpha = 0.3):
# """
# Implementation of the triplet loss as defined by formula (3)
#
# Arguments:
# y_pred -- python list containing three objects:
# anchor -- the encodings for the anchor images, of shape (None, 128)
# positive -- the encodings for the positive images, of shape (None, 128)
# negative -- the encodings for the negative images, of shape (None, 128)
#
# Returns:
# loss -- real number, value of the loss
# """
#
# anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]
#
# pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), axis=-1)
# neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), axis=-1)
# basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
# loss = tf.reduce_sum(tf.maximum(basic_loss, 0.0))
#
# return loss
#
# FRmodel.compile(optimizer = 'adam', loss = triplet_loss, metrics = ['accuracy'])
# load_weights_from_FaceNet(FRmodel)
def ret_model():
return FRmodel

def prepare_database():
pickle_in = open("data.pickle","rb")
database = pickle.load(pickle_in)
return database

def unpickle_something(pickle_file):
pickle_in = open(pickle_file,"rb")
unpickled_file = pickle.load(pickle_in)
return unpickled_file


def webcam_face_recognizer(database):

cv2.namedWindow("preview")
vc = cv2.VideoCapture(0)

while vc.isOpened():
ret, frame = vc.read()
img_rgb = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
img = frame
# We do not want to detect a new identity while the program is in the process of identifying another person
img = process_frame(img,img)

cv2.imshow("Preview", img)
cv2.waitKey(1)

vc.release()

def process_frame(img, frame):
"""
Determine whether the current frame contains the faces of people from our database
"""
# rects = detector(img)
rects = detector.detect_faces(img)
# Loop through all the faces detected and determine whether or not they are in the database
identities = []
for (i,rect) in enumerate(rects):
(x,y,w,h) = rect['box'][0],rect['box'][1],rect['box'][2],rect['box'][3]
img = cv2.rectangle(frame,(x, y),(x+w, y+h),(255,0,0),2)

identity = find_identity(frame, x-50, y-50, x+w+50, y+h+50)
cv2.putText(img, identity,(10,500), cv2.FONT_HERSHEY_SIMPLEX , 4,(255,255,255),2,cv2.LINE_AA)

if identity is not None:
identities.append(identity)

if identities != []:
cv2.imwrite('example.png',img)

return img

def find_identity(frame, x,y,w,h):
"""
Determine whether the face contained within the bounding box exists in our database

x1,y1_____________
| |
| |
|_________________x2,y2

"""
height, width, channels = frame.shape
# The padding is necessary since the OpenCV face detector creates the bounding box around the face and not the head
part_image = frame[y:y+h, x:x+w]

return who_is_it(part_image, database, FRmodel)

def who_is_it(image, database, model):

encoding = img_to_encoding(image, model)


min_dist = 100
# Loop over the database dictionary's names and encodings.
for (name, db_enc) in database.items():

# Compute L2 distance between the target "encoding" and the current "emb" from the database.
dist = np.linalg.norm(db_enc.flatten() - encoding.flatten())

print('distance for %s is %s' %(name, dist))

# If this distance is less than the min_dist, then set min_dist to dist, and identity to name
if dist < min_dist:
min_dist = dist
identity = name

if min_dist >0.1:
print('Unknown person')
else:
print(identity)
return identity


if __name__ == "__main__":
database = prepare_database()
webcam_face_recognizer(database)

我在这里做错了什么?这里的FRmodel是facenet训练好的模型

最佳答案

几点:

  • 我没有看到输入网络的输入人脸图像的大小调整、对齐和美白。

  • 您不能将 50 的固定边距添加到可变大小的面部。必须进行缩放,使人脸区域几乎填满每个输入图像中的相同区域。

  • 我不确定您使用的型号,但如果您使用的是 FaceNet ,你接受的匹配阈值 0.1 似乎很低。它不会接受任何匹配项,除非它是完全相同的图像(距离为 0.0),或者与图库图像的差异非常小。

关于python - facenet 识别不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52963149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com