Unity接入百度AI實現(xiàn)果蔬識別
接口介紹:
識別近千種水果和蔬菜的名稱,適用于識別只含有一種果蔬的圖片,可自定義返回識別結果數(shù),適用于果蔬介紹相關的美食類APP中。
創(chuàng)建應用:
在產(chǎn)品服務中搜索圖像識別,創(chuàng)建應用,獲取AppID、APIKey、SecretKey信息:


查閱官方文檔,以下是果蔬識別接口返回數(shù)據(jù)參數(shù)詳情:

定義數(shù)據(jù)結構:
using System;
/// <summary>
/// 果蔬識別
/// </summary>
[Serializable]
public class IngredientRecognition
{
/// <summary>
/// 唯一的log id,用于問題定位
/// </summary>
public float log_id;
/// <summary>
/// 返回結果數(shù)目,及result數(shù)組中的元素個數(shù)
/// </summary>
public int result_num;
/// <summary>
/// 識別結果數(shù)組
/// </summary>
public IngredientRecognitionResult[] result;
}
/// <summary>
/// 果蔬識別結果
/// </summary>
[Serializable]
public class IngredientRecognitionResult
{
/// <summary>
/// 食材名稱
/// </summary>
public string name;
/// <summary>
/// 置信度
/// </summary>
public float score;
}下載C# SDK:

下載完成后將AipSdk.dll動態(tài)庫導入到Unity中:

以下是調(diào)用接口時傳入的參數(shù)詳情:

在下載的SDK中并未發(fā)現(xiàn)通過url調(diào)用的重載函數(shù),大概是官方文檔未更新:

封裝調(diào)用函數(shù):
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 圖像識別
/// </summary>
public class ImageRecognition
{
//以下信息于百度開發(fā)者中心控制臺創(chuàng)建應用獲取
private const string appID = "";
private const string apiKey = "";
private const string secretKey = "";
/// <summary>
/// 果蔬識別
/// </summary>
/// <param name="bytes">圖片字節(jié)數(shù)據(jù)</param>
/// <param name="topNum">返回預測得分top結果數(shù),如果為空或小于等于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;
}
}測試圖片:

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

以上就是Unity接入百度AI實現(xiàn)果蔬識別的詳細內(nèi)容,更多關于Unity果蔬識別的資料請關注腳本之家其它相關文章!
相關文章
C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結合實例形式簡單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實現(xiàn)技巧,需要的朋友可以參考下2017-02-02
C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03

