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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -29
app.py CHANGED
@@ -15,51 +15,34 @@ model = DPTForDepthEstimation.from_pretrained("Intel/dpt-large")
15
 
16
  def process_image(image_path, depth_map_path=None):
17
  image_path = Path(image_path)
18
- image_raw = Image.open(image_path)
19
  image = image_raw.resize(
20
  (1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
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)
27
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
28
  depth_image = np.array(depth_image)
29
 
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
39
-
40
- prediction = torch.nn.functional.interpolate(
41
- predicted_depth.unsqueeze(1),
42
- size=image.size[::-1],
43
- mode="bicubic",
44
- align_corners=False,
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],
@@ -68,13 +51,17 @@ def process_image(image_path, depth_map_path=None):
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")
 
15
 
16
  def process_image(image_path, depth_map_path=None):
17
  image_path = Path(image_path)
18
+ image_raw = Image.open(image_path).convert("RGB") # Ensure the image is in RGB format
19
  image = image_raw.resize(
20
  (1600, int(1600 * image_raw.size[1] / image_raw.size[0])),
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
+ foreground = foreground.convert("RGB") # Ensure the foreground is in RGB format
28
+
29
  # Check if user-provided depth map is available
30
  if depth_map_path:
31
+ depth_image_raw = Image.open(depth_map_path).convert("L") # Convert to grayscale
32
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
33
  depth_image = np.array(depth_image)
34
 
35
  # Normalize depth image to match expected format
36
  depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
37
  depth_image = (depth_image * 255).astype('uint8')
38
+
39
  else:
40
  # Generate depth map using DPT model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  encoding = feature_extractor(foreground, return_tensors="pt")
42
  with torch.no_grad():
43
  outputs = model(**encoding)
44
  predicted_depth = outputs.predicted_depth
45
 
 
46
  prediction = torch.nn.functional.interpolate(
47
  predicted_depth.unsqueeze(1),
48
  size=image.size[::-1],
 
51
  ).squeeze()
52
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
53
 
54
+ # Step 1: Apply Gaussian smoothing on the depth map
55
+ smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
56
+
57
+ try:
58
+ gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path)
59
+ img = Image.fromarray(smoothed_depth_map)
60
  return [img, gltf_path, gltf_path]
61
  except Exception as e:
62
  print("Error with default depth. Retrying with a shallower depth.")
63
+ gltf_path = create_3d_obj(np.array(image), smoothed_depth_map, image_path, depth=8)
64
+ img = Image.fromarray(smoothed_depth_map)
65
  return [img, gltf_path, gltf_path]
66
  except:
67
  print("Error reconstructing 3D model")