On this page we will use the Microsoft Cognitive API to detect text. In this case we pass an image to the Azure Cloud and it returns the text in the image:
Image Recognition of text using Microsoft Cognitive Services |
Source code
The following outlines the Python code used:
import httplib, urllib, base64 from pprint import pprint import json import sys text='https://about.canva.com/wp-content/uploads/sites/3/2015/01/concert_poster.png' if (len(sys.argv)>1): text=str(sys.argv[1]) headers = { # Request headers. Replace the key below with your subscription key. 'Content-Type': 'application/json', 'Ocp-Apim-Subscription-Key': 'KEY GOES HERE', } params = urllib.urlencode({ # Request parameters. The language setting "unk" means automatically detect the language. 'language': 'unk', 'detectOrientation ': 'false', }) body = "{'url':\'"+text+"\'}" try: conn = httplib.HTTPSConnection('westus.api.cognitive.microsoft.com') conn.request("POST", "/vision/v1.0/ocr?%s" % params, body, headers) response = conn.getresponse() data = response.read() conn.close() d= json.loads(data) z=d['regions'][0]['lines'] ctn=len(z) for x in range(0, ctn): w=z[x] ctn2=len(w['words']) for y in range(0,ctn2): print w['words'][y]['text'] print except KeyError: pass
A sample JSON return is:
{u'regions': [{u'boundingBox': u'161,50,352,863', u'lines': [{u'boundingBox': u'258,50,155,12', u'words': [{u'boundingBox': u'258,50,42,12', u'text': u'FREE'}, {u'boundingBox': u'309,50,104,12', u'text': u'ADMISSION'}]}, {u'boundingBox': u'237,316,196,14', u'words': [{u'boundingBox': u'237,316,66,14', u'text': u'MUSIC'}, {u'boundingBox': u'315,316,118,14', u'text': u'EVOLUTION'}]}, {u'boundingBox': u'238,399,194,53', u'words': [{u'boundingBox': u'238,399,194,53', u'text': u'JAZZ'}]}, {u'boundingBox': u'161,464,352,54', u'words': [{u'boundingBox': u'161,464,352,54', u'text': u'CONCEPT'}]}, {u'boundingBox': u'234,582,209,12', u'words': [{u'boundingBox': u'234,582,21,12', u'text': u'BE'}, {u'boundingBox': u'265,582,46,12', u'text': u'PART'}, {u'boundingBox': u'320,582,22,12', u'text': u'OF'}, {u'boundingBox': u'352,582,91,12', u'text': u"FRANCE'S"}]}, {u'boundingBox': u'205,605,266,12', u'words': [{u'boundingBox': u'205,605,52,12', u'text': u'MOST'}, {u'boundingBox': u'265,605,85,12', u'text': u'AWAITED'}, {u'boundingBox': u'359,605,44,12', u'text': u'JAZZ'}, {u'boundingBox': u'412,605,59,12', u'text': u'MUSIC'}]}, {u'boundingBox': u'216,628,244,12', u'words': [{u'boundingBox': u'216,628,86,12', u'text': u'FESTIVAL'}, {u'boundingBox': u'311,628,23,12', u'text': u'OF'}, {u'boundingBox': u'343,628,34,12', u'text': u'THE'}, {u'boundingBox': u'386,628,74,12', u'text': u'SEASON'}]}, {u'boundingBox': u'232,901,211,12', u'words': [{u'boundingBox': u'232,901,211,12', u'text': u'MUSICEVOLUTION.COM'}]}]}], u'textAngle': 0.0, u'orientation': u'Up', u'language': u'en'}