Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import torch | |
| from PIL import Image | |
| import torchvision.transforms as transforms | |
| from model import SiameseNetwork # Ensure this file exists with the model definition | |
| # Define the device (GPU or CPU) | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| # Load the pre-trained Siamese model | |
| model = SiameseNetwork().to(device) | |
| model.load_state_dict(torch.load("siamese_model.pth", map_location=device)) | |
| model.eval() | |
| # Define data transformation (resize, convert to tensor, normalize if needed) | |
| transform = transforms.Compose([ | |
| transforms.Resize((100, 100)), # Resize to match the input size of the model | |
| transforms.Grayscale(num_output_channels=1), # Convert images to grayscale for signature comparison | |
| transforms.ToTensor(), # Convert image to tensor | |
| ]) | |
| # Streamlit interface | |
| st.title("Signature Forgery Detection with Siamese Network") | |
| st.write("Upload two signature images to check if they are from the same person or if one is forged.") | |
| # Upload images | |
| image1 = st.file_uploader("Upload First Signature Image", type=["png", "jpg", "jpeg"]) | |
| image2 = st.file_uploader("Upload Second Signature Image", type=["png", "jpg", "jpeg"]) | |
| if image1 and image2: | |
| # Load and transform the images | |
| img1 = Image.open(image1).convert("RGB") | |
| img2 = Image.open(image2).convert("RGB") | |
| # Transform the images before feeding them into the model | |
| img1 = transform(img1).unsqueeze(0).to(device) | |
| img2 = transform(img2).unsqueeze(0).to(device) | |
| # Predict similarity using the Siamese model | |
| output1, output2 = model(img1, img2) | |
| euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2) | |
| # Set a threshold for similarity (can be tuned based on model performance) | |
| threshold = 0.5 # You can adjust this threshold based on your model's performance | |
| # Display both images and results side by side | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.image(img1.squeeze(0).cpu().permute(1, 2, 0), caption='First Signature Image', use_container_width=True) | |
| with col2: | |
| st.image(img2.squeeze(0).cpu().permute(1, 2, 0), caption='Second Signature Image', use_container_width=True) | |
| # Display similarity score and interpretation side by side | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| st.success(f'Similarity Score (Euclidean Distance): {euclidean_distance.item():.4f}') | |
| with col2: | |
| if euclidean_distance.item() < threshold: | |
| st.write("The signatures are likely from the **same person**.") | |
| else: | |
| st.write("The signatures **do not match**, one might be **forged**.") | |