最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)OCR文字識(shí)別功能

 更新時(shí)間:2022年01月04日 10:28:54   作者:CoderZ1010  
這篇文章主要介紹了通過(guò)Unity接入百度AI接口,實(shí)現(xiàn)OCR文字識(shí)別功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,需要的可以了解一下

首先登陸百度開(kāi)發(fā)者中心,搜索文字識(shí)別服務(wù):

創(chuàng)建一個(gè)應(yīng)用,獲取AppID、APIKey、SecretKey秘鑰信息:

下載C# SDK,將AipSdk.dll動(dòng)態(tài)庫(kù)導(dǎo)入U(xiǎn)nity:

本文以通用文字識(shí)別為例,查閱官方文檔,以下是通用文字識(shí)別的返回?cái)?shù)據(jù)結(jié)構(gòu):

在Unity中定義相應(yīng)的數(shù)據(jù)結(jié)構(gòu):

using System;
 
/// <summary>
/// 通用文字識(shí)別
/// </summary>
[Serializable]
public class GeneralOcr
{
    /// <summary>
    /// 圖像方向 -1未定義 0正弦 1逆時(shí)針90度 2逆時(shí)針180度 3逆時(shí)針270度
    /// </summary>
    public int direction;
    /// <summary>
    /// 唯一的log id,用于問(wèn)題定位
    /// </summary>
    public int log_id;
    /// <summary>
    /// 識(shí)別結(jié)果數(shù),表示words_result的元素個(gè)數(shù)
    /// </summary>
    public int words_result_num;
    /// <summary>
    /// 定位和識(shí)別結(jié)果數(shù)組
    /// </summary>
    public string[] words_result;
    /// <summary>
    /// 行置信度信息
    /// </summary>
    public Probability probability;
}
 
/// <summary>
/// 行置信度信息
/// </summary>
[Serializable]
public class Probability
{
    /// <summary>
    /// 行置信度平均值
    /// </summary>
    public int average;
    /// <summary>
    /// 行置信度方差
    /// </summary>
    public int variance;
    /// <summary>
    /// 行置信度最小值
    /// </summary>
    public int min;
}

下面是調(diào)用時(shí)傳入的相關(guān)參數(shù):

封裝調(diào)用函數(shù):

using System;
using System.Collections.Generic;
using UnityEngine;
 
public class OCR 
{
    //以下信息于百度開(kāi)發(fā)者中心創(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>
    /// <param name="language">識(shí)別語(yǔ)言類型 默認(rèn)CHN_ENG中英文混合</param>
    /// <param name="detectDirection">是否檢測(cè)圖像朝向</param>
    /// <param name="detectLanguage">是否檢測(cè)語(yǔ)言,當(dāng)前支持中、英、日、韓</param>
    /// <param name="probability">是否返回識(shí)別結(jié)果中每一行的置信度</param>
    /// <returns></returns>
    public static GeneralOcr General(byte[] bytes, string language = "CHN_ENG", bool detectDirection = false, bool detectLanguage = false, bool probability = false)
    {
        var client = new Baidu.Aip.Ocr.Ocr(apiKey, secretKey);
        try
        {
            var options = new Dictionary<string, object>
            {
                { "language_type", language },
                { "detect_direction", detectDirection },
                { "detect_language", detectLanguage },
                { "probability", probability }
            };
            var response = client.GeneralBasic(bytes, options);
            GeneralOcr generalOcr = JsonUtility.FromJson<GeneralOcr>(response.ToString());
            return generalOcr;
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }
}    

以上是傳入圖片字節(jié)數(shù)據(jù)調(diào)用接口的方式,也可以通過(guò)URL調(diào)用,只需將GeneralBasic換為重載函數(shù)GeneralBasicUrl:

測(cè)試圖片:

OCR.General(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));

以上就是Unity實(shí)現(xiàn)OCR文字識(shí)別功能的詳細(xì)內(nèi)容,更多關(guān)于Unity OCR文字識(shí)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

高雄市| 克东县| 彝良县| 泽库县| 康定县| 徐闻县| 剑川县| 德安县| 承德市| 东乡族自治县| 桦川县| 五大连池市| 玉山县| 七台河市| 嘉义县| 米泉市| 修文县| 体育| 邻水| 晋州市| 拜泉县| 县级市| 仲巴县| 伊春市| 深水埗区| 上犹县| 丰县| 岱山县| 伊通| 武汉市| 五大连池市| 蕉岭县| 海淀区| 远安县| 荥阳市| 美姑县| 阿瓦提县| 昌江| 长宁区| 凤台县| 涿鹿县|