Spaces:
Runtime error
Runtime error
Update gradio_demo.py
Browse files- gradio_demo.py +36 -10
gradio_demo.py
CHANGED
@@ -743,7 +743,8 @@ class MaskMover:
|
|
743 |
def __init__(self):
|
744 |
self.extracted_fg = None
|
745 |
self.original_fg = None # Store original foreground
|
746 |
-
|
|
|
747 |
def set_extracted_fg(self, fg_image):
|
748 |
"""Store the extracted foreground with alpha channel"""
|
749 |
if isinstance(fg_image, np.ndarray):
|
@@ -753,11 +754,19 @@ class MaskMover:
|
|
753 |
self.extracted_fg = np.array(fg_image)
|
754 |
self.original_fg = np.array(fg_image)
|
755 |
return self.extracted_fg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
756 |
|
757 |
def create_composite(self, background, x_pos, y_pos, scale=1.0):
|
758 |
"""Create composite with foreground at specified position"""
|
759 |
-
|
760 |
-
|
|
|
761 |
|
762 |
# Convert inputs to PIL Images
|
763 |
if isinstance(background, np.ndarray):
|
@@ -814,6 +823,9 @@ def extract_foreground(image):
|
|
814 |
|
815 |
return result, gr.update(visible=True), gr.update(visible=True)
|
816 |
|
|
|
|
|
|
|
817 |
|
818 |
def compress_image(image):
|
819 |
# Convert Gradio image (numpy array) to PIL Image
|
@@ -1213,6 +1225,7 @@ with block:
|
|
1213 |
with gr.Group():
|
1214 |
gr.Markdown("Position on Background")
|
1215 |
input_bg = gr.Image(type="numpy", label="Background Image", height=480)
|
|
|
1216 |
|
1217 |
with gr.Row():
|
1218 |
x_slider = gr.Slider(
|
@@ -1371,7 +1384,13 @@ with block:
|
|
1371 |
self.original_bg = new_bg.copy()
|
1372 |
self.current_mask = None # Reset current mask when background changes
|
1373 |
print("Background updated successfully.") # Debugging
|
1374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
1375 |
def update_position(self, x_pos, y_pos, scale):
|
1376 |
"""Update composite when position changes."""
|
1377 |
if self.original_bg is None:
|
@@ -1437,12 +1456,12 @@ with block:
|
|
1437 |
# )
|
1438 |
|
1439 |
# Update the input_bg.change function to reset the mask
|
1440 |
-
input_bg.change(
|
1441 |
-
|
1442 |
-
|
1443 |
-
|
1444 |
-
|
1445 |
-
)
|
1446 |
|
1447 |
# Update the slider change functions to apply the current mask
|
1448 |
x_slider.change(
|
@@ -1565,6 +1584,13 @@ with block:
|
|
1565 |
inputs=ips_bg,
|
1566 |
outputs=[result_gallery], show_progress=True
|
1567 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1568 |
|
1569 |
example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
|
1570 |
example_quick_subjects.click(lambda x: x[0], inputs=example_quick_subjects, outputs=prompt, show_progress=False, queue=False)
|
|
|
743 |
def __init__(self):
|
744 |
self.extracted_fg = None
|
745 |
self.original_fg = None # Store original foreground
|
746 |
+
self.current_bg = None
|
747 |
+
|
748 |
def set_extracted_fg(self, fg_image):
|
749 |
"""Store the extracted foreground with alpha channel"""
|
750 |
if isinstance(fg_image, np.ndarray):
|
|
|
754 |
self.extracted_fg = np.array(fg_image)
|
755 |
self.original_fg = np.array(fg_image)
|
756 |
return self.extracted_fg
|
757 |
+
|
758 |
+
def update_background(self, new_bg):
|
759 |
+
"""Update the current background without affecting the mask"""
|
760 |
+
if new_bg is not None:
|
761 |
+
self.current_bg = new_bg.copy() # Overwrite the current background
|
762 |
+
print("Background updated successfully.") # Debugging
|
763 |
+
|
764 |
|
765 |
def create_composite(self, background, x_pos, y_pos, scale=1.0):
|
766 |
"""Create composite with foreground at specified position"""
|
767 |
+
|
768 |
+
if self.original_fg is None or self.current_bg is None:
|
769 |
+
return self.current_bg # Return current background if no foreground
|
770 |
|
771 |
# Convert inputs to PIL Images
|
772 |
if isinstance(background, np.ndarray):
|
|
|
823 |
|
824 |
return result, gr.update(visible=True), gr.update(visible=True)
|
825 |
|
826 |
+
def on_background_upload(new_bg):
|
827 |
+
mask_mover.update_background(new_bg)
|
828 |
+
|
829 |
|
830 |
def compress_image(image):
|
831 |
# Convert Gradio image (numpy array) to PIL Image
|
|
|
1225 |
with gr.Group():
|
1226 |
gr.Markdown("Position on Background")
|
1227 |
input_bg = gr.Image(type="numpy", label="Background Image", height=480)
|
1228 |
+
reset_button = gr.Button(value="Reset Background")
|
1229 |
|
1230 |
with gr.Row():
|
1231 |
x_slider = gr.Slider(
|
|
|
1384 |
self.original_bg = new_bg.copy()
|
1385 |
self.current_mask = None # Reset current mask when background changes
|
1386 |
print("Background updated successfully.") # Debugging
|
1387 |
+
|
1388 |
+
def reset_background(self):
|
1389 |
+
"""Reset the background to its original state"""
|
1390 |
+
if self.original_bg is not None:
|
1391 |
+
self.current_bg = self.original_bg.copy() # Reset to original background
|
1392 |
+
print("Background reset to original state.") # Debugging
|
1393 |
+
|
1394 |
def update_position(self, x_pos, y_pos, scale):
|
1395 |
"""Update composite when position changes."""
|
1396 |
if self.original_bg is None:
|
|
|
1456 |
# )
|
1457 |
|
1458 |
# Update the input_bg.change function to reset the mask
|
1459 |
+
# input_bg.change(
|
1460 |
+
# fn=lambda new_bg: bg_manager.update_background(new_bg) or new_bg,
|
1461 |
+
# inputs=[input_bg],
|
1462 |
+
# outputs=[input_bg],
|
1463 |
+
# show_progress=False
|
1464 |
+
# )
|
1465 |
|
1466 |
# Update the slider change functions to apply the current mask
|
1467 |
x_slider.change(
|
|
|
1584 |
inputs=ips_bg,
|
1585 |
outputs=[result_gallery], show_progress=True
|
1586 |
)
|
1587 |
+
|
1588 |
+
reset_button.click(
|
1589 |
+
fn=lambda: bg_manager.reset_background(), # Call the reset function
|
1590 |
+
inputs=[], # No inputs needed for reset
|
1591 |
+
outputs=[input_bg], # Update the displayed background
|
1592 |
+
show_progress=True
|
1593 |
+
)
|
1594 |
|
1595 |
example_quick_prompts.click(lambda x, y: ', '.join(y.split(', ')[:2] + [x[0]]), inputs=[example_quick_prompts, prompt], outputs=prompt, show_progress=False, queue=False)
|
1596 |
example_quick_subjects.click(lambda x: x[0], inputs=example_quick_subjects, outputs=prompt, show_progress=False, queue=False)
|