Many DataMatrix codes in one image: how to read every code on a case or a sheet
A case of packs, a tray of vials, a sheet of labels off the printer, a pallet during aggregation — one image holds dozens of DataMatrix codes, and you need all of them. A typical reader looks for one code: it returns the first one it finds and stops, or it searches the whole frame and does not finish in reasonable time. Below is how to read every code at once and how long it takes.
Why it is hard
- Many codes, each one small. To fit a whole case in the frame the camera moves back, and a code module shrinks to 2–4 pixels. How many pixels per module you need is covered in the article on camera resolution.
- Full search is expensive. A reader that scans the whole frame hits its candidate limit on a 12 MP image with dozens of symbols and simply does not get to some codes.
- Codes get in each other's way. Neighbouring symbols, label edges and text throw off the search for the code border.
How we do it
Our libraries dmr (DataMatrix) and unir (DataMatrix, QR, barcodes) include a small built-in neural network, the scout network. In tens of milliseconds it finds where the codes are, and each spot is read as a separate crop: first a tight one, right around the code, and if that fails, a wider one. Crops are processed in parallel on all cores. No separate model file is needed and the network switches on by itself (Auto mode): in dmr when more than one code is requested, in unir by default.
How long it takes
Images with 16 DataMatrix codes, dmr 1.2.1, 8 x86 cores:
| Images | With the scout network | Without it |
|---|---|---|
| 12 MP, camera-like | 96.8% of codes, 0.45 s per image | 43.5% of codes, 1.1 s |
| crops of real images | 88.2% of codes | 23.1% |
| 1 to 10 codes per image | 97% of codes | — |
No false reads in any run.
A small computer is slower. Raspberry Pi 4, unir 1.2.15, an A4 sheet with 20 GS1 DataMatrix codes, an industrial camera frame of 2416×2024 (5 MP), 20 frames: median 6.2 s per frame on one thread and 1.9 s on four threads. If you need one code rather than all of them, the same frame reads in 0.25 s. More in the article on Raspberry Pi.
How to turn it on
In Python, use the “all codes” mode and iterate over the results:
# pip install libscanner-unir
import unir
result = unir.scan_file("case.jpg", unir.Settings.multi(0)) # 0 = as many as found
for code in result.codes:
print(code.symbology_name, code.text)If you know how many codes there should be (say, 20 packs in a case), pass that number instead of zero: the reader stops searching once it has found that many. On the command line it is the --codes option:
unir --codes 0 case.jpg
unir --folder ./images --codes 20 > result.tsvThe number of threads is set by the threads setting (the --threads option): by default all threads available to the process, and in a container with a CPU quota no more than the quota. If you already spread frames over your own threads, set it to 1.
Imaging tips
- Resolution for the smallest code. The camera should give at least 2–3 pixels per module for a code at the edge of the frame, not in the centre.
- Light without glare. Shrink film and gloss produce glare that hides part of a symbol. Diffuse side light usually helps more than a more expensive camera.
- Sharp across the whole case. In a tall case the top and bottom rows of packs differ in focus — stop down the aperture or raise the camera.
- Check the count. For aggregation what matters is that every code was read: compare the number read with the expected number and send the case for a retake if they differ.
Try it
You can test an image with several codes in the online decoder without installing anything. The libraries work for the first month without a license — enough to run them on your own frames. Documentation: unir and dmr. If reading needs tuning for your line, see custom development; the assessment on your frames is free.
Chestny ZNAK is a trademark of CRPT LLC (ООО «Оператор-ЦРПТ»); GS1 is a trademark of GS1 AISBL. The product is not affiliated with or certified by their owners.