您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 4浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 接入百度AI - 货币识别

CoderZ1010 发布时间:2022-01-02 20:38:21 ,浏览量:4

接口介绍:

识别图像中的货币类型,以纸币为主,正反面均可准确识别,接口返回货币的名称、代码、面值、年份信息;可识别各类近代常见货币,如美元、欧元、英镑、法郎、澳大利亚元、俄罗斯卢布、日元、韩元、泰铢、印尼卢比等。

创建应用:     

在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:

查阅官方文档,以下是货币识别接口返回数据参数详情:

定义数据结构:

using System;

/// 
/// 货币识别
/// 
[Serializable]
public class CurrencyRecognition 
{
    /// 
    /// 请求标识码,随机数,唯一
    /// 
    public int log_id;
    /// 
    /// 识别结果
    /// 
    public CurrencyRecognitionResult result;
}

/// 
/// 货币识别结果
/// 
[Serializable]
public class CurrencyRecognitionResult
{
    /// 
    /// 判断是否返回详细信息(除货币名称之外的其他字段),含有返回1,不含有返回0
    /// 
    public int hasdetail;
    /// 
    /// 货币名称,无法识别返回空
    /// 
    public string currencyName;
    /// 
    /// 货币代码,hasdetail = 0时,表示无法识别,该字段不返回
    /// 
    public string currencyCode;
    /// 
    /// 货币面值,hasdetail = 0时,表示无法识别,该字段不返回
    /// 
    public string currencyDenomination;
    /// 
    /// 货币年份,hasdetail = 0时,表示无法识别,该字段不返回
    /// 
    public string year;
}

下载C# SDK:

 下载完成后将AipSdk.dll动态库导入到Unity中:

以下是调用接口时传入的参数详情:

封装调用函数: 

using System;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 图像识别
/// 
public class ImageRecognition 
{
    //以下信息于百度开发者中心控制台创建应用获取
    private const string appID = "";
    private const string apiKey = "";
    private const string secretKey = "";

    /// 
    /// 货币识别 
    /// 
    /// 图片字节数据
    /// 
    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(response.ToString());
            return currencyRecognition;
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }
    /// 
    /// 货币识别
    /// 
    /// 图片url地址
    /// 
    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(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"));
    }
}

关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.3510s