How to detect vehicle license plates in photos in PHP

Let’s look at a super easy way to detect license plates in photographs. Our API will not only give the location and dimensions of the license plate (or plates) in question, but also give us the text of the plate and a confidence level for that text. Let’s dive in.

Image for post
Image for post

First we will run this command in our Composer command line:

composer require cloudmersive/cloudmersive_imagerecognition_api_client

Then it’s as simple as calling the recognizeDetectVehicleLicensePlates function:

<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');
$apiInstance = new Swagger\Client\Api\RecognizeApi(
// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.
// This is optional, `GuzzleHttp\Client` will be used as default.
new GuzzleHttp\Client(),
$config
);
$image_file = "/path/to/file"; // \SplFileObject | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.
try {
$result = $apiInstance->recognizeDetectVehicleLicensePlates($image_file);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling RecognizeApi->recognizeDetectVehicleLicensePlates: ', $e->getMessage(), PHP_EOL;
}
?>

And our output will look something like this:

{
"Successful": true,
"DetectedLicensePlates": [
{
"LocationX": 290,
"LocationY": 341,
"Width": 186,
"Height": 81,
"LicensePlateText_BestMatch": "ALPINE",
"LicensePlateText_RunnerUp": "BALPINE",
"LicensePlateRecognitionConfidenceLevel": 0.819610395703125
}
],
"DetectedLicensePlateCount": 1
}

Done.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store