开始用的腾讯云图片转文字接口,识别效果不是太好,而且客户指定用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坐标进行拼接,解析成可用的结果,怎么组装内容这就是涉及到算法了.