问题描述
我有一排包含Oracle中人物的姓名和照片的行,如何进行只能通过使用相机拍照才能识别姓名的人脸识别? 我可以使用什么技术?
解决方法
首先,不要将原始图像存储在blob列中。您应该存储原始图像的矢量表示。以下python代码块将找到人脸图像的矢量表示。
#!pip install deepface
from deepface.basemodels import VGGFace,Facenet
model = VGGFace.loadModel() #you can use google facenet instead of vgg
target_size = model.layers[0].input_shape
#preprocess detects facial area and aligns it
img = functions.preprocess_face(img="img.jpg",target_size=target_size)
representation = model.predict(img)[0,:]
在这里,您可以将确切的图像路径(如img.jpg)或3D数组传递给preprocess_face的img参数。这样,您可以将矢量表示形式存储在oracle数据库的blob列中。
当您拥有新的人脸图像并想要在数据库中找到其身份时,再次找到其表示形式。
#preprocess detects facial area and aligns it
target_img = functions.preprocess_face(img="target.jpg",target_size=target_size)
target_representation = model.predict(target_img )[0,:]
现在,您有了目标图像的矢量表示形式和数据库图像的矢量表示形式。您需要找到目标图像表示形式和数据库表示形式的每个实例的相似性得分。
欧几里得距离是比较向量的最简单方法。
def findEuclideanDistance(source_representation,test_representation):
euclidean_distance = source_representation - test_representation
euclidean_distance = np.sum(np.multiply(euclidean_distance,euclidean_distance))
euclidean_distance = np.sqrt(euclidean_distance)
return euclidean_distance
我们将比较每个数据库实例与目标。假设数据库实例的表示形式存储在表示形式对象中。
distances = []
for i in range(0,len(representations)):
source_representation = representations[i]
#find the distance between target_representation and source_representation
distance = findEuclideanDistance(source_representation,target_representation )
distances.append(distance)
距离列表存储数据库中每个项目到目标的距离。我们需要找到最小的距离。
import numpy as np
idx = np.argmax(distances)
Idx是数据库中目标图像的ID。