Spaces:
Runtime error
Runtime error
Update gradio_demo.py
Browse files- gradio_demo.py +72 -14
gradio_demo.py
CHANGED
@@ -1174,7 +1174,7 @@ with block:
|
|
1174 |
|
1175 |
with gr.Tab("Background", visible=True):
|
1176 |
# empty cache
|
1177 |
-
|
1178 |
mask_mover = MaskMover()
|
1179 |
# with torch.no_grad():
|
1180 |
# # Update the input channels to 12
|
@@ -1331,22 +1331,51 @@ with block:
|
|
1331 |
|
1332 |
# return mask_mover.create_composite(original_bg, x_pos, y_pos, scale)
|
1333 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1334 |
class BackgroundManager:
|
1335 |
def __init__(self):
|
1336 |
self.original_bg = None # To store the original background
|
|
|
1337 |
|
1338 |
def update_background(self, new_bg):
|
1339 |
"""Set a new background."""
|
1340 |
if new_bg is not None:
|
1341 |
self.original_bg = new_bg.copy()
|
|
|
1342 |
print("Background updated successfully.") # Debugging
|
1343 |
-
|
1344 |
-
def update_position(self,
|
1345 |
"""Update composite when position changes."""
|
1346 |
if self.original_bg is None:
|
1347 |
print("No original background set.")
|
1348 |
return None
|
1349 |
-
|
1350 |
# Start from a clean copy of the original background
|
1351 |
fresh_bg = self.original_bg.copy()
|
1352 |
|
@@ -1356,10 +1385,12 @@ with block:
|
|
1356 |
# Composite the foreground onto the fresh background
|
1357 |
composite = mask_mover.create_composite(fresh_bg, x_pos, y_pos, scale)
|
1358 |
|
|
|
|
|
1359 |
return composite
|
1360 |
-
|
|
|
1361 |
# Create an instance of BackgroundManager
|
1362 |
-
bg_manager = BackgroundManager()
|
1363 |
#bg_manager = gr.State(BackgroundManager())
|
1364 |
|
1365 |
def update_position_wrapper(background, x_pos, y_pos, scale):
|
@@ -1376,31 +1407,58 @@ with block:
|
|
1376 |
|
1377 |
|
1378 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1379 |
input_bg.change(
|
1380 |
fn=lambda new_bg: bg_manager.update_background(new_bg) or new_bg,
|
1381 |
inputs=[input_bg],
|
1382 |
outputs=[input_bg],
|
1383 |
show_progress=False
|
1384 |
)
|
1385 |
-
|
|
|
1386 |
x_slider.change(
|
1387 |
-
fn=bg_manager.update_position,
|
1388 |
-
inputs=[
|
1389 |
outputs=[input_bg]
|
1390 |
)
|
1391 |
|
1392 |
y_slider.change(
|
1393 |
-
fn=bg_manager.update_position,
|
1394 |
-
inputs=[
|
1395 |
outputs=[input_bg]
|
1396 |
)
|
1397 |
|
1398 |
fg_scale_slider.change(
|
1399 |
-
fn=bg_manager.update_position,
|
1400 |
-
inputs=[
|
1401 |
outputs=[input_bg]
|
1402 |
)
|
1403 |
-
|
1404 |
# Update inputs list to include fg_scale_slider
|
1405 |
|
1406 |
def process_relight_with_position(*args):
|
|
|
1174 |
|
1175 |
with gr.Tab("Background", visible=True):
|
1176 |
# empty cache
|
1177 |
+
bg_manager = BackgroundManager()
|
1178 |
mask_mover = MaskMover()
|
1179 |
# with torch.no_grad():
|
1180 |
# # Update the input channels to 12
|
|
|
1331 |
|
1332 |
# return mask_mover.create_composite(original_bg, x_pos, y_pos, scale)
|
1333 |
|
1334 |
+
# class BackgroundManager:
|
1335 |
+
# def __init__(self):
|
1336 |
+
# self.original_bg = None # To store the original background
|
1337 |
+
|
1338 |
+
# def update_background(self, new_bg):
|
1339 |
+
# """Set a new background."""
|
1340 |
+
# if new_bg is not None:
|
1341 |
+
# self.original_bg = new_bg.copy()
|
1342 |
+
# print("Background updated successfully.") # Debugging
|
1343 |
+
|
1344 |
+
# def update_position(self, background, x_pos, y_pos, scale):
|
1345 |
+
# """Update composite when position changes."""
|
1346 |
+
# if self.original_bg is None:
|
1347 |
+
# print("No original background set.")
|
1348 |
+
# return None
|
1349 |
+
|
1350 |
+
# # Start from a clean copy of the original background
|
1351 |
+
# fresh_bg = self.original_bg.copy()
|
1352 |
+
|
1353 |
+
# # Debugging
|
1354 |
+
# print(f"Updating position: x={x_pos}, y={y_pos}, scale={scale}")
|
1355 |
+
|
1356 |
+
# # Composite the foreground onto the fresh background
|
1357 |
+
# composite = mask_mover.create_composite(fresh_bg, x_pos, y_pos, scale)
|
1358 |
+
|
1359 |
+
# return composite
|
1360 |
+
|
1361 |
class BackgroundManager:
|
1362 |
def __init__(self):
|
1363 |
self.original_bg = None # To store the original background
|
1364 |
+
self.current_mask = None # To store the current mask applied
|
1365 |
|
1366 |
def update_background(self, new_bg):
|
1367 |
"""Set a new background."""
|
1368 |
if new_bg is not None:
|
1369 |
self.original_bg = new_bg.copy()
|
1370 |
+
self.current_mask = None # Reset current mask when background changes
|
1371 |
print("Background updated successfully.") # Debugging
|
1372 |
+
|
1373 |
+
def update_position(self, x_pos, y_pos, scale):
|
1374 |
"""Update composite when position changes."""
|
1375 |
if self.original_bg is None:
|
1376 |
print("No original background set.")
|
1377 |
return None
|
1378 |
+
|
1379 |
# Start from a clean copy of the original background
|
1380 |
fresh_bg = self.original_bg.copy()
|
1381 |
|
|
|
1385 |
# Composite the foreground onto the fresh background
|
1386 |
composite = mask_mover.create_composite(fresh_bg, x_pos, y_pos, scale)
|
1387 |
|
1388 |
+
# Update the current mask
|
1389 |
+
self.current_mask = composite
|
1390 |
return composite
|
1391 |
+
|
1392 |
+
|
1393 |
# Create an instance of BackgroundManager
|
|
|
1394 |
#bg_manager = gr.State(BackgroundManager())
|
1395 |
|
1396 |
def update_position_wrapper(background, x_pos, y_pos, scale):
|
|
|
1407 |
|
1408 |
|
1409 |
|
1410 |
+
# input_bg.change(
|
1411 |
+
# fn=lambda new_bg: bg_manager.update_background(new_bg) or new_bg,
|
1412 |
+
# inputs=[input_bg],
|
1413 |
+
# outputs=[input_bg],
|
1414 |
+
# show_progress=False
|
1415 |
+
# )
|
1416 |
+
|
1417 |
+
# x_slider.change(
|
1418 |
+
# fn=bg_manager.update_position,
|
1419 |
+
# inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
|
1420 |
+
# outputs=[input_bg]
|
1421 |
+
# )
|
1422 |
+
|
1423 |
+
# y_slider.change(
|
1424 |
+
# fn=bg_manager.update_position,
|
1425 |
+
# inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
|
1426 |
+
# outputs=[input_bg]
|
1427 |
+
# )
|
1428 |
+
|
1429 |
+
# fg_scale_slider.change(
|
1430 |
+
# fn=bg_manager.update_position,
|
1431 |
+
# inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
|
1432 |
+
# outputs=[input_bg]
|
1433 |
+
# )
|
1434 |
+
|
1435 |
+
# Update the input_bg.change function to reset the mask
|
1436 |
input_bg.change(
|
1437 |
fn=lambda new_bg: bg_manager.update_background(new_bg) or new_bg,
|
1438 |
inputs=[input_bg],
|
1439 |
outputs=[input_bg],
|
1440 |
show_progress=False
|
1441 |
)
|
1442 |
+
|
1443 |
+
# Update the slider change functions to apply the current mask
|
1444 |
x_slider.change(
|
1445 |
+
fn=lambda x_pos: bg_manager.update_position(x_pos, y_slider.value, fg_scale_slider.value),
|
1446 |
+
inputs=[x_slider],
|
1447 |
outputs=[input_bg]
|
1448 |
)
|
1449 |
|
1450 |
y_slider.change(
|
1451 |
+
fn=lambda y_pos: bg_manager.update_position(x_slider.value, y_pos, fg_scale_slider.value),
|
1452 |
+
inputs=[y_slider],
|
1453 |
outputs=[input_bg]
|
1454 |
)
|
1455 |
|
1456 |
fg_scale_slider.change(
|
1457 |
+
fn=lambda scale: bg_manager.update_position(x_slider.value, y_slider.value, scale),
|
1458 |
+
inputs=[fg_scale_slider],
|
1459 |
outputs=[input_bg]
|
1460 |
)
|
1461 |
+
|
1462 |
# Update inputs list to include fg_scale_slider
|
1463 |
|
1464 |
def process_relight_with_position(*args):
|