您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 3浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 接入百度AI - 果蔬识别

CoderZ1010 发布时间:2022-01-04 13:34:29 ,浏览量:3

接口介绍:

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

创建应用:     

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

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

定义数据结构:

using System;

/// 
/// 果蔬识别
/// 
[Serializable]
public class IngredientRecognition
{
    /// 
    /// 唯一的log id,用于问题定位
    /// 
    public float log_id;
    /// 
    /// 返回结果数目,及result数组中的元素个数
    /// 
    public int result_num;
    /// 
    /// 识别结果数组
    /// 
    public IngredientRecognitionResult[] result;
}

/// 
/// 果蔬识别结果
/// 
[Serializable]
public class IngredientRecognitionResult
{
    /// 
    /// 食材名称
    /// 
    public string name;
    /// 
    /// 置信度
    /// 
    public float score;
}

下载C# SDK:

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

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

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

封装调用函数: 

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

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

    /// 
    /// 果蔬识别
    /// 
    /// 图片字节数据
    /// 返回预测得分top结果数,如果为空或小于等于0默认为5;如果大于20默认20
    /// 
    public static IngredientRecognition Ingredient(byte[] bytes, int topNum = 5)
    {
        var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey);
        try
        {
            var options = new Dictionary
            {
                { "top_num", topNum },
            };
            var response = client.Ingredient(bytes, options);
            IngredientRecognition ingredientRecognition = JsonConvert.DeserializeObject(response.ToString());
            return ingredientRecognition;
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }
}

测试图片:

using System.IO;
using UnityEngine;

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

 

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

微信扫码登录

0.2484s