codic commited on
Commit
7086037
·
verified ·
1 Parent(s): 2ae1333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -24,25 +24,27 @@ def process_image(image_path, depth_map_path=None):
24
  Image.Resampling.LANCZOS,
25
  )
26
 
27
- # Remove background using rembg and apply clear_border
28
  foreground = remove(image_raw)
29
- foreground = clear_border(np.array(foreground))
30
- foreground = Image.fromarray(img_as_ubyte(foreground)).convert("RGB")
31
 
32
  # Check if user-provided depth map is available
33
  if depth_map_path:
34
  if depth_map_path.endswith('.npy'):
35
  depth_image = np.load(depth_map_path)
 
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 and clip
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
  else:
 
46
  encoding = feature_extractor(foreground, return_tensors="pt")
47
  with torch.no_grad():
48
  outputs = model(**encoding)
@@ -54,40 +56,44 @@ def process_image(image_path, depth_map_path=None):
54
  mode="bicubic",
55
  align_corners=False,
56
  ).squeeze()
57
-
58
- # Normalize prediction to [0, 1] range and clip
59
  depth_image = prediction.cpu().numpy()
60
- depth_image = depth_image / np.max(depth_image)
 
 
61
  depth_image = np.clip(depth_image, 0, 1)
 
62
 
63
- # Convert to 8-bit unsigned integer after normalizing
64
- depth_image = img_as_ubyte(depth_image)
 
65
 
66
- # Apply Gaussian smoothing to depth map
67
- depth_image = gaussian(depth_image, sigma=0.8, preserve_range=True)
68
- depth_image = img_as_ubyte(depth_image)
 
 
 
 
69
 
70
- # Inpaint background where the main subject was removed
 
 
 
71
  background = remove(image_raw, only_mask=True)
72
- background = clear_border(np.array(background))
73
- background_inpainted = cv2.inpaint(
74
- np.array(image_raw),
75
- background[:, :, 0],
76
- inpaintRadius=3,
77
- flags=cv2.INPAINT_TELEA
78
- )
79
 
80
- # Call create_3d_obj and return processed images and 3D model
81
  try:
82
- gltf_path = create_3d_obj(np.array(foreground), depth_image, background_inpainted, image_path)
83
- img = Image.fromarray(depth_image)
84
  return [img, gltf_path, gltf_path]
85
  except Exception as e:
86
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
87
- gltf_path = create_3d_obj(np.array(foreground), depth_image, background_inpainted, image_path, depth=9)
88
- img = Image.fromarray(depth_image)
89
  return [img, gltf_path, gltf_path]
90
 
 
91
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
92
  depth_image_resized = np.array(
93
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)
 
24
  Image.Resampling.LANCZOS,
25
  )
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")
49
  with torch.no_grad():
50
  outputs = model(**encoding)
 
56
  mode="bicubic",
57
  align_corners=False,
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
+ # Inpaint background where main subject was removed
82
  background = remove(image_raw, only_mask=True)
83
+ background = cv2.cvtColor(np.array(background), cv2.COLOR_GRAY2BGR)
84
+ background_inpainted = cv2.inpaint(np.array(image_raw), background[:, :, 0], inpaintRadius=3, flags=cv2.INPAINT_TELEA)
 
 
 
 
 
85
 
 
86
  try:
87
+ gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path)
88
+ img = Image.fromarray(blended_depth_image)
89
  return [img, gltf_path, gltf_path]
90
  except Exception as e:
91
  print(f"Error with default depth: {str(e)}. Retrying with a shallower depth.")
92
+ gltf_path = create_3d_obj(np.array(foreground), blended_depth_image, background_inpainted, image_path, depth=9)
93
+ img = Image.fromarray(blended_depth_image)
94
  return [img, gltf_path, gltf_path]
95
 
96
+
97
  def create_3d_obj(foreground, depth_image, background, image_path, depth=10):
98
  depth_image_resized = np.array(
99
  Image.fromarray(depth_image).resize(foreground.shape[:2][::-1], Image.LANCZOS)