/
BuildModel.java
65 lines (57 loc) · 2.97 KB
/
BuildModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.formrecognizer.administration;
import com.azure.ai.formrecognizer.administration.models.BuildModelOptions;
import com.azure.ai.formrecognizer.administration.models.DocumentModel;
import com.azure.ai.formrecognizer.models.DocumentOperationResult;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.Context;
import com.azure.core.util.polling.SyncPoller;
/**
* Sample to build a model with training data.
* For instructions on setting up documents for training in an Azure Storage Blob Container, see
* <a href="https://aka.ms/azsdk/formrecognizer/buildtrainingset">here</a>.
* <p>
* For this sample, you can use the training documents found in
* <a href="https://aka.ms/azsdk/formrecognizer/sampletrainingfiles">here</a>
* to create your own custom document analysis models.
* For instructions to create a label file for your training forms, please see:
* <a href="https://aka.ms/azsdk/formrecognizer/labelingtool">here</a>.
* <p>
* Further, see AnalyzeCustomDocumentFromUrl.java to analyze a custom document with your built model.
*/
public class BuildModel {
/**
* Main method to invoke this demo.
*
* @param args Unused arguments to the program.
*/
public static void main(String[] args) {
// Instantiate a client that will be used to call the service.
DocumentModelAdministrationClient client = new DocumentModelAdministrationClientBuilder()
.credential(new AzureKeyCredential("{key}"))
.endpoint("https://{endpoint}.cognitiveservices.azure.com/")
.buildClient();
// Build custom document analysis model
String trainingFilesUrl = "{SAS_URL_of_your_container_in_blob_storage}";
// The shared access signature (SAS) Url of your Azure Blob Storage container with your forms.
SyncPoller<DocumentOperationResult, DocumentModel> buildOperationPoller =
client.beginBuildModel(trainingFilesUrl,
"my-build-model",
new BuildModelOptions().setDescription("model desc"),
Context.NONE);
DocumentModel documentModel = buildOperationPoller.getFinalResult();
// Model Info
System.out.printf("Model ID: %s%n", documentModel.getModelId());
System.out.printf("Model Description: %s%n", documentModel.getDescription());
System.out.printf("Model created on: %s%n%n", documentModel.getCreatedOn());
System.out.println("Document Fields:");
documentModel.getDocTypes().forEach((key, docTypeInfo) -> {
docTypeInfo.getFieldSchema().forEach((field, documentFieldSchema) -> {
System.out.printf("Field: %s", field);
System.out.printf("Field type: %s", documentFieldSchema.getType());
System.out.printf("Field confidence: %.2f", docTypeInfo.getFieldConfidence().get(field));
});
});
}
}