通过门铃也能识别出是谁在敲门了吗?
预处理函数是我用来标准化图像并将其重塑为(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
图片新闻
最新活动更多
-
11月22日立即报名>> 【线下论坛】华邦电子与莱迪思联合技术论坛
-
精彩回顾立即查看>> 2024 智能家居出海论坛
-
精彩回顾立即查看>> 【线下论坛】华邦电子与恩智浦联合技术论坛
-
精彩回顾立即查看>> 【限时免费下载】TE暖通空调系统高效可靠的组件解决方案
-
精彩回顾立即查看>> 中国国际胶粘剂及密封剂展
-
精彩回顾立即查看>> 2024(第五届)全球数字经济产业大会暨展览会
发表评论
请输入评论内容...
请输入评论/评论长度6~500个字
暂无评论
暂无评论