接口介绍:
该请求用于检测和识别图片中的品牌LOGO信息。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中LOGO的名称、位置和置信度。当效果欠佳时,可以建立子库(在百度开发者中心控制台创建应用并申请建库)并通过调用logo入口接口完成自定义logo入库,提高识别效果。
创建应用:
在产品服务中搜索图像识别,创建应用,获取AppID、APIKey、SecretKey信息:
查阅官方文档,以下是Logo商标识别接口返回数据参数详情:
定义数据结构:
using System;
///
/// Logo识别响应数据结构
///
[Serializable]
public class LogoRecognition
{
///
/// 请求标识码,随机数,唯一
///
public float log_id;
///
/// 返回结果数目,即:result数组中元素个数
///
public int result_num;
///
/// 返回结果数组,每一项为一个识别出的logo
///
public LogoRecognitionResult[] result;
}
[Serializable]
public class LogoRecognitionResult
{
///
/// 位置信息
///
public LogoRecognitionResultLocation location;
///
/// 识别的品牌名称
///
public string name;
///
/// 分类结果置信度(0--1.0)
///
public float probability;
///
/// type=0为1千种高优商标识别结果;type=1为2万类logo库的结果;其它type为自定义logo库结果
///
public int type;
}
///
/// 位置信息
///
[Serializable]
public class LogoRecognitionResultLocation
{
///
/// 左起像素位置
///
public float left;
///
/// 上起像素位置
///
public float top;
///
/// 像素宽
///
public float width;
///
/// 像素高
///
public float height;
}
下载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 = "";
///
/// Logo商标识别
///
/// Logo图片字节数据
/// 是否只使用自定义logo库的结果,默认false:返回自定义库+默认库的识别结果
///
public static LogoRecognition Logo(byte[] bytes, bool customLib = false)
{
var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey);
try
{
var options = new Dictionary
{
{ "custom_lib", customLib}
};
var response = client.LogoSearch(bytes, options);
LogoRecognition logoRecognition = JsonConvert.DeserializeObject(response.ToString());
return logoRecognition;
}
catch (Exception error)
{
Debug.LogError(error);
}
return null;
}
}
测试图片:
using System.IO;
using UnityEngine;
public class Example : MonoBehaviour
{
private void Start()
{
ImageRecognition.Logo(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));
}
}