Integrating OpenALPR

degustator edited this page Feb 17, 2016 · 5 revisions

Integrating the OpenALPR Library

OpenALPR is available as a C/C++ library and has bindings in C#, Java, and Python. Additionally, the software can be used as a "black box" that can process video streams and make the data available to another system (using any programming language).

C/C++

First, download or compile the OpenALPR library onto your target platform. Make sure that the software runs by testing it using the alpr command-line executable. Pre-compiled binaries are available for 32/64-bit Windows and Ubuntu Linux.

  1. Add "alpr.h" as an include file to your project.
  2. Include the openalpr.dll (Windows) or libopenalpr.so (Unix) file with your binaries
  3. Include all other required shared libraries
  4. Put the openalpr.conf and runtime_data directory in the same location as the binaries. Alternatively, you can specify the location of the runtime_data in openalpr.conf or directly in the code.

Below is a simple usage example of using the OpenALPR library:

// Initialize the library using United States style license plates.  
// You can use other countries/regions as well (for example: "eu", "au", or "kr")
alpr::Alpr openalpr("us", "/path/to/openalpr.conf");

// Optionally specify the top N possible plates to return (with confidences).  Default is 10
openalpr.setTopN(20);

// Optionally, provide the library with a region for pattern matching.  This improves accuracy by 
// comparing the plate text with the regional pattern.
openalpr.setDefaultRegion("md");

// Make sure the library loaded before continuing.  
// For example, it could fail if the config/runtime_data is not found
if (openalpr.isLoaded() == false)
{
    std::cerr << "Error loading OpenALPR" << std::endl;
    return 1;
}

// Recognize an image file.  You could alternatively provide the image bytes in-memory.
alpr::AlprResults results = openalpr.recognize("/path/to/image.jpg");

// Iterate through the results.  There may be multiple plates in an image, 
// and each plate return sthe top N candidates.
for (int i = 0; i < results.plates.size(); i++)
{
  alpr::AlprPlateResult plate = results.plates[i];
  std::cout << "plate" << i << ": " << plate.topNPlates.size() << " results" << std::endl;
  
    for (int k = 0; k < plate.topNPlates.size(); k++)
    {
      alpr::AlprPlate candidate = plate.topNPlates[k];
      std::cout << "    - " << candidate.characters << "\t confidence: " << candidate.overall_confidence;
      std::cout << "\t pattern_match: " << candidate.matches_template << std::endl;
    }
}

Other Language Bindings

OpenALPR includes precompiled binary bindings for C#, Java, and Python. These are located in the 'src/bindings' directory. Each binding library contains an example program that shows how to execute OpenALPR.

C-Sharp

using openalprnet;

var alpr = new AlprNet("us", "/path/to/openalpr.conf", "/path/to/runtime_data");
if (!alpr.IsLoaded())
{
    Console.WriteLine("OpenAlpr failed to load!");
    return;
}
// Optionally apply pattern matching for a particular region
alpr.DefaultRegion = "md";

var results = alpr.Recognize("/path/to/image.jpg");
int i = 0;
foreach (var result in results.Plates)
{
    Console.WriteLine("Plate {0}: {1} result(s)", i++, result.TopNPlates.Count);
    Console.WriteLine("  Processing Time: {0} msec(s)", result.ProcessingTimeMs);
    foreach (var plate in result.TopNPlates)
    {
        Console.WriteLine("  - {0}\t Confidence: {1}\tMatches Template: {2}", plate.Characters,
                          plate.OverallConfidence, plate.MatchesTemplate);
    }
}

Java

import com.openalpr.jni.Alpr;
import com.openalpr.jni.AlprPlate;
import com.openalpr.jni.AlprPlateResult;
import com.openalpr.jni.AlprResults;

Alpr alpr = new Alpr("us", "/path/to/openalpr.conf", "/path/to/runtime_data");

// Set top N candidates returned to 20
alpr.setTopN(20);

// Set pattern to Maryland
alpr.setDefaultRegion("md");

AlprResults results = alpr.recognize("/path/to/image.jpg");
System.out.format("  %-15s%-8s\n", "Plate Number", "Confidence");
for (AlprPlateResult result : results.getPlates())
{
    for (AlprPlate plate : result.getTopNPlates()) {
        if (plate.isMatchesTemplate())
            System.out.print("  * ");
        else
            System.out.print("  - ");
        System.out.format("%-15s%-8f\n", plate.getCharacters(), plate.getOverallConfidence());
    }
}

// Make sure to call this to release memory
alpr.unload();

Python

from openalpr import Alpr

alpr = Alpr("us", "/path/to/openalpr.conf", "/path/to/runtime_data")
if not alpr.is_loaded():
    print("Error loading OpenALPR")
    sys.exit(1)
    
alpr.set_top_n(20)
alpr.set_default_region("md")

results = alpr.recognize_file("/path/to/image.jpg")

i = 0
for plate in results['results']:
    i += 1
    print("Plate #%d" % i)
    print("   %12s %12s" % ("Plate", "Confidence"))
    for candidate in plate['candidates']:
        prefix = "-"
        if candidate['matches_template']:
            prefix = "*"

        print("  %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence']))

# Call when completely done to release memory
alpr.unload()

Node.js

A Node.js binding to OpenALPR is available here: https://www.npmjs.com/package/node-openalpr

The source code is available here: https://github.com/netPark/node-openalpr

OpenALPR Agent

OpenALPR can also be configured as a "black box" that makes data available to other systems. When configured in this mode, OpenALPR is installed as a Linux daemon, and is configured to monitor one or more MJPEG video streams. It automatically processes the images and produces JSON data describing the license plates found in the images. This data can either be pushed to another server (as an HTTP POST) or pulled from another server (via beanstalkd queue).

More information about the OpenALPR agent is available here: https://github.com/openalpr/openalpr/wiki/OpenALPR-Daemon-(alprd)

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.