This is an ONNX export of the
UVDoc document unwarping model,
modified to output a
coordinate grid instead of an image. This enables high-resolution document
unwarping via
cv2.remap().
UVDoc is a deep learning model for correcting perspective distortion and curvature in photographed
documents. Unlike the PaddlePaddle ONNX variant that outputs a fixed 288x288 image, this version
outputs a coordinate mapping grid that can be applied to images of any resolution.
1import cv2
2import numpy as np
3import onnxruntime as ort
4
5# Load model
6session = ort.InferenceSession("UVDoc_grid.onnx", providers=['CPUExecutionProvider'])
7
8# Load and preprocess image
9image = cv2.imread("warped_document.jpg")
10h_orig, w_orig = image.shape[:2]
11
12# Prepare model input (720x496 RGB normalized)
13img_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
14resized = cv2.resize(img_rgb, (496, 720)) # width, height
15blob = resized.astype(np.float32) / 255.0
16blob = np.transpose(blob, (2, 0, 1))[None] # (1, 3, 720, 496)
17
18# Run inference
19result = session.run(None, {'image': blob})[0] # (1, 2, 45, 31)
20
21# Convert grid to remap coordinates
22grid = np.transpose(result[0], (1, 2, 0)) # (45, 31, 2)
23grid_up = cv2.resize(grid, (w_orig, h_orig), interpolation=cv2.INTER_LINEAR)
24
25map_x = ((grid_up[..., 0] + 1) / 2) * (w_orig - 1)
26map_y = ((grid_up[..., 1] + 1) / 2) * (h_orig - 1)
27
28# Apply unwarping to original high-res image
29unwarped = cv2.remap(
30 image,
31 map_x.astype(np.float32),
32 map_y.astype(np.float32),
33 interpolation=cv2.INTER_CUBIC,
34 borderMode=cv2.BORDER_REPLICATE
35)
36
37cv2.imwrite("unwarped_document.jpg", unwarped)
1from huggingface_hub import hf_hub_download
2
3model_path = hf_hub_download(
4 repo_id="YOUR_USERNAME/uvdoc-grid-onnx",
5 filename="UVDoc_grid.onnx"
6)
This model was not retrained. It is a direct ONNX export of the original UVDoc weights from
tanguymagne/UVDoc, with a wrapper to output only the
2D coordinate grid (discarding the 3D shape output).
1@inproceedings{UVDoc,
2 title={{UVDoc}: Neural Grid-based Document Unwarping},
3 author={Floor Verhoeven and Tanguy Magne and Olga Sorkine-Hornung},
4 booktitle = {SIGGRAPH ASIA, Technical Papers},
5 year = {2023},
6 url={https://doi.org/10.1145/3610548.3618174}
7}
This ONNX export is provided under the Apache 2.0 license. The original UVDoc model is also
Apache 2.0 licensed. See the original repository for full license details.