Reading text from a line image with one model works only while the text is a single line, dead center and always the same size. Real parts carry batch codes at an angle, in several places, at several sizes. The standard answer is a two-stage OCR pipeline: a detector finds where text is, a recognizer reads each crop, and a bookkeeping step reassembles every frame's results. This page explains the architecture — detection postprocess, crop resizing, recognizer input, frame-ID synchronization — with the pixel arithmetic to size it and the hardware it runs on.
What the two stages do
An OCR recognizer has a narrow contract: one text line, resized to a fixed height, filling most of the input strip. Hand him a whole frame and it reads one line and ignores the rest — or you slide a search window across the image and pay for dozens of passes per frame. A detector has the opposite contract: it answers where, not what, and it answers for every text region at once. Two models, each doing the half it is good at.
Between them sits glue code, and glue is where these pipelines fail: turning the detector's heatmap into boxes, resizing each crop into the recognizer's input, and matching results back to frames. The stage order is always the same, each stage in its own thread with a bounded queue in between:
preprocess → text detector → detection postprocess → text recognizer → OCR postprocess → output
Stage 1: from heatmap to boxes
Scene-text detectors in the differentiable-binarization family do not regress box corners. They output one probability per pixel — text or background — and your postprocess turns that map into rectangles. Putting the binarization inside the network, the differentiable part of DB, trains sharper probability maps than thresholding a plain segmentation head after it; either way, you consume the map like this:
- Threshold the map at 0.3, the DB postprocess default.
- Find connected components and fit a minimum-area rectangle to each.
- Keep boxes scoring above 0.5; raise the cut toward 0.7 when texture in the scene creates false boxes.
- Expand each box by an unclip ratio of 1.5, up to 2.0 when the outermost characters get cut, then clamp to the frame.
- Stamp every box with its frame ID and box index, and record the box count for that frame.
Zero boxes is a result, not an error. The frame goes straight to the output stage with empty text, and the recognizer is never called.
Stage 2: crop, resize and pad to the recognizer's input
CRNN-style recognizers take a strip of fixed height — 32, 48 or 64 px, with 48 px the common choice — and a fixed width, 320 px on the usual line models. Every crop must arrive at exactly that shape, in three steps:
- Crop from the original frame, never from the detector's downscaled input. Detector inputs run at half to a quarter of frame resolution; a crop taken there has already lost the pixels the recognizer needs.
- Resize with the aspect ratio held: crop height maps onto the recognizer's input height.
- Pad the leftover width with the recognizer's background value, which is zero after normalization.
For the cropping and resizing code itself, detection to OCR pipeline: crop detected regions then read text walks the OpenCV side step by step.
Sizing the numbers: one frame end to end
Take a 5 MP-class frame, 2560 px wide, covering 250 mm of conveyor, with a batch code 4 mm tall at a 2.4 mm character pitch:
- Pixel size: 250 mm ÷ 2560 px = 0.098 mm per pixel.
- Character height: 4 mm ÷ 0.098 mm/px ≈ 41 px in the frame.
- Detector input at 960 px on the long side: 960 ÷ 2560 = 0.375, so the text is 41 × 0.375 ≈ 15 px inside the detector. That clears the practical floor: below about 10 px in the detector input, misses start.
- Code width: 10 characters × 2.4 mm = 24 mm, so 24 ÷ 0.098 ≈ 245 px.
- With 20 % margin: 245 × 1.2 ≈ 294 px wide, 41 × 1.2 ≈ 49 px tall.
- Recognizer input 48 px high: scale = 48 ÷ 49 ≈ 0.98, so width 294 × 0.98 ≈ 288 px.
- Pad to the 320 px input: 320 − 288 = 32 px of padding.
The decision this arithmetic hands you: 4 mm characters pass every stage with margin at this geometry. Characters at 2 mm would be 20 px in the frame and 8 px inside the detector — under the floor. Narrow the field of view or close the distance before touching the models; minimum font size for OCR lists the fixes in order of cost.
The camera resolution calculator runs the first two lines for your own sensor and field of view.
Frame IDs: keeping crops with their frame
Past the detector, every object in flight carries a frame ID. Detection postprocess opens one entry per frame — frame reference, expected box count, empty results list — and each recognizer result lands in its entry. When received equals expected, the collector releases one bundle of frame, texts and boxes, then deletes the entry.
Arrival order is not guaranteed. Three crops enter the recognizer queue back to back and can finish out of order once batching or a second thread is involved; the frame ID, not queue order, decides where a result belongs.
Give the table a sweep on every emitted bundle: any entry more than 10 frames behind the newest is released as a partial read, logged, and deleted.
When a frame stalls, timestamps at each handoff tell you which queue backed up; video pipeline latency per stage covers the instrumentation.
Throughput and latency
Pipeline rate is set by the slowest stage, latency by the sum. Take a detector pass of 25 ms and a recognizer pass of 12 ms per crop on the same accelerator:
- One code per frame: stage costs 25 ms and 12 ms, ceiling 1 ÷ 0.025 = 40 fps.
- Three codes per frame: recognizer stage 3 × 12 = 36 ms, ceiling 1 ÷ 0.036 ≈ 28 fps.
- One frame end to end: 25 + 12 plus queue hops ≈ 40 ms.
When the recognizer becomes the slow stage, two fixes. Batch one frame's crops into a single call — one queue hop, same compute, and the runtime schedules the batch as a unit. Or run a second recognizer thread; scaling holds until the accelerator saturates. Speed up batch OCR processing shows the batching pattern in code.
Where the pipeline runs
A smart camera runs both models on the device, and the frame bookkeeping lives in firmware — the PLC only sees decoded strings over Ethernet. The MV-SC5060XM-08M-WBN, a 6 MP mono smart camera with an 8 mm M12 lens, is the mid-range shape of this class:
- Interface
- GigE
- Resolution
- 6-10 MP
- Sensor format
- 1/2.4"
- Shutter
- Rolling
A fixed code reader with OCR on board is the same two stages with the glue written for you. The LS-CRD-0028 decodes up to 60 codes/s at 30 fps from its 2560 x 2048 sensor, and OCR on a fixed code reader covers the multi-string case.
- Interface
- Fast Ethernet
- Resolution
- 5 MP
- Sensor format
- 1/1.7"
- Shutter
- Global
When the layout changes weekly or you own the models, run your own threads on a vision controller with an industrial camera. The MV-VC3301H-128G66 brings an Intel Core i3, 8 GB of memory and 5+ LAN ports — cameras, PLC and lights on one box. On an edge accelerator the stage structure is identical with models compiled for it; OCR on an embedded AI accelerator covers that variant.
- LAN ports
- 5+ LAN ports
- USB 3 ports
- 3-4 USB 3 ports
- Light outputs
- 4 built-in channels
- Memory
- 8 GB
Downstream, a completed bundle goes to the PLC, a database row, or the HMI. If the display layer is an Ignition Perspective screen, the image text recognition options for Perspective page lays out what runs where. Pick the camera for text height first: the camera finder filters by resolution, mount and shutter.
When one stage is enough
A fixed region beats a detector when the text is one block, one line, same place and same angle every frame. Crop a fixed rectangle and run the recognizer alone: one inference per frame instead of two, no box jitter, half the failure surface. Commit to the detector the moment position, rotation or the number of text blocks varies; it earns its slot on the first format change.
Two limits to respect. Scene-text detectors are trained on printed strokes, so dot-peen and engraved marks get missed unless the detector saw them in training. And strongly curved text needs a detector that outputs quadrilaterals plus a per-line split before recognition — a rectangle crop of a curved line cuts strokes at both ends.
Checklist before you build
- Text height at the worst pose: 40 px or more in the frame. Optics fix this; models cannot.
- Detector input: 960 px on the long side when text sits small in the frame.
- Crop margin: 15-20 % around each box before resizing.
- Recognizer input height: 48 px; drop to 32 px only for short crops.
- Expected box count stored at detection postprocess and carried with every crop.
- Frame-ID table swept every bundle; stale entries released as partial reads.
- Queue depth: shallow, two to three frames, so backpressure surfaces early.
- Zero-box path tested: push empty frames through and confirm empty results, not timeouts.
Run your numbers through the calculator above, then size the rest of the station:
The configurator matches a camera, lens, light, light controller and cables to each other, step by step, and lists only parts that fit what you already picked.
If the geometry does not close — text too small at the distance you have — send the frame size, field of view and text height to our applications desk and we will size it with you.
Questions
Why pad the crop instead of stretching it to the recognizer's input size?
The recognizer was trained on text at true proportions. Stretching a 245-pixel-wide crop to fill a 320-pixel input changes stroke width and letter spacing, and accuracy drops first on the characters you care about. Resizing to the fixed input height keeps proportions, and padding the remaining width with the background value leaves geometry intact. Height is normalized because line height is constant in training data while line length is not.
How does the pipeline know a frame's results are complete?
Detection postprocess counts the boxes it emitted for that frame ID and stores the count with the frame. Every recognizer result carries the same frame ID; the collector appends until received equals expected, then releases the bundle and deletes the entry. The expected count is fixed at detection time — recomputing it later from arriving results cannot tell a missing crop from a slow one.
What happens to frames whose OCR results never all arrive?
Hold them, then expire them. An entry more than 10 frames behind the newest is emitted with the results it has, logged as a partial read, and deleted. Without that sweep the table grows forever, because a dropped crop — an inference error, a queue overflow — leaves an entry that can never complete. At 40 fps the 10-frame window is 250 ms: slower than your cycle time, faster than a memory problem.
When is a fixed ROI better than a text detector?
When the text is one block, in the same place, at the same angle, every frame. Crop a fixed rectangle and run only the recognizer: one inference per frame, no box jitter, half the failure modes. If position or rotation varies by more than about a tenth of the ROI, or the number of text blocks changes between parts, the detector pays for itself on the first format change.
Can one crop hold two lines of text?
No. CRNN-style recognizers decode a single line; give them two and you get interleaved garbage or only the stronger line. Split multi-line regions at detection postprocess — one box per line, one recognizer pass each — and keep the line grouping in the frame bundle so the consumer can rebuild the block. Detectors that output rotated or quadrilateral boxes make the per-line split a geometry step, not a second model.


