MalikShehram's picture
Update app.py
5ee48fa verified
import gradio as gr
import cv2
import numpy as np
def reconstruct_shape(image):
if image is None:
return None
# Check if the image has an alpha (transparency) channel
if image.shape[2] == 4:
mask = image[:, :, 3]
color_img = image[:, :, :3]
else:
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
_, mask = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)
color_img = image
# Find contours based on the mask
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return image
# Find the largest contour (the main object)
c = max(contours, key=cv2.contourArea)
# Extract the average color of the original shape for the reconstructed parts
mean_val = cv2.mean(color_img, mask=mask)
# Make the reconstructed part slightly distinct, or keep it exactly the average color
dominant_color = (int(mean_val[0]), int(mean_val[1]), int(mean_val[2]), 255)
# --- Shape Classification ---
x, y, w, h = cv2.boundingRect(c)
rect_area = w * h
(cx, cy), radius = cv2.minEnclosingCircle(c)
circle_area = np.pi * (radius ** 2)
contour_area = cv2.contourArea(c)
rect_ratio = contour_area / rect_area if rect_area > 0 else 0
circle_ratio = contour_area / circle_area if circle_area > 0 else 0
# Create a blank transparent canvas for the output
output_image = np.zeros((image.shape[0], image.shape[1], 4), dtype=np.uint8)
# --- Step 1: Draw the Reconstructed Base Shape ---
if circle_ratio > rect_ratio:
center = (int(cx), int(cy))
cv2.circle(output_image, center, int(radius), dominant_color, -1)
else:
if 0.85 <= w / h <= 1.15:
side = max(w, h)
cv2.rectangle(output_image, (x, y), (x + side, y + side), dominant_color, -1)
else:
cv2.rectangle(output_image, (x, y), (x + w, y + h), dominant_color, -1)
# --- Step 2: Paste the Original Image Over the Base ---
# This keeps the original texture intact while the missing parts show the dominant color
original_area = mask > 0
output_image[original_area] = image[original_area]
# --- Step 3: Draw the "Crack" / Seam ---
# Draw a dark line along the boundary of the original shape to show where it was reconstructed
crack_color = (40, 40, 40, 255) # Dark gray/black line for the crack
crack_thickness = 3 # Adjust thickness of the line here
cv2.drawContours(output_image, [c], -1, crack_color, crack_thickness)
return output_image
# --- Gradio Interface Setup ---
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🔷 Irregular Shape Reconstructor (With Visible Seams)")
gr.Markdown("Upload an image of an irregular shape. The algorithm reconstructs its original geometric form and draws a visible 'crack' line to show exactly where the new parts were glued on.")
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Upload Irregular Shape", image_mode="RGBA", type="numpy")
submit_btn = gr.Button("Reconstruct Shape", variant="primary")
with gr.Column():
output_image = gr.Image(label="Reconstructed Shape with Crack", image_mode="RGBA")
submit_btn.click(fn=reconstruct_shape, inputs=input_image, outputs=output_image)
if __name__ == "__main__":
demo.launch()