The Ink Recognizer is one of the new Cognitive Services announced and as mentioned in this post, Azure Ink Recognizer is an AI service in the Vision Category that recognizes digital ink content, such as handwriting, shapes, and ink document layout. In this three-part series, we are going to see how to use the Azure Ink Recognizer to transform handwriting to text. In the first part, we learned about Ink stroke data and how to generate them. In the second part, we learned how to create the Azure Recognizer service. In this tutorial, we are going to learn how to use the rest API calls to get our results.
static string subscriptionKey = "{REPLACE WITH YOUR KEY}";
static string endpoint = "{REPLACE WITH YOUR ENDPOINT}";
static string inkRecognitionUrl = "/inkrecognizer/v1.0-preview/recognize";
static readonly string dataPath = @"InkData/sample_Ink.json";
Replace the subscription key and endpoint with the values you collected in the previous tutorial when you created the Ink Recognizer service. Then create a folder and add the .json file you exported from the tutorial part 1 or one that you downloaded from GitHub. Add the file path in the dataPath variable.
Then replace the rest of the file with the following code under Program and run it.
static async Task<string> Request(string apiAddress, string endpoint, string subscriptionKey, string requestData)
{
using (HttpClient client = new HttpClient { BaseAddress = new Uri(apiAddress) })
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
var res = await client.PutAsync(endpoint, content);
if (res.IsSuccessStatusCode)
{
return await res.Content.ReadAsStringAsync();
}
else
{
return $"ErrorCode: {res.StatusCode}";
}
}
}
static void recognizeInk(string requestData)
{
var result = Request(
endpoint,
inkRecognitionUrl,
subscriptionKey,
requestData).Result;
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
}
public static JObject LoadJson(string fileLocation)
{
var jsonObj = new JObject();
using (StreamReader file = File.OpenText(fileLocation))
using (JsonTextReader reader = new JsonTextReader(file))
{
jsonObj = (JObject)JToken.ReadFrom(reader);
}
return jsonObj;
}
static void Main(string[] args)
{
var requestData = LoadJson(dataPath);
string requestString = requestData.ToString(Newtonsoft.Json.Formatting.None);
recognizeInk(requestString);
System.Console.WriteLine("\nPress any key to exit ");
System.Console.ReadKey();
}
System.Console.WriteLine("\nPress any key to exit ");
System.Console.ReadKey();
}
static async Task<string> Request(string apiAddress, string endpoint, string subscriptionKey, string requestData)
{
using (HttpClient client = new HttpClient { BaseAddress = new Uri(apiAddress) })
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
var res = await client.PutAsync(endpoint, content);
if (res.IsSuccessStatusCode)
{
return await res.Content.ReadAsStringAsync();
}
else
{
return $"ErrorCode: {res.StatusCode}";
}
}
}
static void recognizeInk(string requestData)
{
var result = Request(
endpoint,
inkRecognitionUrl,
subscriptionKey,
requestData).Result;
dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
System.Console.WriteLine(jsonObj);
}
public static JObject LoadJson(string fileLocation)
{
var jsonObj = new JObject();
using (StreamReader file = File.OpenText(fileLocation))
using (JsonTextReader reader = new JsonTextReader(file))
{
jsonObj = (JObject)JToken.ReadFrom(reader);
}
return jsonObj;
}
static void Main(string[] args)
{
var requestData = LoadJson(dataPath);
string requestString = requestData.ToString(Newtonsoft.Json.Formatting.None);
recognizeInk(requestString);
System.Console.WriteLine("\nPress any key to exit ");
System.Console.ReadKey();
}
The result will be similar to the following. As you can see although it recognized and compared several versions of Hello from o or zero for the final letter, the final recommendation is the word Hello which is correct!
You can find the complete code for this part of the series in the following Github repository.
Georgia Kalyva
Georgia Kalyva is a Microsoft AI MVP with years of experience in software engineering and is currently working for ITT as Web Applications Developer. She is passionate about AI and Azure and has represented Greece in global competitions in Technology and Entrepreneurship. She is also a member of the Microsoft Student Partners team and has taken over the role of mentor in several Microsoft competitions and trainings.