温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Unity如何接入AI实现果蔬识别

发布时间:2022-02-11 09:11:34 来源:亿速云 阅读:126 作者:iii 栏目:开发技术

这篇“Unity如何接入AI实现果蔬识别”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Unity如何接入AI实现果蔬识别”文章吧。

接口介绍:

识别近千种水果和蔬菜的名称,适用于识别只含有一种果蔬的图片,可自定义返回识别结果数,适用于果蔬介绍相关的美食类APP中。

创建应用:     

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

Unity如何接入AI实现果蔬识别

Unity如何接入AI实现果蔬识别

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

Unity如何接入AI实现果蔬识别

定义数据结构:

using System;
 
/// <summary>
/// 果蔬识别
/// </summary>
[Serializable]
public class IngredientRecognition
{
    /// <summary>
    /// 唯一的log id,用于问题定位
    /// </summary>
    public float log_id;
    /// <summary>
    /// 返回结果数目,及result数组中的元素个数
    /// </summary>
    public int result_num;
    /// <summary>
    /// 识别结果数组
    /// </summary>
    public IngredientRecognitionResult[] result;
}
 
/// <summary>
/// 果蔬识别结果
/// </summary>
[Serializable]
public class IngredientRecognitionResult
{
    /// <summary>
    /// 食材名称
    /// </summary>
    public string name;
    /// <summary>
    /// 置信度
    /// </summary>
    public float score;
}

下载C# SDK:

Unity如何接入AI实现果蔬识别

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

Unity如何接入AI实现果蔬识别

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

Unity如何接入AI实现果蔬识别

在下载的SDK中并未发现通过url调用的重载函数,大概是官方文档未更新:

Unity如何接入AI实现果蔬识别

封装调用函数: 

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>
    /// <param name="topNum">返回预测得分top结果数,如果为空或小于等于0默认为5;如果大于20默认20</param>
    /// <returns></returns>
    public static IngredientRecognition Ingredient(byte[] bytes, int topNum = 5)
    {
        var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey);
        try
        {
            var options = new Dictionary<string, object>
            {
                { "top_num", topNum },
            };
            var response = client.Ingredient(bytes, options);
            IngredientRecognition ingredientRecognition = JsonConvert.DeserializeObject<IngredientRecognition>(response.ToString());
            return ingredientRecognition;
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }
}

测试图片:

Unity如何接入AI实现果蔬识别

using System.IO;
using UnityEngine;
 
public class Example : MonoBehaviour
{
    private void Start()
    {
        ImageRecognition.Ingredient(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));
    }
}

Unity如何接入AI实现果蔬识别

以上就是关于“Unity如何接入AI实现果蔬识别”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI