zeenai2025 commited on
Commit
7b455f0
·
verified ·
1 Parent(s): 0cb65de

Update app.py

Browse files

return as base64

Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -24,7 +24,18 @@ from segment_anything import build_sam, SamPredictor
24
  import spaces
25
  import GroundingDINO.groundingdino.datasets.transforms as T
26
  from GroundingDINO.groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap
 
 
 
27
 
 
 
 
 
 
 
 
 
28
 
29
 
30
  # GroundingDINO config and checkpoint
@@ -336,10 +347,8 @@ def run_local(base_image, base_mask, reference_image, ref_mask, seed, base_mask_
336
  edited_image = crop_back(edited_image, old_tar_image, np.array([H1, W1, H2, W2]), np.array(tar_box_yyxx_crop))
337
  edited_image = Image.fromarray(edited_image)
338
 
339
- if ref_mask_option != "Label to Mask":
340
- return [show_diptych_ref_tar, edited_image]
341
- else:
342
- return [return_ref_mask, show_diptych_ref_tar, edited_image]
343
 
344
  def update_ui(option):
345
  if option == "Draw Mask":
 
24
  import spaces
25
  import GroundingDINO.groundingdino.datasets.transforms as T
26
  from GroundingDINO.groundingdino.util.utils import clean_state_dict, get_phrases_from_posmap
27
+ import base64
28
+ from io import BytesIO
29
+ from PIL import Image
30
 
31
+ def image_to_base64(image: Image.Image, format="PNG") -> str:
32
+ """Convert a PIL Image to a base64 string."""
33
+ buffer = BytesIO()
34
+ image.save(buffer, format=format)
35
+ buffer.seek(0)
36
+ img_bytes = buffer.read()
37
+ base64_str = base64.b64encode(img_bytes).decode("utf-8")
38
+ return base64_str
39
 
40
 
41
  # GroundingDINO config and checkpoint
 
347
  edited_image = crop_back(edited_image, old_tar_image, np.array([H1, W1, H2, W2]), np.array(tar_box_yyxx_crop))
348
  edited_image = Image.fromarray(edited_image)
349
 
350
+
351
+ return image_to_base64(edited_image)
 
 
352
 
353
  def update_ui(option):
354
  if option == "Draw Mask":