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菜品识别内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)