lot_expiry_parser
Reads the batch (lot) number and the expiry date from the OCR text of a product pack. It is pure Dart with no dependencies, so it works with any OCR engine (ML Kit, Tesseract, a cloud OCR...) and can be tested without a device.
import 'package:lot_expiry_parser/lot_expiry_parser.dart';
final result = LotExpiryParser.parse(['LOT AB123', 'EXP 05/2027']);
print(result.batch); // AB123
print(result.expiry); // 2027/05/31 (month-only → last day of the month)
What it reads
| Printed on the pack | batch | expiry |
|---|---|---|
LOT AB123 / EXP 2027-05 |
AB123 |
2027/05/31 |
BATCH: 24K118 EXP: 31.12.2026 |
24K118 |
2026/12/31 |
B.N PM6111 / MFG 04 \ 25 / EXP 03 \ 27 |
PM6111 |
2027/03/31 |
(10)X12 (17)270430 (GS1 human readable) |
X12 |
2027/04/30 |
ISO symbols: LOT MFG column next to 1166/25 10/2025 09/2030 |
1166/25 |
2030/09/30 |
B 144692609U / P 09/26 E 09/29 |
144692609U |
2029/09/30 |
C210Y50002 / M 11/25 / E 11/28 (no batch label) |
C210Y50002 |
2028/11/30 |
25154 OP43 / PRD 06/25 EXP 06/30 |
25154OP43 |
2030/06/30 |
It also handles:
- Labels:
LOT,LOT NO.,BATCH,B.N,BN,L:,EXP,USE BY,BEST BEFORE,BB,BBE,E.D,VALID UNTIL,MFG,MFD,PRD,PKD,MNT, and the one-letterB,E,P,M,F. - Date forms:
2027-05-31,31/05/2027,31 MAY 2027,MAY-27,05/2027,04\2028, and short03/27/270531/26 01 28(short forms only after a label). - OCR mistakes:
2O27→2027,1O/2O2S→10/2025, accents (Míg), the ISO box border read as|LOT|/ILOTI, and a batch split in two (26081 78). - Never the batch: dates, GTIN/EAN numbers, pack sizes (
1000 ML), times (11:08),REF/REVcodes. - Expiry rules: a production date is never taken as the expiry, and the expiry is never before it.
Batches are upper-case and capped at 10 characters (LotExpiryParser.maxBatchLength). Expiry dates are returned as yyyy/MM/dd.
How a reading flows
OCR lines
│
▼ normalize.dart upper-case, accents, ISO box border (|LOT| → LOT)
▼ label_columns.dart pair a label column with its value column
├─▶ batch.dart labelled batch, else a code next to the dates
└─▶ expiry.dart labelled expiry, else the latest plausible date
(both use keys.dart for labels and dates.dart for dates)
▼
LotExpiryResult(batch, expiry)
| File | Does |
|---|---|
keys.dart |
every label regex (LOT, EXP, MFG, one-letter B/E/P/M/F) |
normalize.dart |
cleans each OCR line |
label_columns.dart |
ISO symbol columns |
dates.dart |
finds dates, fixes letter/digit OCR mix-ups |
batch.dart |
the batch (labelled and unlabelled) |
expiry.dart |
the expiry |
lot_expiry_parser.dart |
the public LotExpiryParser that wires the steps |
stabilizer.dart |
voting across camera frames |
dot_matrix_filter.dart |
makes dot-matrix print readable before OCR |
With positions (OCR boxes)
When your OCR gives boxes, use parseOcrLines. It pairs a label column with
the values to its right, whatever order the OCR returned the blocks in:
final result = LotExpiryParser.parseOcrLines(const [
OcrLine('1166/25', 230, 542, 440, 567),
OcrLine('10/2025', 230, 568, 440, 593),
OcrLine('09/2030', 230, 596, 440, 621),
OcrLine('LOT', 60, 540, 140, 565),
OcrLine('MFG', 65, 603, 140, 628),
]);
// batch 1166/25, expiry 2030/09/30
Live camera
OCR on a moving camera gives slightly different text in each frame. Feed every
frame's result to a LotExpiryStabilizer; a value is accepted once it was read
twice in the last 8 frames:
final stabilizer = LotExpiryStabilizer();
// for each frame:
stabilizer.add(LotExpiryParser.parseOcrLines(frameLines));
if (stabilizer.isComplete) print(stabilizer.result);
Dot-matrix and inkjet print
Dot-matrix print is made of separate dots that OCR often can't read.
joinDots spreads dark pixels on the brightness plane of an NV21 frame so the
dots become solid strokes. Run OCR on both frames and combine the results with
LotExpiryParser.mergeReadings(normal, joined).
Values from another reader
LotExpiryParser.validate(batch: ..., expiry: ...) checks values from another
source (e.g. an AI model) with the same rules: it cleans the batch, rejects
dates or GTINs given as the batch, and rejects implausible years.
Accuracy
This is a helper. Printed codes vary a lot between manufacturers, and a pack with no labels can't always be read unambiguously. Always let the user confirm or correct the values.
Libraries
- lot_expiry_parser
- Reads the batch (lot) number and the expiry date from OCR text of product packs.