Unity接入百度AI實(shí)現(xiàn)果蔬識(shí)別
接口介紹:
識(shí)別近千種水果和蔬菜的名稱(chēng),適用于識(shí)別只含有一種果蔬的圖片,可自定義返回識(shí)別結(jié)果數(shù),適用于果蔬介紹相關(guān)的美食類(lèi)APP中。
創(chuàng)建應(yīng)用:
在產(chǎn)品服務(wù)中搜索圖像識(shí)別,創(chuàng)建應(yīng)用,獲取AppID、APIKey、SecretKey信息:
查閱官方文檔,以下是果蔬識(shí)別接口返回?cái)?shù)據(jù)參數(shù)詳情:
定義數(shù)據(jù)結(jié)構(gòu):
using System; /// <summary> /// 果蔬識(shí)別 /// </summary> [Serializable] public class IngredientRecognition { /// <summary> /// 唯一的log id,用于問(wèn)題定位 /// </summary> public float log_id; /// <summary> /// 返回結(jié)果數(shù)目,及result數(shù)組中的元素個(gè)數(shù) /// </summary> public int result_num; /// <summary> /// 識(shí)別結(jié)果數(shù)組 /// </summary> public IngredientRecognitionResult[] result; } /// <summary> /// 果蔬識(shí)別結(jié)果 /// </summary> [Serializable] public class IngredientRecognitionResult { /// <summary> /// 食材名稱(chēng) /// </summary> public string name; /// <summary> /// 置信度 /// </summary> public float score; }
下載C# SDK:
下載完成后將AipSdk.dll動(dòng)態(tài)庫(kù)導(dǎo)入到Unity中:
以下是調(diào)用接口時(shí)傳入的參數(shù)詳情:
在下載的SDK中并未發(fā)現(xiàn)通過(guò)url調(diào)用的重載函數(shù),大概是官方文檔未更新:
封裝調(diào)用函數(shù):
using System; using System.Collections.Generic; using UnityEngine; /// <summary> /// 圖像識(shí)別 /// </summary> public class ImageRecognition { //以下信息于百度開(kāi)發(fā)者中心控制臺(tái)創(chuàng)建應(yīng)用獲取 private const string appID = ""; private const string apiKey = ""; private const string secretKey = ""; /// <summary> /// 果蔬識(shí)別 /// </summary> /// <param name="bytes">圖片字節(jié)數(shù)據(jù)</param> /// <param name="topNum">返回預(yù)測(cè)得分top結(jié)果數(shù),如果為空或小于等于0默認(rèn)為5;如果大于20默認(rèn)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; } }
測(cè)試圖片:
using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { ImageRecognition.Ingredient(File.ReadAllBytes(Application.dataPath + "/Picture.jpg")); } }
以上就是Unity接入百度AI實(shí)現(xiàn)果蔬識(shí)別的詳細(xì)內(nèi)容,更多關(guān)于Unity果蔬識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#/VB.NET讀取條碼類(lèi)型及條碼在圖片中的坐標(biāo)位置實(shí)例
我們?cè)趧?chuàng)建條形碼時(shí),如果以圖片的方式將創(chuàng)建好的條碼保存到指定文件夾路徑,可以在程序中直接加載圖片使用;已生成的條碼圖片,需要通過(guò)讀取圖片中的條碼信息,如條碼類(lèi)型、條碼繪制區(qū)域在圖片中的四個(gè)頂點(diǎn)坐標(biāo)位置等,可參考本文中的方法2023-10-10C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)char字符數(shù)組與字符串相互轉(zhuǎn)換的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了C#字符數(shù)組轉(zhuǎn)字符串及字符串轉(zhuǎn)字符數(shù)組的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02WPF實(shí)現(xiàn)繪制扇形統(tǒng)計(jì)圖的示例代碼
這篇文章主要介紹了如何利用WPF繪制扇形統(tǒng)計(jì)圖,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09C#用記事本編寫(xiě)簡(jiǎn)單WinForm窗體程序
這篇文章主要為大家詳細(xì)介紹了C#用記事本編寫(xiě)簡(jiǎn)單WinForm窗體程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03