货币识别能识别图像中的货币类型,返回货币名称、代码、面值、年份信息,可识别百余种国内外常见货币
应用场景
外汇兑换:金融机构外汇兑换时,自动识别货币类型,弥补人工判断知识面受限、主观失误等问题,提升兑换效率
接口描述
识别图像中的货币类型,以纸币为主,正反面均可准确识别,接口返回货币的名称、代码、面值、年份信息;可识别各类近代常见货币,如美元、欧元、英镑、法郎、澳大利亚元、俄罗斯卢布、日元、韩元、泰铢、印尼卢比等。
请求说明
HTTP 方法: POST
请求 URL: https://aip.baidubce.com/rest/2.0/image-classify/v1/currency
URL参数: access_token
Header 参数: Content-Type
= application/x-www-form-urlencoded
Body 参数:见下表
参数
是否必选
类型
默认值
说明
image
是
string
-
图像数据,base64
编码,要求 base64
编码后大小不超过 4M,最短边至少 15px
,最长边最大 4096px
, 支持 jpg/png/bmp
格式 。注意:图片需要 base64
编码、去掉编码头后再进行 urlencode
返回说明
返回参数如下表:
字段
是否必选
类型
说明
log_id
是
uint64
请求标识码,随机数,唯一
result
是
dict
识别结果
+hasdetail
是
unit
判断是否返回详细信息(除货币名称之外的其他字段),含有返回 1,不含有返回 0
+currencyName
是
string
货币名称,无法识别返回空,示例:新加坡元
+currencyCode
否
string
货币代码,hasdetail = 0
时,表示无法识别,该字段不返回,示例:SGD
+currencyDenomination
否
string
货币面值,hasdetail = 0
时,表示无法识别,该字段不返回,示例:50元
+year
否
string
货币年份,hasdetail = 0
时,表示无法识别,该字段不返回,示例:2004年
返回示例如下:
1 2 3 4 5 6 7 8 9 10 { "log_id" : 4247844653395235754 , "result" : { "currencyName" : "美元" , "hasdetail" : 1 , "currencyCode" : "USD" , "year" : "2001年" , "currencyDenomination" : "50美元" } }
C++ 代码实现调用
这里假设已经将环境配置好了,环境配置的文章可以参考 Windows 下使用 Vcpkg 配置百度 AI 图像识别 C++开发环境(VS2017) 。
为了方便,首先根据返回参数定义了一个结构体,该结构体包括了返回参数中的参数,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 struct CurrencyInfo { bool hasdetail; std ::string currencyName; std ::string currencyCode; std ::string currencyDenomination; std ::string year; void print () { std ::cout << std ::setw(30 ) << std ::setfill('-' ) << '\n' ; std ::cout << "currency name: " << currencyName << '\n' ; if (hasdetail) { std ::cout << "currency code: " << currencyCode << '\n' ; std ::cout << "currency denomination: " << currencyDenomination << '\n' ; std ::cout << "year: " << year << '\n' ; } } };
在 CurrencyInfo
结构体中,定义了一个 print
方法以打印获取的结果。
然后定义了一个类来调用接口并获取结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class Currency { public : Currency(); ~Currency(); Json::Value request (std ::string imgBase64, std ::map <std ::string , std ::string >& options) ; void getResult (CurrencyInfo& result) ; private : Json::Value obj_; std ::string url_; std ::string filename_; };
类中的私有成员 obj_
表示返回结果对应的 json 对象。url_
表示请求的 url,filename_
表示用于存储 access token
的文件的文件名。
request
函数输入请求图像的 base64 编码以及请求参数,返回一个 json 对象,json 对象中包含请求的结果。
getResult
获取货币识别结果。
完整代码如下
util.h
和 util.cpp
代码参见 (简单调用篇 01) 通用物体和场景识别高级版 - C++ 简单调用
Currency.h
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #pragma once #include "util.h" struct CurrencyInfo { bool hasdetail; std ::string currencyName; std ::string currencyCode; std ::string currencyDenomination; std ::string year; void print () { std ::cout << std ::setw(30 ) << std ::setfill('-' ) << '\n' ; std ::cout << "currency name: " << currencyName << '\n' ; if (hasdetail) { std ::cout << "currency code: " << currencyCode << '\n' ; std ::cout << "currency denomination: " << currencyDenomination << '\n' ; std ::cout << "year: " << year << '\n' ; } } }; class Currency { public : Currency(); ~Currency(); Json::Value request (std ::string imgBase64, std ::map <std ::string , std ::string >& options) ; void getResult (CurrencyInfo& result) ; private : Json::Value obj_; std ::string url_; std ::string filename_; }; void currencyTest () ;
Currency.cpp
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 #include "Currency.h" Currency::Currency() { filename_ = "tokenKey" ; url_ = "https://aip.baidubce.com/rest/2.0/image-classify/v1/currency" ; } Currency::~Currency() { } Json::Value Currency::request (std ::string imgBase64, std ::map <std ::string , std ::string >& options) { std ::string response; Json::Value obj; std ::string token; std ::string body; mergeHttpPostBody(body, imgBase64, options); std ::string url = url_; getHttpPostUrl(url, filename_, token); int status_code = httpPostRequest(url, body, response); if (status_code != CURLcode::CURLE_OK) { obj["curl_error_code" ] = status_code; obj_ = obj; return obj; } generateJson(response, obj); if (obj["error_code" ].asInt() == 110 || obj["error_code" ].asInt() == 111 ) { token = getTokenKey(); writeFile(filename_, token); return request(imgBase64, options); } obj_ = obj; checkErrorWithExit(obj); return obj; } void Currency::getResult (CurrencyInfo & result) { result.hasdetail = obj_["result" ]["hasdetail" ].asBool(); result.currencyName = UTF8ToGB(obj_["result" ]["currencyName" ].asString().c_str()); result.currencyCode = obj_["result" ]["currencyCode" ].asString(); result.currencyDenomination = UTF8ToGB(obj_["result" ]["currencyDenomination" ].asString().c_str()); result.year = UTF8ToGB(obj_["result" ]["year" ].asString().c_str()); } void currencyTest () { std ::cout << "size: " << sizeof (CurrencyInfo) << "\n" ; std ::string imgFile = "./images/dollar.png" ; std ::string imgBase64; imgToBase64(imgFile, imgBase64); std ::map <std ::string , std ::string > options; Json::Value obj; Currency currencyObj; obj = currencyObj.request(imgBase64, options); CurrencyInfo result; currencyObj.getResult(result); result.print(); }
main.cpp
代码如下:
1 2 3 4 5 6 7 8 9 10 #include "util.h" #include "Currency.h" #include <stdlib.h> int main () { currencyTest(); system("pause" ); return EXIT_SUCCESS; }
运行结果
测试图像