Unity接入百度AI實現(xiàn)貨幣識別
接口介紹:
識別圖像中的貨幣類型,以紙幣為主,正反面均可準確識別,接口返回貨幣的名稱、代碼、面值、年份信息;可識別各類近代常見貨幣,如美元、歐元、英鎊、法郎、澳大利亞元、俄羅斯盧布、日元、韓元、泰銖、印尼盧比等。
創(chuàng)建應(yīng)用:
在產(chǎn)品服務(wù)中搜索圖像識別,創(chuàng)建應(yīng)用,獲取AppID、APIKey、SecretKey信息:


查閱官方文檔,以下是貨幣識別接口返回數(shù)據(jù)參數(shù)詳情:

定義數(shù)據(jù)結(jié)構(gòu):
using System;
/// <summary>
/// 貨幣識別
/// </summary>
[Serializable]
public class CurrencyRecognition
{
/// <summary>
/// 請求標識碼,隨機數(shù),唯一
/// </summary>
public int log_id;
/// <summary>
/// 識別結(jié)果
/// </summary>
public CurrencyRecognitionResult result;
}
/// <summary>
/// 貨幣識別結(jié)果
/// </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動態(tài)庫導入到Unity中:

以下是調(diào)用接口時傳入的參數(shù)詳情:

封裝調(diào)用函數(shù):
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 圖像識別
/// </summary>
public class ImageRecognition
{
//以下信息于百度開發(fā)者中心控制臺創(chuàng)建應(yīng)用獲取
private const string appID = "";
private const string apiKey = "";
private const string secretKey = "";
/// <summary>
/// 貨幣識別
/// </summary>
/// <param name="bytes">圖片字節(jié)數(shù)據(jù)</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實現(xiàn)貨幣識別的詳細內(nèi)容,更多關(guān)于Unity貨幣識別的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C# 使用Word模板導出數(shù)據(jù)的實現(xiàn)代碼
最近接到個需求,使用word模板導出數(shù)據(jù),怎么實現(xiàn)這個需求呢,今天小編通過實例代碼給大家介紹C# 使用Word模板導出數(shù)據(jù)的方法,感興趣的朋友一起看看吧2021-06-06
C#中把任意類型的泛型集合轉(zhuǎn)換成SQLXML數(shù)據(jù)格式的實例
本文主要分享了C#中把任意類型的泛型集合轉(zhuǎn)換成SQLXML數(shù)據(jù)格式的實例代碼。具有很好的參考價值,需要的朋友可以看下2016-12-12
C#使用Socket實現(xiàn)發(fā)送和接收圖片的方法
這篇文章主要介紹了C#使用Socket實現(xiàn)發(fā)送和接收圖片的方法,涉及C#操作socket發(fā)送與接收文件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
C# Winform實現(xiàn)導入和導出Excel文件
這篇文章主要為大家詳細介紹了C# Winform實現(xiàn)導入和導出Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12

