Skip to content

automatic number plate recognition engine based on deep learning in c++

License

Notifications You must be signed in to change notification settings

lprsoft/lpreditor

Repository files navigation

LPReditor

automatic number plate recognition engine based on deep learning in c++

Deep learning number plate recognition engine, based on YOLOv5 and ONNX. Operates on latin characters.

Build

This code is standard c++ and relies on OpenCV and ONNXRuntime. These two softwares are meant to operate on a vast range of hardwares and os. Based on that, it should be possible to build on various platforms. Among them, I tested successfully Windows 10 and Linux Ubuntu (20.04). Use of CUDA has not (yet) been tested (only CPU).

(Common) Step 1 : Install OpenCV

On Windows :

Step 2 : onnxruntime-win-x64-1.4.0

Download onnxruntime-win-x64-1.4.0.zip and decompress somewhere

Step 3 : modify CMakeLists.txt

In LPReditor_ANPR/CMakeLists.txt, change ../onnxruntime-win-x64-1.4.0/ to point to the actual path of the onnxruntime-win-x64-1.4.0 directory

Step 4 : cmake

From cmake-gui, configure and generate LPReditor_ANPR/CMakeLists.txt

Step 5 : build solution in Visual Studio

On Linux :

Step 2 : onnxruntime-linux-x64-1.6.0

Download onnxruntime-linux-x64-1.6.0.tgz and decompress somewhere

Step 3 : modify CMakeLists.txt

In LPReditor_ANPR/CMakeLists.txt, change ../onnxruntime-linux-x64-1.6.0/ to point to the actual path of the onnxruntime-linux-x64-1.6.0 directory

Step 4 : cmake

From cmake-gui, configure and generate LPReditor_ANPR/CMakeLists.txt

Step 5 : make in the build LPReditor_ANPR/build dir

Integrating to your c++ code

//step 1 declare a global instance of ONNX Runtime api
const OrtApi* g_ort = OrtGetApiBase()->GetApi(ORT_API_VERSION);
//step2 declare an onnx runtime environment
std::string instanceName{ "image-classification-inference" };
// https://github.com/microsoft/onnxruntime/blob/rel-1.6.0/include/onnxruntime/core/session/onnxruntime_c_api.h#L123
Ort::Env env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING,instanceName.c_str());
//step 3 declare options for the runtime environment
Ort::SessionOptions sessionOptions;
sessionOptions.SetIntraOpNumThreads(1);
sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
//step 4 declare an onnx session (ie model), by giving references to the runtime environment, session options and path to the model
std::wstring widestr = std::wstring(model_filename.begin(), model_filename.end());
Yolov5_anpr_onxx_detector onnx_net(env, widestr.c_str(), sessionOptions);
//step 5 call the detect function of the Yolov5_anpr_onxx_detector object, on a cv::mat frame.
//This will retieves the lpn string
std::string lpn;
onnx_net.detect(frame, lpn);

Mandatory : to operate, the executable must load the model file. Doing that, you have to specify, either in the command line or directly in the source code (see step 4) its file path. You can download the model : due to its size, the lpreditor_anpr.onnx file is in the LPReditor_ANPR.zip release, under (LPReditor_ANPR/data/models/) or in the repo as a zipped file (/data/models/lpreditor_anpr.zip). Another option, is to train your own model, on your dataset, using YOLOv5 and then export it.

More detailed description

Building will produce an executable, with command line options (see them in the Open_LPReditor.cpp). It can read lpn(s) from a single image file or alternatively, from multiple image files, in a common directory. If the actual license plate number is provided (see func getTrueLPN in the code), in the image filename, then statistics of the correctness of the readings, are available.


  highgui.  


  Optionaly (in the command line), it can display a window, named with the read lpn :


  highgui.  


Another option is to display bounding boxes of caracters and license plate ROI (by activating show_boxes function, directly in the code) :  


()

Third party software

c++ inference (present code)

OpenCV 4.5.0 and higher

Copyright © 2021 , OpenCV team Apache 2 License

ONNXRuntime

Copyright © 2020 Microsoft. All rights reserved. MIT License

model trained with the use of :

YOLOv5

by Glenn Jocher (Ultralytics.com) GPL-3.0 License

ONNX

Copyright (c) Facebook, Inc. and Microsoft Corporation. All rights reserved. MIT License

License

All files, including the deep learning model file provided, are subject to GNU General Public License v3.0.

Commercial-friendly licensing available.