|
import numpy as np |
|
import gradio as gr |
|
import cv2 |
|
|
|
def calculate_similarity(img1, img2): |
|
if len(img1.shape) == 2: |
|
img1 = cv2.cvtColor(img1, cv2.COLOR_GRAY2RGB) |
|
if len(img2.shape) == 2: |
|
img2 = cv2.cvtColor(img2, cv2.COLOR_GRAY2RGB) |
|
img1_resized = cv2.resize(img1, (img2.shape[1], img2.shape[0])) |
|
diff = cv2.absdiff(img1_resized, img2) |
|
similarity_score = np.mean(diff) |
|
return similarity_score |
|
|
|
def image_similarity(img1, img2): |
|
img1 = img1.astype(np.uint8) |
|
img2 = img2.astype(np.uint8) |
|
similarity_score = calculate_similarity(img1, img2) |
|
result = f"Similarity Score: {similarity_score:.4f}" |
|
return result |
|
|
|
iface = gr.Interface( |
|
fn=image_similarity, |
|
inputs=[ |
|
gr.inputs.Image(type="numpy", label="Image 1"), |
|
gr.inputs.Image(type="numpy", label="Image 2") |
|
], |
|
outputs="text", |
|
title="Image Similarity Calculator", |
|
description="Upload two images to compute their similarity." |
|
) |
|
|
|
iface.launch(share=True) |
|
|