depth2normal 1.0.0__tar.gz → 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {depth2normal-1.0.0 → depth2normal-2.0.0}/.github/workflows/ci.yml +4 -1
  2. {depth2normal-1.0.0 → depth2normal-2.0.0}/.gitignore +3 -0
  3. depth2normal-2.0.0/PKG-INFO +271 -0
  4. depth2normal-2.0.0/README.md +233 -0
  5. depth2normal-2.0.0/assets/CREDITS.md +10 -0
  6. depth2normal-2.0.0/assets/hero.webp +0 -0
  7. depth2normal-2.0.0/assets/pipeline.webp +0 -0
  8. {depth2normal-1.0.0 → depth2normal-2.0.0}/pyproject.toml +22 -4
  9. depth2normal-2.0.0/src/depth2normal/__init__.py +26 -0
  10. depth2normal-2.0.0/src/depth2normal/cli.py +207 -0
  11. depth2normal-2.0.0/src/depth2normal/converter.py +209 -0
  12. depth2normal-2.0.0/src/depth2normal/estimate.py +180 -0
  13. depth2normal-2.0.0/src/depth2normal/filters.py +118 -0
  14. depth2normal-2.0.0/src/depth2normal/onnx_export.py +151 -0
  15. depth2normal-2.0.0/tests/test_cli.py +64 -0
  16. depth2normal-2.0.0/tests/test_converter.py +297 -0
  17. depth2normal-2.0.0/tests/test_estimate.py +65 -0
  18. depth2normal-2.0.0/tests/test_filters.py +91 -0
  19. depth2normal-2.0.0/tests/test_onnx_export.py +86 -0
  20. depth2normal-2.0.0/tools/benchmark.py +108 -0
  21. depth2normal-2.0.0/tools/build_web_models.py +38 -0
  22. {depth2normal-1.0.0 → depth2normal-2.0.0}/uv.lock +178 -140
  23. depth2normal-2.0.0/web/app.js +338 -0
  24. depth2normal-2.0.0/web/index.html +101 -0
  25. depth2normal-2.0.0/web/models/gaussian-0.5.onnx +0 -0
  26. depth2normal-2.0.0/web/models/gaussian-1.onnx +0 -0
  27. depth2normal-2.0.0/web/models/gaussian-2.onnx +0 -0
  28. depth2normal-2.0.0/web/models/gaussian-3.onnx +0 -0
  29. depth2normal-2.0.0/web/models/scharr.onnx +0 -0
  30. depth2normal-2.0.0/web/models/sobel.onnx +0 -0
  31. depth2normal-2.0.0/web/sample-depth.png +0 -0
  32. depth2normal-2.0.0/web/style.css +253 -0
  33. depth2normal-1.0.0/PKG-INFO +0 -157
  34. depth2normal-1.0.0/README.md +0 -126
  35. depth2normal-1.0.0/run.py +0 -25
  36. depth2normal-1.0.0/src/depth2normal/__init__.py +0 -6
  37. depth2normal-1.0.0/src/depth2normal/cli.py +0 -52
  38. depth2normal-1.0.0/src/depth2normal/converter.py +0 -124
  39. depth2normal-1.0.0/tests/test_converter.py +0 -147
  40. {depth2normal-1.0.0 → depth2normal-2.0.0}/.github/workflows/release.yml +0 -0
  41. {depth2normal-1.0.0 → depth2normal-2.0.0}/LICENSE +0 -0
  42. {depth2normal-1.0.0 → depth2normal-2.0.0}/assets/depth.png +0 -0
  43. {depth2normal-1.0.0 → depth2normal-2.0.0}/assets/normal.png +0 -0
  44. {depth2normal-1.0.0 → depth2normal-2.0.0}/src/depth2normal/__main__.py +0 -0
  45. {depth2normal-1.0.0 → depth2normal-2.0.0}/src/depth2normal/py.typed +0 -0
  46. {depth2normal-1.0.0 → depth2normal-2.0.0}/tests/__init__.py +0 -0
@@ -15,13 +15,16 @@ jobs:
15
15
  - run: uv sync --frozen
16
16
  - run: uv run ruff check .
17
17
  - run: uv run ruff format --check .
18
+ # The browser demo ships generated models; they must match the code.
19
+ - run: uv run python tools/build_web_models.py
20
+ - run: git diff --exit-code web/models
18
21
 
19
22
  test:
