cartrade / anpr
Indian number plate reader (ANPR) for PHP: detects and reads vehicle plates in photos with two small ONNX models, fully on your server.
Requires
- php: >=8.2
- ext-ffi: *
- ext-gd: *
- ankane/onnxruntime: ^0.3.9
Requires (Dev)
None
Suggests
- ext-exif: Rotates phone photos by their EXIF orientation before reading
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-12-03 09:28:37 UTC
README
Reads vehicle number plates from photos on your own server, in PHP. It uses the same two ONNX
models and logic as the Python version (anpr.py) and the browser SDK (anpr.min.js), and returns
the same JSON response.
$res = anpr_read($_FILES['image']); if ($res['success']) { echo $res['plate']; // "MH12AB1234" } else { echo $res['error'], ': ', $res['message']; // NO_PLATE | UNREADABLE | BAD_INPUT | LOAD_FAILED | MODEL_ERROR }
Server requirements
| PHP | 8.2+, Linux x64/arm64 (also Windows x64 and Apple Silicon macOS) |
ext-ffi |
with ffi.enable = true in php.ini. The default (preload) only allows FFI on the command line, so web requests fail |
ext-gd |
with JPEG and WebP support |
ext-exif |
optional: turns phone photos upright using their EXIF orientation |
| Memory | memory_limit of 512M is plenty (photos are scaled to 1600 px first) |
That means a VPS or your own server. Most shared hosting doesn't allow FFI.
Ubuntu/Debian with Apache and mod_php, for example:
sudo apt install php8.3 php8.3-gd libapache2-mod-php8.3 php -m | grep -i -E "ffi|gd|exif" # FFI and EXIF usually ship with php8.3-common; install them if missing echo "ffi.enable=true" | sudo tee /etc/php/8.3/apache2/conf.d/99-ffi.ini sudo a2enmod rewrite && sudo systemctl restart apache2
Also set AllowOverride All for the site folder so the example's .htaccess applies.
Install in your app
composer require cartrade/anpr
Add these scripts to your app's composer.json. They download the ONNX Runtime engine (about
20 MB) on composer install. Composer never runs a dependency's own scripts, so this can't be automatic:
"scripts": { "post-install-cmd": "OnnxRuntime\\Vendor::check", "post-update-cmd": "OnnxRuntime\\Vendor::check" }
Then check the server (this also downloads ONNX Runtime if it's missing):
vendor/bin/anpr check
vendor/bin/anpr read photo.jpg
Usage
require 'vendor/autoload.php'; // Optional settings, before the first read Anpr\Anpr::configure([ 'threads' => 2, // CPU threads per read (default: all cores; keep low on a busy web server) 'detThreshold' => 0.4, // lower = finds more faint plates 'allowUrls' => false, // true = also accept http(s) image URLs (the server will fetch any URL it's given) 'maxBytes' => 15000000, // largest image accepted ]); $res = anpr_read($_FILES['image']); // uploaded file $res = anpr_read(file_get_contents('a.jpg')); // image bytes $res = anpr_read('/path/to/car.jpg'); // file path $res = anpr_read($base64OrDataUrl); // base64 / data:image/jpeg;base64,...
anpr_read() is a shortcut for Anpr\Anpr::read(). Responses:
{"success": true, "plate": "KL03P9794", "confidence": 0.872, "box": [698, 538, 890, 590], "plates": [...], "timeMs": 450}
{"success": false, "error": "UNREADABLE", "message": "Plate found but the number can't be read ...", "plates": [...], "timeMs": 430}
error |
Meaning |
|---|---|
NO_PLATE |
No number plate in the photo |
UNREADABLE |
A plate was found but it isn't read confidently (hidden, blurry, no number). It never guesses |
BAD_INPUT |
No image, or not a JPEG/PNG/WebP/GIF/BMP |
LOAD_FAILED |
Models or ONNX Runtime couldn't load (run vendor/bin/anpr check) |
MODEL_ERROR |
The model failed while running |
plates lists every plate found, best first: status (ok/unreadable), text, raw_text,
confidence, detector_score, ocr_score, min_char_score, valid_format, box ([x1, y1, x2, y2]
in the original photo's pixels).
For full control there is also (new Anpr\PlateReader($modelsDir, $detThreshold, $threads))->read($bytes),
which returns just the plates list.
Ready-made Apache API
examples/apache/ is a complete deployable app: api.php, .htaccess,
.user.ini and composer.json.
cp -r vendor/cartrade/anpr/examples/apache /var/www/html/anpr # or copy the folder from this repo cd /var/www/html/anpr && composer install && vendor/bin/anpr check
curl -F "image=@car.jpg" https://your-server/anpr/api.php # file upload curl -H "Content-Type: application/json" -d '{"image":"<base64>"}' https://your-server/anpr/api.php
Settings go in its .htaccess (SetEnv ANPR_THREADS 2, ANPR_ALLOW_URLS, ANPR_CORS_ORIGIN).
Try it with Docker
The Dockerfile builds the exact setup (PHP 8.3, Apache, FFI, GD, EXIF) and runs anpr check
while it builds:
docker build -t anpr-php . docker run --rm -p 8080:80 anpr-php curl -F "image=@tests/images/car_2.jpg" http://localhost:8080/api.php docker run --rm anpr-php php vendor/cartrade/anpr/tests/smoke.php # 4 sample photos vs expected plates
Speed
PHP doesn't keep objects between web requests, so each request loads the models again (13 MB). That's usually a few hundred ms, and the OS keeps the files in memory. Reading a photo then takes about as long as the Python version. Several photos in one request, or a long-running PHP worker (CLI job, queue consumer), load the models only once.
Publishing to Packagist
- Put this folder (
server-php/) in its own Git repository, socomposer.jsonis at the repo root. The models (13 MB) are committed with it; that's fine on GitHub/GitLab. - Tag a release:
git tag v1.0.0 && git push --tags. - On packagist.org → Submit → paste the repository URL, then turn on the GitHub/GitLab hook so new tags show up automatically.
- To ship new models later: replace
models/*.onnx, then tag a new version.
Change name in composer.json (now cartrade/anpr) if you publish under another vendor name, and
set license: it is proprietary for now. The underlying models are MIT-licensed
(open-image-models, fast-plate-ocr), but check the training-data licences before commercial use.
How it works
- The photo is decoded with GD, turned upright (EXIF) and scaled to at most 1600 px.
- Detector (YOLOv9-tiny, 640×640 input) finds plate boxes. Only plates, so other text is ignored.
- OCR reads each plate twice (tight crop + 8% margin), as 128×64 RGB.
- Indian format check: picks letters in letter positions and digits in digit positions
(
MH12AB1234,22BH1234AA), fixing 0/O, 1/I, 8/B mix-ups. - A plate is
okonly when the OCR is ≥ 90% sure overall and ≥ 60% sure of every character.