Samples

C

The following sample uses the Vehicle Recognition Stream API to recognize frames from a video. The video is fed into the library asynchronously, and the processing occurs on the main thread.

To use the API on multiple CPU cores, you should create multiple threads that call the alprstream_process_frame() function. Each thread should have its own Alpr object, but can share the AlprStream object across threads.

// System imports
#include <cstdlib>
#include <vector>
#include <string>
#include <string.h>
#include <sstream>
#include <iostream>

// Import OpenALPR alprstream_c (also pulls in alpr_c.h and vehicleclassifier_c.h)
#include <alprstream_c.h>
#include <vehicle_classifier_c.h>
#include <alpr_c.h>

using namespace std;

int main(int argc, char* argv) {

    cout << "Initializing" << endl;
    const std::string LICENSEPLATE_COUNTRY = "eu";

    // Video buffer frames controls the number of frames to buffer in memory.
    const int VIDEO_BUFFER_SIZE = 15;

    // The stream will assume sequential frames.  If there is no motion from frame to frame, then
    // processing can be skipped for some frames
    const int USE_MOTION_DETECTION = 1;

    // The point in time (ms) to start in the video file
    const int VIDEO_START_MS = 0;

    OPENALPR alpr = openalpr_init(LICENSEPLATE_COUNTRY.c_str(), "", "", "");
    VEHICLECLASSIFIER vehicle_classifier = vehicleclassifier_init("", "", 0,0, 0, "");

    ALPRSTREAM stream = alprstream_init(VIDEO_BUFFER_SIZE, USE_MOTION_DETECTION);

    // Enable to see detailed debug messages related to video read/queue
    putenv("GST_DEBUG=2");

    // AlprStream will spawn a background thread to read the eu-clip.mp4 video file
    // and push to the queue.  Alternatively, you may connect to a stream URL, or
    // push individual image frames into the alprstream object yourself
    alprstream_connect_video_file(stream, "C:\Temp\eu-clip.mp4", VIDEO_START_MS);

    // Process until the video file is done and all remaining frames in the buffer have been processed
    while (true)
    {
        // If the buffer is empty wait for it to replenish
        if (alprstream_get_queue_size(stream) <= 0)
            Sleep(100);

        // AlprStream will now perform recognition on the oldest video frame on the queue
        AlprStreamRecognizedFrameC response = alprstream_process_frame(stream, alpr);

        cout << "Content: " << response->results_str << endl;

        // Writes the image frame to a temp file on disk for demonstration purposes
        if (response->image_available)
        {
            FILE file = fopen("c:\temp\test.jpg", "wb");
            fwrite(response->jpeg_bytes, sizeof(char), response->jpeg_bytes_size, file);
            fclose(file);
        }

        // Free the memory for each response
        alprstream_free_frame_response(response);

        // Get Group results:
        char* group_result = alprstream_pop_completed_groups_and_recognize_vehicle(stream, vehicle_classifier);
        cout << "Groups: " << group_result << endl;
        alprstream_free_response_string(group_result);

        cout << "Stream queue size: " << alprstream_get_queue_size(stream) << endl;
    }

    cout << "Done" << endl;

    // Cleanup the memory for the Alpr object
    openalpr_cleanup(alpr);

    // Cleanup the memory for the AlprStream object
    alprstream_cleanup(stream);
    return 0;
}

Python

Download our sample video file (http://download.openalpr.com/bench/720p.mp4) which has several cars with license plates in a parking lot (or take your own video in .mp4 format). Then, run the following code:

Last updated

Was this helpful?