huntrezz commited on
Commit
d6a18b3
·
verified ·
1 Parent(s): 6380ca2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -7
app.py CHANGED
@@ -33,17 +33,29 @@ model = model.to(device)
33
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
34
 
35
  def preprocess_image(image):
36
- image = cv2.resize(image, (128, 128))
37
  image = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float().to(device)
38
  return image / 255.0
39
 
40
- def plot_depth_map(depth_map, azimuth):
41
  fig = plt.figure(figsize=(16, 9))
42
  ax = fig.add_subplot(111, projection='3d')
43
  x, y = np.meshgrid(range(depth_map.shape[1]), range(depth_map.shape[0]))
44
- ax.plot_surface(x, y, depth_map, cmap='viridis')
45
- ax.view_init(elev=90, azim=azimuth) # Look down onto the depth map
 
 
 
 
 
 
 
 
 
 
 
46
  ax.set_zlim(0, 1)
 
47
  plt.close(fig)
48
 
49
  fig.canvas.draw()
@@ -53,7 +65,7 @@ def plot_depth_map(depth_map, azimuth):
53
  return img
54
 
55
  @torch.inference_mode()
56
- def process_frame(image, azimuth):
57
  if image is None:
58
  return None
59
  preprocessed = preprocess_image(image)
@@ -62,11 +74,11 @@ def process_frame(image, azimuth):
62
 
63
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
64
 
65
- return plot_depth_map(depth_map, azimuth)
66
 
67
  interface = gr.Interface(
68
  fn=process_frame,
69
- inputs=[gr.Image(sources="webcam", streaming=True), gr.Slider(0, 360, step=1)],
70
  outputs="image",
71
  live=True
72
  )
 
33
  processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
34
 
35
  def preprocess_image(image):
36
+ image = cv2.resize(image, (64, 36)) # 16:9 aspect ratio
37
  image = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float().to(device)
38
  return image / 255.0
39
 
40
+ def plot_depth_map(depth_map, original_image):
41
  fig = plt.figure(figsize=(16, 9))
42
  ax = fig.add_subplot(111, projection='3d')
43
  x, y = np.meshgrid(range(depth_map.shape[1]), range(depth_map.shape[0]))
44
+
45
+ # Flip x and y to mirror the plot
46
+ x = np.flip(x, axis=1)
47
+ y = np.flip(y, axis=0)
48
+
49
+ # Rotate by 90 degrees
50
+ x, y = y, x
51
+
52
+ # Use original image colors
53
+ colors = original_image.reshape(-1, 3) / 255.0
54
+
55
+ ax.plot_surface(x, y, depth_map, facecolors=colors, shade=False)
56
+ ax.view_init(elev=70, azim=0) # Tilt by 20 degrees (90 - 20 = 70)
57
  ax.set_zlim(0, 1)
58
+ plt.axis('off')
59
  plt.close(fig)
60
 
61
  fig.canvas.draw()
 
65
  return img
66
 
67
  @torch.inference_mode()
68
+ def process_frame(image):
69
  if image is None:
70
  return None
71
  preprocessed = preprocess_image(image)
 
74
 
75
  depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
76
 
77
+ return plot_depth_map(depth_map, cv2.resize(image, (64, 36)))
78
 
79
  interface = gr.Interface(
80
  fn=process_frame,
81
+ inputs=gr.Image(sources="webcam", streaming=True),
82
  outputs="image",
83
  live=True
84
  )