Testing face recognition using dlib in VS Code. In this code, Treating the faces as the same if the Euclidean distance is less than 0.6,
I've written the following code to get the Cosine Similarity here, and it gives me a Cosine Similarity of more than 1.
I think it's wrong, so how can I get the correct Cosine Similarity?
fig, ax = plt.subplots(1, figsize=(20, 20))
ax.imshow(img_rgb)
for i, desc in enumerate(descriptors):
found = False
for name, saved_desc in descs.items():
dist = np.linalg.norm([desc] - saved_desc, axis=1)
# my code : compute cosine similarity
cosine = np.dot([desc],saved_desc)/(norm([desc]),norm(saved_desc))
if dist < 0.6:
found = True
text = ax.text(rects[i][0][0], rects[i][0][1], name,
color='b', fontsize=40, fontweight='bold')
text2 = ax.text(rects[i][1][0], rects[i][1][1], cosine,
color='b', fontsize=20, fontweight='bold')
text.set_path_effects([path_effects.Stroke(linewidth=10, foreground='white'), path_effects.Normal()])
rect = patches.Rectangle(rects[i][0],
rects[i][1][1] - rects[i][0][1],
rects[i][1][0] - rects[i][0][0],
linewidth=2, edgecolor='w', facecolor='none')
ax.add_patch(rect)
break
neo
[1.35159114 1.36754706]
neo
[1.15541134 1.10536481]
trinity
[1.13942505 1.03432384]
morpheus
[1.26643039 1.28631887]
neo
[1.28240694 1.31706417]
trinity
[1.4723583 1.43481646]
Cosine Similarity less than 1
The denominator of the cosine similarity is incorrect. You should replace (norm([desc]),norm(saved_desc))
with (norm([desc])*norm(saved_desc))
.