huntrezz commited on
Commit
ff63548
·
verified ·
1 Parent(s): 51f6b27

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -31,9 +31,9 @@ model = torch.quantization.quantize_dynamic(
31
 
32
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
33
 
34
- color_map = torch.from_numpy(cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)).to(device)
35
 
36
- input_tensor = torch.zeros((1, 3, 72, 128), dtype=torch.float32, device=device)
37
 
38
  def preprocess_image(image):
39
  return cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
@@ -46,16 +46,14 @@ def process_frame(image):
46
  input_tensor = torch.from_numpy(preprocessed).unsqueeze(0).to(device)
47
 
48
  predicted_depth = model(input_tensor).predicted_depth
49
- depth_map = predicted_depth.squeeze()
50
-
51
- # Normalize depth map to [0, 255] range
52
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
53
- depth_map = (depth_map * 255).byte()
54
-
55
- # Apply color map
56
- depth_map_colored = color_map[depth_map.long()]
57
 
58
- return cv2.cvtColor(depth_map_colored.cpu().numpy(), cv2.COLOR_BGR2RGB)
59
 
60
  interface = gr.Interface(
61
  fn=process_frame,
 
31
 
32
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
33
 
34
+ color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)
35
 
36
+ input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
37
 
38
  def preprocess_image(image):
39
  return cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
 
46
  input_tensor = torch.from_numpy(preprocessed).unsqueeze(0).to(device)
47
 
48
  predicted_depth = model(input_tensor).predicted_depth
49
+ depth_map = predicted_depth.squeeze().cpu().numpy()
50
+ num_bins = 1000
51
+ depth_map = np.digitize(depth_map, bins=np.linspace(depth_map.min(), depth_map.max(), num_bins)) - 1
52
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
53
+ depth_map = (depth_map * 255).astype(np.uint8)
54
+ depth_map_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
 
 
55
 
56
+ return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
57
 
58
  interface = gr.Interface(
59
  fn=process_frame,