oxkitsune commited on
Commit
eec6e0c
1 Parent(s): afc0455

clean up logging

Browse files
Files changed (2) hide show
  1. app.py +62 -42
  2. get_pretrained_models.sh +1 -2
app.py CHANGED
@@ -49,13 +49,9 @@ def predict(frame):
49
 
50
 
51
  @rr.thread_local_stream("rerun_example_ml_depth_pro")
52
- def run_ml_depth_pro(frame):
53
  stream = rr.binary_stream()
54
 
55
- assert model is not None, "Model is None"
56
- assert transform is not None, "Transform is None"
57
- assert frames is not None, "Frames is None"
58
-
59
  blueprint = rrb.Blueprint(
60
  rrb.Vertical(
61
  rrb.Spatial3DView(origin="/"),
@@ -69,38 +65,64 @@ def run_ml_depth_pro(frame):
69
  collapse_panels=True,
70
  )
71
 
 
 
72
  rr.send_blueprint(blueprint)
 
73
 
74
- # for i, frame in enumerate(frames):
75
- rr.set_time_sequence("frame", 0)
76
- rr.log("world/camera/image", rr.Image(frame))
 
 
 
 
77
 
78
- depth, focal_length = predict(frame)
 
 
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
- rr.log(
82
- "world/camera",
83
- rr.Pinhole(
84
- width=frame.shape[1],
85
- height=frame.shape[0],
86
- focal_length=focal_length,
87
- principal_point=(frame.shape[1] / 2, frame.shape[0] / 2),
88
- image_plane_distance=depth.max(),
89
- ),
90
- )
91
 
92
- rr.log(
93
- "world/camera/depth",
94
- # need 0.19 stable for this
95
- # rr.DepthImage(depth, meter=1, depth_range=(depth.min(), depth.max())),
96
- rr.DepthImage(depth, meter=1),
97
- )
98
 
99
- yield stream.read()
100
 
101
 
102
- video_path = Path("hd-cat.mp4")
 
 
 
 
103
 
 
 
 
 
 
104
 
105
  # Load video
106
  frames = []
@@ -114,21 +136,19 @@ while True:
114
  frames.append(frame)
115
 
116
  with gr.Blocks() as demo:
117
- with gr.Tab("Streaming"):
118
- with gr.Row():
119
- img = gr.Image(interactive=True, label="Image")
120
- with gr.Column():
121
- stream_ml_depth_pro = gr.Button("Stream Ml Depth Pro")
122
- with gr.Row():
123
- viewer = Rerun(
124
- streaming=True,
125
- panel_states={
126
- "time": "collapsed",
127
- "blueprint": "hidden",
128
- "selection": "hidden",
129
- },
130
- )
131
- stream_ml_depth_pro.click(run_ml_depth_pro, inputs=[img], outputs=[viewer])
132
 
133
 
134
  if __name__ == "__main__":
 
49
 
50
 
51
  @rr.thread_local_stream("rerun_example_ml_depth_pro")
52
+ def run_rerun(path_to_video):
53
  stream = rr.binary_stream()
54
 
 
 
 
 
55
  blueprint = rrb.Blueprint(
56
  rrb.Vertical(
57
  rrb.Spatial3DView(origin="/"),
 
65
  collapse_panels=True,
66
  )
67
 
68
+
69
+
70
  rr.send_blueprint(blueprint)
71
+ yield stream.read()
72
 
73
+ print("Loading video from", path_to_video)
74
+ video = cv2.VideoCapture(path_to_video)
75
+ frame_idx = 0
76
+ while True:
77
+ read, frame = video.read()
78
+ if not read:
79
+ break
80
 
81
+
82
+ frame = cv2.resize(frame, (320, 240))
83
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
84
 
85
+
86
+ rr.set_time_sequence("frame", frame_idx)
87
+ rr.log("world/camera/image", rr.Image(frame))
88
+ yield stream.read()
89
+
90
+ image = transform(frame)
91
+ depth, focal_length = estimate_depth(image)
92
+
93
+ rr.log(
94
+ "world/camera",
95
+ rr.Pinhole(
96
+ width=frame.shape[1],
97
+ height=frame.shape[0],
98
+ focal_length=,
99
+ principal_point=(frame.shape[1] / 2, frame.shape[0] / 2),
100
+ image_plane_distance=depth.max(),
101
+ ),
102
+ )
103
 
104
+ rr.log(
105
+ "world/camera/depth",
106
+ # need 0.19 stable for this
107
+ # rr.DepthImage(depth, meter=1, depth_range=(depth.min(), depth.max())),
108
+ rr.DepthImage(depth, meter=1),
109
+ )
 
 
 
 
110
 
111
+ yield stream.read()
 
 
 
 
 
112
 
 
113
 
114
 
115
+ @spaces.GPU(duration=20)
116
+ def estimate_depth(image):
117
+ prediction = model.infer(image)
118
+ depth = prediction["depth"].squeeze().detach().cpu().numpy()
119
+ focal_length = prediction["focallength_px"].item()
120
 
121
+ return depth, focal_length
122
+
123
+
124
+
125
+ video_path = Path("hd-cat.mp4")
126
 
127
  # Load video
128
  frames = []
 
136
  frames.append(frame)
137
 
138
  with gr.Blocks() as demo:
139
+ video = gr.Video(interactive=True, label="Video")
140
+ visualize = gr.Button("Visualize ML Depth Pro")
141
+
142
+ with gr.Row():
143
+ viewer = Rerun(
144
+ streaming=True,
145
+ panel_states={
146
+ "time": "collapsed",
147
+ "blueprint": "hidden",
148
+ "selection": "hidden",
149
+ },
150
+ )
151
+ visualize.click(run_rerun, inputs=[video], outputs=[viewer])
 
 
152
 
153
 
154
  if __name__ == "__main__":
get_pretrained_models.sh CHANGED
@@ -4,5 +4,4 @@
4
  # Copyright (C) 2024 Apple Inc. All Rights Reserved.
5
  #
6
  mkdir -p checkpoints
7
- # Place final weights here:
8
- wget https://ml-site.cdn-apple.com/models/depth-pro/depth_pro.pt -P checkpoints
 
4
  # Copyright (C) 2024 Apple Inc. All Rights Reserved.
5
  #
6
  mkdir -p checkpoints
7
+ wget -q https://ml-site.cdn-apple.com/models/depth-pro/depth_pro.pt -P checkpoints