Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,21 @@ import streamlit as st
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import torchvision.transforms as transforms
|
5 |
-
from model import SiameseNetwork
|
|
|
6 |
|
7 |
-
# Define the device (GPU or CPU)
|
8 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
|
10 |
-
|
11 |
model = SiameseNetwork().to(device)
|
12 |
model.load_state_dict(torch.load("siamese_model.pth", map_location=device))
|
13 |
model.eval()
|
14 |
|
15 |
-
|
16 |
transform = transforms.Compose([
|
17 |
-
transforms.Resize((100, 100)),
|
18 |
-
transforms.Grayscale(num_output_channels=1),
|
19 |
-
transforms.ToTensor(), #
|
20 |
])
|
21 |
|
22 |
# Streamlit interface
|
@@ -28,29 +28,29 @@ image1 = st.file_uploader("Upload First Signature Image", type=["png", "jpg", "j
|
|
28 |
image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"])
|
29 |
|
30 |
if image1 and image2:
|
31 |
-
|
32 |
img1 = Image.open(image1).convert("RGB")
|
33 |
img2 = Image.open(image2).convert("RGB")
|
34 |
|
35 |
-
|
36 |
col1, col2 = st.columns(2)
|
37 |
with col1:
|
38 |
st.image(img1, caption='First Signature Image', use_container_width=True)
|
39 |
with col2:
|
40 |
st.image(img2, caption='Second Signature Image', use_container_width=True)
|
41 |
|
42 |
-
#
|
43 |
img1 = transform(img1).unsqueeze(0).to(device)
|
44 |
img2 = transform(img2).unsqueeze(0).to(device)
|
45 |
|
46 |
-
#
|
47 |
output1, output2 = model(img1, img2)
|
48 |
euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
|
49 |
|
50 |
-
#
|
51 |
-
threshold = 0.5
|
52 |
|
53 |
-
# Display
|
54 |
st.success(f'Similarity Score (Euclidean Distance): {euclidean_distance.item():.4f}')
|
55 |
if euclidean_distance.item() < threshold:
|
56 |
st.write("The signatures are likely from the **same person**.")
|
|
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
import torchvision.transforms as transforms
|
5 |
+
from model import SiameseNetwork
|
6 |
+
|
7 |
|
|
|
8 |
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
|
10 |
+
|
11 |
model = SiameseNetwork().to(device)
|
12 |
model.load_state_dict(torch.load("siamese_model.pth", map_location=device))
|
13 |
model.eval()
|
14 |
|
15 |
+
|
16 |
transform = transforms.Compose([
|
17 |
+
transforms.Resize((100, 100)),
|
18 |
+
transforms.Grayscale(num_output_channels=1),
|
19 |
+
transforms.ToTensor(), # Converting image to tensor
|
20 |
])
|
21 |
|
22 |
# Streamlit interface
|
|
|
28 |
image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"])
|
29 |
|
30 |
if image1 and image2:
|
31 |
+
|
32 |
img1 = Image.open(image1).convert("RGB")
|
33 |
img2 = Image.open(image2).convert("RGB")
|
34 |
|
35 |
+
## Displaying input image
|
36 |
col1, col2 = st.columns(2)
|
37 |
with col1:
|
38 |
st.image(img1, caption='First Signature Image', use_container_width=True)
|
39 |
with col2:
|
40 |
st.image(img2, caption='Second Signature Image', use_container_width=True)
|
41 |
|
42 |
+
# Transforming the images before feeding them into the model
|
43 |
img1 = transform(img1).unsqueeze(0).to(device)
|
44 |
img2 = transform(img2).unsqueeze(0).to(device)
|
45 |
|
46 |
+
# Predicting similarity using the Siamese model
|
47 |
output1, output2 = model(img1, img2)
|
48 |
euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
|
49 |
|
50 |
+
# Setting a threshold for similarity
|
51 |
+
threshold = 0.5
|
52 |
|
53 |
+
# Display similaritying score and interpretation
|
54 |
st.success(f'Similarity Score (Euclidean Distance): {euclidean_distance.item():.4f}')
|
55 |
if euclidean_distance.item() < threshold:
|
56 |
st.write("The signatures are likely from the **same person**.")
|