本文主要介绍了如何通过Unity实现动物识别,可以实现识别近八千种动物,接口返回动物名称,并可获取识别结果对应的百科信息,感兴趣的可以了解一下
接口介绍:
识别近八千种动物,接口返回动物名称,并可获取识别结果对应的百科信息;还可使用EasyDL定制训练平台,定制识别分类标签。适用于拍照识图、幼教科普、图像内容分析等场景
创建应用:
在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是动物识别接口返回数据参数详情:
定义数据结构:
using System; ////// 动物识别 /// [Serializable] public class AnimalRecognition { ////// 唯一的log id,用于问题定位 /// public float log_id; ////// 识别结果数组 /// public AnimalRecognitionResult[] result; } [Serializable] public class AnimalRecognitionResult { ////// 动物名称 /// public string name; ////// 置信度 /// public float score; ////// 百科词条 /// public BaikeInfo baike_info; } [Serializable] public class BaikeInfo { ////// 对应识别结果百度百科页面链接 /// public string baike_url; ////// 对应识别结果百科图片链接 /// public string image_url; ////// 对应识别结果百科内容描述 /// public string description; }
下载C# SDK:
下载完成后将AipSdk.dll动态库导入到Unity中:
以下是调用接口时传入的参数详情:
封装调用函数:
using System; using System.Collections.Generic; using UnityEngine; ////// 图像识别 /// public class ImageRecognition { //以下信息于百度开发者中心控制台创建应用获取 private const string appID = ""; private const string apiKey = ""; private const string secretKey = ""; ////// 动物识别 /// /// 图片字节数据 /// 返回预测得分top结果数,默认为6 /// 返回百科信息的结果数,默认不返回 ///public static AnimalRecognition Animal(byte[] bytes, int topNum = 6, int baikeNum = 0) { var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey); try { var options = new Dictionary { { "top_num", topNum }, { "baike_num", baikeNum } }; var response = client.AnimalDetect(bytes, options); AnimalRecognition animalRecognition = JsonConvert.DeserializeObject (response.ToString()); return animalRecognition; } catch (Exception error) { Debug.LogError(error); } return null; } }
测试图片:
using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { ImageRecognition.Animal(File.ReadAllBytes(Application.dataPath + "/Picture.jpg-600")); } }
以上就是Unity实现动物识别的示例代码的详细内容,更多关于Unity动物识别的资料请关注0133技术站其它相关文章!
以上就是Unity实现动物识别的示例代码的详细内容,更多请关注0133技术站其它相关文章!