codic commited on
Commit
2bb519d
·
verified ·
1 Parent(s): 39c2c70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -26
app.py CHANGED
@@ -11,30 +11,10 @@ from pathlib import Path
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,8 +24,9 @@ def process_image(image_path, depth_map_path=None):
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:
@@ -106,11 +87,11 @@ def process_image(image_path, depth_map_path=None):
106
  return [img, gltf_path, gltf_path]
107
  except Exception as e:
108
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
109
- gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=9)
110
  img = Image.fromarray(blended_depth_image)
111
  return [img, gltf_path, gltf_path]
112
 
113
- def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
114
  depth_image_resized = np.array(
115
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
116
  )
@@ -143,11 +124,11 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
143
  raise RuntimeError("Failed to estimate normals for the point cloud.")
144
 
145
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
146
- pcd, depth=depth, width=0, scale=1.2, linear_fit=True
147
  )
148
 
149
  # Flatten the back of the model from the zero point
150
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 400
151
  mesh = mesh_raw.simplify_vertex_clustering(
152
  voxel_size=voxel_size,
153
  contraction=o3d.geometry.SimplificationContraction.Average,
 
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
  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:
 
87
  return [img, gltf_path, gltf_path]
88
  except Exception as e:
89
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
90
+ gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=14)
91
  img = Image.fromarray(blended_depth_image)
92
  return [img, gltf_path, gltf_path]
93
 
94
+ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
95
  depth_image_resized = np.array(
96
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
97
  )
 
124
  raise RuntimeError("Failed to estimate normals for the point cloud.")
125
 
126
  mesh_raw, densities = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(
127
+ pcd, depth=depth, width=1, scale=2, linear_fit=True
128
  )
129
 
130
  # Flatten the back of the model from the zero point
131
+ voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 512
132
  mesh = mesh_raw.simplify_vertex_clustering(
133
  voxel_size=voxel_size,
134
  contraction=o3d.geometry.SimplificationContraction.Average,