Code Samples

To facilitate smooth integration, Rekor CarCheck® offers a range of code samples in different programming languages. These samples serve as practical guides, demonstrating how to interact with the API effectively. Whether you are working with Bash, Python, C#, Java, or JavaScript, you will find detailed examples below to help you understand the implementation process.

Bash

curl -X POST -F image=@/car1.jpg 'https://api.openalpr.com/v3/recognize?recognize_vehicle=1&country=us&secret_key=sk_DEMODEMODEMODEMODEMODEMO'

Python

#!/usr/bin/python

import requests
import base64
import json

# Sample image file is available at http://plates.openalpr.com/ea7the.jpg
IMAGE_PATH = '/tmp/sample.jpg'
SECRET_KEY = 'sk_DEMODEMODEMODEMODEMODEMO'

with open(IMAGE_PATH, 'rb') as image_file:
    img_base64 = base64.b64encode(image_file.read())

url = 'https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=%s' % (SECRET_KEY)
r = requests.post(url, data = img_base64)

print(json.dumps(r.json(), indent=2))

C#

using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.IO;

namespace ConsoleApplicationTest
{
    class Program
    {
        private static readonly HttpClient client = new HttpClient();

        public static async Task<string> ProcessImage(string image_path)
        {
            string SECRET_KEY = "sk_DEMODEMODEMODEMODEMODEMO";

            Byte[] bytes = File.ReadAllBytes(image_path);
            string imagebase64 = Convert.ToBase64String(bytes);

            var content = new StringContent(imagebase64);

            var response = await client.PostAsync("https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + SECRET_KEY, content).ConfigureAwait(false);

            var buffer = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
            var byteArray = buffer.ToArray();
            var responseString = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);

            return responseString;
        }

        static void Main(string[] args)
        {
            Task<string> recognizeTask = Task.Run(() => ProcessImage(@"C:\Temp\car1.jpg"));
            recognizeTask.Wait();
            string task_result = recognizeTask.Result;

            System.Console.WriteLine(task_result);
        }
    }
}

Java

import java.net.;
import java.io.;
import java.nio.file.*;
import java.util.Base64;

class TestOpenALPR {

    public static void main(String[] args)
    {
        try
        {
            String secret_key = "sk_DEMODEMODEMODEMODEMODEMO";

            // Read image file to byte array
            Path path = Paths.get("/storage/projects/alpr/samples/testing/car1.jpg");
            byte[] data = Files.readAllBytes(path);

            // Encode file bytes to base64
            byte[] encoded = Base64.getEncoder().encode(data);

            // Setup the HTTPS connection to api.openalpr.com
            URL url = new URL("https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + secret_key);
            URLConnection con = url.openConnection();
            HttpURLConnection http = (HttpURLConnection)con;
            http.setRequestMethod("POST"); // PUT is another valid option
            http.setFixedLengthStreamingMode(encoded.length);
            http.setDoOutput(true);

            // Send our Base64 content over the stream
            try(OutputStream os = http.getOutputStream()) {
                os.write(encoded);
            }

            int status_code = http.getResponseCode();
            if (status_code == 200)
            {
                // Read the response
                BufferedReader in = new BufferedReader(new InputStreamReader(
                                        http.getInputStream()));
                String json_content = "";
                String inputLine;
                while ((inputLine = in.readLine()) != null)
                    json_content += inputLine;
                in.close();

                System.out.println(json_content);
            }
            else
            {
                System.out.println("Got non-200 response: " + status_code);
            }

        }
        catch (MalformedURLException e)
        {
            System.out.println("Bad URL");
        }
        catch (IOException e)
        {
            System.out.println("Failed to open connection");
        }

    }
}

JavaScript

<html>
    <title>
        CarCheck API Demo
    </title>

    <head>
        <script>
        // Open connection to api.openalpr.com
        var secret_key = "sk_DEMODEMODEMODEMODEMODEMO";
        var url = "https://api.openalpr.com/v3/recognize_bytes?recognize_vehicle=1&country=us&secret_key=" + secret_key;
        var xhr = new XMLHttpRequest();
        xhr.open("POST", url);

