codic commited on
Commit
1af7d33
·
verified ·
1 Parent(s): de7156a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -8
app.py CHANGED
@@ -28,20 +28,16 @@ def process_image(image_path, depth_map_path=None):
28
 
29
  # Check if user-provided depth map is available
30
  if depth_map_path:
31
- depth_map_path = Path(depth_map_path)
32
- if depth_map_path.suffix == '.npy':
33
- # Load depth map from .npy file
34
  depth_image = np.load(depth_map_path)
35
  else:
36
- # Load depth map from image file
37
  depth_image_raw = Image.open(depth_map_path).convert("L") # Convert to grayscale
38
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
39
  depth_image = np.array(depth_image)
40
 
41
- # Normalize depth image to match expected format
42
- depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
43
- depth_image = (depth_image * 255).astype('uint8')
44
-
45
  else:
46
  # Generate depth map using DPT model
47
  encoding = feature_extractor(foreground, return_tensors="pt")
@@ -57,6 +53,9 @@ def process_image(image_path, depth_map_path=None):
57
  ).squeeze()
58
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
59
 
 
 
 
60
  # Step 1: Apply Gaussian smoothing on the depth map
61
  smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
62
 
 
28
 
29
  # Check if user-provided depth map is available
30
  if depth_map_path:
31
+ if depth_map_path.endswith('.npy'):
 
 
32
  depth_image = np.load(depth_map_path)
33
  else:
 
34
  depth_image_raw = Image.open(depth_map_path).convert("L") # Convert to grayscale
35
  depth_image = depth_image_raw.resize(image.size, Image.Resampling.NEAREST)
36
  depth_image = np.array(depth_image)
37
 
38
+ # Normalize depth image to match expected format
39
+ depth_image = (depth_image - np.min(depth_image)) / (np.max(depth_image) - np.min(depth_image))
40
+ depth_image = (depth_image * 255).astype('uint8')
 
41
  else:
42
  # Generate depth map using DPT model
43
  encoding = feature_extractor(foreground, return_tensors="pt")
 
53
  ).squeeze()
54
  depth_image = (prediction.cpu().numpy() * 255 / np.max(prediction.cpu().numpy())).astype("uint8")
55
 
56
+ # Resize depth map to match RGB image dimensions
57
+ depth_image = cv2.resize(depth_image, (image.size[0], image.size[1]), interpolation=cv2.INTER_NEAREST)
58
+
59
  # Step 1: Apply Gaussian smoothing on the depth map
60
  smoothed_depth_map = cv2.GaussianBlur(depth_image, (5, 5), 0)
61