fix mask
Browse files
demo.py
CHANGED
|
@@ -705,12 +705,36 @@ console.log("Hello, world!");
|
|
| 705 |
|
| 706 |
|
| 707 |
def create_demo():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 708 |
# Define all components first
|
| 709 |
mask_input = gr.ImageEditor(
|
| 710 |
label="Binary Mask",
|
| 711 |
height=400,
|
| 712 |
width=400,
|
| 713 |
image_mode="L",
|
|
|
|
| 714 |
type="numpy",
|
| 715 |
brush=gr.Brush(
|
| 716 |
colors=["#ffffff"],
|
|
@@ -1019,7 +1043,7 @@ def create_demo():
|
|
| 1019 |
# Fall back to empty canvas
|
| 1020 |
editor_value = black_background
|
| 1021 |
|
| 1022 |
-
mask_input.value = editor_value
|
| 1023 |
mask_input.render()
|
| 1024 |
class_selection.render()
|
| 1025 |
sampling_steps.render()
|
|
|
|
| 705 |
|
| 706 |
|
| 707 |
def create_demo():
|
| 708 |
+
|
| 709 |
+
black_background = np.zeros((400, 400), dtype=np.uint8)
|
| 710 |
+
|
| 711 |
+
# Load the default mask image if it exists
|
| 712 |
+
try:
|
| 713 |
+
mask_image = Image.open("assets/a4c_seg.png").convert("L")
|
| 714 |
+
mask_image = mask_image.resize((400, 400), Image.Resampling.LANCZOS)
|
| 715 |
+
# Make it binary (0 or 255)
|
| 716 |
+
mask_image = ImageOps.autocontrast(mask_image, cutoff=0)
|
| 717 |
+
mask_image = mask_image.point(lambda p: 255 if p > 127 else 0)
|
| 718 |
+
mask_array = np.array(mask_image)
|
| 719 |
+
|
| 720 |
+
# Create the editor value structure
|
| 721 |
+
editor_value = {
|
| 722 |
+
"background": black_background, # Black background
|
| 723 |
+
"layers": [mask_array], # The mask as an editable layer
|
| 724 |
+
"composite": mask_array, # The composite image (what's displayed)
|
| 725 |
+
}
|
| 726 |
+
except Exception as e:
|
| 727 |
+
print(f"Error loading mask image: {e}")
|
| 728 |
+
# Fall back to empty canvas
|
| 729 |
+
editor_value = black_background
|
| 730 |
+
|
| 731 |
# Define all components first
|
| 732 |
mask_input = gr.ImageEditor(
|
| 733 |
label="Binary Mask",
|
| 734 |
height=400,
|
| 735 |
width=400,
|
| 736 |
image_mode="L",
|
| 737 |
+
value=editor_value,
|
| 738 |
type="numpy",
|
| 739 |
brush=gr.Brush(
|
| 740 |
colors=["#ffffff"],
|
|
|
|
| 1043 |
# Fall back to empty canvas
|
| 1044 |
editor_value = black_background
|
| 1045 |
|
| 1046 |
+
# mask_input.value = editor_value
|
| 1047 |
mask_input.render()
|
| 1048 |
class_selection.render()
|
| 1049 |
sampling_steps.render()
|