Created
January 24, 2019 15:29
-
-
Save jpda/9798b5d3f09da1f0d64d6a3472824a2e to your computer and use it in GitHub Desktop.
Azure OCR sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // once you create a computer vision service in Azure, you'll get the region and service key | |
| // CV OCR docs are here: https://eastus2.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fc | |
| var recognizer = new Recognizer("<REGION OF YOUR CV SERVICE>", "<CV SERVICE KEY>"); | |
| // here's an example if the image to OCR is on the internet somewhere already | |
| recognizer.RecognizeFromImageUrl("<URL TO IMAGE, IF AVAILABLE ON INTERNET>", "ar").Wait(); | |
| // here's an example with a local image | |
| var imageBytes = System.IO.File.ReadAllBytes(@"<LOCAL PATH TO IMAGE ON DISK>"); | |
| recognizer.RecognizeFromImage(imageBytes, "ar").Wait(); | |
| Console.WriteLine("all done, any key to exit"); | |
| Console.ReadLine(); | |
| } | |
| } | |
| public class Recognizer | |
| { | |
| private HttpClient _httpClient; | |
| private const string OcrServiceUrlBase = "https://{0}.api.cognitive.microsoft.com/vision/v1.0/ocr?language={1}"; | |
| private readonly string _serviceRegion; | |
| public Recognizer(string serviceRegion, string serviceKey) | |
| { | |
| _httpClient = new HttpClient(); | |
| _serviceRegion = serviceRegion; | |
| _httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", serviceKey); | |
| } | |
| public async Task RecognizeFromImageUrl(string urlToImage, string imageLanguage) | |
| { | |
| var serviceUrl = string.Format(OcrServiceUrlBase, _serviceRegion, imageLanguage); // e.g., https://eastus2.api.cognitive.microsoft.com/vision/v1.0/ocr?language=ar | |
| Console.WriteLine($"OCR URL: {serviceUrl}"); | |
| var postData = $"{{'url': '{urlToImage}'}}"; | |
| var postBody = new StringContent(postData, System.Text.Encoding.UTF8, "application/json"); | |
| Console.WriteLine($"Post body: {postData}"); | |
| try | |
| { | |
| var serviceResponse = await _httpClient.PostAsync(serviceUrl, postBody); | |
| if (!serviceResponse.IsSuccessStatusCode) | |
| { | |
| Console.WriteLine(serviceResponse.StatusCode); | |
| return; | |
| } | |
| var responseData = await serviceResponse.Content.ReadAsStringAsync(); | |
| Console.WriteLine(responseData); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine(ex.Message); | |
| } | |
| } | |
| public async Task RecognizeFromImage(byte[] image, string imageLanguage) | |
| { | |
| var serviceUrl = string.Format(OcrServiceUrlBase, _serviceRegion, imageLanguage); // e.g., https://eastus2.api.cognitive.microsoft.com/vision/v1.0/ocr?language=ar | |
| Console.WriteLine($"OCR URL: {serviceUrl}"); | |
| var postBody = new ByteArrayContent(image); | |
| postBody.Headers.Remove("Content-Type"); | |
| postBody.Headers.Add("Content-Type", "application/octet-stream"); | |
| try | |
| { | |
| var serviceResponse = await _httpClient.PostAsync(serviceUrl, postBody); | |
| if (!serviceResponse.IsSuccessStatusCode) | |
| { | |
| Console.WriteLine(serviceResponse.StatusCode); | |
| return; | |
| } | |
| var responseData = await serviceResponse.Content.ReadAsStringAsync(); | |
| Console.WriteLine(responseData); | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine(ex.Message); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment