前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Unity 接入百度AI - OCR文字识别

Unity 接入百度AI - OCR文字识别

作者头像
CoderZ
发布2022-08-29 16:25:31
4440
发布2022-08-29 16:25:31
举报

首先登陆百度开发者中心,搜索文字识别服务:

创建一个应用,获取AppID、APIKey、SecretKey秘钥信息:

下载C# SDK,将AipSdk.dll动态库导入Unity:

本文以通用文字识别为例,查阅官方文档,以下是通用文字识别的返回数据结构:

在Unity中定义相应的数据结构:

代码语言:javascript
复制
using System;

/// <summary>
/// 通用文字识别
/// </summary>
[Serializable]
public class GeneralOcr
{
    /// <summary>
    /// 图像方向 -1未定义 0正弦 1逆时针90度 2逆时针180度 3逆时针270度
    /// </summary>
    public int direction;
    /// <summary>
    /// 唯一的log id,用于问题定位
    /// </summary>
    public int log_id;
    /// <summary>
    /// 识别结果数,表示words_result的元素个数
    /// </summary>
    public int words_result_num;
    /// <summary>
    /// 定位和识别结果数组
    /// </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;
}

下面是调用时传入的相关参数:

封装调用函数:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using UnityEngine;

public class OCR 
{
    //以下信息于百度开发者中心创建应用获取
    private const string appID = "";
    private const string apiKey = "";
    private const string secretKey = "";

    /// <summary>
    /// 通用文字识别
    /// </summary>
    /// <param name="bytes">图片字节数据</param>
    /// <param name="language">识别语言类型 默认CHN_ENG中英文混合</param>
    /// <param name="detectDirection">是否检测图像朝向</param>
    /// <param name="detectLanguage">是否检测语言,当前支持中、英、日、韩</param>
    /// <param name="probability">是否返回识别结果中每一行的置信度</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;
    }
}

以上是传入图片字节数据调用接口的方式,也可以通过URL调用,只需将GeneralBasic换为重载函数GeneralBasicUrl:

测试图片:

代码语言:javascript
复制
OCR.General(File.ReadAllBytes(Application.dataPath + "/Picture.jpg"));
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
通用文字识别
通用文字识别(General Optical Character Recognition,General OCR)基于行业前沿的深度学习技术,提供通用印刷体识别、通用印刷体识别(高精度版)、通用手写体识别、英文识别等多种服务,支持将图片上的文字内容,智能识别为可编辑的文本,可应用于随手拍扫描、纸质文档电子化、电商广告审核等多种场景,大幅提升信息处理效率。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档