Unity接入百度AI实现货币识别
程序员文章站
2022-06-23 19:30:08
接口介绍:识别图像中的货币类型,以纸币为主,正反面均可准确识别,接口返回货币的名称、代码、面值、年份信息;可识别各类近代常见货币,如美元、欧元、英镑、法郎、澳大利亚元、俄罗斯卢布、日元、韩元、泰铢、印...
接口介绍:
识别图像中的货币类型,以纸币为主,正反面均可准确识别,接口返回货币的名称、代码、面值、年份信息;可识别各类近代常见货币,如美元、欧元、英镑、法郎、澳大利亚元、俄罗斯卢布、日元、韩元、泰铢、印尼卢比等。
创建应用:
在产品服务中搜索图像识别,创建应用,获取appid、apikey、secretkey信息:
查阅官方文档,以下是货币识别接口返回数据参数详情:
定义数据结构:
using system; /// <summary> /// 货币识别 /// </summary> [serializable] public class currencyrecognition { /// <summary> /// 请求标识码,随机数,唯一 /// </summary> public int log_id; /// <summary> /// 识别结果 /// </summary> public currencyrecognitionresult result; } /// <summary> /// 货币识别结果 /// </summary> [serializable] public class currencyrecognitionresult { /// <summary> /// 判断是否返回详细信息(除货币名称之外的其他字段),含有返回1,不含有返回0 /// </summary> public int hasdetail; /// <summary> /// 货币名称,无法识别返回空 /// </summary> public string currencyname; /// <summary> /// 货币代码,hasdetail = 0时,表示无法识别,该字段不返回 /// </summary> public string currencycode; /// <summary> /// 货币面值,hasdetail = 0时,表示无法识别,该字段不返回 /// </summary> public string currencydenomination; /// <summary> /// 货币年份,hasdetail = 0时,表示无法识别,该字段不返回 /// </summary> public string year; }
下载c# sdk:
下载完成后将aipsdk.dll动态库导入到unity中:
以下是调用接口时传入的参数详情:
封装调用函数:
using system; using system.collections.generic; using unityengine; /// <summary> /// 图像识别 /// </summary> public class imagerecognition { //以下信息于百度开发者中心控制台创建应用获取 private const string appid = ""; private const string apikey = ""; private const string secretkey = ""; /// <summary> /// 货币识别 /// </summary> /// <param name="bytes">图片字节数据</param> /// <returns></returns> public static currencyrecognition currency(byte[] bytes) { var client = new baidu.aip.imageclassify.imageclassify(apikey, secretkey); try { var response = client.currency(bytes); currencyrecognition currencyrecognition = jsonutility.fromjson<currencyrecognition>(response.tostring()); return currencyrecognition; } catch (exception error) { debug.logerror(error); } return null; } /// <summary> /// 货币识别 /// </summary> /// <param name="url">图片url地址</param> /// <returns></returns> public static currencyrecognition currency(string url) { var client = new baidu.aip.imageclassify.imageclassify(apikey, secretkey); try { var response = client.currencyurl(url); currencyrecognition currencyrecognition = jsonutility.fromjson<currencyrecognition>(response.tostring()); return currencyrecognition; } catch (exception error) { debug.logerror(error); } return null; } }
测试图片:
using system.io; using unityengine; public class example : monobehaviour { private void start() { imagerecognition.currency(file.readallbytes(application.datapath + "/picture.jpg")); } }
以上就是unity接入百度ai实现货币识别的详细内容,更多关于unity货币识别的资料请关注其它相关文章!