Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,23 @@
|
|
1 |
import numpy as np
|
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):
|
8 |
if len(img1.shape) == 2:
|
9 |
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
|
10 |
if len(img2.shape) == 2:
|
11 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
12 |
-
|
|
|
|
|
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 |
-
if img1.shape != img2.shape:
|
19 |
-
max_height = max(img1.shape[0], img2.shape[0])
|
20 |
-
max_width = max(img1.shape[1], img2.shape[1])
|
21 |
-
img1 = cv2.resize(img1, (max_width, max_height))
|
22 |
-
img2 = cv2.resize(img2, (max_width, max_height))
|
23 |
similarity_score = calculate_similarity(img1, img2)
|
24 |
result = f"Similarity Score: {similarity_score:.4f}"
|
25 |
return result
|
26 |
|
27 |
-
# Prepare Gradio interface
|
28 |
iface = gr.Interface(
|
29 |
fn=image_similarity,
|
30 |
inputs=[
|
@@ -36,5 +29,4 @@ iface = gr.Interface(
|
|
36 |
description="Upload two images to compute their similarity."
|
37 |
)
|
38 |
|
39 |
-
# Launch the interface
|
40 |
iface.launch()
|
|
|
1 |
import numpy as np
|
|
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
|
|
|
5 |
def calculate_similarity(img1, img2):
|
6 |
if len(img1.shape) == 2:
|
7 |
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB)
|
8 |
if len(img2.shape) == 2:
|
9 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
10 |
+
diff = cv2.absdiff(img1, img2)
|
11 |
+
similarity_score = np.mean(diff)
|
12 |
+
return similarity_score
|
13 |
|
|
|
14 |
def image_similarity(img1, img2):
|
15 |
img1 = img1.astype(np.uint8)
|
16 |
img2 = img2.astype(np.uint8)
|
|
|
|
|
|
|
|
|
|
|
17 |
similarity_score = calculate_similarity(img1, img2)
|
18 |
result = f"Similarity Score: {similarity_score:.4f}"
|
19 |
return result
|
20 |
|
|
|
21 |
iface = gr.Interface(
|
22 |
fn=image_similarity,
|
23 |
inputs=[
|
|
|
29 |
description="Upload two images to compute their similarity."
|
30 |
)
|
31 |
|
|
|
32 |
iface.launch()
|