Skip to main content

License Plate Background Color

This software finds the background color of a license plate. It takes as input an image of a license plate (not the whole vehicle). And it is meant to be used in conjunction with Snapshot. The typical usage is as follows

  • Do a lookup on a full image using Snapshot.
  • Use the result to crop the license plate and save that image.
  • Send the cropped image to plate-color (see below).

Running the Docker Image

  • Ask us for the SECRET_KEY. Contact us.
  • Install Docker if you do not have it.
  • Then run the following Docker command to start server.
    • Note: pass --gpus all to assign all gpus to the Docker container.
    • In this example the server port is set to 8501.
docker run -ti -e KEY=SECRET_KEY -p 8501:8501 platerecognizer/plate-color:latest

Plate Color Lookup

The server expects a base64 encoded image of the license plate only. In the examples below, we use /path/to/plate.png.

With cURL

curl -d "{\"instances\": [{ \"b64\":\"$(base64 -w0 /path/to/plate.png)\"}]}" \
http://localhost:8501/v1/models/classifier:predict

With Python

import requests
import base64
with open(r'/path/to/plate.png', 'rb') as fp:
jpeg_bytes = base64.b64encode(fp.read()).decode('utf-8')
predict_request = '{"instances" : [{"b64": "%s"}]}' % jpeg_bytes

response = requests.post(
'http://localhost:8501/v1/models/classifier:predict',
data=predict_request)
response.raise_for_status()
print(response.json())

Result

The output is the top 3 predictions for the background color. In this case, the most likely color is white with a score of .69 or 69%.

{
"predictions": [
{
"color": ["white", "blue", "black"],
"score": [0.69595, 0.0702599958, 0.06328]
}
]
}

End-to-end Example

snapshot_plate_color.py
import base64
import io

import requests
from PIL import Image

image_path = "car.jpg"
token = 'YOUR_API_TOKEN' # Replace YOUR_API_TOKEN with your API TOKEN from https://app.platerecognizer.com/service/snapshot-cloud/
with open(image_path, 'rb') as fp:
response = requests.post(
'https://api.platerecognizer.com/v1/plate-reader/',
files=dict(upload=fp),
headers={'Authorization': f'Token {token}'},
)
image = Image.open(image_path)
for plate in response.json()['results']:
xmin = plate['box']['xmin']
ymin = plate['box']['ymin']
xmax = plate['box']['xmax']
ymax = plate['box']['ymax']
im_bytes = io.BytesIO()
image.crop((xmin, ymin, xmax, ymax)).save(im_bytes, 'JPEG', quality=95)
im_bytes.seek(0)
b64_data = base64.b64encode(im_bytes.read()).decode('utf-8')
predict_request = '{"instances" : [{"b64": "%s"}]}' % b64_data
response = requests.post(
'http://localhost:8501/v1/models/classifier:predict', data=predict_request
)
print(response.json())
Floating button icon
CTRL + .