        // Send POST data and display response
        xhr.send("base64_string");  // Replace with base64 string of an actual image
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                document.getElementById("response").innerHTML = xhr.responseText;
            } else {
                document.getElementById("response").innerHTML = "Waiting on response...";
            }
        }
        </script>
    </head>

    <body>
        JSON response: <p id="response"></p><br>
    </body>
</html>

Results

Upon successful use of the API, the JSON response will provide detailed information in a structured format. The response includes key data points such as license plate number, vehicle make and model, color, and additional relevant information, ensuring a comprehensive overview of the recognized vehicle.

{
   "data_type" : "alpr_results",
   "epoch_time" : 1592849838217,
   "img_width" : 600,
   "img_height" : 600,
   "error" : false,
   "version" : 3,
   "credit_cost" : 1,
   "uuid" : "",
   "processing_time" : {
      "plates" : 85.0386276245117,
      "vehicles" : 76.9439999994574,
      "total" : 830.30300000064
   },
   "regions_of_interest" : [
      {
         "y" : 0,
         "height" : 600,
         "x" : 0,
         "width" : 600
      }
   ],
   "credits_monthly_used" : 5001,
   "credits_monthly_total" : 100000,
   "results" : [
      {
         "requested_topn" : 10,
         "matches_template" : 1,
         "region_confidence" : 99,
         "plate_index" : 0,
         "plate" : "627WWI",
         "region" : "us-wa",
         "confidence" : 94.0910949707031,
         "coordinates" : [
            {
               "x" : 238,
               "y" : 358
            },
            {
               "x" : 357,
               "y" : 358
            },
            {
               "x" : 357,
               "y" : 408
            },
            {
               "x" : 239,
               "y" : 409
            }
         ],
         "candidates" : [
            {
               "matches_template" : 1,
               "plate" : "627WWI",
               "confidence" : 94.0910949707031
            },
            {
               "matches_template" : 0,
               "plate" : "627WW1",
               "confidence" : 74.0910949707031
            }
         ],
         "vehicle_region" : {
            "y" : 96,
            "x" : 3,
            "width" : 589,
            "height" : 483
         },
         "processing_time_ms" : 8.83281803131104,
         "vehicle_detected" : true,
         "vehicle" : {
            "make" : [
               {
                  "name" : "daihatsu",
                  "confidence" : 67.1475448608398
               },
               {
                  "name" : "mini",
                  "confidence" : 18.6626739501953
               },
               {
                  "name" : "fiat",
                  "confidence" : 8.87434959411621
               },
               {
                  "confidence" : 1.25781571865082,
                  "name" : "scion"
               },
               {
                  "confidence" : 1.04451608657837,
                  "name" : "toyota"
               }
            ],
            "year" : [
               {
                  "name" : "2005-2009",
                  "confidence" : 54.0047645568848
               },
               {
                  "confidence" : 39.7950668334961,
                  "name" : "2000-2004"
               },
               {
                  "name" : "2010-2014",
                  "confidence" : 5.38613367080688
               },
               {
                  "name" : "1995-1999",
                  "confidence" : 0.628315031528473
               },
               {
                  "confidence" : 0.16707843542099,
                  "name" : "1990-1994"
               }
            ],
            "color" : [
               {
                  "confidence" : 77.7851638793945,
                  "name" : "silver-gray"
               },
               {
                  "confidence" : 9.67624187469482,
                  "name" : "blue"
               },
               {
                  "name" : "black",
                  "confidence" : 7.02981376647949
               },
               {
                  "name" : "green",
                  "confidence" : 2.66030049324036
               },
               {
                  "name" : "gold-beige",
                  "confidence" : 1.42329037189484
               }
            ],
            "orientation" : [
               {
                  "name" : "180",
                  "confidence" : 99.9426956176758
               },
               {
                  "name" : "225",
                  "confidence" : 0.0323438383638859
               },
               {
                  "name" : "135",
                  "confidence" : 0.0129895266145468
               },
               {
                  "name" : "45",
                  "confidence" : 0.00725177442654967
               },
               {
                  "confidence" : 0.00194440386258066,
                  "name" : "90"
               }
            ],
            "body_type" : [
               {
                  "confidence" : 99.8512573242188,
                  "name" : "sedan-compact"
               },
               {
                  "name" : "tractor-trailer",
                  "confidence" : 0.0433907173573971
               },
               {
                  "name" : "sedan-sports",
                  "confidence" : 0.0432351566851139
               },
               {
                  "confidence" : 0.0218438617885113,
                  "name" : "suv-crossover"
               },
               {
                  "confidence" : 0.0212091449648142,
                  "name" : "sedan-wagon"
               }
            ],
            "make_model" : [
               {
                  "name" : "mini_cooper-s",
                  "confidence" : 35.001880645752
               },
               {
                  "confidence" : 23.0991153717041,
                  "name" : "mini_cooper"
               },
               {
                  "confidence" : 5.29244709014893,
                  "name" : "daihatsu_sirion"
               },
               {
                  "name" : "mini_hatch",
                  "confidence" : 4.69152307510376
               },
               {
                  "confidence" : 4.35747337341309,
                  "name" : "daihatsu_tanto"
               }
            ]
         }
      }
   ],
   "vehicles" : [
      {
         "x" : 3,
         "y" : 96,
         "width" : 589,
         "height" : 483,
         "details" : {
            "orientation" : [
               {
                  "name" : "180",
                  "confidence" : 99.9426956176758
               },
               {
                  "name" : "225",
                  "confidence" : 0.0323438383638859
               },
               {
                  "name" : "135",
                  "confidence" : 0.0129895266145468
               },
               {
                  "confidence" : 0.00725177442654967,
                  "name" : "45"
               },
               {
                  "name" : "90",
                  "confidence" : 0.00194440386258066
               }
            ],
            "make" : [
               {
                  "name" : "daihatsu",
                  "confidence" : 67.1475448608398
               },
               {
                  "name" : "mini",
                  "confidence" : 18.6626739501953
               },
               {
                  "confidence" : 8.87434959411621,
                  "name" : "fiat"
               },
               {
                  "confidence" : 1.25781571865082,
                  "name" : "scion"
               },
               {
                  "confidence" : 1.04451608657837,
                  "name" : "toyota"
               }
            ],
            "color" : [
               {
                  "name" : "silver-gray",
                  "confidence" : 77.7851638793945
               },
               {
                  "name" : "blue",
                  "confidence" : 9.67624187469482
               },
               {
                  "name" : "black",
                  "confidence" : 7.02981376647949
               },
               {
                  "confidence" : 2.66030049324036,
                  "name" : "green"
               },
               {
                  "confidence" : 1.42329037189484,
                  "name" : "gold-beige"
               }
            ],
            "year" : [
               {
                  "name" : "2005-2009",
                  "confidence" : 54.0047645568848
               },
               {
                  "name" : "2000-2004",
                  "confidence" : 39.7950668334961
               },
               {
                  "name" : "2010-2014",
                  "confidence" : 5.38613367080688
               },
               {
                  "name" : "1995-1999",
                  "confidence" : 0.628315031528473
               },
               {
                  "confidence" : 0.16707843542099,
                  "name" : "1990-1994"
               }
            ],
            "make_model" : [
               {
                  "confidence" : 35.001880645752,
                  "name" : "mini_cooper-s"
               },
               {
                  "confidence" : 23.0991153717041,
                  "name" : "mini_cooper"
               },
               {
                  "name" : "daihatsu_sirion",
                  "confidence" : 5.29244709014893
               },
               {
                  "confidence" : 4.69152307510376,
                  "name" : "mini_hatch"
               },
               {
                  "name" : "daihatsu_tanto",
                  "confidence" : 4.35747337341309
               }
            ],
            "body_type" : [
               {
                  "confidence" : 99.8512573242188,
                  "name" : "sedan-compact"
               },
               {
                  "confidence" : 0.0433907173573971,
                  "name" : "tractor-trailer"
               },
               {
                  "confidence" : 0.0432351566851139,
                  "name" : "sedan-sports"
               },
               {
                  "confidence" : 0.0218438617885113,
                  "name" : "suv-crossover"
               },
               {
                  "confidence" : 0.0212091449648142,
                  "name" : "sedan-wagon"
               }
            ]
         }
      }
   ]
}

Last updated