使用 Azure AI 视觉图像分析读取文本
若要使用 Azure AI 视觉进行图像分析,包括光学字符识别,必须在 Azure 订阅中预配 Azure AI 视觉资源。 资源可以是:
- Azure AI Services 多服务资源(部署为 Azure AI Foundry 中心和项目的一部分,或部署为独立资源)。
- 计算机视觉资源。
若要在应用程序中使用已部署的资源,必须使用基于密钥的身份验证或Microsoft Entra ID 身份验证连接到其 终结点 。 你可以在 Azure 门户中找到资源的终结点,或者如果在 Azure AI Foundry 门户中的 Azure AI Foundry 项目中工作,也可以找到。 终结点采用 URL 的形式,通常如下所示:
https://<resource_name>.cognitiveservices.azure.com/
建立连接后,可以通过调用 ImageAnalysis 函数(通过 REST API 或等效的 SDK 方法)、传递图像 URL 或二进制数据,并选择性地指定文本写入的语言(默认值为 en for English),从而使用 OCR 功能。
https://<endpoint>/computervision/imageanalysis:analyze?features=read&...
若要使用 Azure AI 视觉 Python SDK 从图像中提取文本,请安装 azure-ai-vision-imageanalysis 包。 然后,在代码中使用基于密钥的身份验证或Microsoft Entra ID 身份验证将 ImageAnalysisClient 对象连接到 Azure AI 视觉资源。 若要查找和读取图像中的文本,请调用 analyze (或 analyze_from_url)方法,指定 VisualFeatures.READ 枚举。
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.ai.vision.imageanalysis.models import VisualFeatures
from azure.core.credentials import AzureKeyCredential
client = ImageAnalysisClient(
endpoint="<YOUR_RESOURCE_ENDPOINT>",
credential=AzureKeyCredential("<YOUR_AUTHORIZATION_KEY>")
)
result = client.analyze(
image_data=<IMAGE_DATA_BYTES>, # Binary data from your image file
visual_features=[VisualFeatures.READ],
language="en",
)
若要使用 Azure AI 视觉 .NET SDK 从图像中提取文本,请安装 Azure.AI.Vision.ImageAnalysis 包。 然后,在代码中使用基于密钥的身份验证或Microsoft Entra ID 身份验证将 ImageAnalysisClient 对象连接到 Azure AI 视觉资源。 若要查找和读取图像中的文本,请调用 Analyze 方法,指定 VisualFeatures.Read 枚举。
using Azure.AI.Vision.ImageAnalysis;
ImageAnalysisClient client = new ImageAnalysisClient(
"<YOUR_RESOURCE_ENDPOINT>",
new AzureKeyCredential("<YOUR_AUTHORIZATION_KEY>"));
ImageAnalysisResult result = client.Analyze(
<IMAGE_DATA_BYTES>, // Binary data from your image file
VisualFeatures.Read,
new ImageAnalysisOptions { Language = t"en" });
读取 OCR 函数的结果以 JSON 或类似结构的语言特定对象的形式同步返回。 这些结果在 块(当前服务仅使用一个块),然后 行,然后 单词。 此外,文本值包含在 行 和 单词 级别,因此,如果不需要在单个 单词 级别提取文本,则更易于阅读整个文本行。
{
"metadata":
{
"width": 500,
"height": 430
},
"readResult":
{
"blocks":
[
{
"lines":
[
{
"text": "Hello World!",
"boundingPolygon":
[
{"x":251,"y":265},
{"x":673,"y":260},
{"x":674,"y":308},
{"x":252,"y":318}
],
"words":
[
{
"text":"Hello",
"boundingPolygon":
[
{"x":252,"y":267},
{"x":307,"y":265},
{"x":307,"y":318},
{"x":253,"y":318}
],
"confidence":0.996
},
{
"text":"World!",
"boundingPolygon":
[
{"x":318,"y":264},
{"x":386,"y":263},
{"x":387,"y":316},
{"x":319,"y":318}
],
"confidence":0.99
}
]
},
]
}
]
}
}