Ashoka74 commited on
Commit
f14a178
Β·
verified Β·
1 Parent(s): 52b060b

Update gradio_demo.py

Browse files
Files changed (1) hide show
  1. gradio_demo.py +39 -26
gradio_demo.py CHANGED
@@ -12,6 +12,7 @@ from io import BytesIO
12
  import hydra
13
  from hydra import initialize, compose
14
 
 
15
 
16
 
17
  from PIL import Image
@@ -838,8 +839,12 @@ def compress_image(image):
838
  @spaces.GPU(duration=60)
839
  @torch.inference_mode
840
  @hydra.main(config_path="/home/user/app/configs", config_name="sam2_hiera_l")
841
- def process_image(input_image, input_text):
842
  """Main processing function for the Gradio interface"""
 
 
 
 
843
 
844
  # Initialize configs
845
  API_TOKEN = "9c8c865e10ec1821bea79d9fa9dc8720"
@@ -1289,36 +1294,44 @@ with block:
1289
 
1290
  # return mask_mover.create_composite(original_bg, x_pos, y_pos, scale)
1291
 
1292
- class BackgroundManager:
1293
- def __init__(self):
1294
- self.original_bg = None
1295
 
1296
- # def update_position(self, background, x_pos, y_pos, scale):
1297
- # """Update composite when position changes"""
1298
- # if background is None:
1299
- # return None
1300
 
1301
- # if self.original_bg is None:
1302
- # self.original_bg = background.copy()
1303
 
1304
- # # Convert string values to float
1305
- # x_pos = float(x_pos)
1306
- # y_pos = float(y_pos)
1307
- # scale = float(scale)
1308
 
1309
- # return mask_mover.create_composite(self.original_bg, x_pos, y_pos, scale)
1310
 
1311
- def update_position(background, x_pos, y_pos, scale):
1312
- if background is None:
1313
- return None
1314
- # Restore a fresh copy of the original background
1315
- fresh_bg = bg_manager.original_bg.copy()
1316
- # Composite the foreground once
1317
- return mask_mover.create_composite(fresh_bg, float(x_pos), float(y_pos), float(scale))
 
 
 
 
 
 
 
 
1318
 
1319
 
1320
  # Create an instance of BackgroundManager
1321
- bg_manager = BackgroundManager()
1322
 
1323
  input_bg.change(
1324
  fn=lambda new_bg: setattr(bg_manager, 'original_bg', None) or new_bg,
@@ -1328,19 +1341,19 @@ with block:
1328
  )
1329
 
1330
  x_slider.change(
1331
- fn=lambda bg, x, y, scale: bg_manager.update_position(bg, x, y, scale),
1332
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1333
  outputs=[input_bg]
1334
  )
1335
 
1336
  y_slider.change(
1337
- fn=lambda bg, x, y, scale: bg_manager.update_position(bg, x, y, scale),
1338
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1339
  outputs=[input_bg]
1340
  )
1341
 
1342
  fg_scale_slider.change(
1343
- fn=lambda bg, x, y, scale: bg_manager.update_position(bg, x, y, scale),
1344
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1345
  outputs=[input_bg]
1346
  )
 
12
  import hydra
13
  from hydra import initialize, compose
14
 
15
+ from omegaconf import DictConfig
16
 
17
 
18
  from PIL import Image
 
839
  @spaces.GPU(duration=60)
840
  @torch.inference_mode
841
  @hydra.main(config_path="/home/user/app/configs", config_name="sam2_hiera_l")
842
+ def process_image(cfg: DictConfig, input_image, input_text):
843
  """Main processing function for the Gradio interface"""
844
+
845
+ print(f"Config: {cfg}")
846
+ print(f"Input Image: {input_image}")
847
+ print(f"Input Text: {input_text}")
848
 
849
  # Initialize configs
850
  API_TOKEN = "9c8c865e10ec1821bea79d9fa9dc8720"
 
1294
 
1295
  # return mask_mover.create_composite(original_bg, x_pos, y_pos, scale)
1296
 
1297
+ # class BackgroundManager:
1298
+ # def __init__(self):
1299
+ # self.original_bg = None
1300
 
1301
+ # # def update_position(self, background, x_pos, y_pos, scale):
1302
+ # # """Update composite when position changes"""
1303
+ # # if background is None:
1304
+ # # return None
1305
 
1306
+ # # if self.original_bg is None:
1307
+ # # self.original_bg = background.copy()
1308
 
1309
+ # # # Convert string values to float
1310
+ # # x_pos = float(x_pos)
1311
+ # # y_pos = float(y_pos)
1312
+ # # scale = float(scale)
1313
 
1314
+ # # return mask_mover.create_composite(self.original_bg, x_pos, y_pos, scale)
1315
 
1316
+ # def update_position(background, x_pos, y_pos, scale):
1317
+ # if background is None:
1318
+ # return None
1319
+ # # Restore a fresh copy of the original background
1320
+ # fresh_bg = bg_manager.original_bg.copy()
1321
+ # # Composite the foreground once
1322
+ # return mask_mover.create_composite(fresh_bg, float(x_pos), float(y_pos), float(scale))
1323
+
1324
+ def update_position(background, x_pos, y_pos, scale):
1325
+ if background is None:
1326
+ return None
1327
+ fresh_bg = bg_manager.original_bg.copy() # Start from a clean original background
1328
+ # Composite the extracted foreground onto fresh_bg
1329
+ return mask_mover.create_composite(fresh_bg, float(x_pos), float(y_pos), float(scale))
1330
+
1331
 
1332
 
1333
  # Create an instance of BackgroundManager
1334
+ # bg_manager = BackgroundManager()
1335
 
1336
  input_bg.change(
1337
  fn=lambda new_bg: setattr(bg_manager, 'original_bg', None) or new_bg,
 
1341
  )
1342
 
1343
  x_slider.change(
1344
+ fn=lambda bg, x, y, scale: update_position(bg, x, y, scale),
1345
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1346
  outputs=[input_bg]
1347
  )
1348
 
1349
  y_slider.change(
1350
+ fn=lambda bg, x, y, scale: update_position(bg, x, y, scale),
1351
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1352
  outputs=[input_bg]
1353
  )
1354
 
1355
  fg_scale_slider.change(
1356
+ fn=lambda bg, x, y, scale: update_position(bg, x, y, scale),
1357
  inputs=[input_bg, x_slider, y_slider, fg_scale_slider],
1358
  outputs=[input_bg]
1359
  )