This implementation is based on Microsoft Azure Computer Vision API. The service is paid, but you can create a free subscription. Once you’ve done with subscription, you will have to create Computer Vision resource using the free pricing tier (F0) to try the service, and upgrade later to a paid tier for production. As a result, you will get Computer Vision Endpoint and Subscription Key (let’s suppose they are stored in the environment variables COMPUTER_VISION_ENDPOINT and COMPUTER_VISION_SUBSCRIPTION_KEY respectively).
C#
usingNewtonsoft.Json.Linq;publicclassComputerVisionOcrConnector:IOcrConnector{privateconststringOcrUri="vision/v2.1/ocr";privatereadonlystringSubscriptionKey;privatereadonlystringEndpoint;publicComputerVisionOcrConnector(){Endpoint=Environment.GetEnvironmentVariable("COMPUTER_VISION_ENDPOINT");SubscriptionKey=Environment.GetEnvironmentVariable("COMPUTER_VISION_SUBSCRIPTION_KEY");}publicstringGetServiceUri(booldetectOrientation,stringlanguage){// Request parameters. // The language parameter doesn't specify a language, so the // method detects it automatically.// The detectOrientation parameter is set to true, so the method detects and// and corrects text orientation before detecting text.// Assemble the URI for the REST API method.returnstring.Format("{0}{1}?language={2}&detectOrientation={3}",Endpoint,OcrUri,string.IsNullOrEmpty(language)?"unk":language,detectOrientation).ToLower();}publicRecognizedImageRecognize(StreamimageStream){try{using(WebClientclient=newWebClient()){client.Headers[HttpRequestHeader.Accept]="application/json";// authenticationclient.Headers.Add("Ocp-Apim-Subscription-Key",SubscriptionKey);// payloadBinaryReaderbinaryReader=newBinaryReader(imageStream);vardata=binaryReader.ReadBytes((int)imageStream.Length);client.Headers[HttpRequestHeader.ContentType]="application/octet-stream";stringstringResponse=string.Empty;try{byte[]result=client.UploadData(GetServiceUri(true,null),data);stringResponse=Encoding.UTF8.GetString(result);}catch(Exceptionex){// MS Azure Cognintive services reports 400 Bad requests and other exceptions on small pictures and pictures with no textConsole.WriteLine("Microsoft Azure Cognitive Services consider this image as wrong ({0})",ex.ToString());}if(!string.IsNullOrEmpty(stringResponse)){returnCreateDtoFromResponse(JToken.Parse(stringResponse));}}}catch(Exceptionex){Console.WriteLine("Microsoft Azure Cognitive Services Text Recognition failed: {0}",ex.ToString());}returnnewRecognizedImage(newList<TextLine>());}protectedvirtualRecognizedImageCreateDtoFromResponse(JTokenjToken){// Parse json response to extract lines and words with the bounding rectangles.}...}
The service returns a JSON-serialized with text regions and recognized words, each word with its bounding rectangle.
Along with full featured .NET library we provide simple, but powerful free Apps.
You are welcome to perform redactions for various document formats like PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Redaction App.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.