Azure Ink Recognizer client library for Python

Azure Ink Recognizer SDK is an SDK for developers to work with Azure Ink Recognizer Service. The service recognize a collection of ink strokes and return a tree hierarchy of the recognized units, such as lines, words, shapes, as well as the handwriting recognition result of the words.

Features:

  • Connect to Azure Ink Recognizer Service

  • Convert collections of ink strokes into HTTP requests

  • Parse HTTP response into ink recognition units

Source code | Package (PyPi) | API reference documentation | Product documentation | Samples

Getting started

Install the package

Install the Azure Cosmos DB client library for Python with pip:

pip install azure-cognitiveservices-inkrecognizer

Prerequisites: You must have an Azure subscription. You must have Python 2.7 or Python 3.5.3+ to use this package. Asynchronous features supports Python 3.5.3+ only.

Get URL

Please find the URL at Ink Recognizer Rest API documentation

Get credentials

Please follow the instructions on Ink Recognizer.

Key concepts

Implement ink stroke

If you don’t have any pre-defined ink point or ink stroke classes, you can either follow the Ink Stroke Interfaces to build your stroke, or build your own class that has all required fields. If you already defined ink strokes yourself, you should feed attributes in your class according to the interfaces.

from azure.cognitiveservices.inkrecognizer import InkStrokeKind

InkPoint = namedtuple("InkPoint", "x y")

class InkStroke():
    def __init__(self,
                 ink_stroke_id,
                 ink_points,
                 stroke_kind=InkStrokeKind.UNKNOWN,
                 stroke_language=""):
        self.id = ink_stroke_id
        self.points = ink_points
        self.kind = stroke_kind
        self.language = stroke_language

You can then create a list (or any iterable object) of ink strokes for recognition.

Create a client

Once you got the url for ink recognizer service and an Azure credential instance, you can create an InkRecognizerClient

from azure.cognitiveservices.inkrecognizer import InkRecognizerClient
client = InkRecognizerClient(url, api_key)  # api_key is your key as string

Or use Async version (Python 3.5.3+ only)

from azure.cognitiveservices.inkrecognizer.aio import InkRecognizerClient
client = InkRecognizerClient(url, api_key)  # api_key is your key as string

Send a request

You can then send stroke list to Ink Recognizer Service and get the root of all ink recognition results.

# Sync version
recognition_root = client.recognize_ink(ink_stroke_list)
# Async version
recognition_root = await client.recognize_ink(ink_stroke_list)

Get recognition units from results

You can get all the recognition units either by InkRecognitionUnitKind or by hierarchy, then visit support properties of the units. API reference documentation

lines = recognition_root.lines
for line in lines:
    foo_show_bounding_box(line.bounding_box)
    for word in line.words:
        print(word.recognized_text)

Examples

The Samples provide several code snippets covering some of the most common Ink Recognizer SDK tasks, including:

  • Implement InkPoint and InkStroke classes

  • Convert stroke unit from pixel to mm

  • Set language recognition locale

  • Indexing a key word from recognition results

  • Set application kind if user know expected type of ink content

Troubleshooting

General

Ink Recognizer clients raise exceptions defined in azure-core. For example, if you try to reach an invalid URL, InkRecognizerClient raises ResourceNotFoundError:

from azure.core.exceptions import ResourceNotFoundError
client = InkRecognizerClient("invalid_url", credential)

try:
    client.recognize_ink(ink_strokes)
except ResourceNotFoundError as e:
    print(e.message)

Logging

Network trace logging is disabled by default for this library. When enabled, HTTP requests will be logged at DEBUG level using the logging library. You can configure logging to print debugging information to stdout or write it to a file:

import sys
import logging

# Create a logger for the 'azure' SDK
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Configure a console output
handler = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(handler)

# Configure a file output
file_handler = logging.FileHandler(filename)
logger.addHandler(file_handler)

# Enable network trace logging. Each HTTP request will be logged at DEBUG level.
client = InkRecognizerClient(url=url, credential=credential, logging_enable=True)

Next steps

Please find interactive inking samples at tkinter sample and wxpython sample.

Additional documentation

For more extensive documentation on the Ink Recognizer Service, see the Ink Recognizer Service Documentation.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.