Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,41 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
7 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from skimage.metrics import structural_similarity as ssim
|
5 |
|
6 |
+
def compare_images(image1, image2):
|
7 |
+
# Convert images to grayscale
|
8 |
+
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
|
9 |
+
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
|
10 |
+
|
11 |
+
# Compute SSIM between the two images
|
12 |
+
score, diff = ssim(gray1, gray2, full=True)
|
13 |
+
diff = (diff * 255).astype("uint8")
|
14 |
+
|
15 |
+
# Threshold the difference image to get regions with major changes
|
16 |
+
_, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
|
17 |
+
|
18 |
+
# Find contours of differences
|
19 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
20 |
+
|
21 |
+
# Create a mask to isolate only the significant added object
|
22 |
+
mask = np.zeros_like(image1)
|
23 |
+
cv2.drawContours(mask, contours, -1, (255, 255, 255), thickness=cv2.FILLED)
|
24 |
+
|
25 |
+
# Apply the mask to highlight the object added in the second image
|
26 |
+
highlighted = cv2.bitwise_and(image2, mask)
|
27 |
+
|
28 |
+
return highlighted
|
29 |
+
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=compare_images,
|
32 |
+
inputs=[
|
33 |
+
gr.Image(type="numpy", label="Image Without Object"),
|
34 |
+
gr.Image(type="numpy", label="Image With Object")
|
35 |
+
],
|
36 |
+
outputs=gr.Image(type="numpy", label="Highlighted Differences"),
|
37 |
+
title="Object Difference Highlighter",
|
38 |
+
description="Upload two images: one without an object and one with an object. The app will highlight only the newly added object."
|
39 |
+
)
|
40 |
|
|
|
41 |
demo.launch()
|