I’m working on Face Recognition, for this I’m using Github Repository. I’ve downloaded pre-trained model from there, which is 20180402-114759-vggface2.pt
. I’ve used this in python and it is working fine with great accuracy.
python.py:
from facenet_pytorch import MTCNN, InceptionResnetV1
from PIL import Image
import torch
mtcnn = MTCNN(image_size=160, margin=0)
resnet = InceptionResnetV1(pretrained='vggface2').eval()
resnet.load_state_dict(torch.load('../20180402-114759-vggface2.pt'), strict=False)
img1 = Image.open('../img1')
img2 = Image.open('../img2')
img1_cropped = mtcnn(img1)
img2_cropped = mtcnn(img2)
if img1_cropped is not None and img2_cropped is not None:
img1_embedding = resnet(img1_cropped.unsqueeze(0))
img2_embedding = resnet(img2_cropped.unsqueeze(0))
cos = torch.nn.CosineSimilarity(dim=1, eps=1e-6)
similarity = cos(img1_embedding, img2_embedding)
print(f"Cosine Similarity: {similarity.item()}")
threshold = 0.6
if similarity > threshold:
print("The faces are similar!")
else:
print("The faces are different!")
else:
print("Face not detected in one or both images.")
Now I want to use it in Scala (JVM Environment)
. I’ve searched a lot, and found that we can use .pt
model in scala using DJL (Deep Java Library)
but I’m unable to do so. Guide me is it possible to use .pt
model in scala? If yes then how can we do this?