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