aditya-s-yadav commited on
Commit
47ad201
·
verified ·
1 Parent(s): 7bd9e03

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
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 # 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
@@ -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
- # Load and transform the images
32
  img1 = Image.open(image1).convert("RGB")
33
  img2 = Image.open(image2).convert("RGB")
34
 
35
- # Display images
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
- # Transform 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
- # Predict similarity using the Siamese model
47
  output1, output2 = model(img1, img2)
48
  euclidean_distance = torch.nn.functional.pairwise_distance(output1, output2)
49
 
50
- # Set a threshold for similarity (can be tuned based on model performance)
51
- threshold = 0.5 # You can adjust this threshold based on your model's performance
52
 
53
- # Display similarity 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**.")
 
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**.")