Spaces:
Running
Running
Download source/scripts/reference/make_fixture.py from Segizu/geometria3d-lab: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/spaces/Segizu/geometria3d-lab/resolve/main/source/scripts/reference/make_fixture.py
- Command line
-
hf download hf://spaces/Segizu/geometria3d-lab/source/scripts/reference/make_fixture.py
-
curl -L -o make_fixture.py https://huggingface.co/spaces/Segizu/geometria3d-lab/resolve/main/source/scripts/reference/make_fixture.py
1.93 kB
| import json, numpy as np, onnxruntime as ort, math | |
| from PIL import Image | |
| from scipy.optimize import minimize_scalar | |
| import ref_infer as R | |
| sess = ort.InferenceSession('model.onnx', providers=['CPUExecutionProvider']) | |
| im = Image.open('../public/examples/example-synthetic-room.jpg').convert('RGB').resize((160, 120), Image.BICUBIC) | |
| x = (np.asarray(im).astype(np.float32) / 255).transpose(2, 0, 1)[None] | |
| pts, nrm, msk, sc = sess.run(None, {'image': x, 'num_tokens': np.array(1200, dtype=np.int64)}) | |
| pts, msk = pts[0].astype(np.float32), msk[0].astype(np.float32) | |
| r = R.postprocess(pts.astype(np.float64), msk, float(sc[0])) | |
| # óptimo "exacto" para comparar con el solver JS | |
| H, W = msk.shape; uv = R.uv_grid(W, H); ri, ci = R.nearest_idx(H, 64), R.nearest_idx(W, 64) | |
| m = (msk > 0.5)[ri][:, ci]; U = uv[ri][:, ci][m].reshape(-1, 2); P = pts[ri][:, ci][m].reshape(-1, 3).astype(np.float64) | |
| def cost(s): | |
| xy = P[:, :2] / (P[:, 2] + s)[:, None]; f = (xy * U).sum() / (xy**2).sum(); return 0.5 * ((f * xy - U)**2).sum() | |
| ex = minimize_scalar(cost, bounds=(-P[:, 2].min() + 1e-6, 10), method='bounded', options={'xatol': 1e-10}) | |
| open('../tests/fixtures/synthetic160.bin', 'wb').write(pts.tobytes() + msk.tobytes()) | |
| sample = {f'{i},{j}': r['points'][i, j].tolist() for i, j in [(10, 10), (60, 80), (100, 30), (119, 159)]} | |
| json.dump({'W': W, 'H': H, 'scale': float(sc[0]), 'num_tokens': 1200, 'scipy_lm': {'shift': r['shift'], 'focal': r['focal']}, | |
| 'exact_shift': float(ex.x), 'fx': r['fx'], 'fy': r['fy'], 'fov_x': r['fov_x'], 'fov_y': r['fov_y'], | |
| 'mask_pct': float(r['mask'].mean() * 100), 'sample_points': sample, | |
| 'source': 'Ruicheng/moge-2-vits-normal-onnx@e50ffda41565591092adea54c6ac83d6212e1e23, onnxruntime CPU, examples/example-synthetic-room.jpg a 160x120'}, | |
| open('../tests/fixtures/synthetic160.json', 'w'), indent=1) | |
| print(open('../tests/fixtures/synthetic160.json').read()[:600]) | |