我有一段代碼使用OCR功能調用Microsoft Cognitive Services Vision API。當我將特定圖像傳遞給API調用時,它不會檢測到任何單詞。通話本身成功並返回200狀態。當我通過Microsoft提供的演示UI屏幕使用相同的圖像時,它可以工作並讀取我期望的字符。Microsoft認知服務Vision API檢測代碼中的任何字符
如果我去這個網址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":[]}
我已經做了一整套類似於此的圖像和我可以成功地準備有關我通過在圖像的1/2。另一半沒有像這一樣返回任何東西。
正如以下答案中所建議的,我創建了一個.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();
}
正如另一個數據點:相同的圖像正常工作與自由[OCR.space在線OCR(https://ocr.space),所以就像你說的,Azure的OCR應該能夠檢測它也是如此。通過圖片獲取API調用: [https://api.ocr.space/parse/imageurl?apikey=helloworld&url=https://i.stack.imgur.com/g9riu.jpg](https://api。 ocr.space/parse/imageurl?apikey=helloworld&url=https://i.stack.imgur.com/g9riu.jpg)(<=使用public api key) – Lora129