我通过传递我从手机上拍摄的图像来调用microsoft recognize api,没有发生错误,但每次api都会返回空字符串,因为结果与我发布的图像无关。我用microsoft ocr api尝试了这些图像,但它返回了结果,有人能帮助我吗?
发布于 2019-01-15 09:12:30
我通过传递我从手机上拍摄的图像来调用微软recognize text api,没有发生错误,但每次api都会返回空字符串,因为结果与我发布的图像无关。
在documentation of Recognize Text API中,我们可以找到:
服务已接受请求,稍后将开始处理。
它将立即返回Accepted,并包含一个“Operation-Location”标头。客户端应使用该头部中指定的URL进一步查询操作状态。
我怀疑您是在发出识别文本的请求后直接从响应中获取/提取内容的,所以内容应该是string.Empty。
要进行get recognize text operation result,您需要使用响应头"Operation-Location
“中指定的URL进行进一步请求。
在下面的测试代码中,我们可以发现内容确实是空的。
测试代码:
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{Subscription_Key_here}");
queryString["mode"] = "Printed";
var uri = "https://{region}.api.cognitive.microsoft.com/vision/v2.0/recognizeText?" + queryString;
HttpResponseMessage response;
var imagePath = @"D:\xxx\xxx\xxx\testcontent.PNG";
Stream imageStream = File.OpenRead(imagePath);
BinaryReader binaryReader = new BinaryReader(imageStream);
byte[] byteData = binaryReader.ReadBytes((int)imageStream.Length);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response = await client.PostAsync(uri, content);
if (response.IsSuccessStatusCode)
{
var contentString = await response.Content.ReadAsStringAsync();
var operation_location = response.Headers.GetValues("Operation-Location").FirstOrDefault();
Console.WriteLine($"Response content is empty({contentString == string.Empty}).\n\rYou should further query the operation status using the URL ({operation_location}) specified in response header.");
}
}
测试结果:
注意: Recognize API当前处于预览阶段,仅适用于英文文本。正如您所提到的,计算机视觉中的OCR技术还可以帮助检测图像中的文本内容,并且OCR目前支持25种语言,有时我们可以使用OCR作为替代解决方案。
https://stackoverflow.com/questions/53516121
复制相似问题