00. 目录
文章目录
01. 车牌识别概述
支持识别中国大陆机动车蓝牌、黄牌(单双行)、绿牌、大型新能源(黄绿)、领使馆车牌、警牌、武警牌(单双行)、军牌(单双行)、港澳出入境车牌、农用车牌、民航车牌的地域编号和车牌号,并能同时识别图像中的多张车牌。
视频教程请参见 车牌识别API调用教程(视频版)
02. 车牌识别功能演示
网址:https://ai.baidu.com/tech/ocr_cars/plate
03. 车牌识别应用场景
车辆进出场识别
自动识别车辆车牌信息,应用于停车场、小区、工厂等场景,实现无卡、无人的车辆进出场自动化、规范化管理,有效降低人力成本和通行卡证制作成本,大幅度提升管理效率。
交通违章检测
自动识别定位违章车辆信息,实时检测并记录交通违章行为,有效降低人力监控成本,提升管理效率。
04. 车牌识别特点
多颜色识别
可识别多种颜色大陆机动车车牌,包括蓝色、绿色、黄色、白色等,自动检测并识别车牌号码、车牌颜色、车牌位置。
多车牌识别
支持识别同一张图片中的多张车牌,针对车牌占比过小情况做专项优化,提升在路侧、监控高拍场景下的识别准确率。
夜间车牌识别
支持夜间、弱光场景下的车牌识别,针对复杂光线、车牌反光等情况进行专项优化。
05. 在线调试
您可以在 示例代码中心 中调试该接口,可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
06. 请求说明
请求示例
HTTP 方法: POST
请求URL: https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate
URL参数:
参数 | 值 |
---|---|
access_token | 通过API Key和Secret Key获取的access_token,参考“Access Token获取” |
Header如下:
参数 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded |
Body中放置请求参数,参数详情如下:
请求参数
参数 | 是否必选 | 类型 | 说明 |
---|---|---|---|
image | 和url二选一 | string | 图像数据,base64编码后进行urlencode,要求base64编码和urlencode后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式 |
url | 和image二选一 | string | 图片完整URL,URL长度不超过1024字节,URL对应的图片base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/jpeg/png/bmp格式,当image字段存在时url字段失效 请注意关闭URL防盗链 |
multi_detect | 否 | string | 是否检测多张车牌,默认为false,当置为true的时候可以对一张图片内的多张车牌进行识别 |
multi_scale | 否 | string | 在高拍等车牌较小的场景下可开启,默认为false,当置为true时,能够提高对较小车牌的检测和识别 |
请求代码示例
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
命令
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++代码
#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. 返回说明
返回示例
{
"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"
}
返回参数
参数 | 是否必须 | 类型 | 说明 |
---|---|---|---|
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
words_result | 是 | array[] | 识别结果数组 |
+ color | 是 | string | 车牌颜色:支持blue、green、yellow、white、black、yellow_green(新能源大型汽车黄绿车牌)、unknow(未知颜色) |
+ number | 是 | string | 车牌号码 |
+ probability | 是 | string | 前7个数字为车牌中每个字符的置信度,第8个数字为平均置信度,区间为0-1 |
+ vertexes_location | 是 | array[] | 返回文字外接多边形顶点位置 |
++ x | 是 | uint32 | 水平坐标(坐标0点为左上角) |
++ y | 是 | uint32 | 垂直坐标(坐标0点为左上角) |
08. C++ SDK
最低支持 C++ 11+
直接使用开发包步骤如下:
1.在官方网站下载C++ SDK压缩包。
2.将下载的aip-cpp-sdk-version.zip
解压, 其中文件为包含实现代码的头文件。
3.安装依赖库libcurl(需要支持https) openssl jsoncpp(>1.6.2版本,0.x版本将不被支持)。
4.编译工程时添加 C++11 支持 (gcc/clang 添加编译参数 -std=c++11), 添加第三方库链接参数 lcurl, lcrypto, ljsoncpp。
5.在源码中include ocr.h ,引入压缩包中的头文件以使用aip命名空间下的类和方法。
文字识别 C++ SDK目录结构
├── base
│ ├── base.h // 请求客户端基类
│ ├── base64.h // base64加密相关类
│ ├── http.h // http请求封装类
│ └── utils.h // 工具类
└── ocr.h // 文字识别 交互类
09. 新建client
client是文字识别的C++客户端,为使用文字识别的开发人员提供了一系列的交互方法。当您引入了相应头文件后就可以新建一个client对象。
用户可以参考如下代码新建一个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);
在上面代码中,常量APP_ID
在百度智能云控制台中创建,常量API_KEY
与SECRET_KEY
是在创建完毕应用后,系统分配给用户的,均为字符串,用于标识用户,为访问做签名验证,可在AI服务控制台中的应用列表中查看。
10. 车牌识别API
识别机动车车牌,并返回签发地和号牌。
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);
车牌识别 请求参数详情
参数名称 | 是否必选 | 类型 | 可选值范围 | 默认值 | 说明 |
---|---|---|---|---|---|
image | 是 | std::string | 图片数据的二进制字符串,可以使用aip::get_file_content函数获取 | ||
multi_detect | 否 | std::string | true false | false | 是否检测多张车牌,默认为false,当置为true的时候可以对一张图片内的多张车牌进行识别 |
车牌识别 返回数据参数详情
参数 | 类型 | 是否必须 | 说明 |
---|---|---|---|
log_id | uint64 | 是 | 请求标识码,随机数,唯一。 |
Color | string | 是 | 车牌颜色 |
number | string | 是 | 车牌号码 |
车牌识别 返回示例
{
"log_id": 3583925545,
"words_result": {
"color": "blue",
"number": "粤BS7766"
}
}
11. 车牌识别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;
}
编译和执行结果
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"