How to detect vehicle license plates in photos in Ruby
2 min readAug 20, 2019
Detecting license plates and their numbers can be very useful, and today we will be looking at how to accomplish this task quickly and easily using one of our APIs. The first thing we shall do is add this line of code to our Gemfile:
gem 'cloudmersive-image-recognition-api-client', '~> 1.3.2'
Then call the function recognize_detect_vehicle_license_plates:
# load the gem
require 'cloudmersive-image-recognition-api-client'
# setup authorization
CloudmersiveImageRecognitionApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
endapi_instance = CloudmersiveImageRecognitionApiClient::RecognizeApi.newimage_file = File.new("/path/to/file") # File | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.begin
#Detect vehicle license plates in an image
result = api_instance.recognize_detect_vehicle_license_plates(image_file)
p result
rescue CloudmersiveImageRecognitionApiClient::ApiError => e
puts "Exception when calling RecognizeApi->recognize_detect_vehicle_license_plates: #{e}"
end
And we are all set. Let’s look at an example image:
Our results will look like this:
{
"Successful": true,
"DetectedLicensePlates": [
{
"LocationX": 561,
"LocationY": 451,
"Width": 65,
"Height": 32,
"LicensePlateText_BestMatch": "RTBB221",
"LicensePlateText_RunnerUp": "RBB22",
"LicensePlateRecognitionConfidenceLevel": 0.8885029602050781
}
],
"DetectedLicensePlateCount": 1
}
Notice that a lot of useful information is provided in the response, including the location and size and two possibilities for the license number along with its confidence in the results.