codic commited on
Commit
b74446e
·
verified ·
1 Parent(s): 38f7a16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -14
app.py CHANGED
@@ -27,27 +27,21 @@ def remove_background(image):
27
  image.save(img_bytes, format="PNG")
28
  img_bytes.seek(0)
29
 
30
- # Step 1: Convert the image to base64
31
- base64_image = base64.b64encode(img_bytes.getvalue()).decode('utf-8')
32
-
33
- # Step 2: Remove background using the Gradio API
34
- result = background_removal_client.predict(
35
- base64_image, # Send base64 image
36
  api_name="/png"
37
  )
38
 
39
- # If the API returns a base64 string, decode it back to an image
40
- if isinstance(result, str) and result.startswith('data:image/png;base64,'):
41
- result_data = result.split(',')[1] # Extract base64 data
42
- foreground = Image.open(BytesIO(base64.b64decode(result_data))).convert("RGBA")
43
- else:
44
- # Handle unexpected result format (you may want to raise an error or log)
45
- raise ValueError("Unexpected result format from API")
46
 
47
  # Step 3: Apply `rmbg` for additional cleanup
48
  cleaned_foreground = remove(foreground)
49
 
50
- return cleaned_foreground.convert("RGB") # Directly return cleaned foreground
 
51
  def process_image(image_path, depth_map_path=None):
52
  image_path = Path(image_path)
53
  image_raw = Image.open(image_path).convert("RGB")
 
27
  image.save(img_bytes, format="PNG")
28
  img_bytes.seek(0)
29
 
30
+ # Step 1: Remove background using the Gradio API
31
+ result_url = background_removal_client.predict(
32
+ f=img_bytes, # Pass the BytesIO object directly
 
 
 
33
  api_name="/png"
34
  )
35
 
36
+ # Step 2: Download the image returned by the API
37
+ response = requests.get(result_url)
38
+ foreground = Image.open(BytesIO(response.content)).convert("RGBA")
 
 
 
 
39
 
40
  # Step 3: Apply `rmbg` for additional cleanup
41
  cleaned_foreground = remove(foreground)
42
 
43
+ return cleaned_foreground.convert("RGB") # Directly return cleaned foregroun
44
+
45
  def process_image(image_path, depth_map_path=None):
46
  image_path = Path(image_path)
47
  image_raw = Image.open(image_path).convert("RGB")