Unity 接入百度Ocr图片转文字

1 篇文章 0 订阅

.## 标题 Unity接入百度Ocr图片转文字
代码如下 用来解析返回成功的数据

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;
}```
## 调用Api传入参数
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Baidu.Aip;
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);
            var response = client.GeneralBasic(bytes, options);
            GeneralOcr generalOcr = JsonUtility.FromJson<GeneralOcr>(response.ToString());
            return generalOcr;
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }


}
###调用方法将要识别的图片转为byte

 public void LoadImages()
    {
        string imagePath = EditorUtility.OpenFilePanel("Select Image", "", "png,jpg,jpeg");

        if (!string.IsNullOrEmpty(imagePath))
        {
            Texture2D texture = new Texture2D(2, 2);
            byte[] imageData = File.ReadAllBytes(imagePath);
            texture.LoadImage(imageData);
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
            image.sprite = sprite;

            OCR.General(imageData);
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Unity接入百度AI接口,你需要进行以下步骤: 1. 注册百度AI开放平台账号并创建应用,获取AppID、API Key和Secret Key。 2. 下载百度AI SDK for Unity,并将其导入到Unity项目中。 3. 在Unity项目中创建一个脚本,并在其中编写调用百度AI接口的代码。例如,你可以使用语音识别接口,将用户的语音换成文本。代码示例如下: ``` using Baidu.Aip.Speech; using UnityEngine; public class SpeechRecognition : MonoBehaviour { private const string APP_ID = "你的AppID"; private const string API_KEY = "你的API Key"; private const string SECRET_KEY = "你的Secret Key"; private readonly AudioClip _microphoneClip = Microphone.Start(null, true, 10, 16000); private SpeechRecognizer _speechRecognizer; private void Start() { _speechRecognizer = new SpeechRecognizer(API_KEY, SECRET_KEY); _speechRecognizer.Timeout = 60000; } private void Update() { // 等待录音结束 if (Microphone.IsRecording(null) && Microphone.GetPosition(null) > 0) { return; } // 停止录音 Microphone.End(null); // 调用语音识别接口 var result = _speechRecognizer.Recognize(_microphoneClip.GetData(), "pcm", 16000); if (result != null && result.ErrorCode == 0) { Debug.Log(result.Result[0]); } } } ``` 4. 在Unity中添加麦克风权限,以允许应用访问麦克风。 5. 对于其他的百度AI接口,你可以参考百度AI SDK for Unity中的示例代码,并根据具体需求进行修改。 以上就是在Unity接入百度AI接口的基本步骤。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值