Update app.py
Browse files
app.py
CHANGED
|
@@ -8,92 +8,75 @@ print("Initializing LaMa model...")
|
|
| 8 |
lama = SimpleLama(device='cpu')
|
| 9 |
|
| 10 |
def ensure_rgb(image):
|
| 11 |
-
"""Convert image to RGB format"""
|
| 12 |
if len(image.shape) == 2:
|
| 13 |
return cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 14 |
if len(image.shape) == 3 and image.shape[2] == 4:
|
| 15 |
return cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 16 |
return image
|
| 17 |
|
| 18 |
-
|
| 19 |
-
"""Convert mask to binary grayscale"""
|
| 20 |
-
if len(mask.shape) == 3:
|
| 21 |
-
mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
|
| 22 |
-
_, mask_binary = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
| 23 |
-
return mask_binary
|
| 24 |
-
|
| 25 |
def process_from_ui(image_dict):
|
| 26 |
-
"""Process image from the UI ImageEditor component"""
|
| 27 |
if not image_dict or image_dict["background"] is None:
|
| 28 |
return None
|
| 29 |
-
|
| 30 |
image = ensure_rgb(image_dict["background"])
|
| 31 |
|
| 32 |
# Extract mask from layers
|
| 33 |
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 34 |
if image_dict.get("layers"):
|
| 35 |
for layer in image_dict["layers"]:
|
|
|
|
|
|
|
| 36 |
if len(layer.shape) == 3:
|
| 37 |
alpha = layer[:, :, 3] if layer.shape[2] == 4 else cv2.cvtColor(layer, cv2.COLOR_RGB2GRAY)
|
| 38 |
mask = cv2.bitwise_or(mask, alpha)
|
| 39 |
|
| 40 |
-
mask =
|
| 41 |
return lama.predict(image, mask)
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
This is what external services (like your Cloudflare Worker) will call.
|
| 47 |
-
"""
|
| 48 |
if image is None or mask is None:
|
| 49 |
return None
|
| 50 |
-
|
| 51 |
img_rgb = ensure_rgb(image)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
gr.Markdown("# 💧 AI Watermark & Object Remover")
|
| 59 |
-
gr.Markdown("Paint over watermarks or unwanted objects to remove them from your images.")
|
| 60 |
|
| 61 |
-
with gr.
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
ui_btn.click(process_from_ui, inputs=input_editor, outputs=ui_output)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
api_btn = gr.Button("Process (API Test)", variant="secondary")
|
| 90 |
-
# This creates the API endpoint that can be called via /call/remove_watermark
|
| 91 |
-
api_btn.click(
|
| 92 |
-
process_api,
|
| 93 |
-
inputs=[api_image, api_mask],
|
| 94 |
-
outputs=api_output,
|
| 95 |
-
api_name="remove_watermark"
|
| 96 |
-
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
app.launch(
|
|
|
|
| 8 |
lama = SimpleLama(device='cpu')
|
| 9 |
|
| 10 |
def ensure_rgb(image):
|
|
|
|
| 11 |
if len(image.shape) == 2:
|
| 12 |
return cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
| 13 |
if len(image.shape) == 3 and image.shape[2] == 4:
|
| 14 |
return cv2.cvtColor(image, cv2.COLOR_RGBA2RGB)
|
| 15 |
return image
|
| 16 |
|
| 17 |
+
# Function for the UI (Website)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def process_from_ui(image_dict):
|
|
|
|
| 19 |
if not image_dict or image_dict["background"] is None:
|
| 20 |
return None
|
|
|
|
| 21 |
image = ensure_rgb(image_dict["background"])
|
| 22 |
|
| 23 |
# Extract mask from layers
|
| 24 |
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 25 |
if image_dict.get("layers"):
|
| 26 |
for layer in image_dict["layers"]:
|
| 27 |
+
# Gradio 5 layers can be numpy arrays or FileData.
|
| 28 |
+
# We assume numpy here as 'type="numpy"' is set in the component.
|
| 29 |
if len(layer.shape) == 3:
|
| 30 |
alpha = layer[:, :, 3] if layer.shape[2] == 4 else cv2.cvtColor(layer, cv2.COLOR_RGB2GRAY)
|
| 31 |
mask = cv2.bitwise_or(mask, alpha)
|
| 32 |
|
| 33 |
+
_, mask = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
| 34 |
return lama.predict(image, mask)
|
| 35 |
|
| 36 |
+
# --- NEW: Function for the API (Cloudflare) ---
|
| 37 |
+
# This accepts two simple images instead of a complex dictionary
|
| 38 |
+
def process_simple_api(image, mask):
|
|
|
|
|
|
|
| 39 |
if image is None or mask is None:
|
| 40 |
return None
|
|
|
|
| 41 |
img_rgb = ensure_rgb(image)
|
| 42 |
+
# Ensure mask is grayscale
|
| 43 |
+
if len(mask.shape) == 3:
|
| 44 |
+
mask = cv2.cvtColor(mask, cv2.COLOR_RGB2GRAY)
|
| 45 |
+
_, mask_bin = cv2.threshold(mask, 10, 255, cv2.THRESH_BINARY)
|
| 46 |
+
return lama.predict(img_rgb, mask_bin)
|
| 47 |
|
| 48 |
+
with gr.Blocks(title="AI Watermark Remover") as app:
|
| 49 |
+
gr.Markdown("# 💧 AI Watermark Remover")
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
with gr.Row():
|
| 52 |
+
# VISUAL UI (For humans)
|
| 53 |
+
input_editor = gr.ImageEditor(
|
| 54 |
+
label="Draw Mask", type="numpy",
|
| 55 |
+
brush=gr.Brush(colors=["#FF0000"], default_size=20),
|
| 56 |
+
interactive=True
|
| 57 |
+
)
|
| 58 |
+
# HIDDEN INPUTS (For the API / Cloudflare)
|
| 59 |
+
# We define these so the API is easy to use
|
| 60 |
+
api_image_input = gr.Image(label="API Image", visible=False, type="numpy")
|
| 61 |
+
api_mask_input = gr.Image(label="API Mask", visible=False, type="numpy")
|
| 62 |
|
| 63 |
+
ui_output = gr.Image(label="Result")
|
|
|
|
| 64 |
|
| 65 |
+
ui_btn = gr.Button("Remove Watermark", variant="primary")
|
| 66 |
+
|
| 67 |
+
# UI Button Click
|
| 68 |
+
ui_btn.click(process_from_ui, inputs=input_editor, outputs=ui_output)
|
| 69 |
+
|
| 70 |
+
# --- THIS IS THE KEY ---
|
| 71 |
+
# We create a named API endpoint that takes two SIMPLE images.
|
| 72 |
+
# No "EditorData" dictionary required.
|
| 73 |
+
api_btn = gr.Button("API_RUN", visible=False)
|
| 74 |
+
api_btn.click(
|
| 75 |
+
process_simple_api,
|
| 76 |
+
inputs=[api_image_input, api_mask_input],
|
| 77 |
+
outputs=ui_output,
|
| 78 |
+
api_name="remove_watermark"
|
| 79 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
app.launch()
|