【发布时间】:2020-12-24 01:08:13
【问题描述】:
我正在尝试通过探索 Azure 来提高我的编码和云计算技能。我想自动化一些涉及解密大量手写文档并以电子方式存储文本的管理任务。
下面的 Python 代码是两个代码源的合并。
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 + "vision/v3.0/analyze"
text_recognition_url = endpoint + "/vision/v3.0/read/analyze"
image_url = "https://3j2w6t1pktei3iwq0u47sym8-wpengine.netdna-ssl.com/wp-content/uploads/2014/08/Handwriting-sample-Katie.png"
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["Operation-Location"]
# The recognized text isn't immediately available, so poll to wait for completion.
analysis = {}
poll = True
while (poll):
response_final = requests.get(
response.headers["Operation-Location"], headers=headers)
analysis = response_final.json()
print(json.dumps(analysis, indent=4))
time.sleep(1)
if ("analyzeResult" in analysis):
poll = False
if ("status" in analysis and analysis['status'] == 'failed'):
poll = False
polygons = []
if ("analyzeResult" in analysis):
# Extract the recognized text, with bounding boxes.
polygons = [(line["boundingBox"], line["text"])
for line in analysis["analyzeResult"]["readResults"][0]["lines"]]
# 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="top")
plt.show()
我想做的是在修改脚本方面获得一些帮助,以便它可以处理本地存储的图像文件(而不是使用 URL)。
目前,我正在通过在 Azure 虚拟机上启动 IIS 服务器并访问要通过 HTML 分析的图像的 URL 来解决此问题。它有点笨拙(而且对我的目的来说有点不安全)。
谢谢,WL
【问题讨论】:
标签: python-3.x azure azure-cognitive-services handwriting-recognition