File size: 1,639 Bytes
690ada3 5ff8278 690ada3 42b03f9 690ada3 befd8b5 3a834c5 e20a8b4 befd8b5 dc979d2 befd8b5 90d7188 27b7cb5 690ada3 90d7188 690ada3 90d7188 690ada3 77a3aec |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import numpy as np
from skimage.metrics import structural_similarity as ssim
import gradio as gr
import cv2
# Function to calculate SSIM between two images
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)
max_height = max(img1.shape[0], img2.shape[0])
max_width = max(img1.shape[1], img2.shape[1])
img1_resized = cv2.resize(img1, (max_width, max_height))
img2_resized = cv2.resize(img2, (max_width, max_height))
# Calculate similarity score based on SSIM
ssim_score = ssim(img1_resized, img2_resized, win_size=3, multichannel=True)
# Additional score calculation after text removal (modify this as per your requirement)
text_removed_score = 0.75 # Placeholder value, replace with your own calculation
# Combine the scores or choose the desired score
combined_score = (ssim_score + text_removed_score) / 2
return combined_score
# Rest of the code remains the same
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()
|