From d9c6a4eaa42ba9f8246b09a9a8d7364b7f221cae Mon Sep 17 00:00:00 2001 From: Sefik Ilkin Serengil Date: Fri, 1 May 2020 20:25:09 +0300 Subject: [PATCH] how --- tests/face-recognition-how.py | 84 +++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/face-recognition-how.py diff --git a/tests/face-recognition-how.py b/tests/face-recognition-how.py new file mode 100644 index 0000000..deb201f --- /dev/null +++ b/tests/face-recognition-how.py @@ -0,0 +1,84 @@ +#!pip install deepface +from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace +from deepface.commons import functions + +import matplotlib.pyplot as plt +import numpy as np + +#---------------------------------------------- +#build face recognition model + +model = VGGFace.loadModel() +#model = Facenet.loadModel() +#model = OpenFace.loadModel() +#model = FbDeepFace.loadModel() + +input_shape = model.layers[0].input_shape[1:3] + +print("model input shape: ", model.layers[0].input_shape[1:]) +print("model output shape: ", model.layers[-1].input_shape[-1]) + +#---------------------------------------------- +#load images and find embeddings + +img1 = functions.detectFace("dataset/img1.jpg", input_shape) +img1_representation = model.predict(img1)[0,:] + +img2 = functions.detectFace("dataset/img3.jpg", input_shape) +img2_representation = model.predict(img2)[0,:] + +#---------------------------------------------- +#distance between two images + +distance_vector = np.square(img1_representation - img2_representation) +#print(distance_vector) + +distance = np.sqrt(distance_vector.sum()) +print("Euclidean distance: ",distance) + +#---------------------------------------------- +#expand vectors to be shown better in graph + +img1_graph = []; img2_graph = []; distance_graph = [] + +for i in range(0, 200): + img1_graph.append(img1_representation) + img2_graph.append(img2_representation) + distance_graph.append(distance_vector) + +img1_graph = np.array(img1_graph) +img2_graph = np.array(img2_graph) +distance_graph = np.array(distance_graph) + +#---------------------------------------------- +#plotting + +fig = plt.figure() + +ax1 = fig.add_subplot(3,2,1) +plt.imshow(img1[0][:,:,::-1]) +plt.axis('off') + +ax2 = fig.add_subplot(3,2,2) +im = plt.imshow(img1_graph, interpolation='nearest', cmap=plt.cm.ocean) +plt.colorbar() + +ax3 = fig.add_subplot(3,2,3) +plt.imshow(img2[0][:,:,::-1]) +plt.axis('off') + +ax4 = fig.add_subplot(3,2,4) +im = plt.imshow(img2_graph, interpolation='nearest', cmap=plt.cm.ocean) +plt.colorbar() + +ax5 = fig.add_subplot(3,2,5) +plt.text(0.35, 0, "Distance: %s" % (distance)) +plt.axis('off') + +ax6 = fig.add_subplot(3,2,6) +im = plt.imshow(distance_graph, interpolation='nearest', cmap=plt.cm.ocean) +plt.colorbar() + +plt.show() + +#---------------------------------------------- \ No newline at end of file