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

这篇博客介绍了如何在Unity中集成百度的文字识别服务。首先在百度开发者中心创建应用并获取API密钥,然后下载C# SDK并将动态库导入Unity。接着,定义了与通用文字识别返回数据结构匹配的C#类,并封装了调用接口的函数。通过传入图片字节数据,调用OCR服务进行文字识别。测试过程中,调用示例展示了如何读取本地图片并调用接口进行识别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

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

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;
}

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

封装调用函数:

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:

测试图片:

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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CoderZ1010

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值