[Baidu AI] Baidu OCR license plate recognition V2

00. Catalog

01. Overview of license plate recognition

Supports identification of motor vehicle blue plates, yellow cards (single and double lines), green cards, large new energy vehicles (yellow and green), consulate license plates, police plates, armed police plates (single and double lines), military plates (single and double lines), Hong Kong and Macao entry and exit license plates, agricultural license plates, and civil aviation license plate regional numbers and license plate numbers, and can recognize multiple license plates in the image at the same time.

insert image description here

For video tutorials, please refer to License Plate Recognition API Call Tutorial (Video Version)

02. Demonstration of license plate recognition function

URL: https://ai.baidu.com/tech/ocr_cars/plate

03. License plate recognition application scenarios

Vehicle entry and exit recognition

Automatic identification of vehicle license plate information, applied to parking lots, residential areas, factories and other scenarios, realizes automatic and standardized management of unmanned and unmanned vehicle entry and exit, effectively reduces labor costs and pass card production costs, and greatly improves management efficiency.

Traffic Violation Detection

Automatically identify and locate illegal vehicle information, detect and record traffic violations in real time, effectively reduce the cost of human monitoring and improve management efficiency.

04. Features of license plate recognition

Multi-color recognition

It can recognize multiple colors of mainland motor vehicle license plates, including blue, green, yellow, white, etc., and automatically detect and recognize the license plate number, license plate color, and license plate position.

Multiple license plate recognition

Supports the recognition of multiple license plates in the same picture, and makes special optimizations for the small proportion of license plates to improve the recognition accuracy in roadside and surveillance high-shot scenarios.

License plate recognition at night

It supports license plate recognition at night and in low-light scenes, and is specially optimized for complex lighting and license plate reflections.

05. Online debugging

You can debug this interface in the sample code center , you can perform signature verification, view the request content and return results of online calls, and automatically generate sample codes.

06. Request clarification

request example

HTTP method:POST

Request URL:https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate

URL parameters:

parameter value
access_token Access_token obtained through API Key and Secret Key, refer to " Access Token Obtaining "

Header is as follows:

parameter value
Content-Type application/x-www-form-urlencoded

Place the request parameters in the body, the parameters are as follows:

request parameters

parameter Is it required? type illustrate
image Choose one from url string Image data, urlencoded after base64 encoding, the size after base64 encoding and urlencode is required to be no more than 4M, the shortest side is at least 15px, and the longest side is at most 4096px, supporting jpg/jpeg/png/bmp formats
url Choose one from image string The complete URL of the picture, the length of the URL does not exceed 1024 bytes, the size of the base64-encoded picture corresponding to the URL does not exceed 4M, the shortest side is at least 15px, and the longest side is at most 4096px, supports jpg/jpeg/png/bmp format, when the image field exists The url field is invalid, please pay attention to turn off the URL anti-leech
multi_detect no string Whether to detect multiple license plates, the default is false, when set to true, multiple license plates in a picture can be recognized
multi_scale no string It can be enabled in scenes with small license plates such as Gaopai. The default is false. When set to true, it can improve the detection and recognition of smaller license plates

request code example

Tip 1 : Before using the sample code, please remember to replace the sample Token, image address or Base64 information.

Tip 2 : For classes or libraries that some languages ​​depend on, please check the download address in the code comments.

Order

curl -i -k 'https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=【调用鉴权接口获取的token】' --data 'image=【图片Base64编码,需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'

C++ code


#include <iostream>
#include <curl/curl.h>

// libcurl库下载链接:https://curl.haxx.se/download.html
// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/
const static std::string request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
static std::string licensePlate_result;
/**
* curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中
* @param 参数定义见libcurl文档
* @return 返回值定义见libcurl文档
*/
static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {
    // 获取到的body存放在ptr中,先将其转换为string格式
    licensePlate_result = std::string((char *) ptr, size * nmemb);
    return size * nmemb;
}
/**
* 车牌识别
* @return 调用成功返回0,发生错误返回其他错误码
*/
int licensePlate(std::string &json_result, const std::string &access_token) {
    std::string url = request_url + "?access_token=" + access_token;
    CURL *curl = NULL;
    CURLcode result_code;
    int is_success;
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.data());
        curl_easy_setopt(curl, CURLOPT_POST, 1);
        curl_httppost *post = NULL;
        curl_httppost *last = NULL;
        curl_formadd(&post, &last, CURLFORM_COPYNAME, "image", CURLFORM_COPYCONTENTS, "【base64_img】", CURLFORM_END);

        curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
        result_code = curl_easy_perform(curl);
        if (result_code != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s",
                    curl_easy_strerror(result_code));
            is_success = 1;
            return is_success;
        }
        json_result = licensePlate_result;
        curl_easy_cleanup(curl);
        is_success = 0;
    } else {
        fprintf(stderr, "curl_easy_init() failed.");
        is_success = 1;
    }
    return is_success;
}

07. Return instructions

return example


{
    
    
	"words_result": [
		{
    
    
			"color": "blue",
			"number": "京KBT355",
			"probability": [
				0.9999992847,
				0.999999404,
				0.9999910593,
				0.9999765158,
				0.999994874,
				0.9998959303,
				0.9999984503,
				0.9999793768
			],
			"vertexes_location": [
				{
    
    
					"x": 495,
					"y": 589
				},
				{
    
    
					"x": 800,
					"y": 587
				},
				{
    
    
					"x": 800,
					"y": 676
				},
				{
    
    
					"x": 496,
					"y": 678
				}
			]
		}
	],
	"log_id": "6845817085824549137"
}

return parameter

