codic commited on
Commit
353bd0d
·
verified ·
1 Parent(s): 2bb519d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -65,7 +65,7 @@ def process_image(image_path, depth_map_path=None):
65
 
66
  # Resize and apply lighter Gaussian smoothing for smoother transitions
67
  depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
68
- mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur for transition
69
 
70
  # Ensure matching size and single channel for blending
71
  if len(depth_image.shape) == 3:
@@ -76,22 +76,26 @@ def process_image(image_path, depth_map_path=None):
76
  mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
77
 
78
  # Blend depth_image with mask_blurred_resized for smoother transition
79
- blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
80
 
81
  # Clear border for a smooth edge around main subject
82
  blended_depth_image = clear_border(blended_depth_image)
83
 
84
  try:
85
- gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path)
 
86
  img = Image.fromarray(blended_depth_image)
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
  )
@@ -116,7 +120,7 @@ def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
116
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
117
 
118
  if not pcd.has_normals():
119
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.25, max_nn=50))
120
 
121
  if pcd.has_normals():
122
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
@@ -164,4 +168,4 @@ iface = gr.Interface(
164
  allow_flagging="never",
165
  cache_examples=False,
166
  )
167
- iface.launch(debug=True, show_api=True, share=True)
 
65
 
66
  # Resize and apply lighter Gaussian smoothing for smoother transitions
67
  depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
68
+ mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.9) # Lighter blur for transition
69
 
70
  # Ensure matching size and single channel for blending
71
  if len(depth_image.shape) == 3:
 
76
  mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
77
 
78
  # Blend depth_image with mask_blurred_resized for smoother transition
79
+ blended_depth_image = cv2.addWeighted(depth_image, 0.9, mask_blurred_resized, 0.1, 0)
80
 
81
  # Clear border for a smooth edge around main subject
82
  blended_depth_image = clear_border(blended_depth_image)
83
 
84
  try:
85
+ # Initial call with depth set to 8 for safe resolution
86
+ gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=8)
87
  img = Image.fromarray(blended_depth_image)
88
  return [img, gltf_path, gltf_path]
89
  except Exception as e:
90
+ print(f"Error with depth=8: {str(e)}. Retrying with depth=14.")
91
  gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=14)
92
  img = Image.fromarray(blended_depth_image)
93
  return [img, gltf_path, gltf_path]
94
 
95
  def create_3d_obj(foreground, depth_image, background, image_path, depth=14):
96
+ # Ensure depth is at least 2 for Open3D compatibility
97
+ depth = max(depth, 2)
98
+
99
  depth_image_resized = np.array(
100
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
101
  )
 
120
  raise RuntimeError("Insufficient points in the point cloud for normals estimation.")
121
 
122
  if not pcd.has_normals():
123
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=50))
124
 
125
  if pcd.has_normals():
126
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
 
168
  allow_flagging="never",
169
  cache_examples=False,
170
  )
171
+ iface.launch(debug=True, show_api=True, share=True)