接口介绍:
该请求用于检测一张车辆图片的具体车型。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的车辆品牌及型号。
创建应用:
在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是车型识别接口返回数据参数详情:
定义数据结构:
代码语言:javascript复制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中:
以下是调用接口时传入的参数详情:
封装调用函数:
代码语言:javascript复制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;
}
}
测试图片:
代码语言:javascript复制using System.IO;
using UnityEngine;
public class Example : MonoBehaviour
{
private void Start()
{
ImageRecognition.Car(File.ReadAllBytes(Application.dataPath "/Picture.jpeg"));
}
}