Spaces:
Sleeping
Sleeping
leninqwerty03@gmail.com commited on
Commit ·
212b9db
1
Parent(s): 372271d
Adding MVP code
Browse files- app.py +68 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from skimage.metrics import structural_similarity as ssim # works after installing scikit-image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# ---------------------------
|
| 7 |
+
# Function to compare images
|
| 8 |
+
# ---------------------------
|
| 9 |
+
def compare_images(img1, img2):
|
| 10 |
+
# Convert to grayscale
|
| 11 |
+
grayA = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
| 12 |
+
grayB = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
| 13 |
+
|
| 14 |
+
# Resize to match (if slightly different sizes)
|
| 15 |
+
if grayA.shape != grayB.shape:
|
| 16 |
+
h = min(grayA.shape[0], grayB.shape[0])
|
| 17 |
+
w = min(grayA.shape[1], grayB.shape[1])
|
| 18 |
+
grayA = cv2.resize(grayA, (w, h))
|
| 19 |
+
grayB = cv2.resize(grayB, (w, h))
|
| 20 |
+
|
| 21 |
+
# Compute SSIM score + diff map
|
| 22 |
+
score, diff = ssim(grayA, grayB, full=True)
|
| 23 |
+
diff = (diff * 255).astype("uint8")
|
| 24 |
+
|
| 25 |
+
# Threshold the diff
|
| 26 |
+
thresh = cv2.threshold(diff, 0, 255,
|
| 27 |
+
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
|
| 28 |
+
|
| 29 |
+
# Find contours of differences
|
| 30 |
+
cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
|
| 31 |
+
cv2.CHAIN_APPROX_SIMPLE)
|
| 32 |
+
|
| 33 |
+
# Draw bounding boxes on differences
|
| 34 |
+
img_diff = img2.copy()
|
| 35 |
+
for c in cnts:
|
| 36 |
+
(x, y, w, h) = cv2.boundingRect(c)
|
| 37 |
+
cv2.rectangle(img_diff, (x, y), (x + w, y + h), (0, 0, 255), 2)
|
| 38 |
+
|
| 39 |
+
summary = f"Similarity Score: {score:.4f}. Changes detected: {len(cnts)}"
|
| 40 |
+
|
| 41 |
+
return img1, img2, img_diff, summary
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# ---------------------------
|
| 45 |
+
# Gradio UI
|
| 46 |
+
# ---------------------------
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
gr.Markdown("## 🖼️ Visual Regression\nUpload two screenshots to detect differences.")
|
| 49 |
+
|
| 50 |
+
with gr.Row():
|
| 51 |
+
img1 = gr.Image(type="numpy", label="Baseline Screenshot")
|
| 52 |
+
img2 = gr.Image(type="numpy", label="New Screenshot")
|
| 53 |
+
|
| 54 |
+
btn = gr.Button("Compare")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
out1 = gr.Image(label="Baseline")
|
| 58 |
+
out2 = gr.Image(label="New")
|
| 59 |
+
out3 = gr.Image(label="Differences Highlighted")
|
| 60 |
+
|
| 61 |
+
summary = gr.Label(label="Summary")
|
| 62 |
+
|
| 63 |
+
btn.click(fn=compare_images, inputs=[img1, img2],
|
| 64 |
+
outputs=[out1, out2, out3, summary])
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python-headless
|
| 2 |
+
scikit-image
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|