你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
FormTrainingClient 类
- java.
lang. Object - com.
azure. ai. formrecognizer. training. FormTrainingClient
- com.
public final class FormTrainingClient
此类提供一个同步客户端,用于连接到 Azure 认知服务表单识别器。
此客户端提供同步方法用于:
- 训练自定义模型:训练自定义模型,以使用 beginTraining(String trainingFilesUrl, boolean useTrainingLabels) 方法从特定于业务的表单和文档中分析和提取数据。
- 复制自定义模型:使用 beginCopyModel 方法将自定义表单识别器模型复制到目标表单识别器资源。
- 列出自定义模型:分别使用 getCustomModel(String modelId) 和 listCustomModels() 方法获取有关所有自定义模型的信息。
- 轮询和回调:它包括轮询服务以检查分析操作的状态或注册回调以在分析完成时接收通知的机制。
注意: 此客户端仅支持 V2_1 和 更低版本。 建议使用较新的服务版本 DocumentModelAdministrationAsyncClient 和 DocumentModelAdministrationClient。
若要使用 API 版本 2022-08-31 及更新,请参阅 迁移指南 。
服务客户端是开发人员使用 Azure 表单识别器的交互点。 FormTrainingClient 是同步服务客户端, FormTrainingAsyncClient 是异步服务客户端。 本文档中显示的示例使用名为 DefaultAzureCredential 的凭据对象进行身份验证,该对象适用于大多数方案,包括本地开发和生产环境。 此外,建议在生产环境中使用 托管标识 进行身份验证。 可以在 Azure 标识文档中找到有关不同身份验证方式及其相应凭据类型的详细信息。
示例:使用 DefaultAzureCredential 构造 FormTrainingClient
下面的代码示例演示如何使用“DefaultAzureCredentialBuilder”创建 FormTrainingClient它来配置它。
FormTrainingClient client = new FormTrainingClientBuilder()
.endpoint("{endpoint}")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
此外,请参阅下面的代码示例以用于 AzureKeyCredential 创建客户端。
FormTrainingClient formTrainingClient = new FormTrainingClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("{endpoint}")
.buildClient();
方法摘要
方法继承自 java.lang.Object
方法详细信息
beginCopyModel
public SyncPoller
将源) (存储在此资源中的自定义模型复制到用户指定的目标表单识别器资源。
应将源表单识别器资源 (调用,该模型旨在) 复制。 目标参数应从目标资源的输出方法 getCopyAuthorization(String resourceId, String resourceRegion) 中提供。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String resourceId = "target-resource-Id";
String resourceRegion = "target-resource-region";
String copyModelId = "copy-model-Id";
formTrainingAsyncClient.getCopyAuthorization(resourceId, resourceRegion)
.flatMapMany(copyAuthorization -> formTrainingAsyncClient.beginCopyModel(copyModelId, copyAuthorization))
.flatMap(AsyncPollResponse::getFinalResult)
.subscribe(customFormModelInfo ->
System.out.printf("Copied model has model Id: %s, model status: %s, training started on: %s,"
+ " training completed on: %s.%n",
customFormModelInfo.getModelId(),
customFormModelInfo.getStatus(),
customFormModelInfo.getTrainingStartedOn(),
customFormModelInfo.getTrainingCompletedOn()));
Parameters:
Returns:
beginCopyModel
public SyncPoller
将源) (存储在此资源中的自定义模型复制到用户指定的目标表单识别器资源。
应将源表单识别器资源 (调用,该模型旨在) 复制。 目标参数应从目标资源的输出方法 getCopyAuthorization(String resourceId, String resourceRegion) 中提供。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
// The resource to copy model to
String resourceId = "target-resource-Id";
String resourceRegion = "target-resource-region";
// The Id of the model to be copied
String copyModelId = "copy-model-Id";
CopyAuthorization copyAuthorization = targetFormTrainingClient.getCopyAuthorization(resourceId,
resourceRegion);
formTrainingClient.beginCopyModel(copyModelId, copyAuthorization, Duration.ofSeconds(5), Context.NONE)
.waitForCompletion();
CustomFormModel modelCopy = targetFormTrainingClient.getCustomModel(copyAuthorization.getModelId());
System.out.printf("Copied model has model Id: %s, model status: %s, was requested on: %s,"
+ " transfer completed on: %s.%n",
modelCopy.getModelId(),
modelCopy.getModelStatus(),
modelCopy.getTrainingStartedOn(),
modelCopy.getTrainingCompletedOn());
Parameters:
Returns:
beginCreateComposedModel
public SyncPoller
从帐户中提供的现有模型列表创建组合模型。
如果列表包含无效的、不存在的模型 ID 或重复的 ID,则此操作将失败。 目前仅支持使用标签训练的自定义模型执行此操作。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String labeledModelId1 = "5f21ab8d-71a6-42d8-9856-ef5985c486a8";
String labeledModelId2 = "d7b0904c-841f-46f9-a9f4-3f2273eef7c9";
final CustomFormModel customFormModel
= formTrainingClient.beginCreateComposedModel(Arrays.asList(labeledModelId1, labeledModelId2))
.getFinalResult();
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
System.out.printf("Is this a composed model: %s%n",
customFormModel.getCustomModelProperties().isComposed());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
beginCreateComposedModel
public SyncPoller
从帐户中提供的现有模型列表创建组合模型。
如果列表包含无效的、不存在的模型 ID 或重复的 ID,则此操作将失败。 目前仅支持使用标签训练的自定义模型执行此操作。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示没有取消支持。
代码示例
String labeledModelId1 = "5f21ab8d-71a6-42d8-9856-ef5985c486a8";
String labeledModelId2 = "d7b0904c-841f-46f9-a9f4-3f2273eef7c9";
final CustomFormModel customFormModel =
formTrainingClient.beginCreateComposedModel(Arrays.asList(labeledModelId1, labeledModelId2),
new CreateComposedModelOptions()
.setModelName("my composed model name"),
Context.NONE)
.setPollInterval(Duration.ofSeconds(5))
.getFinalResult();
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
System.out.printf("Model display name: %s%n", customFormModel.getModelName());
System.out.printf("Is this a composed model: %s%n",
customFormModel.getCustomModelProperties().isComposed());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
beginTraining
public SyncPoller
创建和训练自定义模型。
使用以下内容类型的文档训练模型 -“application/pdf”、“image/jpeg”、“image/png”、“image/tiff”。 忽略其他类型的内容。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String trainingFilesUrl = "{SAS-URL-of-your-container-in-blob-storage}";
boolean useTrainingLabels = true;
CustomFormModel customFormModel =
formTrainingClient.beginTraining(trainingFilesUrl, useTrainingLabels).getFinalResult();
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form Type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
beginTraining
public SyncPoller
创建和训练自定义模型。 使用以下内容类型的文档训练模型 -“application/pdf”、“image/jpeg”、“image/png”、“image/tiff”。 忽略其他类型的内容。
该服务不支持取消长时间运行的操作,并返回一条错误消息,指示缺少取消支持。
代码示例
String trainingFilesUrl = "{SAS-URL-of-your-container-in-blob-storage}";
TrainingFileFilter trainingFileFilter = new TrainingFileFilter().setSubfoldersIncluded(false).setPrefix("Invoice");
boolean useTrainingLabels = true;
CustomFormModel customFormModel = formTrainingClient.beginTraining(trainingFilesUrl, useTrainingLabels,
new TrainingOptions()
.setTrainingFileFilter(trainingFileFilter)
.setPollInterval(Duration.ofSeconds(5)), Context.NONE)
.getFinalResult();
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form Type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
deleteModel
public void deleteModel(String modelId)
删除指定的自定义模型。
代码示例
String modelId = "{model_id}";
formTrainingClient.deleteModel(modelId);
System.out.printf("Model Id: %s is deleted.%n", modelId);
Parameters:
deleteModelWithResponse
public Response
删除指定的自定义模型。
代码示例
String modelId = "{model_id}";
Response<Void> response = formTrainingClient.deleteModelWithResponse(modelId, Context.NONE);
System.out.printf("Response Status Code: %d.", response.getStatusCode());
System.out.printf("Model Id: %s is deleted.%n", modelId);
Parameters:
Returns:
getAccountProperties
public AccountProperties getAccountProperties()
获取表单识别器帐户的帐户信息。
代码示例
AccountProperties accountProperties = formTrainingClient.getAccountProperties();
System.out.printf("Max number of models that can be trained for this account: %d%n",
accountProperties.getCustomModelLimit());
System.out.printf("Current count of trained custom models: %d%n", accountProperties.getCustomModelCount());
Returns:
getAccountPropertiesWithResponse
public Response
使用 Http 响应和指定的 Context获取表单识别器帐户的帐户信息。
代码示例
Response<AccountProperties> response = formTrainingClient.getAccountPropertiesWithResponse(Context.NONE);
System.out.printf("Response Status Code: %d.", response.getStatusCode());
AccountProperties accountProperties = response.getValue();
System.out.printf("Max number of models that can be trained for this account: %s%n",
accountProperties.getCustomModelLimit());
System.out.printf("Current count of trained custom models: %d%n", accountProperties.getCustomModelCount());
Parameters:
Returns:
getCopyAuthorization
public CopyAuthorization getCopyAuthorization(String resourceId, String resourceRegion)
生成将自定义模型复制到目标表单识别器资源的授权。
Parameters:
String resourceId = "target-resource-Id"; String resourceRegion = "target-resource-region"; CopyAuthorization copyAuthorization = formTrainingClient.getCopyAuthorization(resourceId, resourceRegion); System.out.printf("Copy Authorization for model id: %s, access token: %s, expiration time: %s, " + "target resource Id; %s, target resource region: %s%n", copyAuthorization.getModelId(), copyAuthorization.getAccessToken(), copyAuthorization.getExpiresOn(), copyAuthorization.getResourceId(), copyAuthorization.getResourceRegion() );
Returns:
getCopyAuthorizationWithResponse
public Response
生成将自定义模型复制到目标表单识别器资源的授权。 这应由目标资源 (调用,其中模型将复制到) ,输出可以作为目标参数 beginCopyModel(String modelId, CopyAuthorization target)传递到 。
Parameters:
String resourceId = "target-resource-Id"; String resourceRegion = "target-resource-region"; Response<CopyAuthorization> copyAuthorizationResponse = formTrainingClient.getCopyAuthorizationWithResponse(resourceId, resourceRegion, Context.NONE); System.out.printf("Copy Authorization operation returned with status: %s", copyAuthorizationResponse.getStatusCode()); CopyAuthorization copyAuthorization = copyAuthorizationResponse.getValue(); System.out.printf("Copy model id: %s, access token: %s, expiration time: %s, " + "target resource Id; %s, target resource region: %s%n", copyAuthorization.getModelId(), copyAuthorization.getAccessToken(), copyAuthorization.getExpiresOn(), copyAuthorization.getResourceId(), copyAuthorization.getResourceRegion() );
Returns:
getCustomModel
public CustomFormModel getCustomModel(String modelId)
获取指定自定义模型 ID 的详细信息。
代码示例
String modelId = "{model_id}";
CustomFormModel customFormModel = formTrainingClient.getCustomModel(modelId);
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Form Type: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
getCustomModelWithResponse
public Response
获取指定自定义模型 ID 的详细信息。
代码示例
String modelId = "{model_id}";
Response<CustomFormModel> response = formTrainingClient.getCustomModelWithResponse(modelId, Context.NONE);
System.out.printf("Response Status Code: %d.", response.getStatusCode());
CustomFormModel customFormModel = response.getValue();
System.out.printf("Model Id: %s%n", customFormModel.getModelId());
System.out.printf("Model Status: %s%n", customFormModel.getModelStatus());
customFormModel.getSubmodels()
.forEach(customFormSubmodel -> customFormSubmodel.getFields()
.forEach((key, customFormModelField) ->
System.out.printf("Field: %s Field Text: %s Field Accuracy: %f%n",
key, customFormModelField.getName(), customFormModelField.getAccuracy())));
Parameters:
Returns:
getFormRecognizerClient
public FormRecognizerClient getFormRecognizerClient()
创建一个新的 FormRecognizerClient 对象。 新的 FormTrainingClient 使用与 相同的请求策略管道 FormTrainingClient。
Returns:
listCustomModels
public PagedIterable
列出表单识别器帐户上每个模型的信息。
代码示例
formTrainingClient.listCustomModels()
.forEach(customModel ->
System.out.printf("Model Id: %s, Model status: %s, Training started on: %s, Training completed on: %s.%n",
customModel.getModelId(),
customModel.getStatus(),
customModel.getTrainingStartedOn(),
customModel.getTrainingCompletedOn())
);
Returns:
listCustomModels
public PagedIterable
使用 Http 响应和指定的 Context列出表单识别器帐户上每个模型的信息。
代码示例
formTrainingClient.listCustomModels(Context.NONE)
.forEach(customModel ->
System.out.printf("Model Id: %s, Model status: %s, Training started on: %s, Training completed on: %s.%n",
customModel.getModelId(),
customModel.getStatus(),
customModel.getTrainingStartedOn(),
customModel.getTrainingCompletedOn())
);
Parameters:
Returns: