oxkitsune commited on
Commit
95377ef
·
1 Parent(s): 6f20d0a
Files changed (5) hide show
  1. README.md +1 -1
  2. app.py +38 -32
  3. pyproject.toml +1 -0
  4. requirements.txt +2 -1
  5. uv.lock +30 -11
README.md CHANGED
@@ -6,7 +6,7 @@ colorTo: gray
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
  app_file: app.py
9
- pinned: false
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
6
  sdk: gradio
7
  sdk_version: 4.44.1
8
  app_file: app.py
 
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
12
+
app.py CHANGED
@@ -5,23 +5,29 @@ import subprocess
5
 
6
  import torch
7
  import cv2
 
8
  from pathlib import Path
9
  import gradio as gr
10
  from gradio_rerun import Rerun
 
11
 
12
  # Run the script to get pretrained models
13
- subprocess.run(["bash", "get_pretrained_models.sh"])
 
 
14
 
15
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
16
 
17
  # Load model and preprocessing transform
 
18
  model, transform = depth_pro.create_model_and_transforms()
19
  model = model.to(device)
20
  model.eval()
21
 
22
 
23
  @rr.thread_local_stream("rerun_example_ml_depth_pro")
24
- def run_ml_depth_pro(model, transform, frames):
 
25
  stream = rr.binary_stream()
26
 
27
  assert model is not None, "Model is None"
@@ -31,41 +37,43 @@ def run_ml_depth_pro(model, transform, frames):
31
  blueprint = rrb.Blueprint(
32
  rrb.Spatial3DView(origin="/"),
33
  rrb.Horizontal(
34
- rrb.Spatial2DView(origin="/world/camera/depth", title="Depth"),
35
- rrb.Spatial2DView(origin="/world/camera/image", title="Image"),
 
 
36
  ),
37
  collapse_panels=True,
38
  )
39
 
40
  rr.send_blueprint(blueprint)
41
 
42
- for i, frame in enumerate(frames):
43
- rr.set_time_sequence("frame", i)
44
- rr.log("world/camera/image", rr.Image(frame))
45
-
46
- image = transform(frame)
47
- prediction = model.infer(image)
48
- depth = prediction["depth"].squeeze().detach().cpu().numpy()
49
-
50
- rr.log(
51
- "world/camera",
52
- rr.Pinhole(
53
- width=frame.shape[1],
54
- height=frame.shape[0],
55
- focal_length=prediction["focallength_px"].item(),
56
- principal_point=(frame.shape[1] / 2, frame.shape[0] / 2),
57
- image_plane_distance=depth.max(),
58
- ),
59
- )
60
 
61
- rr.log(
62
- "world/camera/depth",
63
- # need 0.19 stable for this
64
- # rr.DepthImage(depth, meter=1, depth_range=(depth.min(), depth.max())),
65
- rr.DepthImage(depth, meter=1),
66
- )
67
 
68
- yield stream.read()
69
 
70
 
71
  video_path = Path("hd-cat.mp4")
@@ -97,9 +105,7 @@ with gr.Blocks() as demo:
97
  "selection": "hidden",
98
  },
99
  )
100
- stream_ml_depth_pro.click(
101
- run_ml_depth_pro, inputs=[model, transform, frames], outputs=[viewer]
102
- )
103
 
104
 
105
  if __name__ == "__main__":
 
5
 
6
  import torch
7
  import cv2
8
+ import os
9
  from pathlib import Path
10
  import gradio as gr
11
  from gradio_rerun import Rerun
12
+ import spaces
13
 
14
  # Run the script to get pretrained models
15
+ if not os.path.exists("checkpoints/depth_pro.pt"):
16
+ print("downloading pretrained model")
17
+ subprocess.run(["bash", "get_pretrained_models.sh"])
18
 
19
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
20
 
21
  # Load model and preprocessing transform
22
+ print("loading model...")
23
  model, transform = depth_pro.create_model_and_transforms()
24
  model = model.to(device)
25
  model.eval()
26
 
27
 
28
  @rr.thread_local_stream("rerun_example_ml_depth_pro")
29
+ @spaces.GPU(duration=20)
30
+ def run_ml_depth_pro(frame):
31
  stream = rr.binary_stream()
32
 
33
  assert model is not None, "Model is None"
 
37
  blueprint = rrb.Blueprint(
38
  rrb.Spatial3DView(origin="/"),
39
  rrb.Horizontal(
40
+ rrb.Spatial2DView(
41
+ origin="/world/camera/depth",
42
+ ),
43
+ rrb.Spatial2DView(origin="/world/camera/image"),
44
  ),
45
  collapse_panels=True,
46
  )