20
23
  runs-on: ${{ matrix.os }}
21
24
  strategy:
22
25
  matrix:
23
26
  os: [ubuntu-latest, macos-latest]
24
- python-version: ["3.10", "3.11", "3.12", "3.13"]
27
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
25
28
  steps:
26
29
  - uses: actions/checkout@v4
27
30
  - uses: astral-sh/setup-uv@v5
@@ -39,3 +39,6 @@ htmlcov/
39
39
  # OS
40
40
  .DS_Store
41
41
  Thumbs.db
42
+
43
+ # wrangler
44
+ .wrangler/
@@ -0,0 +1,271 @@
1
+ Metadata-Version: 2.5
2
+ Name: depth2normal
3
+ Version: 2.0.0
4
+ Summary: Convert depth maps to normal maps, from a photo if you have no depth map
5
+ Project-URL: Homepage, https://github.com/cobanov/depth2normal
6
+ Project-URL: Repository, https://github.com/cobanov/depth2normal
7
+ Project-URL: Issues, https://github.com/cobanov/depth2normal/issues
8
+ Author: Mert Cobanov
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: 3d,computer-vision,depth-estimation,depth-map,image-processing,normal-map,onnx
12
+ Classifier: Development Status :: 5 - Production/Stable
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Multimedia :: Graphics
24
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
25
+ Classifier: Typing :: Typed
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: click
28
+ Requires-Dist: numpy
29
+ Requires-Dist: pillow
30
+ Provides-Extra: all
31
+ Requires-Dist: onnx; extra == 'all'
32
+ Requires-Dist: onnxruntime>=1.20; (python_version >= '3.11') and extra == 'all'
33
+ Provides-Extra: estimate
34
+ Requires-Dist: onnxruntime>=1.20; (python_version >= '3.11') and extra == 'estimate'
35
+ Provides-Extra: onnx
36
+ Requires-Dist: onnx; extra == 'onnx'
37
+ Description-Content-Type: text/markdown
38
+
39
+ <p align="center">
40
+ <img src="assets/hero.webp" alt="A Mars photograph next to the normal map depth2normal produced from it" width="700">
41
+ </p>
42
+
43
+ <p align="center">
44
+ A depth map goes in, a normal map comes out.<br>
45
+ If you do not have a depth map, it estimates one.
46
+ </p>
47
+
48
+ <p align="center">
49
+ <a href="https://pypi.org/project/depth2normal/"><img alt="pypi" src="https://img.shields.io/pypi/v/depth2normal?color=8c8cff&labelColor=1a1a1a"></a>
50
+ <a href="https://github.com/cobanov/depth2normal/actions/workflows/ci.yml"><img alt="ci" src="https://img.shields.io/github/actions/workflow/status/cobanov/depth2normal/ci.yml?branch=main&color=8c8cff&labelColor=1a1a1a"></a>
51
+ <img alt="tests" src="https://img.shields.io/badge/tests-71-8c8cff?labelColor=1a1a1a">
52
+ <img alt="python" src="https://img.shields.io/badge/python-3.10%2B-8c8cff?labelColor=1a1a1a">
53
+ <a href="LICENSE"><img alt="licence" src="https://img.shields.io/badge/licence-MIT-8c8cff?labelColor=1a1a1a"></a>
54
+ </p>
55
+
56
+ ---
57
+
58
+ Turning depth into normals is a small piece of arithmetic: two gradients, a
59
+ vector, one normalisation. The parts that are usually missing sit on either
60
+ side of it. You need a depth map before you can start, and you need Python at
61
+ the other end to run the result. This package covers both.
62
+
63
+ ```sh
64
+ pip install depth2normal
65
+ depth2normal depth.png -o normal.png
66
+ ```
67
+
68
+ - **No depth map, no problem.** `--estimate` runs Depth Anything V2 Small on
69
+ the photo first, so a photograph is a valid input.
70
+ - **The conversion exports to ONNX**, in about 1.7 KB, so it runs in a browser,
71
+ in C#, in Unity, in a ComfyUI node, anywhere ONNX Runtime goes.
72
+ - **Scale invariant.** An 8-bit and a 16-bit copy of one depth map produce
73
+ identical normals, so `strength` means the same thing in every file.
74
+ - **Three runtime dependencies**: NumPy, Pillow and Click. No SciPy, no OpenCV.
75
+ - **A browser demo in `web/`**, no build step, nothing uploaded.
76
+
77
+ ## Install
78
+
79
+ ```sh
80
+ pip install depth2normal # the converter
81
+ pip install 'depth2normal[all]' # plus ONNX export and depth estimation
82
+ ```
83
+
84
+ `uv add depth2normal` works the same way. `[onnx]` is the export, `[estimate]`
85
+ is depth estimation, which needs Python 3.11 or newer because ONNX Runtime
86
+ publishes no 3.10 wheels.
87
+
88
+ ## Use
89
+
90
+ ```sh
91
+ depth2normal depth.png -o normal.png # the common case
92
+ depth2normal depth.png -s 3 -m scharr # stronger relief, sharper filter
93
+ depth2normal depth.png --invert # for maps where bright means far
94
+ depth2normal depth.tif --focal 1200 --range raw # metric depth from a real camera
95
+ depth2normal photo.jpg --estimate -o normal.png # no depth map needed
96
+ depth2normal estimate photo.jpg -o depth.png # just the depth map
97
+ depth2normal export -o depth2normal.onnx # just the graph
98
+ ```
99
+
100
+ | Option | Default | What it does |
101
+ | --- | --- | --- |
102
+ | `-o`, `--output` | `normal_map.png` | Where the normal map goes |
103
+ | `-s`, `--strength` | `1.0` | Gradient multiplier, so how pronounced the relief is |
104
+ | `-m`, `--method` | `gaussian` | `gaussian`, `sobel` or `scharr` |
105
+ | `--sigma` | `1.0` | Gaussian kernel width, smoothness against detail |
106
+ | `--range` | `auto` | `auto`, `minmax` or `raw`, see [How it works](#how-it-works) |
107
+ | `--invert` | off | Flip near and far |
108
+ | `--focal` | | Focal length in pixels, for metric depth (see below) |
109
+ | `--estimate` | off | Treat the input as a photo and estimate its depth first |
110
+ | `--save-depth` | | Keep the estimated depth map as well |
111
+
112
+ ```python
113
+ import depth2normal
114
+
115
+ depth2normal.convert("depth.png", "normal.png", strength=2.0, method="scharr")
116
+
117
+ depth = depth2normal.estimate_depth("photo.jpg") # needs [estimate]
118
+ normal = depth2normal.depth_to_normal(depth, strength=3.0)
119
+
120
+ depth2normal.export_onnx("depth2normal.onnx") # needs [onnx]
121
+ ```
122
+
123
+ ## From a photo
124
+
125
+ <p align="center">
126
+ <img src="assets/pipeline.webp" alt="A photograph, the depth map estimated from it, and the resulting normal map" width="700">
127
+ </p>
128
+
129
+ `--estimate` runs [Depth Anything V2 Small](https://huggingface.co/onnx-community/depth-anything-v2-small)
130
+ (Apache-2.0, 99 MB) through ONNX Runtime, on CUDA if `onnxruntime-gpu` is
131
+ installed. The model is downloaded once, checksummed, and cached in
132
+ `~/.cache/depth2normal`, or wherever `DEPTH2NORMAL_CACHE` points.
133
+
134
+ What comes back is relative depth, not metres. That is what a normal map
135
+ needs, and it is not what a measurement needs.
136
+
137
+ ## Anywhere ONNX Runtime goes
138
+
139
+ ```sh
140
+ depth2normal export -o depth2normal.onnx --method gaussian --sigma 1
141
+ ```
142
+
143
+ | | |
144
+ | --- | --- |
145
+ | Input `depth` | float32 `[1, 1, H, W]`, on the 0-255 range, height and width dynamic |
146
+ | Input `strength` | float32 scalar, so relief is tunable without re-exporting |
147
+ | Output `normal_map` | uint8 `[1, H, W, 3]`, RGB, ready for a canvas or an image file |
148
+ | Size | 1.1 KB for `sobel`, 6.1 KB for `gaussian` at sigma 3 |
149
+
150
+ Method and sigma are baked into the convolution weights, because they are the
151
+ kernel. Strength is not, so one file covers every strength.
152
+
153
+ The graph and the NumPy path agree to within **one level out of 255** on at
154
+ most 3% of pixels, which is float32 against float64 at a rounding boundary.
155
+ `tests/test_onnx_export.py` asserts it.
156
+
157
+ Prebuilt models for every method are attached to
158
+ [the latest release](https://github.com/cobanov/depth2normal/releases/latest).
159
+
160
+ ## In the browser
161
+
162
+ **[depth2normal.cobanov.dev](https://depth2normal.cobanov.dev)** is the same
163
+ conversion as a single page: it loads the exported graph and runs everything
164
+ client side. The source is `web/`, with no build step and no bundler:
165
+
166
+ ```sh
167
+ cd web && python3 -m http.server
168
+ ```
169
+
170
+ A depth map converts in a few milliseconds of WebAssembly. Press **Estimate
171
+ depth** on a photograph and the quantised 27 MB model runs in the tab as well,
172
+ about 6.5 seconds for a 640x480 photo. Nothing is uploaded either way.
173
+
174
+ ## How it works
175
+
176
+ 1. Bring the depth values onto a fixed 0-255 range (see the table below).
177
+ 2. Estimate `dx` and `dy` with a separable derivative filter, reflecting at the
178
+ border.
179
+ 3. Build `(-dx * strength, -dy * strength, 1)`, normalise it to unit length,
180
+ and map it to 8-bit RGB.
181
+
182
+ Step 1 is the one that matters: it is what makes `strength` mean the same
183
+ thing whatever the input's bit depth.
184
+
185
+ | `--range` | What it does | When |
186
+ | --- | --- | --- |
187
+ | `auto` | Divides by the range the values came from: 0-1 float, 8-bit, or 16-bit | The default. Steady across a sequence |
188
+ | `minmax` | Stretches this image's min and max to 0-255 | Low contrast maps |
189
+ | `raw` | Leaves the values alone | Metric depth, or your own scaling |
190
+
191
+ The gradient filters:
192
+
193
+ | Method | Quality | Notes |
194
+ | --- | --- | --- |
195
+ | `gaussian` | Best | Gaussian derivative. `--sigma` trades smoothness against detail |
196
+ | `sobel` | Good | Classic 3x3. Sharp, but staircases on quantised depth |
197
+ | `scharr` | Good | Better rotational accuracy than Sobel, same speed |
198
+
199
+ All three are separable, which is what lets the package drop SciPy and still
200
+ match it to 1e-13 at the same speed, with 19.5 MB less to install. Their
201
+ kernels are normalised, so `strength` means the same thing whichever you pick.
202
+
203
+ ### Metric depth
204
+
205
+ The default treats depth as a height field, which is what a shading normal map
206
+ wants. Given `--focal` (in pixels) it treats the depth as metric distance from
207
+ a pinhole camera instead, unprojects each pixel, and returns the normals of
208
+ that 3-D surface: the same plane twice as far away then comes out with the same
209
+ orientation. Pair it with `--range raw` so the metric values survive. The
210
+ exported ONNX graph is the height field form.
211
+
212
+ ### Rings in the output
213
+
214
+ Contour rings are the source depth map's 8-bit steps, amplified by the
215
+ derivative. Raising `--sigma` smooths them away, and a 16-bit depth map does
216
+ not have them at all. Error against the same surface before quantisation:
217
+
218
+ | Source | Mean error |
219
+ | --- | --- |
220
+ | 8-bit, `--sigma 0.5` | 7.1 levels |
221
+ | 8-bit, `--sigma 1` | 2.4 levels |
222
+ | 8-bit, `--sigma 3` | 0.2 levels |
223
+ | 16-bit, `--sigma 1` | 0.01 levels |
224
+
225
+ ## Measured
226
+
227
+ `tools/benchmark.py` prints these: median of five runs, `gaussian` at sigma 1,
228
+ on synthetic depth maps.
229
+
230
+ **Apple M4 Pro**, Python 3.14, ONNX Runtime on the CPU
231
+
232
+ | Size | NumPy | ONNX Runtime |
233
+ | --- | --- | --- |
234
+ | 1920 x 1080 | 53 ms | 12 ms |
235
+ | 1080 x 1920 | 52 ms | 10 ms |
236
+ | 4096 x 2304 | 243 ms | 54 ms |
237
+ | 2304 x 4096 | 243 ms | 55 ms |
238
+ | 8192 x 4608 | 958 ms | 211 ms |
239
+
240
+ **Intel i5-9600K with an RTX 3090**, Python 3.12
241
+
242
+ | Size | NumPy | ONNX Runtime | CUDA |
243
+ | --- | --- | --- | --- |
244
+ | 1920 x 1080 | 188 ms | 40 ms | 3 ms |
245
+ | 1080 x 1920 | 191 ms | 40 ms | 3 ms |
246
+ | 4096 x 2304 | 1003 ms | 216 ms | 13 ms |
247
+ | 2304 x 4096 | 984 ms | 184 ms | 13 ms |
248
+
249
+ Orientation is free: a portrait image and a landscape one of the same pixel
250
+ count land within a few percent of each other. The exported graph is four to
251
+ five times faster than NumPy on a CPU, and on a GPU it stops being the part
252
+ worth timing. The 37 megapixel row is missing from the second table because
253
+ that card was busy serving another model.
254
+
255
+ Depth estimation of a 640x480 photo takes 231 ms on the M4 Pro CPU and 32 ms on
256
+ the 3090.
257
+
258
+ ## Development
259
+
260
+ ```sh
261
+ uv sync --extra all
262
+ uv run pytest
263
+ uv run ruff check .
264
+ uv run ruff format --check .
265
+ uv run python tools/build_web_models.py
266
+ ```
267
+
268
+ ## Licence
269
+
270
+ MIT. The depth model is Apache-2.0 and is downloaded, not vendored. Image
271
+ credits are in [assets/CREDITS.md](assets/CREDITS.md).
@@ -0,0 +1,233 @@
1
+ <p align="center">
2
+ <img src="assets/hero.webp" alt="A Mars photograph next to the normal map depth2normal produced from it" width="700">
3
+ </p>
4
+
5
+ <p align="center">
6
+ A depth map goes in, a normal map comes out.<br>
7
+ If you do not have a depth map, it estimates one.
8
+ </p>
9
+
10
+ <p align="center">
11
+ <a href="https://pypi.org/project/depth2normal/"><img alt="pypi" src="https://img.shields.io/pypi/v/depth2normal?color=8c8cff&labelColor=1a1a1a"></a>
12
+ <a href="https://github.com/cobanov/depth2normal/actions/workflows/ci.yml"><img alt="ci" src="https://img.shields.io/github/actions/workflow/status/cobanov/depth2normal/ci.yml?branch=main&color=8c8cff&labelColor=1a1a1a"></a>
13
+ <img alt="tests" src="https://img.shields.io/badge/tests-71-8c8cff?labelColor=1a1a1a">
14
+ <img alt="python" src="https://img.shields.io/badge/python-3.10%2B-8c8cff?labelColor=1a1a1a">
15
+ <a href="LICENSE"><img alt="licence" src="https://img.shields.io/badge/licence-MIT-8c8cff?labelColor=1a1a1a"></a>
16
+ </p>
17
+
18
+ ---
19
+
20
+ Turning depth into normals is a small piece of arithmetic: two gradients, a
21
+ vector, one normalisation. The parts that are usually missing sit on either
22
+ side of it. You need a depth map before you can start, and you need Python at
23
+ the other end to run the result. This package covers both.
24
+
25
+ ```sh
26
+ pip install depth2normal
27
+ depth2normal depth.png -o normal.png
28
+ ```
29
+
30
+ - **No depth map, no problem.** `--estimate` runs Depth Anything V2 Small on
31
+ the photo first, so a photograph is a valid input.
32
+ - **The conversion exports to ONNX**, in about 1.7 KB, so it runs in a browser,
33
+ in C#, in Unity, in a ComfyUI node, anywhere ONNX Runtime goes.
34
+ - **Scale invariant.** An 8-bit and a 16-bit copy of one depth map produce
35
+ identical normals, so `strength` means the same thing in every file.
36
+ - **Three runtime dependencies**: NumPy, Pillow and Click. No SciPy, no OpenCV.
37
+ - **A browser demo in `web/`**, no build step, nothing uploaded.
38
+
39
+ ## Install
40
+
41
+ ```sh
42
+ pip install depth2normal # the converter
43
+ pip install 'depth2normal[all]' # plus ONNX export and depth estimation
44
+ ```
45
+
46
+ `uv add depth2normal` works the same way. `[onnx]` is the export, `[estimate]`
47
+ is depth estimation, which needs Python 3.11 or newer because ONNX Runtime
48
+ publishes no 3.10 wheels.
49
+
50
+ ## Use
51
+
52
+ ```sh
53
+ depth2normal depth.png -o normal.png # the common case
54
+ depth2normal depth.png -s 3 -m scharr # stronger relief, sharper filter
55
+ depth2normal depth.png --invert # for maps where bright means far
56
+ depth2normal depth.tif --focal 1200 --range raw # metric depth from a real camera
57
+ depth2normal photo.jpg --estimate -o normal.png # no depth map needed
58
+ depth2normal estimate photo.jpg -o depth.png # just the depth map
59
+ depth2normal export -o depth2normal.onnx # just the graph
60
+ ```
61
+
62
+ | Option | Default | What it does |
63
+ | --- | --- | --- |
64
+ | `-o`, `--output` | `normal_map.png` | Where the normal map goes |
65
+ | `-s`, `--strength` | `1.0` | Gradient multiplier, so how pronounced the relief is |
66
+ | `-m`, `--method` | `gaussian` | `gaussian`, `sobel` or `scharr` |
67
+ | `--sigma` | `1.0` | Gaussian kernel width, smoothness against detail |
68
+ | `--range` | `auto` | `auto`, `minmax` or `raw`, see [How it works](#how-it-works) |
69
+ | `--invert` | off | Flip near and far |
70
+ | `--focal` | | Focal length in pixels, for metric depth (see below) |
71
+ | `--estimate` | off | Treat the input as a photo and estimate its depth first |
72
+ | `--save-depth` | | Keep the estimated depth map as well |
73
+
74
+ ```python
75
+ import depth2normal
76
+
77
+ depth2normal.convert("depth.png", "normal.png", strength=2.0, method="scharr")
78
+
79
+ depth = depth2normal.estimate_depth("photo.jpg") # needs [estimate]
80
+ normal = depth2normal.depth_to_normal(depth, strength=3.0)
81
+
82
+ depth2normal.export_onnx("depth2normal.onnx") # needs [onnx]
83
+ ```
84
+
85
+ ## From a photo
86
+
87
+ <p align="center">
88
+ <img src="assets/pipeline.webp" alt="A photograph, the depth map estimated from it, and the resulting normal map" width="700">
89
+ </p>
90
+
91
+ `--estimate` runs [Depth Anything V2 Small](https://huggingface.co/onnx-community/depth-anything-v2-small)
92
+ (Apache-2.0, 99 MB) through ONNX Runtime, on CUDA if `onnxruntime-gpu` is
93
+ installed. The model is downloaded once, checksummed, and cached in
94
+ `~/.cache/depth2normal`, or wherever `DEPTH2NORMAL_CACHE` points.
95
+
96
+ What comes back is relative depth, not metres. That is what a normal map
97
+ needs, and it is not what a measurement needs.
98
+
99
+ ## Anywhere ONNX Runtime goes
100
+
101
+ ```sh
102
+ depth2normal export -o depth2normal.onnx --method gaussian --sigma 1
103
+ ```
104
+
105
+ | | |
106
+ | --- | --- |
107
+ | Input `depth` | float32 `[1, 1, H, W]`, on the 0-255 range, height and width dynamic |
108
+ | Input `strength` | float32 scalar, so relief is tunable without re-exporting |
109
+ | Output `normal_map` | uint8 `[1, H, W, 3]`, RGB, ready for a canvas or an image file |
110
+ | Size | 1.1 KB for `sobel`, 6.1 KB for `gaussian` at sigma 3 |
111
+
112
+ Method and sigma are baked into the convolution weights, because they are the
113
+ kernel. Strength is not, so one file covers every strength.
114
+
115
+ The graph and the NumPy path agree to within **one level out of 255** on at
116
+ most 3% of pixels, which is float32 against float64 at a rounding boundary.
117
+ `tests/test_onnx_export.py` asserts it.
118
+
119
+ Prebuilt models for every method are attached to
120
+ [the latest release](https://github.com/cobanov/depth2normal/releases/latest).
121
+
122
+ ## In the browser
123
+
124
+ **[depth2normal.cobanov.dev](https://depth2normal.cobanov.dev)** is the same
125
+ conversion as a single page: it loads the exported graph and runs everything
126
+ client side. The source is `web/`, with no build step and no bundler:
127
+
128
+ ```sh
129
+ cd web && python3 -m http.server
130
+ ```
131
+
132
+ A depth map converts in a few milliseconds of WebAssembly. Press **Estimate
133
+ depth** on a photograph and the quantised 27 MB model runs in the tab as well,
134
+ about 6.5 seconds for a 640x480 photo. Nothing is uploaded either way.
135
+
136
+ ## How it works
137
+
138
+ 1. Bring the depth values onto a fixed 0-255 range (see the table below).
139
+ 2. Estimate `dx` and `dy` with a separable derivative filter, reflecting at the
140
+ border.
141
+ 3. Build `(-dx * strength, -dy * strength, 1)`, normalise it to unit length,
142
+ and map it to 8-bit RGB.
143
+
144
+ Step 1 is the one that matters: it is what makes `strength` mean the same
145
+ thing whatever the input's bit depth.
146
+
147
+ | `--range` | What it does | When |
148
+ | --- | --- | --- |
149
+ | `auto` | Divides by the range the values came from: 0-1 float, 8-bit, or 16-bit | The default. Steady across a sequence |
150
+ | `minmax` | Stretches this image's min and max to 0-255 | Low contrast maps |
151
+ | `raw` | Leaves the values alone | Metric depth, or your own scaling |
152
+
153
+ The gradient filters:
154
+
155
+ | Method | Quality | Notes |
156
+ | --- | --- | --- |
157
+ | `gaussian` | Best | Gaussian derivative. `--sigma` trades smoothness against detail |
158
+ | `sobel` | Good | Classic 3x3. Sharp, but staircases on quantised depth |
159
+ | `scharr` | Good | Better rotational accuracy than Sobel, same speed |
160
+
161
+ All three are separable, which is what lets the package drop SciPy and still
162
+ match it to 1e-13 at the same speed, with 19.5 MB less to install. Their
163
+ kernels are normalised, so `strength` means the same thing whichever you pick.
164
+
165
+ ### Metric depth
166
+
167
+ The default treats depth as a height field, which is what a shading normal map
168
+ wants. Given `--focal` (in pixels) it treats the depth as metric distance from
169
+ a pinhole camera instead, unprojects each pixel, and returns the normals of
170
+ that 3-D surface: the same plane twice as far away then comes out with the same
171
+ orientation. Pair it with `--range raw` so the metric values survive. The
172
+ exported ONNX graph is the height field form.
173
+
174
+ ### Rings in the output
175
+
176
+ Contour rings are the source depth map's 8-bit steps, amplified by the
177
+ derivative. Raising `--sigma` smooths them away, and a 16-bit depth map does
178
+ not have them at all. Error against the same surface before quantisation:
179
+
180
+ | Source | Mean error |
181
+ | --- | --- |
182
+ | 8-bit, `--sigma 0.5` | 7.1 levels |
183
+ | 8-bit, `--sigma 1` | 2.4 levels |
184
+ | 8-bit, `--sigma 3` | 0.2 levels |
185
+ | 16-bit, `--sigma 1` | 0.01 levels |
186
+
187
+ ## Measured
188
+
189
+ `tools/benchmark.py` prints these: median of five runs, `gaussian` at sigma 1,
190
+ on synthetic depth maps.
191
+
192
+ **Apple M4 Pro**, Python 3.14, ONNX Runtime on the CPU
193
+
194
+ | Size | NumPy | ONNX Runtime |
195
+ | --- | --- | --- |
196
+ | 1920 x 1080 | 53 ms | 12 ms |
197
+ | 1080 x 1920 | 52 ms | 10 ms |
198
+ | 4096 x 2304 | 243 ms | 54 ms |
199
+ | 2304 x 4096 | 243 ms | 55 ms |
200
+ | 8192 x 4608 | 958 ms | 211 ms |
201
+
202
+ **Intel i5-9600K with an RTX 3090**, Python 3.12
203
+
204
+ | Size | NumPy | ONNX Runtime | CUDA |
205
+ | --- | --- | --- | --- |
206
+ | 1920 x 1080 | 188 ms | 40 ms | 3 ms |
207
+ | 1080 x 1920 | 191 ms | 40 ms | 3 ms |
208
+ | 4096 x 2304 | 1003 ms | 216 ms | 13 ms |
209
+ | 2304 x 4096 | 984 ms | 184 ms | 13 ms |
210
+
211
+ Orientation is free: a portrait image and a landscape one of the same pixel
212
+ count land within a few percent of each other. The exported graph is four to
213
+ five times faster than NumPy on a CPU, and on a GPU it stops being the part
214
+ worth timing. The 37 megapixel row is missing from the second table because
215
+ that card was busy serving another model.
216
+
217
+ Depth estimation of a 640x480 photo takes 231 ms on the M4 Pro CPU and 32 ms on
218
+ the 3090.
219
+
220
+ ## Development
221
+
222
+ ```sh
223
+ uv sync --extra all
224
+ uv run pytest
225
+ uv run ruff check .
226
+ uv run ruff format --check .
227
+ uv run python tools/build_web_models.py
228
+ ```
229
+
230
+ ## Licence
231
+
232
+ MIT. The depth model is Apache-2.0 and is downloaded, not vendored. Image
233
+ credits are in [assets/CREDITS.md](assets/CREDITS.md).
@@ -0,0 +1,10 @@
1
+ # Image credits
2
+
3
+ `hero.webp` and `pipeline.webp` are built from *Curiosity Self-Portrait at
4
+ Mojave Site on Mount Sharp* (NASA/JPL-Caltech/MSSS, PIA19142), a NASA image in
5
+ the public domain: https://images.nasa.gov/details/PIA19142
6
+
7
+ The depth map beside it was estimated by this package, and the normal map was
8
+ converted from that depth map.
9
+
10
+ `depth.png` and `normal.png` are the project's own sample pair.
Binary file
Binary file
@@ -4,13 +4,21 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "depth2normal"
7
- version = "1.0.0"
8
- description = "Convert depth map images to normal map images"
7
+ version = "2.0.0"
8
+ description = "Convert depth maps to normal maps, from a photo if you have no depth map"
9
9
  readme = "README.md"
10
10
  license = "MIT"
11
11
  requires-python = ">=3.10"
12
12
  authors = [{ name = "Mert Cobanov" }]
13
- keywords = ["depth-map", "normal-map", "3d", "computer-vision", "image-processing"]
13
+ keywords = [
14
+ "depth-map",
15
+ "normal-map",
16
+ "3d",
17
+ "computer-vision",
18
+ "image-processing",
19
+ "onnx",
20
+ "depth-estimation",
21
+ ]
14
22
  classifiers = [
15
23
  "Development Status :: 5 - Production/Stable",
16
24
  "Intended Audience :: Developers",
@@ -22,6 +30,7 @@ classifiers = [
22
30
  "Programming Language :: Python :: 3.11",
23
31
  "Programming Language :: Python :: 3.12",
24
32
  "Programming Language :: Python :: 3.13",
33
+ "Programming Language :: Python :: 3.14",
25
34
  "Topic :: Multimedia :: Graphics",
26
35
  "Topic :: Scientific/Engineering :: Image Processing",
27
36
  "Typing :: Typed",
@@ -29,10 +38,17 @@ classifiers = [
29
38
  dependencies = [
30
39
  "numpy",
31
40
  "Pillow",
32
- "scipy",
33
41
  "click",
34
42
  ]
35
43
 
44
+ [project.optional-dependencies]
45
+ # Export the conversion as a standalone ONNX graph.
46
+ onnx = ["onnx"]
47
+ # Estimate a depth map from a photo with Depth Anything V2 Small.
48
+ # ONNX Runtime stopped publishing wheels for Python 3.10, so estimation needs 3.11.
49
+ estimate = ["onnxruntime>=1.20; python_version >= '3.11'"]
50
+ all = ["onnx", "onnxruntime>=1.20; python_version >= '3.11'"]
51
+
36
52
  [project.scripts]
37
53
  depth2normal = "depth2normal.cli:cli"
38
54
 
@@ -53,6 +69,8 @@ testpaths = ["tests"]
53
69
 
54
70
  [dependency-groups]
55
71
  dev = [
72
+ "onnx",
73
+ "onnxruntime>=1.20; python_version >= '3.11'",
56
74
  "pytest",
57
75
  "ruff",
58
76
  ]
@@ -0,0 +1,26 @@
1
+ """depth2normal -- convert depth maps to normal maps."""
2
+
3
+ from depth2normal.converter import (
4
+ METHODS,
5
+ RANGES,
6
+ convert,
7
+ depth_to_normal,
8
+ load_depth,
9
+ rescale_depth,
10
+ )
11
+ from depth2normal.estimate import estimate_depth
12
+ from depth2normal.onnx_export import build_model, export_onnx
13
+
14
+ __version__ = "2.0.0"
15
+ __all__ = [
16
+ "METHODS",
17
+ "RANGES",
18
+ "__version__",
19
+ "build_model",
20
+ "convert",
21
+ "depth_to_normal",
22
+ "estimate_depth",
23
+ "export_onnx",
24
+ "load_depth",
25
+ "rescale_depth",
26
+ ]