Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this quickstart, you will extract handwritten or printed text from an image using the Computer Vision SDK for C#. If you wish, you can download the code in this guide as a complete sample app from the Cognitive Services Csharp Vision repo on GitHub.
- A Computer Vision subscription key. You can follow the instructions in Create a Cognitive Services account to subscribe to Computer Vision and get your key.
- Any edition of Visual Studio 2015 or 2017.
- The Microsoft.Azure.CognitiveServices.Vision.ComputerVision client library NuGet package. It isn't necessary to download the package. Installation instructions are provided below.
To run the sample, do the following steps:
Create a new Visual C# Console App in Visual Studio.
Install the Computer Vision client library NuGet package.
- On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for Solution.
- Click the Browse tab, and in the Search box type "Microsoft.Azure.CognitiveServices.Vision.ComputerVision".
- Select Microsoft.Azure.CognitiveServices.Vision.ComputerVision when it displays, then click the checkbox next to your project name, and Install.
Replace
Program.cs
with the following code. TheBatchReadFileAsync
andBatchReadFileInStreamAsync
methods wrap the Batch Read API for remote and local images, respectively. TheGetReadOperationResultAsync
method wraps the Get Read Operation Result API.using Microsoft.Azure.CognitiveServices.Vision.ComputerVision; using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models; using System; using System.IO; using System.Threading.Tasks; namespace ExtractText { class Program { // subscriptionKey = "0123456789abcdef0123456789ABCDEF" private const string subscriptionKey = "<Subscription key>"; // localImagePath = @"C:\Documents\LocalImage.jpg" private const string localImagePath = @"<LocalImage>"; private const string remoteImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Cursive_Writing_on_Notebook_paper.jpg/800px-Cursive_Writing_on_Notebook_paper.jpg"; private const int numberOfCharsInOperationId = 36; static void Main(string[] args) { ComputerVisionClient computerVision = new ComputerVisionClient( new ApiKeyServiceClientCredentials(subscriptionKey), new System.Net.Http.DelegatingHandler[] { }); computerVision.Endpoint = "https://api.cognitive.azure.cn"; Console.WriteLine("Images being analyzed ..."); var t1 = ExtractRemoteTextAsync(computerVision, remoteImageUrl); var t2 = ExtractLocalTextAsync(computerVision, localImagePath); Task.WhenAll(t1, t2).Wait(5000); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); } // Read text from a remote image private static async Task ExtractRemoteTextAsync( ComputerVisionClient computerVision, string imageUrl) { if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute)) { Console.WriteLine( "\nInvalid remoteImageUrl:\n{0} \n", imageUrl); return; } // Start the async process to read the text BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync( imageUrl); await GetTextAsync(computerVision, textHeaders.OperationLocation); } // Recognize text from a local image private static async Task ExtractLocalTextAsync( ComputerVisionClient computerVision, string imagePath) { if (!File.Exists(imagePath)) { Console.WriteLine( "\nUnable to open or read localImagePath:\n{0} \n", imagePath); return; } using (Stream imageStream = File.OpenRead(imagePath)) { // Start the async process to recognize the text BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync( imageStream); await GetTextAsync(computerVision, textHeaders.OperationLocation); } } // Retrieve the recognized text private static async Task GetTextAsync( ComputerVisionClient computerVision, string operationLocation) { // Retrieve the URI where the recognized text will be // stored from the Operation-Location header string operationId = operationLocation.Substring( operationLocation.Length - numberOfCharsInOperationId); Console.WriteLine("\nCalling GetHandwritingRecognitionOperationResultAsync()"); ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId); // Wait for the operation to complete int i = 0; int maxRetries = 10; while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries) { Console.WriteLine( "Server status: {0}, waiting {1} seconds...", result.Status, i); await Task.Delay(1000); result = await computerVision.GetReadOperationResultAsync(operationId); } // Display the results Console.WriteLine(); var recResults = result.RecognitionResults; foreach (TextRecognitionResult recResult in recResults) { foreach (Line line in recResult.Lines) { Console.WriteLine(line.Text); } } Console.WriteLine(); } } }
Replace
<Subscription Key>
with your valid subscription key.Change
computerVision.Endpoint
to the Azure region associated with your subscription keys, if necessary.Replace
<LocalImage>
with the path and file name of a local image.Optionally, set
remoteImageUrl
to a different image.Run the program.
A successful response prints the lines of recognized text for each image.
Calling GetHandwritingRecognitionOperationResultAsync()
Calling GetHandwritingRecognitionOperationResultAsync()
Server status: Running, waiting 1 seconds...
Server status: Running, waiting 1 seconds...
dog
The quick brown fox jumps over the lazy
Pack my box with five dozen liquor jugs
Explore the Computer Vision APIs used to analyze an image, detect celebrities and landmarks, create a thumbnail, and extract printed and handwritten text.