47
 
48
  rr.send_blueprint(blueprint)
49
 
50
+ # for i, frame in enumerate(frames):
51
+ rr.set_time_sequence("frame", 0)
52
+ rr.log("world/camera/image", rr.Image(frame))
53
+
54
+ image = transform(frame)
55
+ prediction = model.infer(image)
56
+ depth = prediction["depth"].squeeze().detach().cpu().numpy()
57
+
58
+ rr.log(
59
+ "world/camera",
60
+ rr.Pinhole(
61
+ width=frame.shape[1],
62
+ height=frame.shape[0],
63
+ focal_length=prediction["focallength_px"].item(),
64
+ principal_point=(frame.shape[1] / 2, frame.shape[0] / 2),
65
+ image_plane_distance=depth.max(),
66
+ ),
67
+ )
68
 
69
+ rr.log(
70
+ "world/camera/depth",
71
+ # need 0.19 stable for this
72
+ # rr.DepthImage(depth, meter=1, depth_range=(depth.min(), depth.max())),
73
+ rr.DepthImage(depth, meter=1),
74
+ )
75
 
76
+ yield stream.read()
77
 
78
 
79
  video_path = Path("hd-cat.mp4")
 
105
  "selection": "hidden",
106
  },
107
  )
108
+ stream_ml_depth_pro.click(run_ml_depth_pro, inputs=[img], outputs=[viewer])
 
 
109
 
110
 
111
  if __name__ == "__main__":
pyproject.toml CHANGED
@@ -11,6 +11,7 @@ dependencies = [
11
  "gradio-rerun>=0.0.6",
12
  "opencv-python>=4.10.0.84",
13
  "rerun-sdk==0.18.2",
 
14
  ]
15
 
16
  [tool.uv]
 
11
  "gradio-rerun>=0.0.6",
12
  "opencv-python>=4.10.0.84",
13
  "rerun-sdk==0.18.2",
14
+ "spaces>=0.30.3",
15
  ]
16
 
17
  [tool.uv]
requirements.txt CHANGED
@@ -57,7 +57,7 @@ pillow==10.4.0
57
  pillow-heif==0.18.0
58
  platformdirs==4.3.6
59
  prompt-toolkit==3.0.48
60
- psutil==6.0.0
61
  ptyprocess==0.7.0
62
  pure-eval==0.2.3
63
  pyarrow==17.0.0
@@ -80,6 +80,7 @@ semantic-version==2.10.0
80
  shellingham==1.5.4
81
  six==1.16.0
82
  sniffio==1.3.1
 
83
  stack-data==0.6.3
84
  starlette==0.38.6
85
  sympy==1.13.3
 
57
  pillow-heif==0.18.0
58
  platformdirs==4.3.6
59
  prompt-toolkit==3.0.48
60
+ psutil==5.9.8
61
  ptyprocess==0.7.0
62
  pure-eval==0.2.3
63
  pyarrow==17.0.0
 
80
  shellingham==1.5.4
81
  six==1.16.0
82
  sniffio==1.3.1
83
+ spaces==0.30.3
84
  stack-data==0.6.3
85
  starlette==0.38.6
86
  sympy==1.13.3
