carlosriverat's picture
Update app.py
d6888a9 verified
raw
history blame
1.49 kB
import gradio as gr
import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
def compare_images(image1, image2):
# Convert images to grayscale
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# Compute SSIM between the two images
score, diff = ssim(gray1, gray2, full=True)
diff = (diff * 255).astype("uint8")
# Threshold the difference image to get regions with major changes
_, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# Find contours of differences
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a mask to isolate only the significant added object
mask = np.zeros_like(image1)
cv2.drawContours(mask, contours, -1, (255, 255, 255), thickness=cv2.FILLED)
# Apply the mask to highlight the object added in the second image
highlighted = cv2.bitwise_and(image2, mask)
return highlighted
demo = gr.Interface(
fn=compare_images,
inputs=[
gr.Image(type="numpy", label="Image Without Object"),
gr.Image(type="numpy", label="Image With Object")
],
outputs=gr.Image(type="numpy", label="Highlighted Differences"),
title="Object Difference Highlighter",
description="Upload two images: one without an object and one with an object. The app will highlight only the newly added object."
)
demo.launch()