Created
March 19, 2021 20:57
-
-
Save bogdanbujdea/a3a06af46693b890e968fbf810d2b50d to your computer and use it in GitHub Desktop.
Analyze ID with Azure Form Recognizer
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
| /// <summary> | |
| /// Sends an image to Form Recognizer and returns back the URL that contains the result | |
| /// </summary> | |
| /// <returns></returns> | |
| private async Task<string> StartAnalyzingImage() | |
| { | |
| var queryString = HttpUtility.ParseQueryString(string.Empty); | |
| var client = _httpClientFactory.CreateClient(); | |
| // set the key | |
| client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "<Azure key>"); | |
| // set the endpoint | |
| var formRecognizerEndpoint = "https://northeurope.api.cognitive.microsoft.com/"; | |
| queryString["includeTextDetails"] = "true"; | |
| var uri = $"{formRecognizerEndpoint}/formrecognizer/v2.1-preview.3/prebuilt/idDocument/analyze?{queryString}"; | |
| var image = System.IO.File.ReadAllBytes(@"d:\license.jpg"); | |
| using var content = new ByteArrayContent(image); | |
| content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); | |
| var response = await client.PostAsync(uri, content); | |
| if (response.IsSuccessStatusCode) | |
| { | |
| // if the response is successful, it means that Form Recognizer started processing our image | |
| // in order to get the result, we have to make GET requests to an URL | |
| // that URL is provided in a header named "Operation-Location" | |
| // below we're going to retrieve that URL and send it back to the caller of the function | |
| var location = response.Headers.FirstOrDefault(h => h.Key == "Operation-Location"); | |
| return location.Value.FirstOrDefault(); | |
| } | |
| return string.Empty; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment