Spaces:
Running
Running
clean up logging
Browse files- app.py +62 -42
- 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
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
principal_point=(frame.shape[1] / 2, frame.shape[0] / 2),
|
| 88 |
-
image_plane_distance=depth.max(),
|
| 89 |
-
),
|
| 90 |
-
)
|
| 91 |
|
| 92 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 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 |
-
|
| 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
|
|
|