codic commited on
Commit
f4df5b1
·
verified ·
1 Parent(s): c493dc7

expected an indented block after 'if' statement on line 38

Browse files
Files changed (1) hide show
  1. app.py +32 -17
app.py CHANGED
@@ -26,18 +26,23 @@ def process_image(image_path, depth_map_path=None):
26
 
27
  # Remove background using rembg
28
  foreground = remove(image_raw)
29
- foreground_np = np.array(foreground)
30
-
31
- # Convert to grayscale and clear borders on mask
32
- mask = np.array(foreground_np[:, :, 3] > 0).astype(np.uint8) * 255
33
- mask = clear_border(mask)
34
-
35
- # Create foreground with clear borders
36
- foreground = Image.fromarray(cv2.bitwise_and(np.array(foreground.convert("RGB")), np.array(foreground.convert("RGB")), mask=mask))
37
 
 
38
  if depth_map_path:
39
- # Load and normalize user-provided depth map
40
- # ... existing code for handling depth_map_path
 
 
 
 
 
 
 
 
 
 
 
41
  else:
42
  # Generate depth map using DPT model
43
  encoding = feature_extractor(foreground, return_tensors="pt")
@@ -53,19 +58,29 @@ def process_image(image_path, depth_map_path=None):
53
  ).squeeze()
54
  depth_image = prediction.cpu().numpy()
55
 
56
- # Normalize depth image to [0, 1] range
57
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
58
  depth_image = np.clip(depth_image, 0, 1)
59
  depth_image = img_as_ubyte(depth_image)
60
 
61
- # Resize and smooth depth transitions at the subject's edges
62
  depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_CUBIC)
63
- depth_image = cv2.bitwise_and(depth_image, depth_image, mask=mask) # Keep depth only for the main subject
64
- mask_blurred = cv2.GaussianBlur(depth_image, (3, 3), 0.8)
 
 
 
 
 
 
 
 
 
 
65
 
66
- blended_depth_image = cv2.addWeighted(depth_image, 0.8, mask_blurred, 0.2, 0)
 
67
 
68
- # Attempt 3D reconstruction with updated depth
69
  try:
70
  gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path)
71
  img = Image.fromarray(blended_depth_image)
@@ -75,7 +90,7 @@ def process_image(image_path, depth_map_path=None):
75
  gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=9)
76
  img = Image.fromarray(blended_depth_image)
77
  return [img, gltf_path, gltf_path]
78
-
79
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
80
  depth_image_resized = np.array(
81
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
 
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:
33
+ if depth_map_path.endswith('.npy'):
34
+ depth_image = np.load(depth_map_path)
35
+ # Invert depth only for npy files
36
+ depth_image = 1 - depth_image
37
+ else:
38
+ depth_image_raw = Image.open(depth_map_path).convert("L")
39
+ depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
40
+ depth_image = np.array(depth_image)
41
+
42
+ # Normalize depth image to [0, 1] range
43
+ depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
44
+ depth_image = np.clip(depth_image, 0, 1)
45
+ depth_image = img_as_ubyte(depth_image)
46
  else:
47
  # Generate depth map using DPT model
48
  encoding = feature_extractor(foreground, return_tensors="pt")
 
58
  ).squeeze()
59
  depth_image = prediction.cpu().numpy()
60
 
61
+ # Normalize depth image to [0, 1] range before converting
62
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
63
  depth_image = np.clip(depth_image, 0, 1)
64
  depth_image = img_as_ubyte(depth_image)
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:
72
+ depth_image = cv2.cvtColor(depth_image, cv2.COLOR_BGR2GRAY)
73
+ if len(mask_blurred.shape) == 3:
74
+ mask_blurred = cv2.cvtColor(mask_blurred, cv2.COLOR_BGR2GRAY)
75
+
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)
 
90
  gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, None, image_path, depth=9)
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=10):
95
  depth_image_resized = np.array(
96
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)