title: Handwriting recognition in Azure Computer Vision API from locally stored image file
body: <p>I am trying to brush up my coding and cloud-computing skills by exploring Azure. I would like to automate some admin tasks that involves deciphering lot of handwritten documents and storing the text electronically.</p>
  <p>The Python code below is a merge of two code sources.</p>
  <ol>
  <li><p>A blog from Taygan Rifat <a href="https://www.taygan.co/blog/2018/4/28/image-processing-with-cognitive-services" rel="nofollow noreferrer">https://www.taygan.co/blog/2018/4/28/image-processing-with-cognitive-services</a></p>
  </li>
  <li><p>Microsoft's own demo code over at <a href="https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/python-hand-text" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts/python-hand-text</a></p>
  </li>
  </ol>
  <pre><code>import json
  import os
  import sys
  import requests
  import time
  import matplotlib.pyplot as plt
  from matplotlib.patches import Polygon
  from PIL import Image
  from io import BytesIO
  
  subscription_key = 'XX79fdc005d542XXXb5f29ce04ab1cXXX'
  endpoint = 'https://handwritng.cognitiveservices.azure.com/'
  analyze_url = endpoint + &quot;vision/v3.0/analyze&quot;
  text_recognition_url = endpoint + &quot;/vision/v3.0/read/analyze&quot;
  
  image_url = &quot;https://3j2w6t1pktei3iwq0u47sym8-wpengine.netdna-ssl.com/wp-content/uploads/2014/08/Handwriting-sample-Katie.png&quot;
  
  headers = {'Ocp-Apim-Subscription-Key': subscription_key}
  data = {'url': image_url}
  response = requests.post(
      text_recognition_url, headers=headers, json=data)
  response.raise_for_status()
  
  # Extracting text requires two API calls: One call to submit the
  # image for processing, the other to retrieve the text found in the image.
  
  # Holds the URI used to retrieve the recognized text.
  operation_url = response.headers[&quot;Operation-Location&quot;]
  
  # The recognized text isn't immediately available, so poll to wait for completion.
  analysis = {}
  poll = True
  while (poll):
      response_final = requests.get(
          response.headers[&quot;Operation-Location&quot;], headers=headers)
      analysis = response_final.json()
  
      print(json.dumps(analysis, indent=4))
  
      time.sleep(1)
      if (&quot;analyzeResult&quot; in analysis):
          poll = False
      if (&quot;status&quot; in analysis and analysis['status'] == 'failed'):
          poll = False
  
  polygons = []
  if (&quot;analyzeResult&quot; in analysis):
      # Extract the recognized text, with bounding boxes.
      polygons = [(line[&quot;boundingBox&quot;], line[&quot;text&quot;])
                  for line in analysis[&quot;analyzeResult&quot;][&quot;readResults&quot;][0][&quot;lines&quot;]]
  
  # Display the image and overlay it with the extracted text.
  image = Image.open(BytesIO(requests.get(image_url).content))
  ax = plt.imshow(image)
  for polygon in polygons:
      vertices = [(polygon[0][i], polygon[0][i + 1])
                  for i in range(0, len(polygon[0]), 2)]
      text = polygon[1]
      print(text)
      patch = Polygon(vertices, closed=True, fill=False, linewidth=2, color='y')
      ax.axes.add_patch(patch)
      plt.text(vertices[0][0], vertices[0][1], text, fontsize=20, va=&quot;top&quot;)
  
  
  plt.show()
  </code></pre>
  <p>What I would like to do is get some help in modifying the script so that it can work with image files stored locally (instead of using URLs).</p>
  <p>Currently I am working around this by spinning up an IIS server on an Azure virtual machine and accessing the URL of the image I want to analyse via HTML. It's a little unwieldy (and somewhat unsecure for my purposes).</p>
  <p>Thanks, WL</p>