Unity實(shí)現(xiàn)紅酒識(shí)別的示例代碼
接口介紹:
識(shí)別圖像中的紅酒標(biāo)簽,返回紅酒名稱(chēng)、國(guó)家、產(chǎn)區(qū)、酒莊、類(lèi)型、糖分、葡萄品種、酒品描述等信息,可識(shí)別數(shù)十萬(wàn)中外紅酒;支持自定義紅酒圖庫(kù),在自建庫(kù)中搜索特定紅酒信息。
創(chuàng)建應(yīng)用:
在產(chǎn)品服務(wù)中搜索圖像識(shí)別,創(chuàng)建應(yīng)用,獲取AppID、APIKey、SecretKey信息:


查閱官方文檔,以下是紅酒識(shí)別接口返回?cái)?shù)據(jù)參數(shù)詳情:

定義數(shù)據(jù)結(jié)構(gòu):
using System;
[Serializable]
public class RedwineRecognition
{
/// <summary>
/// 請(qǐng)求標(biāo)識(shí)碼,隨機(jī)數(shù),唯一
/// </summary>
public float log_id;
/// <summary>
/// 識(shí)別結(jié)果
/// </summary>
public RedwineRecognitionResult result;
}
/// <summary>
/// 識(shí)別結(jié)果
/// </summary>
[Serializable]
public class RedwineRecognitionResult
{
/// <summary>
/// 判斷是否返回詳細(xì)信息(除紅酒中文名之外的其他字段),含有返回1,不含有返回0
/// </summary>
public int hasdetail;
/// <summary>
/// 紅酒中文名,無(wú)法識(shí)別返回空
/// </summary>
public string wineNameCn;
/// <summary>
/// 紅酒英文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string wineNameEn;
/// <summary>
/// 國(guó)家中文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string countryCn;
/// <summary>
/// 國(guó)家英文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string countryEn;
/// <summary>
/// 產(chǎn)區(qū)中文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string regionCn;
/// <summary>
/// 產(chǎn)區(qū)英文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string regionEn;
/// <summary>
/// 子產(chǎn)區(qū)中文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string subRegionCn;
/// <summary>
/// 子產(chǎn)區(qū)英文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string subRegionEn;
/// <summary>
/// 酒莊中文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string wineryCn;
/// <summary>
/// 酒莊英文名,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string wineryEn;
/// <summary>
/// 酒類(lèi)型,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string classifyByColor;
/// <summary>
/// 糖分類(lèi)型,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string classifyBySugar;
/// <summary>
/// 色澤,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string color;
/// <summary>
/// 葡萄品種,可能有多種葡萄,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string grapeCn;
/// <summary>
/// 葡萄品種英文名,可能有多種葡萄,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string grapeEn;
/// <summary>
/// 品嘗溫度,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string tasteTemperature;
/// <summary>
/// 酒品描述,hasdetail = 0時(shí),表示無(wú)法識(shí)別,該字段不返回
/// </summary>
public string description;
}
下載C# SDK:

下載完成后將AipSdk.dll動(dòng)態(tài)庫(kù)導(dǎo)入到Unity中:

以下是調(diào)用接口時(shí)傳入的參數(shù)詳情:

封裝調(diào)用函數(shù):
using System;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 圖像識(shí)別
/// </summary>
public class ImageRecognition
{
//以下信息于百度開(kāi)發(fā)者中心控制臺(tái)創(chuàng)建應(yīng)用獲取
private const string appID = "";
private const string apiKey = "";
private const string secretKey = "";
/// <summary>
/// 紅酒識(shí)別
/// </summary>
/// <param name="bytes">圖片字節(jié)數(shù)據(jù)</param>
/// <returns></returns>
public static RedwineRecognition Redwine(byte[] bytes)
{
var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey);
try
{
var response = client.Redwine(bytes);
RedwineRecognition redwineRecognition = JsonConvert.DeserializeObject<RedwineRecognition>(response.ToString());
return redwineRecognition;
}
catch (Exception error)
{
Debug.LogError(error);
}
return null;
}
/// <summary>
/// 紅酒識(shí)別
/// </summary>
/// <param name="url">圖片url地址</param>
/// <returns></returns>
public static RedwineRecognition Redwine(string url)
{
var client = new Baidu.Aip.ImageClassify.ImageClassify(apiKey, secretKey);
try
{
var response = client.RedwineUrl(url);
RedwineRecognition redwineRecognition = JsonConvert.DeserializeObject<RedwineRecognition>(response.ToString());
return redwineRecognition;
}
catch (Exception error)
{
Debug.LogError(error);
}
return null;
}
}
測(cè)試圖片:

using System.IO;
using UnityEngine;
public class Example : MonoBehaviour
{
private void Start()
{
ImageRecognition.Redwine(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));
}
}

到此這篇關(guān)于Unity實(shí)現(xiàn)紅酒識(shí)別的示例代碼的文章就介紹到這了,更多相關(guān)Unity紅酒識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SQL DataReader訪問(wèn)數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例
今天小編就為大家分享一篇關(guān)于C#使用SQL DataReader訪問(wèn)數(shù)據(jù)的優(yōu)點(diǎn)和實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
C#中使用NLog庫(kù)進(jìn)行日志記錄的流程詳解
NLog 是 .NET 的日志記錄框架,具有豐富的日志路由和管理能力,極大地幫助您生成和管理日志,NLog 是一個(gè)庫(kù),可以輕松地同時(shí)記錄和管理多個(gè)不同區(qū)域中的數(shù)據(jù),本文將給大家介紹在C#中使用 NLog 庫(kù)進(jìn)行日志記錄的教程,需要的朋友可以參考下2024-06-06
C# List集合中獲取重復(fù)值及集合運(yùn)算詳解
這篇文章主要介紹了C# List集合中獲取重復(fù)值及集合運(yùn)算詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例
這篇文章主要介紹了c#檢測(cè)端口是否被占用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12
利用C#實(shí)現(xiàn)最基本的小說(shuō)爬蟲(chóng)示例代碼
最近在學(xué)習(xí)c#,碰巧遇到個(gè)小說(shuō)站不錯(cuò),就索性當(dāng)個(gè)練習(xí),所以這篇文章主要給大家介紹了關(guān)于利用C#實(shí)現(xiàn)最基本的小說(shuō)爬蟲(chóng)的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

