codic commited on
Commit
504ab41
·
verified ·
1 Parent(s): f4b5fa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -25,6 +25,10 @@ def process_image(image_path, depth_map_path=None):
25
  foreground = remove(image_raw)
26
  foreground = Image.fromarray(np.array(foreground)).convert("RGB")
27
 
 
 
 
 
28
  # Check if user-provided depth map is available
29
  if depth_map_path:
30
  if depth_map_path.endswith('.npy'):
@@ -52,9 +56,9 @@ def process_image(image_path, depth_map_path=None):
52
  ).squeeze()
53
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
54
 
55
- # Resize and apply lighter Gaussian smoothing
56
  depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
57
- mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8) # Lighter blur for more detail
58
 
59
  # Ensure matching size and single channel for blending
60
  if len(depth_image.shape) == 3:
@@ -63,23 +67,20 @@ def process_image(image_path, depth_map_path=None):
63
  if len(mask_blurred.shape) == 3:
64
  mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
65
 
66
- # Resize mask_blurred to match depth_image dimensions
67
  mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
68
-
69
- # Blend depth_image with mask_blurred_resized
70
- blended_depth_image = cv2.addWeighted(depth_image, 0.9, mask_blurred_resized, 0.1, 0)
71
 
72
  try:
73
- gltf_path = create_3d_obj(np.array(image), blended_depth_image, image_path)
74
  img = Image.fromarray(blended_depth_image)
75
  return [img, gltf_path, gltf_path]
76
  except Exception:
77
  print("Error with default depth. Retrying with a shallower depth.")
78
- gltf_path = create_3d_obj(np.array(image), blended_depth_image, image_path, depth=14)
79
  img = Image.fromarray(blended_depth_image)
80
  return [img, gltf_path, gltf_path]
81
 
82
- def create_3d_obj(rgb_image, depth_image, image_path, depth=14):
83
  # Resize and normalize the depth image
84
  depth_image_resized = np.array(
85
  Image.fromarray(depth_image).resize(rgb_image.shape[:2][::-1], Image.LANCZOS)
@@ -111,7 +112,7 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=14):
111
  # Estimate and orient normals
112
  if not pcd.has_normals():
113
  print("Estimating normals with increased search radius for sparse data...")
114
- pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=60))
115
 
116
  if pcd.has_normals():
117
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
@@ -124,12 +125,12 @@ def create_3d_obj(rgb_image, depth_image, image_path, depth=14):
124
  )
125
 
126
  # Mesh simplification and reduced smoothing for better detail
127
- voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 812 # Decreased voxel size for finer detail
128
  mesh = mesh_raw.simplify_vertex_clustering(
129
  voxel_size=voxel_size,
130
  contraction=o3d.geometry.SimplificationContraction.Average,
131
  )
132
- mesh = mesh.filter_smooth_simple(number_of_iterations=4) # Reduced smoothing iterations for sharpness
133
  bbox = pcd.get_axis_aligned_bounding_box()
134
  mesh_crop = mesh.crop(bbox)
135
 
 
25
  foreground = remove(image_raw)
26
  foreground = Image.fromarray(np.array(foreground)).convert("RGB")
27
 
28
+ # Duplicate the background layer
29
+ bg_copy = image_raw.copy()
30
+ image_combined = Image.alpha_composite(bg_copy, foreground)
31
+
32
  # Check if user-provided depth map is available
33
  if depth_map_path:
34
  if depth_map_path.endswith('.npy'):
 
56
  ).squeeze()
57
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
58
 
59
+ # Resize and apply light Gaussian smoothing
60
  depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
61
+ mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8)
62
 
63
  # Ensure matching size and single channel for blending
64
  if len(depth_image.shape) == 3:
 
67
  if len(mask_blurred.shape) == 3:
68
  mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
69
 
 
70
  mask_blurred_resized = cv2.resize(mask_blurred, (depth_image.shape[1], depth_image.shape[0]), interpolation=cv2.INTER_LINEAR)
71
+ blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred_resized, 0.2, 0)
 
 
72
 
73
  try:
74
+ gltf_path = create_3d_obj(np.array(image_combined), blended_depth_image, image_path)
75
  img = Image.fromarray(blended_depth_image)
76
  return [img, gltf_path, gltf_path]
77
  except Exception:
78
  print("Error with default depth. Retrying with a shallower depth.")
79
+ gltf_path = create_3d_obj(np.array(image_combined), blended_depth_image, image_path, depth=12)
80
  img = Image.fromarray(blended_depth_image)
81
  return [img, gltf_path, gltf_path]
82
 
83
+ def create_3d_obj(rgb_image, depth_image, image_path, depth=12):
84
  # Resize and normalize the depth image
85
  depth_image_resized = np.array(
86
  Image.fromarray(depth_image).resize(rgb_image.shape[:2][::-1], Image.LANCZOS)
 
112
  # Estimate and orient normals
113
  if not pcd.has_normals():
114
  print("Estimating normals with increased search radius for sparse data...")
115
+ pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.15, max_nn=40))
116
 
117
  if pcd.has_normals():
118
  pcd.orient_normals_towards_camera_location(camera_location=np.array([0.0, 0.0, 1500.0]))
 
125
  )
126
 
127
  # Mesh simplification and reduced smoothing for better detail
128
+ voxel_size = max(mesh_raw.get_max_bound() - mesh_raw.get_min_bound()) / 612
129
  mesh = mesh_raw.simplify_vertex_clustering(
130
  voxel_size=voxel_size,
131
  contraction=o3d.geometry.SimplificationContraction.Average,
132
  )
133
+ mesh = mesh.filter_smooth_simple(number_of_iterations=2)
134
  bbox = pcd.get_axis_aligned_bounding_box()
135
  mesh_crop = mesh.crop(bbox)
136