只要你资料集越完整,辨识模型就越强大
这个系列偏重於方法的介绍与使用,因此今天我们不会重头开始训练模型,而是使用:
此模型是由Hiroki Taniai的github网站中取得
模型的快速下载点在这
接下来我们就一样用之前文章中下载的资料集来做孪生网路的人脸辨识吧!
tensorflow-gpu
或是其他更高版号的tensorflow)face_recognition
目录下新建一个档案siamese.py
facenet_keras.h5
下载下来import ntpath
import sys
# resolve module import error in PyCharm
import cv2
import numpy as np
sys.path.append(ntpath.dirname(ntpath.dirname(ntpath.abspath(__file__))))
import argparse
import random
from scipy.spatial import distance
from sklearn.preprocessing import normalize
from tensorflow.keras.models import load_model
from dataset.load_dataset import images_to_faces
def l2_normalize(x, axis=-1, epsilon=1e-10):
output = x / np.sqrt(np.maximum(np.sum(np.square(x), axis=axis, keepdims=True), epsilon))
return output
def main():
# 初始化arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", type=str, required=True, help="the input dataset path")
ap.add_argument("-m", "--model", type=str, required=True, help="the facenet model path")
args = vars(ap.parse_args())
# 载入模型 (模型输入为(160, 160)大小的图片,输出为128-dimensions embeddings)
model = load_model(args["model"])
# 你可以取消下方的注解看看这个模型的长相
# print(model.summary())
print("[INFO] loading dataset....")
(faces, labels) = images_to_faces(args["input"])
print(f"[INFO] {len(faces)} images in dataset")
# 随机抽取图片检查相似度 (抽取两组:一组为同样的人、一组为不同的人)
classes = np.unique(labels)
idxs = [np.where(labels == cls)[0] for cls in classes]
idx = random.choice(idxs)
pos_ids = random.sample(list(idx), 2)
pos_faces = faces[pos_ids]
pos_label = labels[pos_ids[0]]
neg_face = faces[random.choice(np.where(labels != pos_label)[0])]
# 对这两组资料进行相似度比对
for compares in (pos_faces, [pos_faces[0], neg_face]):
# 将照片改为模型输入的大小
img1 = cv2.resize(compares[0], (160, 160))
img2 = cv2.resize(compares[1], (160, 160))
# 将照片由灰阶扩展到3-channels
img1 = np.dstack([img1] * 3)
img2 = np.dstack([img2] * 3)
# 预测图片的embeddings
p1 = model.predict(np.expand_dims(img1, axis=0))
p2 = model.predict(np.expand_dims(img2, axis=0))
# 将结果正规化到[0 ,1]
p1 = normalize(p1)
p2 = normalize(p2)
# 透过判断欧式距离来比较相似度 (数值越小越相似)
dist = distance.euclidean(p1, p2)
final = np.hstack([img1, img2])
cv2.putText(final, f"similarity: {round(dist, 3)}", (10, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("result", final)
cv2.waitKey(0)
if __name__ == '__main__':
main()
python face_recognition/siamese.py -i dataset/caltech_faces -m facenet_keras.h5
:程序码在这里
<<: Day 19 [Python ML、资料视觉化] Seaborn介绍
今天来练习怎麽让动态的物件在影片中消除, 影片范例 但因为没有付钱买,有浮水印,所以不知道能不能去掉...
时常为自己排序 这是一个老生常谈的问题了,工作、家庭、财富、人际、健康,什麽对我们来说是最重要的?...
废话不多说,直接附上code 影片含有程序码详细解说,若有误再烦请告知,谢谢 library(plm...
贪婪(Greedy)演算法 贪婪演算法是考虑局部最佳解,在子结构中解决问题是相当有利的,但放入整体问...
再过一天,比赛就结束了。时间真的过得好快啊! 今晚我从 open data 网站,下载...