接口介绍:
创建应用:
在产品服务中搜索图像识别,创建应用,获取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")); } }
您可能感兴趣的文章: