Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,61 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import torchvision.transforms as transforms
|
| 5 |
-
from model import SiameseNetwork # Ensure this file exists with the model definition
|
| 6 |
-
|
| 7 |
-
# Define the device (GPU or CPU)
|
| 8 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
-
|
| 10 |
-
# Load the pre-trained Siamese model
|
| 11 |
-
model = SiameseNetwork().to(device)
|
| 12 |
-
model.load_state_dict(torch.load("siamese_model.pth", map_location=device))
|
| 13 |
-
model.eval()
|
| 14 |
-
|
| 15 |
-
# Define data transformation (resize, convert to tensor, normalize if needed)
|
| 16 |
-
transform = transforms.Compose([
|
| 17 |
-
transforms.Resize((100, 100)), # Resize to match the input size of the model
|
| 18 |
-
transforms.Grayscale(num_output_channels=1), # Convert images to grayscale for signature comparison
|
| 19 |
-
transforms.ToTensor(), # Convert image to tensor
|
| 20 |
-
])
|
| 21 |
-
|
| 22 |
-
# Streamlit interface
|
| 23 |
-
st.title("Signature Forgery Detection with Siamese Network")
|
| 24 |
-
st.write("Upload two signature images to check if they are from the same person or if one is forged.")
|
| 25 |
-
|
| 26 |
-
# Upload images
|
| 27 |
-
image1 = st.file_uploader("Upload First Signature Image", type=["png", "jpg", "jpeg"])
|
| 28 |
-
image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"])
|
| 29 |
-
|
| 30 |
-
if image1 and image2:
|
| 31 |
-
# Load and transform the images
|
| 32 |
-
img1 = Image.open(image1).convert("RGB")
|
| 33 |
-
img2 = Image.open(image2).convert("RGB")
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Display similarity score and interpretation
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
st.
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torchvision.transforms as transforms
|
| 5 |
+
from model import SiameseNetwork # Ensure this file exists with the model definition
|
| 6 |
+
|
| 7 |
+
# Define the device (GPU or CPU)
|
| 8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained Siamese model
|
| 11 |
+
model = SiameseNetwork().to(device)
|
| 12 |
+
model.load_state_dict(torch.load("siamese_model.pth", map_location=device))
|
| 13 |
+
model.eval()
|
| 14 |
+
|
| 15 |
+
# Define data transformation (resize, convert to tensor, normalize if needed)
|
| 16 |
+
transform = transforms.Compose([
|
| 17 |
+
transforms.Resize((100, 100)), # Resize to match the input size of the model
|
| 18 |
+
transforms.Grayscale(num_output_channels=1), # Convert images to grayscale for signature comparison
|
| 19 |
+
transforms.ToTensor(), # Convert image to tensor
|
| 20 |
+
])
|
| 21 |
+
|
| 22 |
+
# Streamlit interface
|
| 23 |
+
st.title("Signature Forgery Detection with Siamese Network")
|
| 24 |
+
st.write("Upload two signature images to check if they are from the same person or if one is forged.")
|
| 25 |
+
|
| 26 |
+
# Upload images
|
| 27 |
+
image1 = st.file_uploader("Upload First Signature Image", type=["png", "jpg", "jpeg"])
|
| 28 |
+
image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"])
|
| 29 |
+
|
| 30 |
+
if image1 and image2:
|
| 31 |
+
# Load and transform the images
|
| 32 |
+
img1 = Image.open(image1).convert("RGB")
|
| 33 |
+
img2 = Image.open(image2).convert("RGB")
|
| 34 |
+
|
| 35 |
+
# Transform the images before feeding them into the model
|
| 36 |
+
img1 = transform(img1).unsqueeze(0).to(device)
|
| 37 |
+
img2 = transform(img2).unsqueeze(0).to(device)
|
| 38 |
+
|
| 39 |
+
# Predict similarity using the Siamese model
|
| 40 |
+
output1, output2 = model(img1, img2)
|
| 41 |
+
euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
|
| 42 |
+
|
| 43 |
+
# Set a threshold for similarity (can be tuned based on model performance)
|
| 44 |
+
threshold = 0.5 # You can adjust this threshold based on your model's performance
|
| 45 |
+
|
| 46 |
+
# Display both images and results side by side
|
| 47 |
+
col1, col2 = st.columns(2)
|
| 48 |
+
with col1:
|
| 49 |
+
st.image(img1.squeeze(0).cpu().permute(1, 2, 0), caption='First Signature Image', use_container_width=True)
|
| 50 |
+
with col2:
|
| 51 |
+
st.image(img2.squeeze(0).cpu().permute(1, 2, 0), caption='Second Signature Image', use_container_width=True)
|
| 52 |
+
|
| 53 |
+
# Display similarity score and interpretation side by side
|
| 54 |
+
col1, col2 = st.columns(2)
|
| 55 |
+
with col1:
|
| 56 |
+
st.success(f'Similarity Score (Euclidean Distance): {euclidean_distance.item():.4f}')
|
| 57 |
+
with col2:
|
| 58 |
+
if euclidean_distance.item() < threshold:
|
| 59 |
+
st.write("The signatures are likely from the **same person**.")
|
| 60 |
+
else:
|
| 61 |
+
st.write("The signatures **do not match**, one might be **forged**.")
|