codic commited on
Commit
05d3a28
·
verified ·
1 Parent(s): 502daac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -15,6 +15,7 @@ from rembg import remove
15
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
16
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
17
 
 
18
  def process_image(image_path, depth_map_path=None):
19
  image_path = Path(image_path)
20
  image_raw = Image.open(image_path).convert("RGB")
@@ -23,7 +24,7 @@ def process_image(image_path, depth_map_path=None):
23
  Image.Resampling.LANCZOS,
24
  )
25
 
26
- # Remove background using rembg
27
  foreground = remove(image_raw)
28
  foreground = Image.fromarray(np.array(foreground)).convert("RGB")
29
 
@@ -38,7 +39,6 @@ def process_image(image_path, depth_map_path=None):
38
  depth_image = np.array(depth_image, dtype=np.float32)
39
 
40
  # Normalize depth image to [0, 1] range
41
- depth_image = depth_image.astype(np.float32)
42
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
43
  depth_image = np.clip(depth_image, 0, 1)
44
  else:
@@ -61,29 +61,34 @@ def process_image(image_path, depth_map_path=None):
61
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
62
  depth_image = np.clip(depth_image, 0, 1)
63
 
64
- # Resize and apply edge-preserving blur
65
- depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_LANCZOS4)
66
  mask_blurred = cv2.bilateralFilter(depth_image, 5, 50, 50)
67
 
68
- # Blend depth_image with mask_blurred for smoother transition
69
- blended_depth_image = cv2.addWeighted(depth_image, 0.9, mask_blurred, 0.05, 0)
 
 
 
 
70
 
71
- # Normalize to [0, 1] before converting to uint8 for display
72
- blended_depth_image = np.clip(blended_depth_image, 0, 1)
73
  depth_display = img_as_ubyte(blended_depth_image)
74
 
75
- # Inpaint background where the main subject was removed
76
  background = remove(image_raw, only_mask=True)
77
  background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
78
  background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=2, flags=cv2.INPAINT_TELEA)
79
 
80
  try:
81
- gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path)
 
82
  img = Image.fromarray(depth_display)
83
  return [img, gltf_path, gltf_path]
84
  except Exception as e:
85
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
86
- gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path, depth=8)
87
  img = Image.fromarray(depth_display)
88
  return [img, gltf_path, gltf_path]
89
 
@@ -160,7 +165,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=12):
160
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
161
 
162
  return gltf_path
163
-
164
  # Gradio Interface
165
  title = "Depth Estimation & 3D Reconstruction Demo"
166
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."
 
15
  feature_extractor = DPTFeatureExtractor.from_pretrained("Intel/dpt-large")
16
  model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
17
 
18
+
19
  def process_image(image_path, depth_map_path=None):
20
  image_path = Path(image_path)
21
  image_raw = Image.open(image_path).convert("RGB")
 
24
  Image.Resampling.LANCZOS,
25
  )
26
 
27
+ # Remove background using rembg to isolate main subject
28
  foreground = remove(image_raw)
29
  foreground = Image.fromarray(np.array(foreground)).convert("RGB")
30
 
 
39
  depth_image = np.array(depth_image, dtype=np.float32)
40
 
41
  # Normalize depth image to [0, 1] range
 
42
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
43
  depth_image = np.clip(depth_image, 0, 1)
44
  else:
 
61
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image) + 1e-3)
62
  depth_image = np.clip(depth_image, 0, 1)
63
 
64
+ # Resize and apply edge-preserving blur on depth map for main subject only
65
+ depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
66
  mask_blurred = cv2.bilateralFilter(depth_image, 5, 50, 50)
67
 
68
+ # Apply depth only to main subject
69
+ foreground_array = np.array(foreground)
70
+ mask = np.all(foreground_array != [0, 0, 0], axis=-1) # Mask to isolate main subject
71
+
72
+ depth_image_subject = depth_image * mask # Only main subject has depth, background is flat
73
+ depth_image_subject = np.clip(depth_image_subject, 0, 1)
74
 
75
+ # Smooth transitions along edges
76
+ blended_depth_image = cv2.addWeighted(depth_image_subject, 0.9, mask_blurred * mask, 0.05, 0)
77
  depth_display = img_as_ubyte(blended_depth_image)
78
 
79
+ # Keep background flat and at original size with inpainting
80
  background = remove(image_raw, only_mask=True)
81
  background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
82
  background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=2, flags=cv2.INPAINT_TELEA)
83
 
84
  try:
85
+ # Pass updated depth and background to 3D model creation function
86
+ gltf_path = create_3d_obj(foreground_array, blended_depth_image, background_inpainted, image_path)
87
  img = Image.fromarray(depth_display)
88
  return [img, gltf_path, gltf_path]
89
  except Exception as e:
90
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
91
+ gltf_path = create_3d_obj(foreground_array, blended_depth_image, background_inpainted, image_path, depth=8)
92
  img = Image.fromarray(depth_display)
93
  return [img, gltf_path, gltf_path]
94
 
 
165
  o3d.io.write_triangle_mesh(gltf_path, mesh_crop, write_triangle_uvs=True)
166
 
167
  return gltf_path
168
+
169
  # Gradio Interface
170
  title = "Depth Estimation & 3D Reconstruction Demo"
171
  description = "Upload an image and optionally a depth map (in .npy or image format) to generate a 3D model. If no depth map is provided, the DPT model will generate it."