Ashoka74 commited on
Commit
c729fc5
Β·
verified Β·
1 Parent(s): 402bda6

Update gradio_demo.py

Browse files
Files changed (1) hide show
  1. gradio_demo.py +31 -32
gradio_demo.py CHANGED
@@ -972,7 +972,7 @@ def process_image(input_image, input_text):
972
  # input_boxes = np.array(input_boxes)
973
  # class_ids = np.array(class_ids)
974
 
975
- classes = [x.strip().lower() for x in TEXT_PROMPT.split('.') if x]
976
  class_name_to_id = {name: id for id, name in enumerate(classes)}
977
  class_id_to_name = {id: name for name, id in class_name_to_id.items()}
978
 
@@ -1331,31 +1331,30 @@ with block:
1331
 
1332
  class BackgroundManager:
1333
  def __init__(self):
1334
- self.original_bg = None
1335
-
1336
- # # def update_position(self, background, x_pos, y_pos, scale):
1337
- # # """Update composite when position changes"""
1338
- # # if background is None:
1339
- # # return None
1340
-
1341
- # # if self.original_bg is None:
1342
- # # self.original_bg = background.copy()
1343
-
1344
- # # # Convert string values to float
1345
- # # x_pos = float(x_pos)
1346
- # # y_pos = float(y_pos)
1347
- # # scale = float(scale)
1348
-
1349
- # # return mask_mover.create_composite(self.original_bg, x_pos, y_pos, scale)
1350
-
1351
- def update_position(self, background, x_pos, y_pos, scale, *args):
1352
  if self.original_bg is None:
1353
  print("No original background set.")
1354
  return None
1355
- fresh_bg = self.original_bg.copy() # Start from a clean original background
1356
- return mask_mover.create_composite(fresh_bg, float(x_pos), float(y_pos), float(scale))
1357
-
1358
-
 
 
 
 
 
 
 
1359
 
1360
  # Create an instance of BackgroundManager
1361
  bg_manager = BackgroundManager()
@@ -1375,26 +1374,26 @@ with block:
1375
 
1376
 
1377
  input_bg.change(
1378
- fn=lambda new_bg: setattr(bg_manager, 'original_bg', None) or new_bg,
1379
- inputs=[input_bg],
1380
- outputs=[input_bg],
1381
- show_progress=False
1382
  )
1383
 
1384
  x_slider.change(
1385
- fn=update_position_wrapper,
1386
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1387
  outputs=[input_bg]
1388
  )
1389
-
1390
  y_slider.change(
1391
- fn=update_position_wrapper,
1392
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1393
  outputs=[input_bg]
1394
  )
1395
-
1396
  fg_scale_slider.change(
1397
- fn=update_position_wrapper,
1398
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1399
  outputs=[input_bg]
1400
  )
 
972
  # input_boxes = np.array(input_boxes)
973
  # class_ids = np.array(class_ids)
974
 
975
+ classes = [x.strip().lower() for x in input_text.split('.') if x]
976
  class_name_to_id = {name: id for id, name in enumerate(classes)}
977
  class_id_to_name = {id: name for name, id in class_name_to_id.items()}
978
 
 
1331
 
1332
  class BackgroundManager:
1333
  def __init__(self):
1334
+ self.original_bg = None # To store the original background
1335
+
1336
+ def update_background(self, new_bg):
1337
+ """Set a new background."""
1338
+ if new_bg is not None:
1339
+ self.original_bg = new_bg.copy()
1340
+ print("Background updated successfully.") # Debugging
1341
+
1342
+ def update_position(self, background, x_pos, y_pos, scale):
1343
+ """Update composite when position changes."""
 
 
 
 
 
 
 
 
1344
  if self.original_bg is None:
1345
  print("No original background set.")
1346
  return None
1347
+
1348
+ # Start from a clean copy of the original background
1349
+ fresh_bg = self.original_bg.copy()
1350
+
1351
+ # Debugging
1352
+ print(f"Updating position: x={x_pos}, y={y_pos}, scale={scale}")
1353
+
1354
+ # Composite the foreground onto the fresh background
1355
+ composite = mask_mover.create_composite(fresh_bg, x_pos, y_pos, scale)
1356
+
1357
+ return composite
1358
 
1359
  # Create an instance of BackgroundManager
1360
  bg_manager = BackgroundManager()
 
1374
 
1375
 
1376
  input_bg.change(
1377
+ fn=lambda new_bg: bg_manager.update_background(new_bg) or new_bg,
1378
+ inputs=[input_bg],
1379
+ outputs=[input_bg],
1380
+ show_progress=False
1381
  )
1382
 
1383
  x_slider.change(
1384
+ fn=bg_manager.update_position,
1385
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1386
  outputs=[input_bg]
1387
  )
1388
+
1389
  y_slider.change(
1390
+ fn=bg_manager.update_position,
1391
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1392
  outputs=[input_bg]
1393
  )
1394
+
1395
  fg_scale_slider.change(
1396
+ fn=bg_manager.update_position,
1397
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1398
  outputs=[input_bg]
1399
  )