c# - Microsoft Cognitive Services Vision API does detect any characters from code -
i have block of code calls microsoft cognitive services vision api using ocr capabilities. when pass specific image api call doesn't detect words. call succeeds , returns 200 status. when use same image through demo ui screen provided microsoft works , reads characters expect.
if go url https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/ , upload image
then works , comes 201 19 4501.
when try use below code against same image return doesn't return characters.
here code. scaleimageifneeded method below doesn't because image scaled right size (it returns 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 json result this
{"language":"unk","orientation":"notdetected","regions":[]}
i have done whole set of images similar , can ready 1/2 of images pass in. other half not returning one.
as suggested in answer below created .net framework 4.5.2 console application , tried uploading image using that, got 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 path image text wish read: "); string imagefilepath = @"c:\temp\image.jpg";// console.readline(); // execute rest api call. makeocrrequest(imagefilepath); console.writeline("\nplease wait moment results appear. then, press enter 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 uri rest api call. const string uri = uribase + "?" + requestparameters; // request body. posts locally stored jpeg image. var bytedata = getimageasbytearray(imagefilepath); using (var content = new bytearraycontent(bytedata)) { // example uses content type "application/octet-stream". // other content types can use "application/json" , "multipart/form-data". content.headers.contenttype = new mediatypeheadervalue("application/octet-stream"); // execute rest api call. var response = await client.postasync(uri, content); // json response. string contentstring = await response.content.readasstringasync(); // display 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(); }
this sample worked me image input: https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/csharp#optical-character-recognition-ocr-with-computer-vision-api-using-ca-nameocr-a
section: optical character recognition (ocr) computer vision api using c#.
Comments
Post a Comment