<jupyter_start><jupyter_text>Recognising handwritten text using Azure Cognitive Services (Computer Vision APIs)To run the code below - select code cells and press *CTRL+Enter* to execute, then move to the next cell, and so on.<jupyter_code>import http.client, urllib.request, urllib.parse, urllib.error, base64, requests, time, json, sys

subscription_key = '2f7e689380cf4b66870ad4d98d42156b' # Free tier key - will be changed soon. 
# To avoid server error messages - generate your own free subscribtion key and insert it here.

uri_base = 'https://westcentralus.api.cognitive.microsoft.com'
requestHeaders = {
    # Request headers.
    # Another valid content type is "application/octet-stream".
    'Content-Type': 'application/json',
    #'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscription_key,
}

# The URL of a JPEG image containing handwritten text.
body = {'url' : 'https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Cursive_Writing_on_Notebook_paper.jpg/800px-Cursive_Writing_on_Notebook_paper.jpg'}
#body = open(sys.argv[1], 'rb').read()
# For printed text, set "handwriting" to false.
params = {'handwriting' : 'true'}<jupyter_output><empty_output><jupyter_text>Image from the link above: [https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Cursive_Writing_on_Notebook_paper.jpg/800px-Cursive_Writing_on_Notebook_paper.jpg]![handwritten text](https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Cursive_Writing_on_Notebook_paper.jpg/800px-Cursive_Writing_on_Notebook_paper.jpg)<jupyter_code># This operation requrires two REST API calls. One to submit the image for processing,
# the other to retrieve the text found in the image. 
#
# This executes the first REST API call and gets the response.
response = requests.request('POST', uri_base + '/vision/v1.0/RecognizeText', json=body, data=None, headers=requestHeaders, params=params)

# Success is indicated by a status of 202.
if response.status_code != 202:
    # if the first REST API call was not successful, display JSON data and exit.
    parsed = json.loads(response.text)
    print ("Error:")
    print (json.dumps(parsed, sort_keys=True, indent=2))
    exit()

# The 'Operation-Location' in the response contains the URI to retrieve the recognized text.
operationLocation = response.headers['Operation-Location']
# Execute the second REST API call and get the response.
response = requests.request('GET', operationLocation, json=None, data=None, headers=requestHeaders, params=None)

# 'data' contains the JSON data. The following formats the JSON data for display.
parsed = json.loads(response.text)
print ("Response:")
print (json.dumps(parsed, sort_keys=True, indent=2))<jupyter_output>Response:
{
  "recognitionResult": {
    "lines": [
      {
        "boundingBox": [
          2,
          52,
          65,
          46,
          69,
          89,
          7,
          95
        ],
        "text": "dog",
        "words": [
          {
            "boundingBox": [
              0,
              58,
              62,
              41,
              76,
              84,
              2,
              101
            ],
            "text": "dog"
          }
        ]
      },
      {
        "boundingBox": [
          6,
          2,
          771,
          13,
          770,
          75,
          5,
          64
        ],
        "text": "The quick brown fox jumps over the lazy",
        "words": [
          {
            "boundingBox": [
              0,
              3,
              91,
              4,
              76,
              70,
              0,
              70
            ],
            "text": "The"
          },
          {
            "boundin[...]
{'fraction_non_alphanumeric': 0.10818791946308724, 'fraction_numerical': 0.026845637583892617, 'mean_word_length': 2.332737030411449, 'pattern_counts': {'":': 12, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '297976', 'license': 'MIT', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1106, 'n_words': 298, 'path': 'handwriting.ipynb', 'repo_name': 'vakarpus/workshop-notebooks', 'size': '3725'}