I have a block of code that calls the Microsoft Cognitive Services Vision API using the OCR capabilities. When I pass a specific image into the API call it doesn't detect any words. The call itself succeeds and returns a 200 status. When I use that same image through the demo UI screen provided by Microsoft it works and reads the characters that I expect.
If I go to this URL https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/ and upload this image

then it works and comes back with 201 19 4501.
When I try to use the below code against the same image it return doesn't return any characters.
Here is the code. The ScaleImageIfNeeded method below doesn't do anything because the image is already scaled to the right size (it just returns the same passed in byte array).
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;
}
}
The the JSON result is this
{"language":"unk","orientation":"NotDetected","regions":[]}
I have done a whole set of images that are similar to this and I can successfully ready about 1/2 of the images that I pass in. The other half are not returning anything just like this one.
As suggested in the answer below I created a .NET Framework 4.5.2 Console application and tried uploading the image using that, but got the same results.
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();
}