Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
|
| 5 |
import numpy as np
|
|
@@ -11,6 +9,9 @@ import os
|
|
| 11 |
|
| 12 |
def apply_transformations(
|
| 13 |
image,
|
|
|
|
|
|
|
|
|
|
| 14 |
apply_mosaic_trigger,
|
| 15 |
crop_box,
|
| 16 |
scale_factor,
|
|
@@ -35,34 +36,52 @@ def apply_transformations(
|
|
| 35 |
"""
|
| 36 |
Applies a series of transformations to an input image.
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
img = Image.fromarray(image_data).convert("RGB")
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if apply_mosaic_trigger:
|
| 48 |
w, h = img.size
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if crop_box is not None:
|
| 60 |
try:
|
|
|
|
| 61 |
x1, y1, x2, y2 = map(int, crop_box)
|
| 62 |
if x1 < x2 and y1 < y2:
|
| 63 |
img = img.crop((x1, y1, x2, y2))
|
| 64 |
except (ValueError, TypeError):
|
| 65 |
-
pass
|
| 66 |
|
| 67 |
if scale_factor != 1.0: img = img.resize((int(img.width * scale_factor), int(img.height * scale_factor)), Image.Resampling.LANCZOS)
|
| 68 |
if shear_x != 0 or shear_y != 0: img = img.transform(img.size, Image.Transform.AFFINE, (1, shear_x, 0, shear_y, 1, 0), Image.Resampling.BICUBIC)
|
|
@@ -72,39 +91,45 @@ def apply_transformations(
|
|
| 72 |
if brightness != 1.0: img = ImageEnhance.Brightness(img).enhance(brightness)
|
| 73 |
if contrast != 1.0: img = ImageEnhance.Contrast(img).enhance(contrast)
|
| 74 |
if saturation != 1.0: img = ImageEnhance.Color(img).enhance(saturation)
|
| 75 |
-
|
| 76 |
if hue != 0:
|
| 77 |
hsv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2HSV)
|
| 78 |
hsv_img[:, :, 0] = (hsv_img[:, :, 0].astype(int) + hue) % 180
|
| 79 |
img = Image.fromarray(cv2.cvtColor(hsv_img, cv2.COLOR_HSV2RGB))
|
| 80 |
-
|
| 81 |
-
if gamma != 1.0:
|
| 82 |
inv_gamma = 1.0 / gamma
|
| 83 |
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
|
| 86 |
if sharpen_factor > 0:
|
| 87 |
for _ in range(int(sharpen_factor)): img = img.filter(ImageFilter.SHARPEN)
|
| 88 |
if blur_radius > 0: img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 89 |
-
|
| 90 |
if cutout_n_holes > 0 and cutout_ratio > 0:
|
| 91 |
-
np_img = np.array(img)
|
| 92 |
h, w, _ = np_img.shape
|
| 93 |
hole_w, hole_h = int(w * cutout_ratio), int(h * cutout_ratio)
|
| 94 |
if hole_w > 0 and hole_h > 0:
|
| 95 |
for _ in range(cutout_n_holes):
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
img = Image.fromarray(np_img)
|
| 99 |
|
| 100 |
if noise_intensity > 0:
|
| 101 |
np_img = np.array(img)
|
| 102 |
noise = np.random.normal(0, noise_intensity, np_img.shape)
|
| 103 |
img = Image.fromarray(np.clip(np_img + noise, 0, 255).astype(np.uint8))
|
| 104 |
-
|
| 105 |
if grayscale: img = ImageOps.grayscale(img)
|
| 106 |
-
|
|
|
|
| 107 |
|
|
|
|
| 108 |
return img, False
|
| 109 |
|
| 110 |
# --- UI Helper Functions ---
|
|
@@ -117,14 +142,16 @@ def update_slider(min_val, max_val, current_val):
|
|
| 117 |
return gr.update(minimum=min_val, maximum=max_val, value=new_val)
|
| 118 |
|
| 119 |
def reset_all_controls():
|
|
|
|
| 120 |
return (
|
| 121 |
-
False, None, 1.0, 0, False, False, 0.0, 0.0, 1.0, 1.0, 1.0, 0, 1.0, False, False, 0.0, 0, 0, 0, 0.0,
|
| 122 |
0.1, 3.0, -180, 180, -0.5, 0.5, -0.5, 0.5, 0.0, 3.0, 0.0, 3.0, 0.0, 3.0,
|
| 123 |
-90, 90, 0.2, 2.2, 0.0, 15.0, 0, 50, 0, 50, 0.0, 0.5,
|
| 124 |
None, None
|
| 125 |
)
|
| 126 |
|
| 127 |
def on_upload():
|
|
|
|
| 128 |
return reset_all_controls()[:-2] + (None,)
|
| 129 |
|
| 130 |
# --- Gradio UI Layout ---
|
|
@@ -132,8 +159,7 @@ def on_upload():
|
|
| 132 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 133 |
gr.Markdown("# Advanced Image Augmentation Tool (Manual Control)")
|
| 134 |
gr.Markdown("Set your parameters on the left, then click **Apply Transformations** to see the result.")
|
| 135 |
-
|
| 136 |
-
# State and Control Panels remain the same...
|
| 137 |
crop_box_state = gr.State(None)
|
| 138 |
mosaic_trigger = gr.State(False)
|
| 139 |
|
|
@@ -141,13 +167,18 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 141 |
with gr.Column(scale=1, min_width=400):
|
| 142 |
gr.Markdown("### Control Panel")
|
| 143 |
with gr.Accordion("Geometric Transformations", open=True):
|
|
|
|
|
|
|
| 144 |
scale_slider = gr.Slider(0.1, 3.0, 1.0, step=0.05, label="Scale")
|
| 145 |
rotation_slider = gr.Slider(-180, 180, 0, step=1, label="Rotation Angle")
|
| 146 |
with gr.Row():
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
shear_x_slider, shear_y_slider = gr.Slider(-0.5, 0.5, 0.0, step=0.01, label="Shear X"), gr.Slider(-0.5, 0.5, 0.0, step=0.01, label="Shear Y")
|
| 149 |
h_flip_check, v_flip_check = gr.Checkbox(label="Horizontal Flip"), gr.Checkbox(label="Vertical Flip")
|
| 150 |
-
|
| 151 |
with gr.Accordion("Color & Tone Adjustments", open=True):
|
| 152 |
brightness_slider, contrast_slider, saturation_slider = gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Brightness"), gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Contrast"), gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Saturation")
|
| 153 |
hue_slider, gamma_slider = gr.Slider(-90, 90, 0, step=1, label="Hue"), gr.Slider(0.2, 2.2, 1.0, step=0.05, label="Exposure (Gamma)")
|
|
@@ -155,12 +186,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 155 |
|
| 156 |
with gr.Accordion("Filters & Distortions", open=True):
|
| 157 |
blur_slider, sharpen_slider, noise_slider = gr.Slider(0.0, 15.0, 0.0, step=0.1, label="Blur Radius"), gr.Slider(0, 5, 0, step=1, label="Sharpen Intensity"), gr.Slider(0, 50, 0, step=1, label="Add Noise")
|
| 158 |
-
|
| 159 |
with gr.Accordion("Enhanced Augmentations", open=False):
|
| 160 |
cutout_n_slider, cutout_ratio_slider = gr.Slider(0, 50, 0, step=1, label="Number of Holes"), gr.Slider(0.0, 0.5, 0.0, step=0.01, label="Hole Size Ratio")
|
| 161 |
-
mosaic_btn = gr.Button("Apply Mosaic")
|
| 162 |
|
| 163 |
with gr.Accordion("Parameter Range Control (Advanced)", open=False):
|
|
|
|
| 164 |
with gr.Tabs():
|
| 165 |
with gr.TabItem("Geometric"):
|
| 166 |
scale_min, scale_max, rotation_min, rotation_max = gr.Number(0.1, label="Scale Min"), gr.Number(3.0, label="Scale Max"), gr.Number(-180, label="Rotation Min"), gr.Number(180, label="Rotation Max")
|
|
@@ -179,21 +211,47 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 179 |
apply_btn = gr.Button("Apply Transformations", variant="primary")
|
| 180 |
image_input = gr.ImageEditor(type="numpy", label="Original Image (Select an area to crop)", interactive=True)
|
| 181 |
image_output = gr.Image(label="Transformed Image", interactive=False)
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
partial_resettable_components = all_resettable_components[:-2] + [image_output]
|
| 187 |
|
| 188 |
-
# FIX 2: Added `api_name="predict"` to the main button's click event.
|
| 189 |
apply_btn.click(fn=apply_transformations, inputs=all_inputs_for_transform, outputs=[image_output, mosaic_trigger], api_name="predict")
|
| 190 |
-
|
| 191 |
-
image_input.select(fn=process_selection, inputs=None, outputs=[crop_box_state])
|
| 192 |
image_input.upload(fn=on_upload, inputs=None, outputs=partial_resettable_components)
|
| 193 |
image_input.clear(fn=reset_all_controls, inputs=None, outputs=all_resettable_components)
|
| 194 |
reset_btn.click(fn=reset_all_controls, inputs=None, outputs=all_resettable_components)
|
| 195 |
-
|
| 196 |
-
|
|
|
|
| 197 |
rotate_180_btn.click(lambda: 180, inputs=None, outputs=[rotation_slider])
|
| 198 |
rotate_270_btn.click(lambda: -90, inputs=None, outputs=[rotation_slider])
|
| 199 |
mosaic_btn.click(lambda: True, None, mosaic_trigger)
|
|
@@ -203,4 +261,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 203 |
min_comp.change(fn=update_slider, inputs=[min_comp, max_comp, slider], outputs=[slider])
|
| 204 |
max_comp.change(fn=update_slider, inputs=[min_comp, max_comp, slider], outputs=[slider])
|
| 205 |
|
| 206 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image, ImageEnhance, ImageFilter, ImageOps
|
| 3 |
import numpy as np
|
|
|
|
| 9 |
|
| 10 |
def apply_transformations(
|
| 11 |
image,
|
| 12 |
+
# New parameter for resizing
|
| 13 |
+
resize_percentage,
|
| 14 |
+
# ---
|
| 15 |
apply_mosaic_trigger,
|
| 16 |
crop_box,
|
| 17 |
scale_factor,
|
|
|
|
| 36 |
"""
|
| 37 |
Applies a series of transformations to an input image.
|
| 38 |
"""
|
| 39 |
+
# BUG FIX: More robust input handling for gr.ImageEditor or standard np.array
|
| 40 |
+
if image is None:
|
| 41 |
+
return None, False # Return nothing if no image is present
|
| 42 |
+
|
| 43 |
+
# Get the background image from the ImageEditor's dictionary output, or use the array directly
|
| 44 |
+
image_data = image.get("background") if isinstance(image, dict) else image
|
| 45 |
+
|
| 46 |
+
if image_data is None:
|
| 47 |
+
return None, False # Return nothing if image_data is still None
|
| 48 |
|
| 49 |
img = Image.fromarray(image_data).convert("RGB")
|
| 50 |
|
| 51 |
+
# --- FEATURE: Resize functionality added ---
|
| 52 |
+
# Applied first to improve performance on subsequent operations.
|
| 53 |
+
if resize_percentage != 100:
|
| 54 |
+
new_width = int(img.width * resize_percentage / 100.0)
|
| 55 |
+
new_height = int(img.height * resize_percentage / 100.0)
|
| 56 |
+
# Ensure dimensions are at least 1x1
|
| 57 |
+
if new_width > 0 and new_height > 0:
|
| 58 |
+
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 59 |
+
|
| 60 |
+
# Mosaic is a special one-shot trigger
|
| 61 |
if apply_mosaic_trigger:
|
| 62 |
w, h = img.size
|
| 63 |
+
# Ensure dimensions are even for clean quadrants
|
| 64 |
+
w, h = w - (w % 2), h - (h % 2)
|
| 65 |
+
if w > 0 and h > 0:
|
| 66 |
+
img = img.crop((0, 0, w, h))
|
| 67 |
+
cx, cy = w // 2, h // 2
|
| 68 |
+
crops = [img.crop((0, 0, cx, cy)), img.crop((cx, 0, w, cy)), img.crop((0, cy, w, h)), img.crop((cx, cy, w, h))]
|
| 69 |
+
random.shuffle(crops)
|
| 70 |
+
mosaic_img = Image.new('RGB', (w, h))
|
| 71 |
+
mosaic_img.paste(crops[0], (0, 0))
|
| 72 |
+
mosaic_img.paste(crops[1], (cx, 0))
|
| 73 |
+
mosaic_img.paste(crops[2], (0, cy))
|
| 74 |
+
mosaic_img.paste(crops[3], (cx, cy))
|
| 75 |
+
img = mosaic_img
|
| 76 |
|
| 77 |
if crop_box is not None:
|
| 78 |
try:
|
| 79 |
+
# Ensure crop coordinates are within image bounds
|
| 80 |
x1, y1, x2, y2 = map(int, crop_box)
|
| 81 |
if x1 < x2 and y1 < y2:
|
| 82 |
img = img.crop((x1, y1, x2, y2))
|
| 83 |
except (ValueError, TypeError):
|
| 84 |
+
pass # Ignore if crop box is invalid
|
| 85 |
|
| 86 |
if scale_factor != 1.0: img = img.resize((int(img.width * scale_factor), int(img.height * scale_factor)), Image.Resampling.LANCZOS)
|
| 87 |
if shear_x != 0 or shear_y != 0: img = img.transform(img.size, Image.Transform.AFFINE, (1, shear_x, 0, shear_y, 1, 0), Image.Resampling.BICUBIC)
|
|
|
|
| 91 |
if brightness != 1.0: img = ImageEnhance.Brightness(img).enhance(brightness)
|
| 92 |
if contrast != 1.0: img = ImageEnhance.Contrast(img).enhance(contrast)
|
| 93 |
if saturation != 1.0: img = ImageEnhance.Color(img).enhance(saturation)
|
| 94 |
+
|
| 95 |
if hue != 0:
|
| 96 |
hsv_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2HSV)
|
| 97 |
hsv_img[:, :, 0] = (hsv_img[:, :, 0].astype(int) + hue) % 180
|
| 98 |
img = Image.fromarray(cv2.cvtColor(hsv_img, cv2.COLOR_HSV2RGB))
|
| 99 |
+
|
| 100 |
+
if gamma != 1.0 and gamma > 0:
|
| 101 |
inv_gamma = 1.0 / gamma
|
| 102 |
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
|
| 103 |
+
# For RGB images, PIL's point operation applies the LUT to each channel
|
| 104 |
+
img = Image.eval(img, lambda p: table[p])
|
| 105 |
+
|
| 106 |
|
| 107 |
if sharpen_factor > 0:
|
| 108 |
for _ in range(int(sharpen_factor)): img = img.filter(ImageFilter.SHARPEN)
|
| 109 |
if blur_radius > 0: img = img.filter(ImageFilter.GaussianBlur(radius=blur_radius))
|
| 110 |
+
|
| 111 |
if cutout_n_holes > 0 and cutout_ratio > 0:
|
| 112 |
+
np_img = np.array(img).copy() # Use copy to avoid overwriting original array if needed elsewhere
|
| 113 |
h, w, _ = np_img.shape
|
| 114 |
hole_w, hole_h = int(w * cutout_ratio), int(h * cutout_ratio)
|
| 115 |
if hole_w > 0 and hole_h > 0:
|
| 116 |
for _ in range(cutout_n_holes):
|
| 117 |
+
if h > hole_h and w > hole_w:
|
| 118 |
+
y1, x1 = random.randint(0, h - hole_h), random.randint(0, w - hole_w)
|
| 119 |
+
# BUG FIX: Fill with a consistent gray color instead of a calculated mean
|
| 120 |
+
np_img[y1:y1+hole_h, x1:x1+hole_w] = 128
|
| 121 |
img = Image.fromarray(np_img)
|
| 122 |
|
| 123 |
if noise_intensity > 0:
|
| 124 |
np_img = np.array(img)
|
| 125 |
noise = np.random.normal(0, noise_intensity, np_img.shape)
|
| 126 |
img = Image.fromarray(np.clip(np_img + noise, 0, 255).astype(np.uint8))
|
| 127 |
+
|
| 128 |
if grayscale: img = ImageOps.grayscale(img)
|
| 129 |
+
# BUG FIX: Simplified invert logic since image is already converted to RGB at the start
|
| 130 |
+
if invert: img = ImageOps.invert(img)
|
| 131 |
|
| 132 |
+
# The second return value resets the mosaic_trigger state to False after one use
|
| 133 |
return img, False
|
| 134 |
|
| 135 |
# --- UI Helper Functions ---
|
|
|
|
| 142 |
return gr.update(minimum=min_val, maximum=max_val, value=new_val)
|
| 143 |
|
| 144 |
def reset_all_controls():
|
| 145 |
+
# Added reset value for the new resize slider (100)
|
| 146 |
return (
|
| 147 |
+
100, False, None, 1.0, 0, False, False, 0.0, 0.0, 1.0, 1.0, 1.0, 0, 1.0, False, False, 0.0, 0, 0, 0, 0.0,
|
| 148 |
0.1, 3.0, -180, 180, -0.5, 0.5, -0.5, 0.5, 0.0, 3.0, 0.0, 3.0, 0.0, 3.0,
|
| 149 |
-90, 90, 0.2, 2.2, 0.0, 15.0, 0, 50, 0, 50, 0.0, 0.5,
|
| 150 |
None, None
|
| 151 |
)
|
| 152 |
|
| 153 |
def on_upload():
|
| 154 |
+
# Slicing is adjusted to correctly reset components without touching the image inputs
|
| 155 |
return reset_all_controls()[:-2] + (None,)
|
| 156 |
|
| 157 |
# --- Gradio UI Layout ---
|
|
|
|
| 159 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 160 |
gr.Markdown("# Advanced Image Augmentation Tool (Manual Control)")
|
| 161 |
gr.Markdown("Set your parameters on the left, then click **Apply Transformations** to see the result.")
|
| 162 |
+
|
|
|
|
| 163 |
crop_box_state = gr.State(None)
|
| 164 |
mosaic_trigger = gr.State(False)
|
| 165 |
|
|
|
|
| 167 |
with gr.Column(scale=1, min_width=400):
|
| 168 |
gr.Markdown("### Control Panel")
|
| 169 |
with gr.Accordion("Geometric Transformations", open=True):
|
| 170 |
+
# NEW: Resize Slider
|
| 171 |
+
resize_percent_slider = gr.Slider(1, 200, 100, step=1, label="Resize (%)")
|
| 172 |
scale_slider = gr.Slider(0.1, 3.0, 1.0, step=0.05, label="Scale")
|
| 173 |
rotation_slider = gr.Slider(-180, 180, 0, step=1, label="Rotation Angle")
|
| 174 |
with gr.Row():
|
| 175 |
+
# BUG FIX: Buttons now set the angle directly for predictable behavior
|
| 176 |
+
rotate_90_btn = gr.Button("Rotate 90°")
|
| 177 |
+
rotate_180_btn = gr.Button("Rotate 180°")
|
| 178 |
+
rotate_270_btn = gr.Button("Rotate 270°")
|
| 179 |
shear_x_slider, shear_y_slider = gr.Slider(-0.5, 0.5, 0.0, step=0.01, label="Shear X"), gr.Slider(-0.5, 0.5, 0.0, step=0.01, label="Shear Y")
|
| 180 |
h_flip_check, v_flip_check = gr.Checkbox(label="Horizontal Flip"), gr.Checkbox(label="Vertical Flip")
|
| 181 |
+
|
| 182 |
with gr.Accordion("Color & Tone Adjustments", open=True):
|
| 183 |
brightness_slider, contrast_slider, saturation_slider = gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Brightness"), gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Contrast"), gr.Slider(0.0, 3.0, 1.0, step=0.05, label="Saturation")
|
| 184 |
hue_slider, gamma_slider = gr.Slider(-90, 90, 0, step=1, label="Hue"), gr.Slider(0.2, 2.2, 1.0, step=0.05, label="Exposure (Gamma)")
|
|
|
|
| 186 |
|
| 187 |
with gr.Accordion("Filters & Distortions", open=True):
|
| 188 |
blur_slider, sharpen_slider, noise_slider = gr.Slider(0.0, 15.0, 0.0, step=0.1, label="Blur Radius"), gr.Slider(0, 5, 0, step=1, label="Sharpen Intensity"), gr.Slider(0, 50, 0, step=1, label="Add Noise")
|
| 189 |
+
|
| 190 |
with gr.Accordion("Enhanced Augmentations", open=False):
|
| 191 |
cutout_n_slider, cutout_ratio_slider = gr.Slider(0, 50, 0, step=1, label="Number of Holes"), gr.Slider(0.0, 0.5, 0.0, step=0.01, label="Hole Size Ratio")
|
| 192 |
+
mosaic_btn = gr.Button("Apply Mosaic (then click Apply Transformations)")
|
| 193 |
|
| 194 |
with gr.Accordion("Parameter Range Control (Advanced)", open=False):
|
| 195 |
+
# This section remains the same, no new controls needed here
|
| 196 |
with gr.Tabs():
|
| 197 |
with gr.TabItem("Geometric"):
|
| 198 |
scale_min, scale_max, rotation_min, rotation_max = gr.Number(0.1, label="Scale Min"), gr.Number(3.0, label="Scale Max"), gr.Number(-180, label="Rotation Min"), gr.Number(180, label="Rotation Max")
|
|
|
|
| 211 |
apply_btn = gr.Button("Apply Transformations", variant="primary")
|
| 212 |
image_input = gr.ImageEditor(type="numpy", label="Original Image (Select an area to crop)", interactive=True)
|
| 213 |
image_output = gr.Image(label="Transformed Image", interactive=False)
|
| 214 |
+
|
| 215 |
+
# Use a helper function to find example images if they exist
|
| 216 |
+
def find_examples():
|
| 217 |
+
example_files = ["cat.jpg", "cheetah.jpg", "lion.jpg"]
|
| 218 |
+
existing_examples = []
|
| 219 |
+
# __file__ might not work in all environments (like notebooks), so be defensive
|
| 220 |
+
try:
|
| 221 |
+
script_dir = os.path.dirname(__file__)
|
| 222 |
+
for f in example_files:
|
| 223 |
+
path = os.path.join(script_dir, f)
|
| 224 |
+
if os.path.exists(path):
|
| 225 |
+
existing_examples.append(path)
|
| 226 |
+
except NameError:
|
| 227 |
+
# __file__ is not defined, so skip examples
|
| 228 |
+
pass
|
| 229 |
+
return existing_examples
|
| 230 |
+
|
| 231 |
+
example_paths = find_examples()
|
| 232 |
+
if example_paths:
|
| 233 |
+
gr.Examples(examples=example_paths, inputs=image_input, label="Example Images")
|
| 234 |
|
| 235 |
+
# --- Event Listeners ---
|
| 236 |
+
|
| 237 |
+
# Updated list of inputs for the main function
|
| 238 |
+
all_inputs_for_transform = [image_input, resize_percent_slider, mosaic_trigger, crop_box_state, scale_slider, rotation_slider, h_flip_check, v_flip_check, shear_x_slider, shear_y_slider, brightness_slider, contrast_slider, saturation_slider, hue_slider, gamma_slider, grayscale_check, invert_check, blur_slider, sharpen_slider, noise_slider, cutout_n_slider, cutout_ratio_slider]
|
| 239 |
+
|
| 240 |
+
# Updated list of all components that can be reset
|
| 241 |
+
all_resettable_components = [resize_percent_slider, mosaic_trigger, crop_box_state, scale_slider, rotation_slider, h_flip_check, v_flip_check, shear_x_slider, shear_y_slider, brightness_slider, contrast_slider, saturation_slider, hue_slider, gamma_slider, grayscale_check, invert_check, blur_slider, sharpen_slider, noise_slider, cutout_n_slider, cutout_ratio_slider, scale_min, scale_max, rotation_min, rotation_max, shear_x_min, shear_x_max, shear_y_min, shear_y_max, brightness_min, brightness_max, contrast_min, contrast_max, saturation_min, saturation_max, hue_min, hue_max, gamma_min, gamma_max, blur_min, blur_max, noise_min, noise_max, cutout_n_min, cutout_n_max, cutout_ratio_min, cutout_ratio_max, image_input, image_output]
|
| 242 |
+
|
| 243 |
+
# A subset used for upload/clear events
|
| 244 |
partial_resettable_components = all_resettable_components[:-2] + [image_output]
|
| 245 |
|
|
|
|
| 246 |
apply_btn.click(fn=apply_transformations, inputs=all_inputs_for_transform, outputs=[image_output, mosaic_trigger], api_name="predict")
|
| 247 |
+
|
| 248 |
+
image_input.select(fn=process_selection, inputs=None, outputs=[crop_box_state], show_progress="hidden")
|
| 249 |
image_input.upload(fn=on_upload, inputs=None, outputs=partial_resettable_components)
|
| 250 |
image_input.clear(fn=reset_all_controls, inputs=None, outputs=all_resettable_components)
|
| 251 |
reset_btn.click(fn=reset_all_controls, inputs=None, outputs=all_resettable_components)
|
| 252 |
+
|
| 253 |
+
# BUG FIX: Buttons now set a specific value, not increment
|
| 254 |
+
rotate_90_btn.click(lambda: 90, inputs=None, outputs=[rotation_slider])
|
| 255 |
rotate_180_btn.click(lambda: 180, inputs=None, outputs=[rotation_slider])
|
| 256 |
rotate_270_btn.click(lambda: -90, inputs=None, outputs=[rotation_slider])
|
| 257 |
mosaic_btn.click(lambda: True, None, mosaic_trigger)
|
|
|
|
| 261 |
min_comp.change(fn=update_slider, inputs=[min_comp, max_comp, slider], outputs=[slider])
|
| 262 |
max_comp.change(fn=update_slider, inputs=[min_comp, max_comp, slider], outputs=[slider])
|
| 263 |
|
| 264 |
+
demo.launch(debug=True)
|