parameter Is it necessary type illustrate
log_id yes uint64 Unique log id for problem location
words_result yes array[] recognition result array
+ color yes string License plate color: support blue, green, yellow, white, black, yellow_green (new energy large vehicle yellow-green license plate), unknown (unknown color)
+ number yes string License plate number
+ probability yes string The first 7 numbers are the confidence level of each character in the license plate, the eighth number is the average confidence level, the interval is 0-1
+ vertexes_location yes array[] Returns the position of the vertices of the polygon circumscribing the text
++ x yes uint32 Horizontal coordinates (coordinate 0 is the upper left corner)
++ y yes uint32 Vertical coordinates (coordinate 0 is the upper left corner)

08. C++ SDK

Minimum support for C++ 11+

The steps to directly use the development kit are as follows :

1. Download the C++ SDK compressed package from the official website .

2. Unzip the downloaded file aip-cpp-sdk-version.zip, where the file is the header file containing the implementation code.

3. Install the dependent library libcurl (https needs to be supported) openssl jsoncpp (>1.6.2 version, 0.x version will not be supported).

4. Add C++11 support when compiling the project (gcc/clang add compilation parameter -std=c++11), add third-party library link parameters lcurl, lcrypto, ljsoncpp.

5. Include ocr.h in the source code, import the header files in the compressed package to use the classes and methods under the aip namespace.

Text recognition C++ SDK directory structure

├── base
│  ├── base.h                                // 请求客户端基类
│  ├── base64.h                              // base64加密相关类
│  ├── http.h                                // http请求封装类
│  └── utils.h                               // 工具类
└── ocr.h                             // 文字识别 交互类

09. New client

client is a C++ client for text recognition, which provides a series of interactive methods for developers who use text recognition. After you import the corresponding header file, you can create a new client object.

Users can refer to the following code to create a new client:

    #include "ocr.h"

    // 设置APPID/AK/SK
    std::string app_id = "你的 App ID";
    std::string api_key = "你的 Api key";
    std::string secret_key = "你的 Secret Key";

    aip::Ocr client(app_id, api_key, secret_key);

In the above code, the constants APP_IDare created in the Baidu Smart Cloud console. The constants API_KEYare SECRET_KEYassigned to the user by the system after the application is created. Both are strings, which are used to identify the user and perform signature verification for access. They can be controlled in the AI ​​​​service Check out the list of applications in Taichung .

10. License Plate Recognition API

Recognize the motor vehicle license plate, and return the place of issuance and the license plate.

   Json::Value result;

   std::string image;
   aip::get_file_content("/assets/sample.jpg", &image);

   // 调用车牌识别
   result = client.license_plate(image, aip::null);

   // 如果有可选参数
   std::map<std::string, std::string> options;
   options["multi_detect"] = "true";

   // 带参数调用车牌识别
   result = client.license_plate(image, options);

License plate recognition request parameter details

parameter name Is it required? type Range of optional values Defaults illustrate
image yes std::string The binary string of image data can be obtained by using the aip::get_file_content function
multi_detect no std::string true false false Whether to detect multiple license plates, the default is false, when set to true, multiple license plates in a picture can be recognized

License plate recognition return data parameter details

parameter type Is it necessary illustrate
log_id uint64 yes Request identification code, random number, unique.
Color string yes license plate color
number string yes License plate number

License plate recognition return example

{
    
    
    "log_id": 3583925545,
    "words_result": {
    
    
        "color": "blue",
        "number": "粤BS7766"
    }
}

11. License plate recognition V2

test.cpp


#include <iostream>
#include "ocr.h"

using namespace std;

string resultAnalysis(Json::Value result, const char *str);

int main(void)
{

    std::string app_id = "357510336";
    std::string api_key = "QFgzuvVGEp3F9ifsdpnSfsyL2u";
    std::string secret_key = "YNpnn4tsMcBgs3yGkXsptISDC60ah8SNAa";

    aip::Ocr client(app_id, api_key, secret_key);



   Json::Value result;

   std::string image;
   aip::get_file_content("./1.jpg", &image);

   // 调用车牌识别
   result = client.license_plate(image, aip::null);

#if 0
   // 如果有可选参数
   std::map<std::string, std::string> options;
   options["multi_detect"] = "true";

   // 带参数调用车牌识别
   result = client.license_plate(image, options);
#endif

    cout << result << endl;

    cout << resultAnalysis(result, "nunmber") << endl;

    return 0;
}


//结果解析 解析车牌
string resultAnalysis(Json::Value result, const char *str)
{
    Json::FastWriter fastWriter;

    Json::Value value = result["words_result"]["number"];

    string ret = fastWriter.write(value);

    return ret;
}


Compilation and execution results

deng@local:~/car/test1$ ./test
{
    
    
        "log_id" : 1679107429077750701,
        "words_result" :
        {
    
    
                "color" : "blue",
                "number" : "\u7ca4B88888",
                "probability" :
                [
                        0.99970215559999998,
                        0.99969899650000005,
                        0.99961864950000001,
                        0.99991810319999996,
                        0.99973064659999999,
                        0.99987888339999997,
                        0.9997404218,
                        0.99975538249999996
                ],
                "vertexes_location" :
                [
                        {
    
    
                                "x" : 44,
                                "y" : 28
                        },
                        {
    
    
                                "x" : 774,
                                "y" : 35
                        },
                        {
    
    
                                "x" : 770,
                                "y" : 217
                        },
                        {
    
    
                                "x" : 36,
                                "y" : 211
                        }
                ]
        }
}
"\u7ca4B88888"

12. Appendix

Reference code: license-plate-v2(dj).rar

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/131689981