codic commited on
Commit
d2a3d69
·
verified ·
1 Parent(s): f32eb65

trying cut the main subject from the bg --- before was working

Browse files
Files changed (1) hide show
  1. app.py +16 -12
app.py CHANGED
@@ -64,30 +64,35 @@ def process_image(image_path, depth_map_path=None):
64
  depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
65
  mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1)
66
 
67
- # Improved edge detection for the main subject
68
- edges = cv2.Canny(img_as_ubyte(mask), threshold1=40, threshold2=140)
69
- edges_dilated = cv2.dilate(edges, np.ones((3, 3), np.uint8), iterations=3)
70
- edges_eroded = cv2.erode(edges_dilated, np.ones((3, 3), np.uint8), iterations=4)
71
 
72
- # Use the refined edge mask to clean up the depth map
73
- depth_subject_only = np.where(edges_eroded == 0, depth_image * mask, 0)
74
- depth_subject_only[~mask] = 0 # Ensure background areas are zeroed out
 
 
 
 
 
 
75
 
76
  # Generate the final depth image with reduced blur artifacts
77
  depth_display = img_as_ubyte(depth_subject_only)
78
 
79
  try:
80
  # Generate the 3D model
81
- gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, 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), depth_subject_only, image_path, depth=8)
87
  img = Image.fromarray(depth_display)
88
  return [img, gltf_path, gltf_path]
89
-
90
- def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.15):
91
  """
92
  Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
93
 
@@ -169,7 +174,6 @@ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0
169
  mesh_crop.compute_vertex_normals()
170
  uvs = np.array([[v[0] / w, 1 - v[1] / h] for v in mesh_crop.vertices])
171
  mesh_crop.triangle_uvs = o3d.utility.Vector2dVector(np.repeat(uvs, 3, axis=0))
172
-
173
  # Save texture image
174
  texture_path = f"./{image_path.stem}_texture.png"
175
  Image.fromarray(foreground).save(texture_path)
 
64
  depth_image = cv2.resize(depth_image, (foreground.size[0], foreground.size[1]), interpolation=cv2.INTER_LANCZOS4)
65
  mask = np.all(np.array(foreground) != [0, 0, 0], axis=-1)
66
 
67
+ # Create an edge mask to detect the edges of the main subject
68
+ edges = cv2.Canny(img_as_ubyte(mask), threshold1=150, threshold2=250)
69
+ edges_dilated = cv2.dilate(edges, np.ones((5, 5), np.uint8), iterations=2)
 
70
 
71
+ # Erode the depth map more precisely around edges to remove any background extensions
72
+ kernel = np.ones((7, 7), np.uint8)
73
+ depth_eroded = cv2.erode(depth_image * mask, kernel, iterations=3)
74
+
75
+ # Apply the edge-aware mask to limit depth to the main subject only
76
+ depth_subject_only = np.where(edges_dilated == 0, depth_eroded, 0)
77
+
78
+ # Set depth to zero in the background and around subject edges
79
+ depth_subject_only[~mask] = 0
80
 
81
  # Generate the final depth image with reduced blur artifacts
82
  depth_display = img_as_ubyte(depth_subject_only)
83
 
84
  try:
85
  # Generate the 3D model
86
+ gltf_path = create_3d_obj(np.array(foreground), depth_subject_only, image_path, edge_strength=0.25)
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(np.array(foreground), depth_subject_only, image_path, depth=8, edge_strength=0.25)
92
  img = Image.fromarray(depth_display)
93
  return [img, gltf_path, gltf_path]
94
+
95
+ def create_3d_obj(foreground, depth_image, image_path, depth=12, edge_strength=0.25):
96
  """
97
  Generates a 3D model by combining an image and a masked depth map focused only on the main subject.
98
 
 
174
  mesh_crop.compute_vertex_normals()
175
  uvs = np.array([[v[0] / w, 1 - v[1] / h] for v in mesh_crop.vertices])
176
  mesh_crop.triangle_uvs = o3d.utility.Vector2dVector(np.repeat(uvs, 3, axis=0))
 
177
  # Save texture image
178
  texture_path = f"./{image_path.stem}_texture.png"
179
  Image.fromarray(foreground).save(texture_path)