alexho/azure-ocr
0
1import http.client, urllib.request, urllib.parse, urllib.error, base64 , json2 3# call azure ocr service4# pass image url to azure.5 6headers = {7 # Request headers8 'Content-Type': 'application/json',9 'Ocp-Apim-Subscription-Key': '',10}11 12params = urllib.parse.urlencode({13 # Request parameters14 'language': 'zh-Hant',15 'detectOrientation': 'true',16 'model-version': 'latest',17})18 19body = {"url":"https://rachel-nutrition.com/wp-content/uploads/2019/11/infographic_nutritionlabel.jpg"}20json_data = json.dumps(body)21 22try:23 conn = http.client.HTTPSConnection('')24 conn.request("POST", "/vision/v3.2/ocr?%s" % params, json_data , headers)25 response = conn.getresponse()26 data = response.read()27 ocr_result = json.loads(data)28 print(ocr_result["language"])29 conn.close()30 31 aLine = ""32 for region in ocr_result['regions']:33 for line in region['lines']:34 aLine = ""35 for txt in line['words']:36 aLine += txt["text"]37 print(aLine)38 39 40except Exception as e:41 print("[Errno {0}] {1}".format(e.errno, e.strerror))42 43 44 45 