huntrezz commited on
Commit
44656db
·
verified ·
1 Parent(s): fed8dd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -13
app.py CHANGED
@@ -32,30 +32,30 @@ model = torch.quantization.quantize_dynamic(
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
 
40
 
41
  @torch.inference_mode()
42
  def process_frame(image):
43
  if image is None:
44
  return None
45
- preprocessed = preprocess_image(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().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
- unique_values = np.unique(depth_map)
53
- print(f"Number of unique depth values: {len(unique_values)}")
54
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
55
- depth_map = (depth_map * 255).astype(np.uint8)
56
- depth_map_colored = cv2.applyColorMap(depth_map, cv2.COLORMAP_INFERNO)
 
57
 
58
- return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
59
 
60
  interface = gr.Interface(
61
  fn=process_frame,
 
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
+ color_map_gpu = torch.from_numpy(color_map).to(device)
 
36
 
37
  def preprocess_image(image):
38
+ resized = cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA)
39
+ return torch.from_numpy(resized.transpose(2, 0, 1)).float().div(255).unsqueeze(0).to(device)
40
 
41
  @torch.inference_mode()
42
  def process_frame(image):
43
  if image is None:
44
  return None
45
+ input_tensor = preprocess_image(image)
 
46
 
47
  predicted_depth = model(input_tensor).predicted_depth
48
+ depth_map = predicted_depth.squeeze()
49
+
50
+ # Rounding instead of discretization
51
+ depth_map = torch.round(depth_map * 100) / 100
52
+
53
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
54
+ depth_map = (depth_map * 255).byte()
55
+
56
+ depth_map_colored = color_map_gpu[depth_map]
57
 
58
+ return cv2.cvtColor(depth_map_colored.cpu().numpy(), cv2.COLOR_BGR2RGB)
59
 
60
  interface = gr.Interface(
61
  fn=process_frame,