@llein/anpr-plate-cleaner
v1.0.0
Published
Production-grade OCR/ANPR license plate text cleaner, character confusion disambiguation (0/O, 1/I, 8/B), 2-line combiner, and fuzzy matcher.
Maintainers
Readme
@llein/anpr-plate-cleaner
Production-grade OCR / ANPR license plate text cleaner, character confusion disambiguation (
0vsO,1vsI,8vsB), 2-line combiner, and whitelist fuzzy matcher.
⚡ Why This Package?
In real-world parking barrier systems and traffic cameras (ANPR / ALPR), raw OCR output from models like YOLO, PaddleOCR, or EasyOCR is often corrupted by:
- Night infrared reflections, dirt, scratches, and screw heads on the license plate.
- OCR character confusions:
- Letters read where digits are required (e.g.,
Sinstead of5,I/Linstead of1,Binstead of8,O/Dinstead of0). - Digits read where letters are required (e.g.,
8instead ofB,0instead ofD/O,1instead ofI).
- Letters read where digits are required (e.g.,
- Two-line plates (top line: province & series, bottom line: sequence numbers).
@llein/anpr-plate-cleaner solves this using position-aware syntactic disambiguation, 2-line merging, and confusion-discounted fuzzy matching for resident whitelists.
🚀 Features
- Position-Aware Error Correction:
- Automatically fixes province digits, series letters, and serial numbers according to vehicle registration standards (Circular 24/2023/TT-BCA).
- Two-Line Plate Stitching:
- Combines multi-line OCR detections (e.g.
59-P1on line 1 and123.45on line 2 ->59P112345).
- Combines multi-line OCR detections (e.g.
- Vehicle Type Classification:
- Classifies into
car,motorbike,electric_motorbike,military, anddiplomatic.
- Classifies into
- Confusion-Weighted Fuzzy Matching:
- Levenshtein distance with fractional penalties for known OCR confusions (e.g. mistaking
8forBis penalized with only0.25instead of1.0). - Perfect for matching parking gate scans against database whitelist records.
- Levenshtein distance with fractional penalties for known OCR confusions (e.g. mistaking
- Zero External Dependencies: Pure TypeScript, minimal footprint (< 4KB).
📦 Installation
# Using npm
npm install @llein/anpr-plate-cleaner
# Using pnpm
pnpm add @llein/anpr-plate-cleaner
# Using yarn
yarn add @llein/anpr-plate-cleaner🛠️ Usage Examples
1. Clean Corrupted OCR Output
import { cleanVietnamPlate, cleanANPRText } from '@llein/anpr-plate-cleaner';
// OCR misread 'S' as 5, 'I' as 1, 'B' as 8
const result = cleanVietnamPlate('SIK-999.9B');
console.log(result.compact); // "51K99998"
console.log(result.formatted); // "51K-999.98"
console.log(result.vehicleType); // "car"
console.log(result.isValid); // true2. Multi-line Motorbike Plate
// Bounding boxes read top & bottom lines
const rawOCR = "59-P1\n123.45";
const result = cleanVietnamPlate(rawOCR);
console.log(result.compact); // "59P112345"
console.log(result.formatted); // "59-P1 123.45"
console.log(result.vehicleType); // "motorbike"3. Match Plate Against Parking Whitelist (Fuzzy Search)
import { findBestPlateMatch } from '@llein/anpr-plate-cleaner';
const whitelist = [
'51K99999',
'29A12345',
'59P167890'
];
// OCR reading has a glitch: 'O' instead of '0'
const scanned = '59P16789O';
const match = findBestPlateMatch(scanned, whitelist, 0.85);
if (match) {
console.log(`Open Barrier! Matched: ${match.match} (Confidence: ${match.score * 100}%)`);
}📖 API Reference
cleanVietnamPlate(raw: string): PlateCleanResult
Full syntactic analysis returning { raw, compact, formatted, vehicleType, isValid, score }.
cleanANPRText(raw: string, options?: CleanOptions): string
Returns cleaned string directly (compact or formatted).
combinePlateLines(topLine: string, bottomLine: string): string
Joins 2-line OCR boxes into a unified text string.
calculatePlateSimilarity(plateA: string, plateB: string): number
Returns a similarity score between 0.0 and 1.0 utilizing OCR confusion matrices.
findBestPlateMatch(query: string, candidates: string[], threshold?: number)
Searches a candidate array for the closest matching registered plate.
🧪 Testing
npm test📄 License
MIT © Llein-Dev
