Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import numpy as np
|
|
2 |
from skimage.metrics import structural_similarity as ssim
|
3 |
import gradio as gr
|
4 |
import cv2
|
5 |
-
import os
|
6 |
|
7 |
# Function to calculate SSIM between two images
|
8 |
def calculate_similarity(img1, img2):
|
@@ -12,38 +11,24 @@ def calculate_similarity(img1, img2):
|
|
12 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
13 |
return ssim(img1, img2, win_size=3)
|
14 |
|
15 |
-
# Function to compute similarity scores for all images
|
16 |
-
def compute_similarity(target_image, image_list):
|
17 |
-
scores = []
|
18 |
-
target_image_resized = cv2.resize(target_image, (target_image.shape[1], target_image.shape[0]))
|
19 |
-
for image_path in image_list:
|
20 |
-
image = cv2.imread(image_path)
|
21 |
-
image_resized = cv2.resize(image, (target_image.shape[1], target_image.shape[0]))
|
22 |
-
similarity_score = calculate_similarity(target_image_resized, image_resized)
|
23 |
-
scores.append(similarity_score)
|
24 |
-
return scores
|
25 |
-
|
26 |
# Function to handle the Gradio interface
|
27 |
-
def image_similarity(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
result = f"Image: {image_path}\nScore: {score:.4f}\n"
|
34 |
-
results.append(result)
|
35 |
-
return "".join(results)
|
36 |
|
37 |
# Prepare Gradio interface
|
38 |
iface = gr.Interface(
|
39 |
fn=image_similarity,
|
40 |
inputs=[
|
41 |
-
gr.inputs.Image(type="numpy", label="
|
42 |
-
gr.inputs.
|
43 |
],
|
44 |
outputs="text",
|
45 |
title="Image Similarity Calculator",
|
46 |
-
description="Upload
|
47 |
)
|
48 |
|
49 |
# Launch the interface
|
|
|
2 |
from skimage.metrics import structural_similarity as ssim
|
3 |
import gradio as gr
|
4 |
import cv2
|
|
|
5 |
|
6 |
# Function to calculate SSIM between two images
|
7 |
def calculate_similarity(img1, img2):
|
|
|
11 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
12 |
return ssim(img1, img2, win_size=3)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Function to handle the Gradio interface
|
15 |
+
def image_similarity(img1, img2):
|
16 |
+
img1 = img1.astype(np.uint8)
|
17 |
+
img2 = img2.astype(np.uint8)
|
18 |
+
similarity_score = calculate_similarity(img1, img2)
|
19 |
+
result = f"Similarity Score: {similarity_score:.4f}"
|
20 |
+
return result
|
|
|
|
|
|
|
21 |
|
22 |
# Prepare Gradio interface
|
23 |
iface = gr.Interface(
|
24 |
fn=image_similarity,
|
25 |
inputs=[
|
26 |
+
gr.inputs.Image(type="numpy", label="Image 1"),
|
27 |
+
gr.inputs.Image(type="numpy", label="Image 2")
|
28 |
],
|
29 |
outputs="text",
|
30 |
title="Image Similarity Calculator",
|
31 |
+
description="Upload two images to compute their similarity."
|
32 |
)
|
33 |
|
34 |
# Launch the interface
|