cloudmersive OCR识别聊天记录图片转文字

开始用的腾讯云图片转文字接口,识别效果不是太好,而且客户指定用cloudmersive转文字

cloudmersive是国外的网站,经测试识别率还是挺高的

接口地址:https://cloudmersive.com/ocr-api

接口有点难找,试了很久api控制台,总算找到可用的接口-----Optical Character Recognition (OCR) API  光学字符识别(OCR)API的/ ocr / photo / to / words-with-location

以下是SDK提供的接入代码

安装包Cloudmersive.APIClient.NETCore.OCR-版本2.0.2
using Cloudmersive.APIClient.NETCore.OCR.Api; 
using Cloudmersive.APIClient.NETCore.OCR.Client; 
using Cloudmersive.APIClient.NETCore.OCR.Model;

            Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");//秘钥
            var apiInstance = new ImageOcrApi();
            var imageFile = new System.IO.Stream();//传入的文件流
            var recognitionMode = recognitionMode_example; //识别模式
串  一般为Normal
            var language = language_example;//语言  ZHO(中文-简体)
            var preprocessing = preprocessing_example; //不填  默认“自动”
            var diagnostics = diagnostics_example; //诊断模式  true

            try
            {
                // Convert a photo of a document or receipt into words with location
                PhotoToWordsWithLocationResult result = apiInstance.ImageOcrPhotoWordsWithLocation(imageFile, recognitionMode, language, preprocessing, diagnostics);
                Debug.WriteLine(result);
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling ImageOcrApi.ImageOcrPhotoWordsWithLocation: " + e.Message );
            }

跑通了后,发现放道服务器上会遇到---------调用接口提示无法连接SSL,System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效

也就是下一篇文章提到的问题

试了很多方法跳过远程方法都不行

后来改成了不用SDK,自己写方法用url传参,再加上忽略远程证书,默认设置为true的代码,就跑通了

        public Object getresponse(byte[] imgbyte)
        {
            var result = string.Empty;
            //跳过远程证书验证
            HttpClientHandler httpClientHandler = new HttpClientHandler();
            httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;

               using (HttpClient client = new HttpClient(httpClientHandler))
                {
                    MultipartFormDataContent content = new MultipartFormDataContent();
                    client.DefaultRequestHeaders.Add("recognitionMode", "Normal");
                    client.DefaultRequestHeaders.Add("language", "ZHO");
                    client.DefaultRequestHeaders.Add("preprocessing", "");
                    client.DefaultRequestHeaders.Add("diagnostics", "true");
                    client.DefaultRequestHeaders.Add("Apikey", "b***********************************");
                    //添加文件参数
                    content.Add(new ByteArrayContent(imgbyte), "file", "聊天图片");
                    Logs.WriteLog(LogType.Info, "----content: -----" + content);
                    var requestUri = "https://api.cloudmersive.com/ocr/photo/to/words-with-location";
                    Logs.WriteLog(LogType.Info, "----requestUri: -----" + requestUri);
                    result = client.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result;
                    Logs.WriteLog(LogType.Info, "----结果: -----" + result);
                    return result;

                }

            }

识别出来是这种,不是识别出来一条一条的完整数据,只是识别出来两三个字符,需要自己去根据X和Y坐标进行拼接,解析成可用的结果,怎么组装内容这就是涉及到算法了.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值