Unity實現菜品識別的示例代碼
接口介紹:
識別超過9千種菜品,支持客戶創建屬於自己的菜品圖庫,可準確識別圖片中的菜品名稱、位置、卡路裡信息,並獲取百科信息,適用於多種客戶識別菜品的業務場景中。
創建應用:
在產品服務中搜索圖像識別,創建應用,獲取AppID、APIKey、SecretKey信息:
查閱官方文檔,以下是菜品識別接口返回數據參數詳情:
定義數據結構:
using System; /// <summary> /// 菜品識別 /// </summary> [Serializable] public class DishRecognition { /// <summary> /// 唯一的log id,用於問題定位 /// </summary> public float log_id; /// <summary> /// 返回結果數目,及result數組中的元素個數 /// </summary> public int result_num; /// <summary> /// 菜品識別結果數組 /// </summary> public DishRecognitionResult[] result; } /// <summary> /// 菜品識別結果 /// </summary> [Serializable] public class DishRecognitionResult { /// <summary> /// 菜名 /// </summary> public string name; /// <summary> /// 卡路裡,每100g的卡路裡含量 /// </summary> public float calorie; /// <summary> /// 識別結果中每一行的置信度值,0-1 /// </summary> public float probability; /// <summary> /// 百科詞條 /// </summary> public BaikeInfo baike_info; } [Serializable] public class BaikeInfo { /// <summary> /// 對應識別結果百度百科頁面鏈接 /// </summary> public string baike_url; /// <summary> /// 對應識別結果百科圖片鏈接 /// </summary> public string image_url; /// <summary> /// 對應識別結果百科內容描述 /// </summary> public string description; }
下載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> /// <param name="topNum">返回預測得分top結果數,默認為5</param> /// <param name="filterThreshold">默認0.95,可以通過該參數調節識別效果,降低非菜識別率.</param> /// <param name="baikeNum"> 返回百科信息的結果數,默認不返回</param> /// <returns></returns> public static DishRecognition Dish(byte[] bytes, int topNum = 5, float filterThreshold = 0.95f, int baikeNum = 0) { var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey); try { var options = new Dictionary<string, object> { { "top_num", topNum }, { "filter_threshold", Mathf.Clamp01(filterThreshold)}, { "baike_num", baikeNum} }; var response = client.DishDetect(bytes, options); DishRecognition dishRecognition = JsonConvert.DeserializeObject<DishRecognition>(response.ToString()); return dishRecognition; } catch (Exception error) { Debug.LogError(error); } return null; } }
測試圖片:
using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { ImageRecognition.Dish(File.ReadAllBytes(Application.dataPath + "/Picture.jpg")); } }
到此這篇關於Unity實現菜品識別的示例代碼的文章就介紹到這瞭,更多相關Unity菜品識別內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!