Unity接入百度AI实现货币识别

 更新时间:2022年01月04日 10:05:51   作者:CoderZ1010  
本文主要介绍了在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货币识别的资料请关注脚本之家其它相关文章!

相关文章

  • 遍历Hashtable 的几种方法

    遍历Hashtable 的几种方法

    遍历Hashtable 的几种方法...
    2007-03-03
  • C#之Socket操作类实例解析

    C#之Socket操作类实例解析

    这篇文章主要介绍了C#的Socket操作类用法,需要的朋友可以参考下
    2014-08-08
  • C#实现将json转换为DataTable的方法

    C#实现将json转换为DataTable的方法

    这篇文章主要介绍了C#实现将json转换为DataTable的方法,涉及C#操作json及DataTable的技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-03-03
  • 深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。
    2016-11-11
  • C#中使用Spire.doc对word的操作方式

    C#中使用Spire.doc对word的操作方式

    这篇文章主要介绍了C#中使用Spire.doc对word的操作方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • C# IEnumerator枚举器的具体使用

    C# IEnumerator枚举器的具体使用

    本文主要介绍了C# IEnumerator枚举器的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 基于字符集、字符编码与HTTP编码解码之万象详解

    基于字符集、字符编码与HTTP编码解码之万象详解

    本篇文章小编为大家介绍,基于字符集、字符编码与HTTP编码解码之万象详解。需要的朋友参考下
    2013-04-04
  • 详解C#中==、Equals、ReferenceEquals的区别

    详解C#中==、Equals、ReferenceEquals的区别

    C#中Equals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等,本篇文章详解C#中Equals , == , ReferenceEquals都可以用于判断两个对象的个体是不是相等,有兴趣的可以了解一下。
    2016-12-12
  • C#中foreach循环对比for循环的优势和劣势

    C#中foreach循环对比for循环的优势和劣势

    循环语句是编程的基本语句,在C#中除了沿用C语言的循环语句外,还提供了foreach语句来实现循环,下面这篇文章主要给大家介绍了关于C#中foreach循环对比for循环的优势和劣势,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-09-09
  • c#处理3种json数据的实例

    c#处理3种json数据的实例

    这篇文章主要介绍了c#处理包含数组、对象的复杂json数据的方法,,需要的朋友可以参考下
    2014-03-03

最新评论