跪拜 Guibai
← All articles
Backend · Frontend

Baidu's 6 MB OCR Model Runs Entirely in the Browser with No Backend

By 半刻纬度 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Client-side OCR at 6 MB removes the operational overhead of deploying, scaling, and paying for a Python inference service. For internal tools and low-volume apps, the browser handles the GPU work directly, and the model is small enough to load over a spotty connection without a noticeable wait.

Summary

Baidu's PP-OCRv6 tiny packs a text-detection model (1.74 MB) and a text-recognition model (4.28 MB) into a combined 6 MB payload that loads in the browser and runs inference through ONNX Runtime Web on WebGL or WebGPU. The pipeline resizes images, runs DBNet-based detection with binarization and connected-component post-processing, then feeds cropped text regions into a CRNN recognizer with CTC greedy decoding. The entire flow—upload, detect, recognize, display—happens client-side with no backend service.

Three concrete pitfalls surfaced during the build. A mismatched character dictionary caused garbled output because the ONNX model outputs 6,906 classes but the downloaded dictionary held only 6,622 entries; extracting the character set directly from the model's protobuf metadata fixed it. NaN values in model output propagated through softmax and required explicit finite-value guards during argmax and exponentiation. A 0.3 confidence threshold on softmax over 6,906 dimensions wiped out all results because per-character probabilities rarely exceed 0.05.

Switching to the small model variant broke the frontend post-processing entirely: small uses a single-channel binary map with contour detection instead of DBNet's three-channel probability map and unclip expansion, so the detection pipeline needs a rewrite.

Takeaways
PP-OCRv6 tiny's detection model is 1.74 MB and its recognition model is 4.28 MB, totaling just over 6 MB.
The detection model uses DBNet and outputs a three-channel probability map; post-processing requires binarization at threshold 0.2, connected-component labeling via BFS, and unclip expansion with a coefficient of 1.4.
Input images must be resized so the longest side is ≤ 960 and dimensions are multiples of 32, otherwise the model's stride-32 downsampling truncates the output.
The recognition model outputs a [1, T, 6906] tensor decoded with CTC greedy decoding: take argmax per timestep, drop blanks (index 0), merge consecutive duplicates.
A character dictionary mismatch—6,622 entries vs. the model's 6,906 output classes—produced total gibberish; extracting the charset from the ONNX model's protobuf metadata resolved it.
NaN values in model output propagate through Math.exp during softmax and require guards: skip !isFinite values in argmax, skip diff < -50 or !isFinite in softmax, and clamp confidence to [0.001, 0.999].
A softmax confidence threshold of 0.3 filtered out every result because per-character probability over 6,906 classes rarely exceeds 0.05; the threshold was dropped to 0.
The small model variant replaces DBNet with a single-channel binary map and contour detection, breaking the tiny model's post-processing code entirely.
Conclusions

Running a production-grade OCR pipeline in the browser at 6 MB is a genuine alternative to server-side deployment for many internal tools, not just a demo trick.

The jump from tiny to small is not a drop-in upgrade; the detection head architecture changes completely, which means model selection locks in your post-processing code.

Extracting character sets from ONNX metadata is a more reliable pattern than shipping a separate dictionary file that can drift from the model's actual output classes.

Softmax over very large vocabularies (6,906 classes) produces per-class probabilities so low that conventional confidence thresholds become meaningless without recalibration.

Concepts & terms
ONNX Runtime Web
Microsoft's browser-based inference engine that runs ONNX models using WebGL or WebGPU, enabling client-side AI without a backend server.
DBNet
A text-detection architecture that outputs a probability map; post-processing binarizes it, finds connected components, and expands boundaries with an unclip operation to recover full text regions.
CTC Greedy Decoding
A decoding algorithm for sequence models that picks the highest-probability class per timestep, removes blank tokens, and merges consecutive duplicates to produce the final text string.
PP-OCRv6
Baidu's latest open-source OCR model series available in tiny, small, and medium sizes; the tiny variant uses DBNet for detection and CRNN for recognition.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