你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
DocumentAnalysisClient class
用于与表单识别器服务分析功能的交互的客户端。
例子:
表单识别器服务和客户端支持两种身份验证方式:
Azure Active Directory
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(endpoint, credential);
API 密钥(订阅密钥)
import { DocumentAnalysisClient, AzureKeyCredential } from "@azure/ai-form-recognizer";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new AzureKeyCredential("<api key>");
const client = new DocumentAnalysisClient(endpoint, credential);
Document |
从资源终结点和静态 API 密钥( 例:JavaScript
|
Document |
从资源终结点和 Azure 标识 有关使用 Azure Active Directory 进行身份验证的详细信息,请参阅 例:JavaScript
|
begin |
使用模型的唯一 ID 从输入中提取数据。 此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。
例子此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS JavaScript
|
begin |
使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。
例子此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS 如果提供的输入是字符串,它将被视为要分析的文档位置的 URL。 有关详细信息,请参阅 beginAnalyzeDocumentFromUrl 方法。 使用 URL 时,首选使用此方法,并且仅在此方法中提供 URL 支持以实现向后兼容性。 TypeScript
|
begin |
使用模型的唯一 ID 从输入中提取数据。 此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。
例子此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。 JavaScript
|
begin |
使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。
例子此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。 TypeScript
|
begin |
使用其 ID 提供的自定义分类器对文档进行分类。 此方法生成一个长时间运行的作(轮询器),最终将生成 例此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS TypeScript
|
begin |
使用 ID 提供的自定义分类器从 URL 对文档进行分类。 此方法生成一个长时间运行的作(轮询器),最终将生成 例此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。 TypeScript
|
从资源终结点和静态 API 密钥(KeyCredential
)创建 DocumentAnalysisClient
实例
例:
import { DocumentAnalysisClient, AzureKeyCredential } from "@azure/ai-form-recognizer";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new AzureKeyCredential("<api key>");
const client = new DocumentAnalysisClient(endpoint, credential);
new DocumentAnalysisClient(endpoint: string, credential: KeyCredential, options?: DocumentAnalysisClientOptions)
参数
- endpoint
-
string
Azure 认知服务实例的终结点 URL
- credential
- KeyCredential
包含认知服务实例订阅密钥的 KeyCredential
- options
- DocumentAnalysisClientOptions
用于配置客户端中所有方法的可选设置
从资源终结点和 Azure 标识 TokenCredential
创建 DocumentAnalysisClient
实例。
有关使用 Azure Active Directory 进行身份验证的详细信息,请参阅 @azure/identity
包。
例:
import { DocumentAnalysisClient } from "@azure/ai-form-recognizer";
import { DefaultAzureCredential } from "@azure/identity";
const endpoint = "https://<resource name>.cognitiveservices.azure.com";
const credential = new DefaultAzureCredential();
const client = new DocumentAnalysisClient(endpoint, credential);
new DocumentAnalysisClient(endpoint: string, credential: TokenCredential, options?: DocumentAnalysisClientOptions)
参数
- endpoint
-
string
Azure 认知服务实例的终结点 URL
- credential
- TokenCredential
@azure/identity
包中的 TokenCredential 实例
- options
- DocumentAnalysisClientOptions
用于配置客户端中所有方法的可选设置
beginAnalyzeDocument(string, FormRecognizerRequestBody, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)
使用模型的唯一 ID 从输入中提取数据。
此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。
AnalyzeResult
中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。
例子
此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream
对象、浏览器 Blob
和 ArrayBuffer
。 正文的内容将上传到服务进行分析。
import * as fs from "fs";
const file = fs.createReadStream("path/to/receipt.pdf");
// The model that is passed to the following function call determines the type of the eventual result. In the
// example, we will use the prebuilt receipt model, but you could use a custom model ID/name instead.
const poller = await client.beginAnalyzeDocument("prebuilt-receipt", file);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain lines and words
tables, // extracted tables, organized into cells that contain their contents
styles, // text styles (ex. handwriting) that were observed in the document
keyValuePairs, // extracted pairs of elements (directed associations from one element in the input to another)
entities, // extracted entities in the input's content, which are categorized (ex. "Location" or "Organization")
documents // extracted documents (instances of one of the model's document types and its field schema)
} = await poller.pollUntilDone();
// Extract the fields of the first document. These fields constitute a receipt, because we used the receipt model
const [{ fields: receipt }] = documents;
// The fields correspond to the model's document types and their field schemas. Refer to the Form Recognizer
// documentation for information about the document types and field schemas within a model, or use the `getModel`
// operation to view this information programmatically.
console.log("The type of this receipt is:", receipt?.["ReceiptType"]?.value);
function beginAnalyzeDocument(modelId: string, document: FormRecognizerRequestBody, options?: AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
参数
- modelId
-
string
此客户端资源中模型的唯一 ID (名称)
- document
- FormRecognizerRequestBody
将随请求一起上传的 FormRecognizerRequestBody
分析作和轮询器的可选设置
返回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
一个长时间运行的作(轮询器),最终将产生 AnalyzeResult
beginAnalyzeDocument<Result>(DocumentModel<Result>, FormRecognizerRequestBody, AnalyzeDocumentOptions<Result>)
使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。
AnalyzeResult
中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel
的类型推断。
例子
此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream
对象、浏览器 Blob
和 ArrayBuffer
。 正文的内容将上传到服务进行分析。
如果提供的输入是字符串,它将被视为要分析的文档位置的 URL。 有关详细信息,请参阅 beginAnalyzeDocumentFromUrl 方法。 使用 URL 时,首选使用此方法,并且仅在此方法中提供 URL 支持以实现向后兼容性。
import * as fs from "fs";
// See the `prebuilt` folder in the SDK samples (http://aka.ms/azsdk/formrecognizer/js/samples) for examples of
// DocumentModels for known prebuilts.
import { PrebuiltReceiptModel } from "./prebuilt-receipt.ts";
const file = fs.createReadStream("path/to/receipt.pdf");
// The model that is passed to the following function call determines the type of the eventual result. In the
// example, we will use the prebuilt receipt model.
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, file);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain lines and words
tables, // extracted tables, organized into cells that contain their contents
styles, // text styles (ex. handwriting) that were observed in the document
keyValuePairs, // extracted pairs of elements (directed associations from one element in the input to another)
documents // extracted documents (instances of one of the model's document types and its field schema)
} = await poller.pollUntilDone();
// Extract the fields of the first document. These fields constitute a receipt, because we used the receipt model
const [{ fields: receipt }] = documents;
// Since we used the strongly-typed PrebuiltReceiptModel object instead of the "prebuilt-receipt" model ID
// string, the fields of the receipt are strongly-typed and have camelCase names (as opposed to PascalCase).
console.log("The type of this receipt is:", receipt.receiptType?.value);
function beginAnalyzeDocument<Result>(model: DocumentModel<Result>, document: FormRecognizerRequestBody, options?: AnalyzeDocumentOptions<Result>): Promise<AnalysisPoller<Result>>
参数
- model
-
DocumentModel<Result>
DocumentModel 表示要用于分析和预期输出类型的模型
- document
- FormRecognizerRequestBody
将随请求一起上传的 FormRecognizerRequestBody
- options
-
AnalyzeDocumentOptions<Result>
分析作和轮询器的可选设置
返回
Promise<AnalysisPoller<Result>>
一个长时间运行的作(轮询器),最终将生成一个具有与输入模型关联的结果类型的文档的 AnalyzeResult
beginAnalyzeDocumentFromUrl(string, string, AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>)
使用模型的唯一 ID 从输入中提取数据。
此作支持自定义模型和预生成模型。 例如,若要使用预生成的发票模型,请提供模型 ID“prebuilt-invoice”,或使用更简单的预生成布局模型,提供模型 ID“prebuilt-layout”。
AnalyzeResult
中生成的字段取决于用于分析的模型,并且任何提取的文档字段中的值取决于模型中的文档类型(如果有)及其相应的字段架构。
例子
此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。
// the URL must be publicly accessible
const url = "<receipt document url>";
// The model that is passed to the following function call determines the type of the eventual result. In the
// example, we will use the prebuilt receipt model, but you could use a custom model ID/name instead.
const poller = await client.beginAnalyzeDocument("prebuilt-receipt", url);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain lines and words
tables, // extracted tables, organized into cells that contain their contents
styles, // text styles (ex. handwriting) that were observed in the document
keyValuePairs, // extracted pairs of elements (directed associations from one element in the input to another)
documents // extracted documents (instances of one of the model's document types and its field schema)
} = await poller.pollUntilDone();
// Extract the fields of the first document. These fields constitute a receipt, because we used the receipt model
const [{ fields: receipt }] = documents;
// The fields correspond to the model's document types and their field schemas. Refer to the Form Recognizer
// documentation for information about the document types and field schemas within a model, or use the `getModel`
// operation to view this information programmatically.
console.log("The type of this receipt is:", receipt?.["ReceiptType"]?.value);
function beginAnalyzeDocumentFromUrl(modelId: string, documentUrl: string, options?: AnalyzeDocumentOptions<AnalyzeResult<AnalyzedDocument>>): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
参数
- modelId
-
string
此客户端资源中模型的唯一 ID (名称)
- documentUrl
-
string
可从公共 Internet 访问的输入文档的 URL(字符串)
分析作和轮询器的可选设置
返回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
一个长时间运行的作(轮询器),最终将产生 AnalyzeResult
使用具有已知强类型文档架构的模型(DocumentModel)从输入中提取数据。
AnalyzeResult
中生成的字段取决于用于分析的模型。 在 TypeScript 中,此方法重载的结果类型从输入 DocumentModel
的类型推断。
例子
此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。
// See the `prebuilt` folder in the SDK samples (http://aka.ms/azsdk/formrecognizer/js/samples) for examples of
// DocumentModels for known prebuilts.
import { PrebuiltReceiptModel } from "./prebuilt-receipt.ts";
// the URL must be publicly accessible
const url = "<receipt document url>";
// The model that is passed to the following function call determines the type of the eventual result. In the
// example, we will use the prebuilt receipt model.
const poller = await client.beginAnalyzeDocument(PrebuiltReceiptModel, url);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain lines and words
tables, // extracted tables, organized into cells that contain their contents
styles, // text styles (ex. handwriting) that were observed in the document
keyValuePairs, // extracted pairs of elements (directed associations from one element in the input to another)
documents // extracted documents (instances of one of the model's document types and its field schema)
} = await poller.pollUntilDone();
// Extract the fields of the first document. These fields constitute a receipt, because we used the receipt model
const [{ fields: receipt }] = documents;
// Since we used the strongly-typed PrebuiltReceiptModel object instead of the "prebuilt-receipt" model ID
// string, the fields of the receipt are strongly-typed and have camelCase names (as opposed to PascalCase).
console.log("The type of this receipt is:", receipt.receiptType?.value);
function beginAnalyzeDocumentFromUrl<Result>(model: DocumentModel<Result>, documentUrl: string, options?: AnalyzeDocumentOptions<Result>): Promise<AnalysisPoller<Result>>
参数
- model
-
DocumentModel<Result>
DocumentModel 表示要用于分析和预期输出类型的模型
- documentUrl
-
string
可从公共 Internet 访问的输入文档的 URL(字符串)
- options
-
AnalyzeDocumentOptions<Result>
分析作和轮询器的可选设置
返回
Promise<AnalysisPoller<Result>>
一个长时间运行的作(轮询器),最终将产生 AnalyzeResult
使用其 ID 提供的自定义分类器对文档进行分类。
此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult
。 此类型与 beginAnalyzeDocument
和 beginAnalyzeDocumentFromUrl
相同,但结果将仅包含其字段的一小部分。 仅填充 documents
字段和 pages
字段,只返回最少的页面信息。
documents
字段将包含有关所有已标识文档及其分类为 docType
的信息。
例
此方法支持可流式传输的请求正文(FormRecognizerRequestBody),例如Node.JS ReadableStream
对象、浏览器 Blob
和 ArrayBuffer
。 正文的内容将上传到服务进行分析。
import * as fs from "fs";
const file = fs.createReadStream("path/to/file.pdf");
const poller = await client.beginClassifyDocument("<classifier ID>", file);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain only basic information for classifiers
documents // extracted documents and their types
} = await poller.pollUntilDone();
// We'll print the documents and their types
for (const { docType } of documents) {
console.log("The type of this document is:", docType);
}
function beginClassifyDocument(classifierId: string, document: FormRecognizerRequestBody, options?: ClassifyDocumentOptions): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
参数
- classifierId
-
string
要用于分析的自定义分类器的 ID
- document
- FormRecognizerRequestBody
要分类的文档
- options
- ClassifyDocumentOptions
分类作的选项
返回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
一个长时间运行的作(轮询器),最终将产生 AnalyzeResult
使用 ID 提供的自定义分类器从 URL 对文档进行分类。
此方法生成一个长时间运行的作(轮询器),最终将生成 AnalyzeResult
。 此类型与 beginAnalyzeDocument
和 beginAnalyzeDocumentFromUrl
相同,但结果将仅包含其字段的一小部分。 仅填充 documents
字段和 pages
字段,只返回最少的页面信息。
documents
字段将包含有关所有已标识文档及其分类为 docType
的信息。
例
此方法支持从给定 URL 的文件中提取数据。 表单识别器服务将尝试使用提交的 URL 下载文件,因此必须可从公共 Internet 访问该 URL。 例如,SAS 令牌可用于授予对 Azure 存储中 Blob 的读取访问权限,服务将使用 SAS 编码的 URL 请求文件。
// the URL must be publicly accessible
const url = "<file url>";
const poller = await client.beginClassifyDocument("<classifier ID>", url);
// The result is a long-running operation (poller), which must itself be polled until the operation completes
const {
pages, // pages extracted from the document, which contain only basic information for classifiers
documents // extracted documents and their types
} = await poller.pollUntilDone();
// We'll print the documents and their types
for (const { docType } of documents) {
console.log("The type of this document is:", docType);
}
function beginClassifyDocumentFromUrl(classifierId: string, documentUrl: string, options?: ClassifyDocumentOptions): Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>
参数
- classifierId
-
string
要用于分析的自定义分类器的 ID
- documentUrl
-
string
要分类的文档的 URL
- options
- ClassifyDocumentOptions
返回
Promise<AnalysisPoller<AnalyzeResult<AnalyzedDocument>>>