Unity实现车型识别的示例代码
这篇文章主要介绍了在Unity中接入百度AI,实现检测一张车辆图片的具体车型。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的车辆品牌及型号。需要的可以参考一下
接口介绍:
该请求用于检测一张车辆图片的具体车型。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的车辆品牌及型号。
创建应用:
在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是车型识别接口返回数据参数详情:
定义数据结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using System; /// <summary> /// 车型识别 /// </summary> [Serializable] public class CarDetect { /// <summary> /// 唯一的log id 用于定位问题 /// </summary> public int log_id; /// <summary> /// 颜色 /// </summary> public string color_result; /// <summary> /// 车型识别结果数组 /// </summary> public CarDetectResult[] result; } [Serializable] public class CarDetectResult { /// <summary> /// 车型名称 /// </summary> public string name; /// <summary> /// 置信度 /// </summary> public float score; /// <summary> /// 年份 /// </summary> public string year; /// <summary> /// 对应识别结果的百科词条名称 /// </summary> public BaikeInfo baike_info; /// <summary> /// 车在图片中的位置信息 /// </summary> public string location_result; } [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中:
以下是调用接口时传入的参数详情:
封装调用函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | using System; using UnityEngine; using Newtonsoft.Json; using System.Collections.Generic; /// <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结果数</param> /// <param name="baikeNum">返回百科信息的结果数</param> /// <returns></returns> public static CarDetect Car( byte [] bytes, int topNum = 5, int baikeNum = 0) { var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey); try { var options = new Dictionary< string , object > { { "top_num" , topNum }, { "baike_num" , baikeNum } }; var response = client.CarDetect(bytes, options); CarDetect carDetect = JsonUtility.FromJson<CarDetect>(response.ToString()); return carDetect; } catch (Exception error) { Debug.LogError(error); } return null ; } } |
测试图片:
1 2 3 4 5 6 7 8 9 10 | using System.IO; using UnityEngine; public class Example : MonoBehaviour { private void Start() { ImageRecognition.Car(File.ReadAllBytes(Application.dataPath + "/Picture.jpeg" )); } } |
到此这篇关于Unity实现车型识别的示例代码的文章就介绍到这了
原文链接:https://blog.csdn.net/qq_42139931/article/details/122270381