codic commited on
Commit
c455f34
·
verified ·
1 Parent(s): f4df5b1

making the dpth better by applying remove any thing -first deploy

Browse files

the deploy before this was working very will but the cut on the borders was bad

Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -11,10 +11,30 @@ from pathlib import Path
11
  import os
12
  import cv2
13
  from rembg import remove
 
 
 
14
 
15
  # Initialize model and feature extractor for depth estimation
16
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
17
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  def process_image(image_path, depth_map_path=None):
20
  image_path = Path(image_path)
@@ -24,9 +44,8 @@ def process_image(image_path, depth_map_path=None):
24
  Image.Resampling.LANCZOS,
25
  )
26
 
27
- # Remove background using rembg
28
- foreground = remove(image_raw)
29
- foreground = Image.fromarray(np.array(foreground)).convert("RGB")
30
 
31
  # Check if user-provided depth map is available
32
  if depth_map_path:
@@ -164,4 +183,4 @@ iface = gr.Interface(
164
  allow_flagging="never",
165
  cache_examples=False,
166
  )
167
- iface.launch(debug=True, show_api=True, share=True)
 
11
  import os
12
  import cv2
13
  from rembg import remove
14
+ from gradio_client import Client, handle_file
15
+ import requests
16
+ from io import BytesIO
17
 
18
  # Initialize model and feature extractor for depth estimation
19
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
20
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
21
+ background_removal_client = Client("not-lain/background-removal")
22
+
23
+ def remove_background_with_api(image):
24
+ # Step 1: Use the `not-lain/background-removal` API
25
+ result_url = background_removal_client.predict(
26
+ f=handle_file(image),
27
+ api_name="/png"
28
+ )
29
+
30
+ # Step 2: Download the image returned by the API
31
+ response = requests.get(result_url)
32
+ foreground = Image.open(BytesIO(response.content)).convert("RGBA")
33
+
34
+ # Step 3: Apply `rmbg` for additional cleanup
35
+ cleaned_foreground = remove(foreground)
36
+
37
+ return Image.fromarray(np.array(cleaned_foreground)).convert("RGB")
38
 
39
  def process_image(image_path, depth_map_path=None):
40
  image_path = Path(image_path)
 
44
  Image.Resampling.LANCZOS,
45
  )
46
 
47
+ # Remove background using the API + rmbg combination
48
+ foreground = remove_background_with_api(image_path)
 
49
 
50
  # Check if user-provided depth map is available
51
  if depth_map_path:
 
183
  allow_flagging="never",
184
  cache_examples=False,
185
  )
186
+ iface.launch(debug=True, show_api=True, share=True)