接口介绍:
该请求用于检测一张车辆图片的具体车型。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的车辆品牌及型号。
创建应用:
在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是车型识别接口返回数据参数详情:
定义数据结构:
using System;
///
/// 车型识别
///
[Serializable]
public class CarDetect
{
///
/// 唯一的log id 用于定位问题
///
public int log_id;
///
/// 颜色
///
public string color_result;
///
/// 车型识别结果数组
///
public CarDetectResult[] result;
}
[Serializable]
public class CarDetectResult
{
///
/// 车型名称
///
public string name;
///
/// 置信度
///
public float score;
///
/// 年份
///
public string year;
///
/// 对应识别结果的百科词条名称
///
public BaikeInfo baike_info;
///
/// 车在图片中的位置信息
///
public string location_result;
}
[Serializable]
public class BaikeInfo
{
///
/// 对应识别结果百度百科页面链接
///
public string baike_url;
///
/// 对应识别结果百科图片链接
///
public string image_url;
///
/// 对应识别结果百科内容描述
///
public string description;
}
下载C# SDK:
下载完成后将AipSdk.dll动态库导入到Unity中:
以下是调用接口时传入的参数详情:
封装调用函数:
using System;
using UnityEngine;
using Newtonsoft.Json;
using System.Collections.Generic;
///
/// 图像识别
///
public class ImageRecognition
{
//以下信息于百度开发者中心控制台创建应用获取
private const string appID = "";
private const string apiKey = "";
private const string secretKey = "";
///
/// 车型识别
///
/// 图片字节数据
/// 返回预测得分top结果数
/// 返回百科信息的结果数
///
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
{
{"top_num", topNum },
{"baike_num", baikeNum }
};
var response = client.CarDetect(bytes, options);
CarDetect carDetect = JsonUtility.FromJson(response.ToString());
return carDetect;
}
catch(Exception error)
{
Debug.LogError(error);
}
return null;
}
}
测试图片:
using System.IO;
using UnityEngine;
public class Example : MonoBehaviour
{
private void Start()
{
ImageRecognition.Car(File.ReadAllBytes(Application.dataPath + "/Picture.jpeg"));
}
}