Spaces:
Running
on
Zero
Running
on
Zero
improve
Browse files- app.py +68 -45
- pyproject.toml +3 -2
- requirements.txt +1 -1
- uv.lock +10 -14
app.py
CHANGED
@@ -10,6 +10,7 @@ from gradio_rerun import Rerun
|
|
10 |
import spaces
|
11 |
from PIL import Image
|
12 |
import tempfile
|
|
|
13 |
|
14 |
# Run the script to get pretrained models
|
15 |
if not os.path.exists("./checkpoints/depth_pro.pt"):
|
@@ -25,8 +26,8 @@ model = model.to(device)
|
|
25 |
model.eval()
|
26 |
|
27 |
|
28 |
-
def resize_image(
|
29 |
-
with Image.
|
30 |
# Calculate the new size while maintaining aspect ratio
|
31 |
ratio = max_size / max(img.size)
|
32 |
new_size = tuple([int(x * ratio) for x in img.size])
|
@@ -73,7 +74,7 @@ def predict_depth(input_image):
|
|
73 |
|
74 |
|
75 |
@rr.thread_local_stream("rerun_example_ml_depth_pro")
|
76 |
-
def run_rerun(
|
77 |
stream = rr.binary_stream()
|
78 |
|
79 |
blueprint = rrb.Blueprint(
|
@@ -81,9 +82,9 @@ def run_rerun(path_to_image):
|
|
81 |
rrb.Spatial3DView(origin="/"),
|
82 |
rrb.Horizontal(
|
83 |
rrb.Spatial2DView(
|
84 |
-
origin="/world/
|
85 |
),
|
86 |
-
rrb.Spatial2DView(origin="/world/
|
87 |
),
|
88 |
),
|
89 |
collapse_panels=True,
|
@@ -92,40 +93,62 @@ def run_rerun(path_to_image):
|
|
92 |
rr.send_blueprint(blueprint)
|
93 |
|
94 |
yield stream.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
# Clean up the temporary file
|
127 |
-
if temp_file and os.path.exists(temp_file):
|
128 |
-
os.remove(temp_file)
|
129 |
|
130 |
yield stream.read()
|
131 |
|
@@ -147,18 +170,18 @@ with gr.Blocks() as interface:
|
|
147 |
)
|
148 |
with gr.Row():
|
149 |
with gr.Column(variant="compact"):
|
150 |
-
|
151 |
visualize = gr.Button("Visualize ML Depth Pro")
|
152 |
-
examples = gr.Examples(
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
)
|
157 |
with gr.Column():
|
158 |
viewer = Rerun(
|
159 |
streaming=True,
|
160 |
)
|
161 |
-
visualize.click(run_rerun, inputs=[
|
162 |
|
163 |
|
164 |
if __name__ == "__main__":
|
|
|
10 |
import spaces
|
11 |
from PIL import Image
|
12 |
import tempfile
|
13 |
+
import cv2
|
14 |
|
15 |
# Run the script to get pretrained models
|
16 |
if not os.path.exists("./checkpoints/depth_pro.pt"):
|
|
|
26 |
model.eval()
|
27 |
|
28 |
|
29 |
+
def resize_image(image_buffer, max_size=128):
|
30 |
+
with Image.fromarray(image_buffer) as img:
|
31 |
# Calculate the new size while maintaining aspect ratio
|
32 |
ratio = max_size / max(img.size)
|
33 |
new_size = tuple([int(x * ratio) for x in img.size])
|
|
|
74 |
|
75 |
|
76 |
@rr.thread_local_stream("rerun_example_ml_depth_pro")
|
77 |
+
def run_rerun(path_to_video):
|
78 |
stream = rr.binary_stream()
|
79 |
|
80 |
blueprint = rrb.Blueprint(
|
|
|
82 |
rrb.Spatial3DView(origin="/"),
|
83 |
rrb.Horizontal(
|
84 |
rrb.Spatial2DView(
|
85 |
+
origin="/world/depth",
|
86 |
),
|
87 |
+
rrb.Spatial2DView(origin="/world/video"),
|
88 |
),
|
89 |
),
|
90 |
collapse_panels=True,
|
|
|
93 |
rr.send_blueprint(blueprint)
|
94 |
|
95 |
yield stream.read()
|
96 |
+
video_asset = rr.AssetVideo(path=path_to_video)
|
97 |
+
rr.log("world/video", video_asset, static=True)
|
98 |
+
|
99 |
+
# Send automatically determined video frame timestamps.
|
100 |
+
frame_timestamps_ns = video_asset.read_frame_timestamps_ns()
|
101 |
+
|
102 |
+
# load the video using opencv
|
103 |
+
cap = cv2.VideoCapture(path_to_video)
|
104 |
+
# loop through the video and log the frames using the video timestamps
|
105 |
+
for i in range(len(frame_timestamps_ns)):
|
106 |
+
ret, frame = cap.read()
|
107 |
+
if not ret:
|
108 |
+
break
|
109 |
+
|
110 |
+
temp_file = None
|
111 |
+
try:
|
112 |
+
temp_file = resize_image(frame, max_size=128)
|
113 |
+
|
114 |
+
depth, focal_length = predict_depth(temp_file)
|
115 |
+
|
116 |
+
rr.set_time_nanos("video_time", frame_timestamps_ns[i])
|
117 |
+
rr.log(
|
118 |
+
"world/depth",
|
119 |
+
rr.DepthImage(depth, meter=1),
|
120 |
+
)
|
121 |
|
122 |
+
rr.log(
|
123 |
+
"world/frame",
|
124 |
+
rr.VideoFrameReference(
|
125 |
+
timestamp=rr.components.VideoTimestamp(seconds=1.0),
|
126 |
+
video_reference="world/video",
|
127 |
+
),
|
128 |
+
)
|
129 |
+
|
130 |
+
rr.log(
|
131 |
+
"world/camera",
|
132 |
+
rr.Pinhole(
|
133 |
+
focal_length=focal_length,
|
134 |
+
width=depth.shape[1],
|
135 |
+
height=depth.shape[0],
|
136 |
+
principal_point=(depth.shape[1] / 2, depth.shape[0] / 2),
|
137 |
+
camera_xyz=rr.ViewCoordinates.FLU,
|
138 |
+
image_plane_distance=depth.max(),
|
139 |
+
),
|
140 |
+
)
|
141 |
+
|
142 |
+
yield stream.read()
|
143 |
+
except Exception as e:
|
144 |
+
rr.log(
|
145 |
+
"error",
|
146 |
+
rr.TextLog(f"An error has occurred: {e}", level=rr.TextLogLevel.ERROR),
|
147 |
+
)
|
148 |
+
finally:
|
149 |
+
# Clean up the temporary file
|
150 |
+
if temp_file and os.path.exists(temp_file):
|
151 |
+
os.remove(temp_file)
|
|
|
|
|
|
|
152 |
|
153 |
yield stream.read()
|
154 |
|
|
|
170 |
)
|
171 |
with gr.Row():
|
172 |
with gr.Column(variant="compact"):
|
173 |
+
video = gr.Video(format="mp4", interactive=True, label="Video")
|
174 |
visualize = gr.Button("Visualize ML Depth Pro")
|
175 |
+
# examples = gr.Examples(
|
176 |
+
# example_images,
|
177 |
+
# label="Example Images",
|
178 |
+
# inputs=[image],
|
179 |
+
# )
|
180 |
with gr.Column():
|
181 |
viewer = Rerun(
|
182 |
streaming=True,
|
183 |
)
|
184 |
+
visualize.click(run_rerun, inputs=[video], outputs=[viewer])
|
185 |
|
186 |
|
187 |
if __name__ == "__main__":
|
pyproject.toml
CHANGED
@@ -8,8 +8,8 @@ dependencies = [
|
|
8 |
"attrs>=24.2.0",
|
9 |
"depth-pro",
|
10 |
"gradio>=4.44.1",
|
11 |
-
"gradio-rerun
|
12 |
-
"rerun-sdk==0.
|
13 |
"spaces>=0.30.3",
|
14 |
]
|
15 |
|
@@ -24,3 +24,4 @@ dev-dependencies = [
|
|
24 |
|
25 |
[tool.uv.sources]
|
26 |
depth-pro = { git = "https://github.com/apple/ml-depth-pro" }
|
|
|
|
8 |
"attrs>=24.2.0",
|
9 |
"depth-pro",
|
10 |
"gradio>=4.44.1",
|
11 |
+
"gradio-rerun",
|
12 |
+
"rerun-sdk==0.19.0",
|
13 |
"spaces>=0.30.3",
|
14 |
]
|
15 |
|
|
|
24 |
|
25 |
[tool.uv.sources]
|
26 |
depth-pro = { git = "https://github.com/apple/ml-depth-pro" }
|
27 |
+
gradio-rerun = { git = "https://github.com/oxkitsune/gradio-rerun-viewer", branch = "gijs/rerun-0.19" }
|
requirements.txt
CHANGED
@@ -72,7 +72,7 @@ pytz==2024.2
|
|
72 |
pyyaml==6.0.2
|
73 |
pyzmq==26.2.0
|
74 |
requests==2.32.3
|
75 |
-
rerun-sdk==0.
|
76 |
rich==13.9.2
|
77 |
ruff==0.6.9
|
78 |
safetensors==0.4.5
|
|
|
72 |
pyyaml==6.0.2
|
73 |
pyzmq==26.2.0
|
74 |
requests==2.32.3
|
75 |
+
rerun-sdk==0.19.0
|
76 |
rich==13.9.2
|
77 |
ruff==0.6.9
|
78 |
safetensors==0.4.5
|
uv.lock
CHANGED
@@ -500,17 +500,13 @@ wheels = [
|
|
500 |
|
501 |
[[package]]
|
502 |
name = "gradio-rerun"
|
503 |
-
version = "0.0.
|
504 |
-
source = {
|
505 |
dependencies = [
|
506 |
{ name = "gradio" },
|
507 |
{ name = "opencv-python" },
|
508 |
{ name = "rerun-sdk" },
|
509 |
]
|
510 |
-
sdist = { url = "https://files.pythonhosted.org/packages/4f/78/0e70e572f159ca4060ded3a49f8bbb25fb4dca57ab7b0c2b0ca759e57c57/gradio_rerun-0.0.6.tar.gz", hash = "sha256:291f5c827f6db1ce01eeed1ef461ac252a4209cdaaf2792b40d7f06e8ffccba3", size = 52014801 }
|
511 |
-
wheels = [
|
512 |
-
{ url = "https://files.pythonhosted.org/packages/fc/d4/90fb4f54a7707b2beec2fb0ef5c97659eeb6776b45ff5d46193bea7c2f7b/gradio_rerun-0.0.6-py3-none-any.whl", hash = "sha256:5619aed9580c79f4eae6871602ca7409db2d67cf5cc35cfe4a6083be26a75fa2", size = 10842538 },
|
513 |
-
]
|
514 |
|
515 |
[[package]]
|
516 |
name = "h11"
|
@@ -1730,8 +1726,8 @@ requires-dist = [
|
|
1730 |
{ name = "attrs", specifier = ">=24.2.0" },
|
1731 |
{ name = "depth-pro", git = "https://github.com/apple/ml-depth-pro" },
|
1732 |
{ name = "gradio", specifier = ">=4.44.1" },
|
1733 |
-
{ name = "gradio-rerun",
|
1734 |
-
{ name = "rerun-sdk", specifier = "==0.
|
1735 |
{ name = "spaces", specifier = ">=0.30.3" },
|
1736 |
]
|
1737 |
|
@@ -1746,7 +1742,7 @@ dev = [
|
|
1746 |
|
1747 |
[[package]]
|
1748 |
name = "rerun-sdk"
|
1749 |
-
version = "0.
|
1750 |
source = { registry = "https://pypi.org/simple" }
|
1751 |
dependencies = [
|
1752 |
{ name = "attrs" },
|
@@ -1756,11 +1752,11 @@ dependencies = [
|
|
1756 |
{ name = "typing-extensions" },
|
1757 |
]
|
1758 |
wheels = [
|
1759 |
-
{ url = "https://files.pythonhosted.org/packages/
|
1760 |
-
{ url = "https://files.pythonhosted.org/packages/
|
1761 |
-
{ url = "https://files.pythonhosted.org/packages/
|
1762 |
-
{ url = "https://files.pythonhosted.org/packages/
|
1763 |
-
{ url = "https://files.pythonhosted.org/packages/
|
1764 |
]
|
1765 |
|
1766 |
[[package]]
|
|
|
500 |
|
501 |
[[package]]
|
502 |
name = "gradio-rerun"
|
503 |
+
version = "0.0.7"
|
504 |
+
source = { git = "https://github.com/oxkitsune/gradio-rerun-viewer?branch=gijs%2Frerun-0.19#19b28a2045047f0ae4480d0a450811a075c79056" }
|
505 |
dependencies = [
|
506 |
{ name = "gradio" },
|
507 |
{ name = "opencv-python" },
|
508 |
{ name = "rerun-sdk" },
|
509 |
]
|
|
|
|
|
|
|
|
|
510 |
|
511 |
[[package]]
|
512 |
name = "h11"
|
|
|
1726 |
{ name = "attrs", specifier = ">=24.2.0" },
|
1727 |
{ name = "depth-pro", git = "https://github.com/apple/ml-depth-pro" },
|
1728 |
{ name = "gradio", specifier = ">=4.44.1" },
|
1729 |
+
{ name = "gradio-rerun", git = "https://github.com/oxkitsune/gradio-rerun-viewer?branch=gijs%2Frerun-0.19" },
|
1730 |
+
{ name = "rerun-sdk", specifier = "==0.19.0" },
|
1731 |
{ name = "spaces", specifier = ">=0.30.3" },
|
1732 |
]
|
1733 |
|
|
|
1742 |
|
1743 |
[[package]]
|
1744 |
name = "rerun-sdk"
|
1745 |
+
version = "0.19.0"
|
1746 |
source = { registry = "https://pypi.org/simple" }
|
1747 |
dependencies = [
|
1748 |
{ name = "attrs" },
|
|
|
1752 |
{ name = "typing-extensions" },
|
1753 |
]
|
1754 |
wheels = [
|
1755 |
+
{ url = "https://files.pythonhosted.org/packages/c4/59/cfea9527a2f56652c9f2e54151c8a0d2b572b7f1255ca9bc6ea8ad2fd7ce/rerun_sdk-0.19.0-cp38-abi3-macosx_10_12_x86_64.whl", hash = "sha256:49a48d6d5d7de662ef3e83dc262e65705fa719726f6bc6deefad27c4b6d34e98", size = 37415314 },
|
1756 |
+
{ url = "https://files.pythonhosted.org/packages/d9/16/0d7099d537bf2f73988ac93f5075d4fd717e96c25697b3ea16af8bcc2cda/rerun_sdk-0.19.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:c8012b4e517a911a782472dc97ebbdc6ed4261b44bfdd0e0a0c64496f17ddc91", size = 35743438 },
|
1757 |
+
{ url = "https://files.pythonhosted.org/packages/cf/35/eaabd19deaa2bbb121df3a6949206f02ad6cff122d8ad6ba0fcdeeb972c6/rerun_sdk-0.19.0-cp38-abi3-manylinux_2_31_aarch64.whl", hash = "sha256:65bb8ddf9611827c31d5502f9d1ee997c1facbbdf5dce9a268f0aa6bcaea5439", size = 39794278 },
|
1758 |
+
{ url = "https://files.pythonhosted.org/packages/8b/2c/1e06376a531431855c4ea12865aba0d83a1a1d0537a544191d09d3b44eea/rerun_sdk-0.19.0-cp38-abi3-manylinux_2_31_x86_64.whl", hash = "sha256:6a49fde1c9d0691166402707ec3b317bf0d82fb1b4412b98e02ccbf499d9b92d", size = 41301646 },
|
1759 |
+
{ url = "https://files.pythonhosted.org/packages/b2/57/948b518f3db30b8dd27d1dc0280acc6510289a79675dcb523ccfcced39d6/rerun_sdk-0.19.0-cp38-abi3-win_amd64.whl", hash = "sha256:da304927485cb4e6afe25ea8ed84c0cb7e63f3ba8ce2c72a1034ae1ffc69a6c0", size = 33573220 },
|
1760 |
]
|
1761 |
|
1762 |
[[package]]
|