uv.lock CHANGED
@@ -1341,19 +1341,18 @@ wheels = [
1341
 
1342
  [[package]]
1343
  name = "psutil"
1344
- version = "6.0.0"
1345
  source = { registry = "https://pypi.org/simple" }
1346
- sdist = { url = "https://files.pythonhosted.org/packages/18/c7/8c6872f7372eb6a6b2e4708b88419fb46b857f7a2e1892966b851cc79fc9/psutil-6.0.0.tar.gz", hash = "sha256:8faae4f310b6d969fa26ca0545338b21f73c6b15db7c4a8d934a5482faa818f2", size = 508067 }
1347
  wheels = [
1348
- { url = "https://files.pythonhosted.org/packages/c5/66/78c9c3020f573c58101dc43a44f6855d01bbbd747e24da2f0c4491200ea3/psutil-6.0.0-cp27-none-win32.whl", hash = "sha256:02b69001f44cc73c1c5279d02b30a817e339ceb258ad75997325e0e6169d8b35", size = 249766 },
1349
- { url = "https://files.pythonhosted.org/packages/e1/3f/2403aa9558bea4d3854b0e5e567bc3dd8e9fbc1fc4453c0aa9aafeb75467/psutil-6.0.0-cp27-none-win_amd64.whl", hash = "sha256:21f1fb635deccd510f69f485b87433460a603919b45e2a324ad65b0cc74f8fb1", size = 253024 },
1350
- { url = "https://files.pythonhosted.org/packages/0b/37/f8da2fbd29690b3557cca414c1949f92162981920699cd62095a984983bf/psutil-6.0.0-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:c588a7e9b1173b6e866756dde596fd4cad94f9399daf99ad8c3258b3cb2b47a0", size = 250961 },
1351
- { url = "https://files.pythonhosted.org/packages/35/56/72f86175e81c656a01c4401cd3b1c923f891b31fbcebe98985894176d7c9/psutil-6.0.0-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6ed2440ada7ef7d0d608f20ad89a04ec47d2d3ab7190896cd62ca5fc4fe08bf0", size = 287478 },
1352
- { url = "https://files.pythonhosted.org/packages/19/74/f59e7e0d392bc1070e9a70e2f9190d652487ac115bb16e2eff6b22ad1d24/psutil-6.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5fd9a97c8e94059b0ef54a7d4baf13b405011176c3b6ff257c247cae0d560ecd", size = 290455 },
1353
- { url = "https://files.pythonhosted.org/packages/cd/5f/60038e277ff0a9cc8f0c9ea3d0c5eb6ee1d2470ea3f9389d776432888e47/psutil-6.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e2e8d0054fc88153ca0544f5c4d554d42e33df2e009c4ff42284ac9ebdef4132", size = 292046 },
1354
- { url = "https://files.pythonhosted.org/packages/8b/20/2ff69ad9c35c3df1858ac4e094f20bd2374d33c8643cf41da8fd7cdcb78b/psutil-6.0.0-cp37-abi3-win32.whl", hash = "sha256:a495580d6bae27291324fe60cea0b5a7c23fa36a7cd35035a16d93bdcf076b9d", size = 253560 },
1355
- { url = "https://files.pythonhosted.org/packages/73/44/561092313ae925f3acfaace6f9ddc4f6a9c748704317bad9c8c8f8a36a79/psutil-6.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:33ea5e1c975250a720b3a6609c490db40dae5d83a4eb315170c4fe0d8b1f34b3", size = 257399 },
1356
- { url = "https://files.pythonhosted.org/packages/7c/06/63872a64c312a24fb9b4af123ee7007a306617da63ff13bcc1432386ead7/psutil-6.0.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:ffe7fc9b6b36beadc8c322f84e1caff51e8703b88eee1da46d1e3a6ae11b4fd0", size = 251988 },
1357
  ]
1358
 
1359
  [[package]]
@@ -1715,6 +1714,7 @@ dependencies = [
1715
  { name = "gradio-rerun" },
1716
  { name = "opencv-python" },
1717
  { name = "rerun-sdk" },
 
1718
  ]
1719
 
1720
  [package.dev-dependencies]
@@ -1734,6 +1734,7 @@ requires-dist = [
1734
  { name = "gradio-rerun", specifier = ">=0.0.6" },
1735
  { name = "opencv-python", specifier = ">=4.10.0.84" },
1736
  { name = "rerun-sdk", specifier = "==0.18.2" },
 
1737
  ]
1738
 
1739
  [package.metadata.requires-dev]
@@ -1900,6 +1901,24 @@ wheels = [
1900
  { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 },
1901
  ]
1902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1903
  [[package]]
1904
  name = "stack-data"
1905
  version = "0.6.3"
 
1341
 
1342
  [[package]]
1343
  name = "psutil"
1344
+ version = "5.9.8"
1345
  source = { registry = "https://pypi.org/simple" }
1346
+ sdist = { url = "https://files.pythonhosted.org/packages/90/c7/6dc0a455d111f68ee43f27793971cf03fe29b6ef972042549db29eec39a2/psutil-5.9.8.tar.gz", hash = "sha256:6be126e3225486dff286a8fb9a06246a5253f4c7c53b475ea5f5ac934e64194c", size = 503247 }
1347
  wheels = [
1348
+ { url = "https://files.pythonhosted.org/packages/fe/5f/c26deb822fd3daf8fde4bdb658bf87d9ab1ffd3fca483816e89a9a9a9084/psutil-5.9.8-cp27-none-win32.whl", hash = "sha256:36f435891adb138ed3c9e58c6af3e2e6ca9ac2f365efe1f9cfef2794e6c93b4e", size = 248660 },
1349
+ { url = "https://files.pythonhosted.org/packages/32/1d/cf66073d74d6146187e2d0081a7616df4437214afa294ee4f16f80a2f96a/psutil-5.9.8-cp27-none-win_amd64.whl", hash = "sha256:bd1184ceb3f87651a67b2708d4c3338e9b10c5df903f2e3776b62303b26cb631", size = 251966 },
1350
+ { url = "https://files.pythonhosted.org/packages/e7/e3/07ae864a636d70a8a6f58da27cb1179192f1140d5d1da10886ade9405797/psutil-5.9.8-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:aee678c8720623dc456fa20659af736241f575d79429a0e5e9cf88ae0605cc81", size = 248702 },
1351
+ { url = "https://files.pythonhosted.org/packages/b3/bd/28c5f553667116b2598b9cc55908ec435cb7f77a34f2bff3e3ca765b0f78/psutil-5.9.8-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:8cb6403ce6d8e047495a701dc7c5bd788add903f8986d523e3e20b98b733e421", size = 285242 },
1352
+ { url = "https://files.pythonhosted.org/packages/c5/4f/0e22aaa246f96d6ac87fe5ebb9c5a693fbe8877f537a1022527c47ca43c5/psutil-5.9.8-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d06016f7f8625a1825ba3732081d77c94589dca78b7a3fc072194851e88461a4", size = 288191 },
1353
+ { url = "https://files.pythonhosted.org/packages/6e/f5/2aa3a4acdc1e5940b59d421742356f133185667dd190b166dbcfcf5d7b43/psutil-5.9.8-cp37-abi3-win32.whl", hash = "sha256:bc56c2a1b0d15aa3eaa5a60c9f3f8e3e565303b465dbf57a1b730e7a2b9844e0", size = 251252 },
1354
+ { url = "https://files.pythonhosted.org/packages/93/52/3e39d26feae7df0aa0fd510b14012c3678b36ed068f7d78b8d8784d61f0e/psutil-5.9.8-cp37-abi3-win_amd64.whl", hash = "sha256:8db4c1b57507eef143a15a6884ca10f7c73876cdf5d51e713151c1236a0e68cf", size = 255090 },
1355
+ { url = "https://files.pythonhosted.org/packages/05/33/2d74d588408caedd065c2497bdb5ef83ce6082db01289a1e1147f6639802/psutil-5.9.8-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:d16bbddf0693323b8c6123dd804100241da461e41d6e332fb0ba6058f630f8c8", size = 249898 },
 
1356
  ]
1357
 
1358
  [[package]]
 
1714
  { name = "gradio-rerun" },
1715
  { name = "opencv-python" },
1716
  { name = "rerun-sdk" },
1717
+ { name = "spaces" },
1718
  ]
