我有一段使用OCR功能调用Microsoft Cognitive Services Vision API的代码。当我将特定图像传递给API调用时,它不会检测到任何单词。呼叫本身成功并返回200状态。当我通过Microsoft提供的演示UI屏幕使用相同的图像时,它可以工作并读取我期望的字符。
如果我转到此网址https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/并上传此图片
然后它起作用并返回201 19 4501。
当我尝试对同一图像使用以下代码时,它返回并不返回任何字符。
这是代码。下面的ScaleImageIfNeeded方法没有做任何事情,因为图像已经缩放到正确的大小(它只返回在字节数组中传递的相同)。
public async Task<string> ProcessImage(byte[] imageData)
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", ConnectionString);
const string requestParameters = "language=unk&detectOrientation=true";
const string uri = "https://eastus2.api.cognitive.microsoft.com/vision/v1.0/ocr?" + requestParameters;
var byteData = ScaleImageIfNeeded(imageData);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
var response = await client.PostAsync(uri, content);
var results = await response.Content.ReadAsStringAsync();
return results;
}
}
JSON结果是这个
{"language":"unk","orientation":"NotDetected","regions":[]}
我完成了一整套与此相似的图像,我可以成功准备好大约一半的图像。另一半不会像这样返回任何内容。
正如下面的答案所示,我创建了一个.NET Framework 4.5.2控制台应用程序并尝试使用它上传图像,但结果相同。
internal class Program
{
private const string SubscriptionKey = "XXXXXXXXXX";
//private const string UriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr";
private const string UriBase = "https://eastus2.api.cognitive.microsoft.com/vision/v1.0/ocr";
private static void Main()
{
Console.WriteLine("Optical Character Recognition:");
Console.Write("Enter the path to an image with text you wish to read: ");
string imageFilePath = @"c:\temp\image.jpg";// Console.ReadLine();
// Execute the REST API call.
MakeOcrRequest(imageFilePath);
Console.WriteLine("\nPlease wait a moment for the results to appear. Then, press Enter to exit...\n");
Console.ReadLine();
}
private static async void MakeOcrRequest(string imageFilePath)
{
var client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
// Request parameters.
const string requestParameters = "language=unk&detectOrientation=true";
// Assemble the URI for the REST API Call.
const string uri = UriBase + "?" + requestParameters;
// Request body. Posts a locally stored JPEG image.
var byteData = GetImageAsByteArray(imageFilePath);
using (var content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
var response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettyPrint(contentString));
}
}
private static byte[] GetImageAsByteArray(string imageFilePath)
{
var fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
var binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
private static string JsonPrettyPrint(string json)
{
if (string.IsNullOrEmpty(json))
return string.Empty;
json = json.Replace(Environment.NewLine, "").Replace("\t", "");
var sb = new StringBuilder();
var quote = false;
var ignore = false;
var offset = 0;
const int indentLength = 3;
foreach (var ch in json)
{
switch (ch)
{
case '"':
if (!ignore) quote = !quote;
break;
case '\'':
if (quote) ignore = !ignore;
break;
}
if (quote)
sb.Append(ch);
else
{
switch (ch)
{
case '{':
case '[':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', ++offset * indentLength));
break;
case '}':
case ']':
sb.Append(Environment.NewLine);
sb.Append(new string(' ', --offset * indentLength));
sb.Append(ch);
break;
case ',':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', offset * indentLength));
break;
case ':':
sb.Append(ch);
sb.Append(' ');
break;
default:
if (ch != ' ') sb.Append(ch);
break;
}
}
}
return sb.ToString().Trim();
}
答案 0 :(得分:0)