Spaces:
Sleeping
Sleeping
slau8405 commited on
Commit ·
4530dbe
1
Parent(s): c495fed
Added few more features
Browse files
app.py
CHANGED
|
@@ -373,12 +373,12 @@ def augment_image(image: Image.Image, json_file: Any, aug_type: str, aug_param:
|
|
| 373 |
try:
|
| 374 |
# Validate aug_param based on aug_type
|
| 375 |
aug_ranges = {
|
| 376 |
-
"
|
| 377 |
-
"
|
| 378 |
-
"
|
| 379 |
-
"
|
| 380 |
-
"
|
| 381 |
-
"
|
| 382 |
}
|
| 383 |
if aug_type not in aug_ranges:
|
| 384 |
raise ValueError(f"Invalid augmentation type: {aug_type}")
|
|
@@ -448,12 +448,12 @@ def augment_image(image: Image.Image, json_file: Any, aug_type: str, aug_param:
|
|
| 448 |
|
| 449 |
# Define augmentation types and parameter ranges
|
| 450 |
aug_options = {
|
| 451 |
-
"
|
| 452 |
-
"Horizontal Flip"
|
| 453 |
-
"Vertical Flip"
|
| 454 |
-
"
|
| 455 |
-
"Brightness/Contrast"
|
| 456 |
-
"Pixel Dropout"
|
| 457 |
}
|
| 458 |
|
| 459 |
def create_interface():
|
|
@@ -466,26 +466,28 @@ def create_interface():
|
|
| 466 |
image_input = gr.Image(type="pil", label="Input Image")
|
| 467 |
json_input = gr.File(label="LabelMe JSON File", file_types=[".json"])
|
| 468 |
aug_type = gr.Dropdown(
|
| 469 |
-
choices=
|
| 470 |
label="Augmentation Type",
|
| 471 |
-
value="
|
| 472 |
)
|
| 473 |
aug_param = gr.Slider(
|
| 474 |
-
minimum=aug_options["
|
| 475 |
-
maximum=aug_options["
|
| 476 |
-
value=aug_options["
|
| 477 |
-
label=aug_options["
|
| 478 |
step=0.01
|
| 479 |
)
|
| 480 |
|
| 481 |
-
def update_slider(
|
|
|
|
|
|
|
| 482 |
return {
|
| 483 |
aug_param: gr.update(
|
| 484 |
-
minimum=aug_options[
|
| 485 |
-
maximum=aug_options[
|
| 486 |
-
value=aug_options[
|
| 487 |
-
label=aug_options[
|
| 488 |
-
step=0.01 if
|
| 489 |
)
|
| 490 |
}
|
| 491 |
|
|
@@ -497,8 +499,13 @@ def create_interface():
|
|
| 497 |
output_image = gr.Image(type="pil", label="Augmented Image with Colored Polygons and Masks")
|
| 498 |
output_json = gr.Textbox(label="Augmented LabelMe JSON", lines=10, max_lines=20)
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
submit_btn.click(
|
| 501 |
-
fn=
|
| 502 |
inputs=[image_input, json_input, aug_type, aug_param],
|
| 503 |
outputs=[output_image, output_json]
|
| 504 |
)
|
|
|
|
| 373 |
try:
|
| 374 |
# Validate aug_param based on aug_type
|
| 375 |
aug_ranges = {
|
| 376 |
+
"rotate": (-30, 30),
|
| 377 |
+
"horizontal_flip": (0, 1),
|
| 378 |
+
"vertical_flip": (0, 1),
|
| 379 |
+
"scale": (0.5, 1.5),
|
| 380 |
+
"brightness_contrast": (-0.3, 0.3),
|
| 381 |
+
"pixel_dropout": (0.01, 0.1)
|
| 382 |
}
|
| 383 |
if aug_type not in aug_ranges:
|
| 384 |
raise ValueError(f"Invalid augmentation type: {aug_type}")
|
|
|
|
| 448 |
|
| 449 |
# Define augmentation types and parameter ranges
|
| 450 |
aug_options = {
|
| 451 |
+
"rotate": {"display_name": "Rotate", "param_name": "Angle (degrees)", "range": (-30, 30), "default": 0},
|
| 452 |
+
"horizontal_flip": {"display_name": "Horizontal Flip", "param_name": "Apply Flip (0 or 1)", "range": (0, 1), "default": 0},
|
| 453 |
+
"vertical_flip": {"display_name": "Vertical Flip", "param_name": "Apply Flip (0 or 1)", "range": (0, 1), "default": 0},
|
| 454 |
+
"scale": {"display_name": "Scale", "param_name": "Scale Factor", "range": (0.5, 1.5), "default": 1.0},
|
| 455 |
+
"brightness_contrast": {"display_name": "Brightness/Contrast", "param_name": "Brightness/Contrast Limit", "range": (-0.3, 0.3), "default": 0},
|
| 456 |
+
"pixel_dropout": {"display_name": "Pixel Dropout", "param_name": "Dropout Probability", "range": (0.01, 0.1), "default": 0.05}
|
| 457 |
}
|
| 458 |
|
| 459 |
def create_interface():
|
|
|
|
| 466 |
image_input = gr.Image(type="pil", label="Input Image")
|
| 467 |
json_input = gr.File(label="LabelMe JSON File", file_types=[".json"])
|
| 468 |
aug_type = gr.Dropdown(
|
| 469 |
+
choices=[v["display_name"] for v in aug_options.values()],
|
| 470 |
label="Augmentation Type",
|
| 471 |
+
value=aug_options["rotate"]["display_name"]
|
| 472 |
)
|
| 473 |
aug_param = gr.Slider(
|
| 474 |
+
minimum=aug_options["rotate"]["range"][0],
|
| 475 |
+
maximum=aug_options["rotate"]["range"][1],
|
| 476 |
+
value=aug_options["rotate"]["default"],
|
| 477 |
+
label=aug_options["rotate"]["param_name"],
|
| 478 |
step=0.01
|
| 479 |
)
|
| 480 |
|
| 481 |
+
def update_slider(display_name):
|
| 482 |
+
# Map display_name back to internal key
|
| 483 |
+
aug_key = next(k for k, v in aug_options.items() if v["display_name"] == display_name)
|
| 484 |
return {
|
| 485 |
aug_param: gr.update(
|
| 486 |
+
minimum=aug_options[aug_key]["range"][0],
|
| 487 |
+
maximum=aug_options[aug_key]["range"][1],
|
| 488 |
+
value=aug_options[aug_key]["default"],
|
| 489 |
+
label=aug_options[aug_key]["param_name"],
|
| 490 |
+
step=0.01 if aug_key in ["pixel_dropout", "brightness_contrast", "scale"] else 1
|
| 491 |
)
|
| 492 |
}
|
| 493 |
|
|
|
|
| 499 |
output_image = gr.Image(type="pil", label="Augmented Image with Colored Polygons and Masks")
|
| 500 |
output_json = gr.Textbox(label="Augmented LabelMe JSON", lines=10, max_lines=20)
|
| 501 |
|
| 502 |
+
def submit(image, json_file, display_name, aug_param):
|
| 503 |
+
# Map display_name to internal key
|
| 504 |
+
aug_key = next(k for k, v in aug_options.items() if v["display_name"] == display_name)
|
| 505 |
+
return augment_image(image, json_file, aug_key, aug_param)
|
| 506 |
+
|
| 507 |
submit_btn.click(
|
| 508 |
+
fn=submit,
|
| 509 |
inputs=[image_input, json_input, aug_type, aug_param],
|
| 510 |
outputs=[output_image, output_json]
|
| 511 |
)
|