1719
 
1720
  [package.dev-dependencies]
 
1734
  { name = "gradio-rerun", specifier = ">=0.0.6" },
1735
  { name = "opencv-python", specifier = ">=4.10.0.84" },
1736
  { name = "rerun-sdk", specifier = "==0.18.2" },
1737
+ { name = "spaces", specifier = ">=0.30.3" },
1738
  ]
1739
 
1740
  [package.metadata.requires-dev]
 
1901
  { url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235 },
1902
  ]
1903
 
1904
+ [[package]]
1905
+ name = "spaces"
1906
+ version = "0.30.3"
1907
+ source = { registry = "https://pypi.org/simple" }
1908
+ dependencies = [
1909
+ { name = "gradio" },
1910
+ { name = "httpx" },
1911
+ { name = "packaging" },
1912
+ { name = "psutil" },
1913
+ { name = "pydantic" },
1914
+ { name = "requests" },
1915
+ { name = "typing-extensions" },
1916
+ ]
1917
+ sdist = { url = "https://files.pythonhosted.org/packages/cd/17/24e3ba4c812eeafb6647e1a3742d6065a2463d3bda25955f08000965cf50/spaces-0.30.3.tar.gz", hash = "sha256:f25cc0db4831e97df0f8dc5edeaddc11c35d75a51493fdb6eb5a2e8c98d4e923", size = 21139 }
1918
+ wheels = [
1919
+ { url = "https://files.pythonhosted.org/packages/b0/47/a280fd934462a57802d842c5c8c69ad2feb228c7e33232488d879adf08e5/spaces-0.30.3-py3-none-any.whl", hash = "sha256:3d8a9ff66f440c3d72351307f6ea5e30ff12feedd0c43beb0e909fb020029e25", size = 27413 },
1920
+ ]
1921
+
1922
  [[package]]
1923
  name = "stack-data"
1924
  version = "0.6.3"