Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,6 @@ def calculate_similarity(img1, img2):
|
|
11 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
12 |
return ssim(img1, img2, win_size=3)
|
13 |
|
14 |
-
|
15 |
# Function to compute similarity scores for all images
|
16 |
def compute_similarity(target_image, image_list):
|
17 |
scores = []
|
@@ -22,47 +21,25 @@ def compute_similarity(target_image, image_list):
|
|
22 |
scores.append(similarity_score)
|
23 |
return scores
|
24 |
|
25 |
-
|
26 |
-
# Function to handle the Gradio interface
|
27 |
# Function to handle the Gradio interface
|
28 |
-
def image_similarity(target_image,
|
29 |
target_image = target_image.astype(np.uint8)
|
30 |
-
|
31 |
-
scores = compute_similarity(target_image,
|
32 |
-
|
33 |
-
|
34 |
-
formatted_image = format_image(image)
|
35 |
-
result = f"Image: {formatted_image}\nScore: {score:.4f}\n"
|
36 |
-
results.append(result)
|
37 |
-
return "".join(results)
|
38 |
-
|
39 |
-
# Function to format the image representation as a string
|
40 |
-
def format_image(image):
|
41 |
-
formatted_image = ""
|
42 |
-
for row in image:
|
43 |
-
formatted_row = ", ".join([f"[{', '.join(map(str, pixel))}]" for pixel in row])
|
44 |
-
formatted_image += f"[{formatted_row}],\n"
|
45 |
-
return formatted_image[:-2]
|
46 |
|
47 |
# Prepare Gradio interface
|
48 |
iface = gr.Interface(
|
49 |
fn=image_similarity,
|
50 |
inputs=[
|
51 |
gr.inputs.Image(type="numpy", label="Target Image"),
|
52 |
-
gr.inputs.Image(type="numpy", label="Image
|
53 |
],
|
54 |
outputs="text",
|
55 |
title="Image Similarity Calculator",
|
56 |
-
description="Upload a target image and
|
57 |
)
|
58 |
|
59 |
# Launch the interface
|
60 |
iface.launch()
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
11 |
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB)
|
12 |
return ssim(img1, img2, win_size=3)
|
13 |
|
|
|
14 |
# Function to compute similarity scores for all images
|
15 |
def compute_similarity(target_image, image_list):
|
16 |
scores = []
|
|
|
21 |
scores.append(similarity_score)
|
22 |
return scores
|
23 |
|
|
|
|
|
24 |
# Function to handle the Gradio interface
|
25 |
+
def image_similarity(target_image, image):
|
26 |
target_image = target_image.astype(np.uint8)
|
27 |
+
image = image.astype(np.uint8)
|
28 |
+
scores = compute_similarity(target_image, [image])
|
29 |
+
result = f"Image: {image.tolist()}\nScore: {scores[0]:.4f}"
|
30 |
+
return [result]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
# Prepare Gradio interface
|
33 |
iface = gr.Interface(
|
34 |
fn=image_similarity,
|
35 |
inputs=[
|
36 |
gr.inputs.Image(type="numpy", label="Target Image"),
|
37 |
+
gr.inputs.Image(type="numpy", label="Image")
|
38 |
],
|
39 |
outputs="text",
|
40 |
title="Image Similarity Calculator",
|
41 |
+
description="Upload a target image and another image. Get the similarity score."
|
42 |
)
|
43 |
|
44 |
# Launch the interface
|
45 |
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|