Unity接入百度AI實現貨幣識別

接口介紹:

識別圖像中的貨幣類型,以紙幣為主,正反面均可準確識別,接口返回貨幣的名稱、代碼、面值、年份信息;可識別各類近代常見貨幣,如美元、歐元、英鎊、法郎、澳大利亞元、俄羅斯盧佈、日元、韓元、泰銖、印尼盧比等。

創建應用:     

在產品服務中搜索圖像識別,創建應用,獲取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貨幣識別的資料請關註WalkonNet其它相關文章!

推薦閱讀: