codic commited on
Commit
0d535a9
·
verified ·
1 Parent(s): c4a9fb1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -21,10 +21,6 @@ def process_image(image_path, depth_map_path=None):
21
  Image.Resampling.LANCZOS,
22
  )
23
 
24
- # Remove background using rembg
25
- foreground = remove(image_raw) # Remove background
26
- foreground = Image.fromarray(np.array(foreground)) # Convert back to PIL Image
27
-
28
  # Check if user-provided depth map is available
29
  if depth_map_path:
30
  depth_image_raw = Image.open(depth_map_path)
@@ -34,10 +30,9 @@ def process_image(image_path, depth_map_path=None):
34
  # Normalize depth image to match expected format
35
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
36
  depth_image = (depth_image * 255).astype('uint8')
37
-
38
  else:
39
  # Generate depth map using DPT model
40
- encoding = feature_extractor(foreground, return_tensors="pt")
41
  with torch.no_grad():
42
  outputs = model(**encoding)
43
  predicted_depth = outputs.predicted_depth
@@ -50,17 +45,36 @@ def process_image(image_path, depth_map_path=None):
50
  ).squeeze()
51
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
52
 
53
- # Step 1: Apply Gaussian smoothing on the depth map
54
- smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
55
-
56
  try:
57
- gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path)
58
- img = Image.fromarray(smoothed_depth_map)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  return [img, gltf_path, gltf_path]
60
  except Exception as e:
61
  print("Error with default depth. Retrying with a shallower depth.")
62
- gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path, depth=8)
63
- img = Image.fromarray(smoothed_depth_map)
64
  return [img, gltf_path, gltf_path]
65
  except:
66
  print("Error reconstructing 3D model")
 
21
  Image.Resampling.LANCZOS,
22
  )
23
 
 
 
 
 
24
  # Check if user-provided depth map is available
25
  if depth_map_path:
26
  depth_image_raw = Image.open(depth_map_path)
 
30
  # Normalize depth image to match expected format
31
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
32
  depth_image = (depth_image * 255).astype('uint8')
 
33
  else:
34
  # Generate depth map using DPT model
35
+ encoding = feature_extractor(image, return_tensors="pt")
36
  with torch.no_grad():
37
  outputs = model(**encoding)
38
  predicted_depth = outputs.predicted_depth
 
45
  ).squeeze()
46
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
47
 
 
 
 
48
  try:
49
+ # Handle the foreground image and ensure correct format
50
+ foreground = remove(image_raw)
51
+ foreground = Image.fromarray(np.array(foreground))
52
+ # Convert to RGB if it's not already in that mode
53
+ if foreground.mode != 'RGB':
54
+ foreground = foreground.convert('RGB')
55
+
56
+ # Pass the foreground to the depth estimator
57
+ encoding = feature_extractor(foreground, return_tensors="pt")
58
+ with torch.no_grad():
59
+ outputs = model(**encoding)
60
+ predicted_depth = outputs.predicted_depth
61
+
62
+ # Now follow through with the rest of the function as before
63
+ prediction = torch.nn.functional.interpolate(
64
+ predicted_depth.unsqueeze(1),
65
+ size=image.size[::-1],
66
+ mode="bicubic",
67
+ align_corners=False,
68
+ ).squeeze()
69
+ depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
70
+
71
+ gltf_path = create_3d_obj(np.array(image), depth_image, image_path)
72
+ img = Image.fromarray(depth_image)
73
  return [img, gltf_path, gltf_path]
74
  except Exception as e:
75
  print("Error with default depth. Retrying with a shallower depth.")
76
+ gltf_path = create_3d_obj(np.array(image), depth_image, image_path, depth=8)
77
+ img = Image.fromarray(depth_image)
78
  return [img, gltf_path, gltf_path]
79
  except:
80
  print("Error reconstructing 3D model")