Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,12 @@ import cv2
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
def compare_images(
|
| 6 |
-
#
|
| 7 |
-
original_image = cv2.imread(original_image_path)
|
| 8 |
-
adjusted_image = cv2.imread(adjusted_image_path)
|
| 9 |
-
|
| 10 |
-
# Check if images loaded correctly
|
| 11 |
if original_image is None or adjusted_image is None:
|
| 12 |
-
return "Error: One or both images could not be loaded."
|
| 13 |
|
| 14 |
-
# Resize
|
| 15 |
original_image_resized = cv2.resize(original_image, (adjusted_image.shape[1], adjusted_image.shape[0]))
|
| 16 |
|
| 17 |
# Step 1: Compare Dimensions
|
|
@@ -23,23 +19,19 @@ def compare_images(original_image_path, adjusted_image_path):
|
|
| 23 |
# Convert images to grayscale
|
| 24 |
original_gray = cv2.cvtColor(original_image_resized, cv2.COLOR_BGR2GRAY)
|
| 25 |
adjusted_gray = cv2.cvtColor(adjusted_image, cv2.COLOR_BGR2GRAY)
|
| 26 |
-
|
| 27 |
# Calculate average brightness
|
| 28 |
original_brightness = np.mean(original_gray)
|
| 29 |
adjusted_brightness = np.mean(adjusted_gray)
|
| 30 |
brightness_diff = abs(original_brightness - adjusted_brightness)
|
| 31 |
-
brightness_match = brightness_diff < 10 #
|
| 32 |
|
| 33 |
# Step 3: Highlight Differences
|
| 34 |
# Subtract images to find differences
|
| 35 |
difference = cv2.absdiff(original_gray, adjusted_gray)
|
| 36 |
_, threshold_diff = cv2.threshold(difference, 30, 255, cv2.THRESH_BINARY)
|
| 37 |
-
|
| 38 |
-
# Save the difference image
|
| 39 |
-
difference_image_path = "difference_image.jpg"
|
| 40 |
-
cv2.imwrite(difference_image_path, threshold_diff)
|
| 41 |
|
| 42 |
-
# Prepare
|
| 43 |
comparison_result = {
|
| 44 |
"Dimension Match": "Yes" if dimension_match else "No",
|
| 45 |
"Original Dimensions": f"{original_width}x{original_height}",
|
|
@@ -48,40 +40,24 @@ def compare_images(original_image_path, adjusted_image_path):
|
|
| 48 |
"Original Brightness": f"{original_brightness:.2f}",
|
| 49 |
"Adjusted Brightness": f"{adjusted_brightness:.2f}",
|
| 50 |
"Brightness Difference": f"{brightness_diff:.2f}",
|
| 51 |
-
"Difference Image": difference_image_path
|
| 52 |
}
|
| 53 |
|
| 54 |
return comparison_result, threshold_diff
|
| 55 |
|
| 56 |
# Gradio interface
|
| 57 |
-
def gradio_compare_images(original_image, adjusted_image):
|
| 58 |
-
# Save images temporarily
|
| 59 |
-
original_image_path = "original_image.jpg"
|
| 60 |
-
adjusted_image_path = "adjusted_image.jpg"
|
| 61 |
-
|
| 62 |
-
cv2.imwrite(original_image_path, original_image)
|
| 63 |
-
cv2.imwrite(adjusted_image_path, adjusted_image)
|
| 64 |
-
|
| 65 |
-
# Compare images
|
| 66 |
-
result, diff_image = compare_images(original_image_path, adjusted_image_path)
|
| 67 |
-
|
| 68 |
-
return result, diff_image
|
| 69 |
-
|
| 70 |
-
# Create Gradio interface
|
| 71 |
iface = gr.Interface(
|
| 72 |
-
fn=
|
| 73 |
inputs=[
|
| 74 |
-
gr.
|
| 75 |
-
gr.
|
| 76 |
],
|
| 77 |
outputs=[
|
| 78 |
-
gr.
|
| 79 |
-
gr.
|
| 80 |
],
|
| 81 |
title="Image Comparison Tool",
|
| 82 |
description="Upload two images to compare dimensions, brightness, and highlight differences."
|
| 83 |
)
|
| 84 |
|
| 85 |
-
# Launch the Gradio app
|
| 86 |
iface.launch()
|
| 87 |
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
def compare_images(original_image, adjusted_image):
|
| 6 |
+
# Check if images are loaded correctly
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if original_image is None or adjusted_image is None:
|
| 8 |
+
return "Error: One or both images could not be loaded.", None
|
| 9 |
|
| 10 |
+
# Resize adjusted image to match original image dimensions
|
| 11 |
original_image_resized = cv2.resize(original_image, (adjusted_image.shape[1], adjusted_image.shape[0]))
|
| 12 |
|
| 13 |
# Step 1: Compare Dimensions
|
|
|
|
| 19 |
# Convert images to grayscale
|
| 20 |
original_gray = cv2.cvtColor(original_image_resized, cv2.COLOR_BGR2GRAY)
|
| 21 |
adjusted_gray = cv2.cvtColor(adjusted_image, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
|
| 23 |
# Calculate average brightness
|
| 24 |
original_brightness = np.mean(original_gray)
|
| 25 |
adjusted_brightness = np.mean(adjusted_gray)
|
| 26 |
brightness_diff = abs(original_brightness - adjusted_brightness)
|
| 27 |
+
brightness_match = brightness_diff < 10 # Allow small tolerance
|
| 28 |
|
| 29 |
# Step 3: Highlight Differences
|
| 30 |
# Subtract images to find differences
|
| 31 |
difference = cv2.absdiff(original_gray, adjusted_gray)
|
| 32 |
_, threshold_diff = cv2.threshold(difference, 30, 255, cv2.THRESH_BINARY)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Prepare result summary
|
| 35 |
comparison_result = {
|
| 36 |
"Dimension Match": "Yes" if dimension_match else "No",
|
| 37 |
"Original Dimensions": f"{original_width}x{original_height}",
|
|
|
|
| 40 |
"Original Brightness": f"{original_brightness:.2f}",
|
| 41 |
"Adjusted Brightness": f"{adjusted_brightness:.2f}",
|
| 42 |
"Brightness Difference": f"{brightness_diff:.2f}",
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
return comparison_result, threshold_diff
|
| 46 |
|
| 47 |
# Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
iface = gr.Interface(
|
| 49 |
+
fn=compare_images,
|
| 50 |
inputs=[
|
| 51 |
+
gr.Image(type="numpy", label="Original Image"),
|
| 52 |
+
gr.Image(type="numpy", label="Adjusted Image")
|
| 53 |
],
|
| 54 |
outputs=[
|
| 55 |
+
gr.JSON(label="Comparison Result"),
|
| 56 |
+
gr.Image(type="numpy", label="Difference Image")
|
| 57 |
],
|
| 58 |
title="Image Comparison Tool",
|
| 59 |
description="Upload two images to compare dimensions, brightness, and highlight differences."
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
iface.launch()
|
| 63 |
|