侵权投诉
订阅
纠错
加入自媒体

通过门铃也能识别出是谁在敲门了吗?

2021-03-13 10:05
磐创AI
关注

预处理函数是我用来标准化图像并将其重塑为(160,160,3)的函数,而识别函数是一个执行编码函数的类。如果你注意到了,我将这些编码保存为字典。在执行实时识别时,这个字典很方便,因为它是存储人名和编码的一种简单方法。实时人脸识别现在我们有了我们想要识别的人的图像,那么实时识别过程是如何工作的呢?如下图所示:

门铃响时,下载一个视频,选择多个帧。利用这些帧,用detect_faces方法进行多实例的人脸检测。下面是face_recognition.py 类的一个片段:import cv2
import mtcnn
face_detector = mtcnn.MTCNN()
conf_t = 0.99
def detect_faces(cv2_img):
   img_rgb = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
   results = face_detector.detect_faces(img_rgb)
   faces = []
   for res in results:
       x1, y1, width, height = res['box']
       x1, y1 = abs(x1), abs(y1)
       x2, y2 = x1 + width, y1 + height
       confidence = res['confidence']
       if confidence < conf_t:
           continue
       faces.append(cv2_img[y1:y2, x1:x2])
   return faces
def detect_face(cv2_img):
   img_rgb = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2RGB)
   results = face_detector.detect_faces(img_rgb)
   x1, y1, width, height = results[0]['box']
   cv2.waitKey(1)
   x1, y1 = abs(x1), abs(y1)
   x2, y2 = x1 + width, y1 + height
   confidence = results[0]['confidence']
   if confidence < conf_t:
       return None
   return cv2_img[y1:y2, x1:x2]

对图像进行预处理并送入FaceNet。FaceNet将输出每个人脸的128维嵌入。然后使用余弦相似度将这些向量与encode .pkl中存储的向量进行比较。人脸与输入人脸最接近的人被返回。如果一张脸距离它最近的脸有一个特定的阈值,则返回“未知”。这表明这张脸不像任何已知的脸。下面是face_recognition.py类的其余部分:from utils import preprocess
from model.facenet_loader import model
import numpy as np
from scipy.spatial.distance import cosine
import pickle
from sklearn.preprocessing import Normalizer
l2_normalizer = Normalizer('l2')
def encode(img):
   img = np.expand_dims(img, axis=0)
   out = model.predict(img)[0]
   return out
def load_database():
   with open('data/encodings/encoding.pkl', 'rb') as f:
       database = pickle.load(f)
   return database
recog_t = 0.35
def recognize(img):
   people = detect_faces(img)
   if len(people) == 0:
       return None
   best_people = []
   people = [preprocess(person) for person in people]
   encoded = [encode(person) for person in people]
   encoded = [l2_normalizer.transform(encoding.reshape(1, -1))[0]
              for encoding in encoded]
   database = load_database()
   for person in encoded:
       best = 1
       best_name = ''
       for k, v in database.items():
           dist = cosine(person, v)
           if dist < best:
               best = dist
               best_name = k
       if best > recog_t:
           best_name = 'UNKNOWN'
       best_people.append(best_name)
   return best_people

<上一页  1  2  3  4  5  下一页>  余下全文
声明: 本文由入驻维科号的作者撰写,观点仅代表作者本人,不代表OFweek立场。如有侵权或其他问题,请联系举报。

发表评论

0条评论,0人参与

请输入评论内容...

请输入评论/评论长度6~500个字

您提交的评论过于频繁,请输入验证码继续

暂无评论

暂无评论

智能家居 猎头职位 更多
文章纠错
x
*文字标题:
*纠错内容:
联系邮箱:
*验 证 码:

粤公网安备 44030502002758号