@azure/ai-form-recognizer 4.0.0-beta.2 → 4.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +143 -49
- package/dist/index.js +1683 -601
- package/dist/index.js.map +1 -1
- package/dist-esm/src/constants.js +4 -0
- package/dist-esm/src/constants.js.map +1 -1
- package/dist-esm/src/documentAnalysisClient.js +121 -33
- package/dist-esm/src/documentAnalysisClient.js.map +1 -1
- package/dist-esm/src/documentModelAdministrationClient.js +54 -44
- package/dist-esm/src/documentModelAdministrationClient.js.map +1 -1
- package/dist-esm/src/generated/generatedClient.js +122 -19
- package/dist-esm/src/generated/generatedClient.js.map +1 -1
- package/dist-esm/src/generated/generatedClientContext.js +6 -6
- package/dist-esm/src/generated/generatedClientContext.js.map +1 -1
- package/dist-esm/src/generated/index.js +0 -1
- package/dist-esm/src/generated/index.js.map +1 -1
- package/dist-esm/src/generated/models/index.js +70 -1
- package/dist-esm/src/generated/models/index.js.map +1 -1
- package/dist-esm/src/generated/models/mappers.js +441 -112
- package/dist-esm/src/generated/models/mappers.js.map +1 -1
- package/dist-esm/src/generated/models/parameters.js +39 -4
- package/dist-esm/src/generated/models/parameters.js.map +1 -1
- package/dist-esm/src/index.js.map +1 -1
- package/dist-esm/src/lro/analyze.js +26 -34
- package/dist-esm/src/lro/analyze.js.map +1 -1
- package/dist-esm/src/lro/training.js +2 -0
- package/dist-esm/src/lro/training.js.map +1 -1
- package/dist-esm/src/lro/util/poller.js.map +1 -1
- package/dist-esm/src/models/{GenericDocumentResult.js → GeneralDocumentResult.js} +6 -6
- package/dist-esm/src/models/GeneralDocumentResult.js.map +1 -0
- package/dist-esm/src/models/LayoutResult.js +1 -2
- package/dist-esm/src/models/LayoutResult.js.map +1 -1
- package/dist-esm/src/models/ReadResult.js +18 -0
- package/dist-esm/src/models/ReadResult.js.map +1 -0
- package/dist-esm/src/models/documentElements.js +4 -0
- package/dist-esm/src/models/documentElements.js.map +1 -0
- package/dist-esm/src/models/fields.js +9 -1
- package/dist-esm/src/models/fields.js.map +1 -1
- package/dist-esm/src/models/index.js.map +1 -1
- package/dist-esm/src/options/AnalyzeDocumentsOptions.js.map +1 -1
- package/dist-esm/src/options/BuildModelOptions.js +16 -1
- package/dist-esm/src/options/BuildModelOptions.js.map +1 -1
- package/dist-esm/src/options/FormRecognizerClientOptions.js +3 -6
- package/dist-esm/src/options/FormRecognizerClientOptions.js.map +1 -1
- package/dist-esm/src/options/GetCopyAuthorizationOptions.js.map +1 -1
- package/dist-esm/src/options/PollerOptions.js.map +1 -1
- package/dist-esm/src/options/index.js +2 -1
- package/dist-esm/src/options/index.js.map +1 -1
- package/dist-esm/src/prebuilt/index.js +5 -4
- package/dist-esm/src/prebuilt/index.js.map +1 -1
- package/dist-esm/src/prebuilt/modelSchemas/businessCard.js +2 -116
- package/dist-esm/src/prebuilt/modelSchemas/businessCard.js.map +1 -1
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-businessCard.json.js +93 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-businessCard.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-document.json.js +12 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-document.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-healthInsuranceCard.us.json.js +153 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-healthInsuranceCard.us.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-idDocument.json.js +89 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-idDocument.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-invoice.json.js +144 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-invoice.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-layout.json.js +12 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-layout.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-read.json.js +12 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-read.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-receipt.json.js +341 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-receipt.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-tax.us.w2.json.js +155 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-tax.us.w2.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-vaccinationCard.json.js +50 -0
- package/dist-esm/src/prebuilt/modelSchemas/data/prebuilt-vaccinationCard.json.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/healthInsuranceCard.js +5 -0
- package/dist-esm/src/prebuilt/modelSchemas/healthInsuranceCard.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/idDocument.js +2 -130
- package/dist-esm/src/prebuilt/modelSchemas/idDocument.js.map +1 -1
- package/dist-esm/src/prebuilt/modelSchemas/invoice.js +2 -197
- package/dist-esm/src/prebuilt/modelSchemas/invoice.js.map +1 -1
- package/dist-esm/src/prebuilt/modelSchemas/receipt.js +2 -143
- package/dist-esm/src/prebuilt/modelSchemas/receipt.js.map +1 -1
- package/dist-esm/src/prebuilt/modelSchemas/vaccinationCard.js +5 -0
- package/dist-esm/src/prebuilt/modelSchemas/vaccinationCard.js.map +1 -0
- package/dist-esm/src/prebuilt/modelSchemas/w2.js +5 -0
- package/dist-esm/src/prebuilt/modelSchemas/w2.js.map +1 -0
- package/dist-esm/src/prebuilt/models.js +43 -5
- package/dist-esm/src/prebuilt/models.js.map +1 -1
- package/dist-esm/src/prebuilt/schema.js.map +1 -1
- package/dist-esm/src/transforms/polygon.js +26 -0
- package/dist-esm/src/transforms/polygon.js.map +1 -0
- package/dist-esm/src/util.js +7 -0
- package/dist-esm/src/util.js.map +1 -1
- package/package.json +32 -30
- package/types/ai-form-recognizer.d.ts +1133 -509
- package/CHANGELOG.md +0 -200
- package/dist-esm/src/models/GenericDocumentResult.js.map +0 -1
package/README.md
CHANGED
|
@@ -4,6 +4,7 @@ Azure Cognitive Services [Form Recognizer](https://azure.microsoft.com/services/
|
|
|
4
4
|
|
|
5
5
|
- Layout - Extract text, table structures, and selection marks, along with their bounding region coordinates, from documents.
|
|
6
6
|
- Document - Analyze entities, key-value pairs, tables, and selection marks from documents using the general prebuilt document model.
|
|
7
|
+
- Read - Read information about textual elements, such as page words and lines in addition to text language information.
|
|
7
8
|
- Prebuilt - Analyze data from certain types of common documents (such as receipts, invoices, business cards, or identity documents) using prebuilt models.
|
|
8
9
|
- Custom - Build custom models to extract text, field values, selection marks, and table data from documents. Custom models are built with your own data, so they're tailored to your documents.
|
|
9
10
|
|
|
@@ -15,23 +16,23 @@ Azure Cognitive Services [Form Recognizer](https://azure.microsoft.com/services/
|
|
|
15
16
|
|
|
16
17
|
#### **_Breaking Change Advisory_ ⚠️**
|
|
17
18
|
|
|
18
|
-
In version 4 (currently beta), this package introduces a full redesign of the Azure Form Recognizer client library. To leverage features of the newest Form Recognizer service API (version "
|
|
19
|
+
In version 4 (currently beta), this package introduces a full redesign of the Azure Form Recognizer client library. To leverage features of the newest Form Recognizer service API (version "2022-06-30-preview" and newer), the new SDK is required, and application code must be changed to use the new clients. Please see the [Migration Guide](https://github.com/azure/azure-sdk-for-js/blob/main/sdk/formrecognizer/ai-form-recognizer/MIGRATION-v3_v4.md) for detailed instructions on how to update application code from version 3.x of the Form Recognizer SDK to the new version (4.x). Additionally, the [CHANGELOG](https://github.com/azure/azure-sdk-for-js/blob/main/sdk/formrecognizer/ai-form-recognizer/CHANGELOG.md) contains an outline of the changes. This package targets Azure Form Recognizer service API version `2022-06-30-preview` and newer. To continue to use Form Recognizer API version 2.1, please use major version 3 of the client package (`@azure/ai-form-recognizer@^3.2.0`).
|
|
19
20
|
|
|
20
21
|
### Install the `@azure/ai-form-recognizer` Package
|
|
21
22
|
|
|
22
23
|
Install the Azure Form Recognizer client library for JavaScript with `npm`:
|
|
23
24
|
|
|
24
25
|
```bash
|
|
25
|
-
npm install @azure/ai-form-recognizer@4.0.0-beta.
|
|
26
|
+
npm install @azure/ai-form-recognizer@4.0.0-beta.5
|
|
26
27
|
```
|
|
27
28
|
|
|
28
29
|
## Getting Started
|
|
29
30
|
|
|
30
31
|
```javascript
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
const { DocumentAnalysisClient } = require("@azure/ai-form-recognizer");
|
|
33
|
+
const { DefaultAzureCredential } = require("@azure/identity");
|
|
33
34
|
|
|
34
|
-
|
|
35
|
+
const fs = require("fs");
|
|
35
36
|
|
|
36
37
|
const credential = new DefaultAzureCredential();
|
|
37
38
|
const client = new DocumentAnalysisClient(
|
|
@@ -41,7 +42,7 @@ const client = new DocumentAnalysisClient(
|
|
|
41
42
|
|
|
42
43
|
// Form Recognizer supports many different types of files.
|
|
43
44
|
const file = fs.createReadStream("path/to/file.jpg");
|
|
44
|
-
const poller = await client.
|
|
45
|
+
const poller = await client.beginAnalyzeDocument("<model ID>", file);
|
|
45
46
|
|
|
46
47
|
const { pages, tables, styles, keyValuePairs, entities, documents } = await poller.pollUntilDone();
|
|
47
48
|
```
|
|
@@ -89,7 +90,7 @@ In order to interact with the Form Recognizer service, you'll need to select eit
|
|
|
89
90
|
You can find the endpoint for your Form Recognizer resource either in the [Azure Portal][azure_portal] or by using the [Azure CLI][azure_cli] snippet below:
|
|
90
91
|
|
|
91
92
|
```bash
|
|
92
|
-
az cognitiveservices account show --name <your-resource-name> --resource-group <your-resource-group-name> --query "endpoint"
|
|
93
|
+
az cognitiveservices account show --name <your-resource-name> --resource-group <your-resource-group-name> --query "properties.endpoint"
|
|
93
94
|
```
|
|
94
95
|
|
|
95
96
|
#### Using an API Key
|
|
@@ -105,7 +106,7 @@ az cognitiveservices account keys list --resource-group <your-resource-group-nam
|
|
|
105
106
|
Once you have an API key and endpoint, you can use it as follows:
|
|
106
107
|
|
|
107
108
|
```js
|
|
108
|
-
|
|
109
|
+
const { DocumentAnalysisClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
|
|
109
110
|
|
|
110
111
|
const client = new DocumentAnalysisClient("<endpoint>", new AzureKeyCredential("<API key>"));
|
|
111
112
|
```
|
|
@@ -123,8 +124,8 @@ To authenticate using a service principal, you will also need to [register an AA
|
|
|
123
124
|
Set the values of the client ID, tenant ID, and client secret of the AAD application as environment variables: `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_CLIENT_SECRET`.
|
|
124
125
|
|
|
125
126
|
```js
|
|
126
|
-
|
|
127
|
-
|
|
127
|
+
const { DocumentAnalysisClient } = require("@azure/ai-form-recognizer");
|
|
128
|
+
const { DefaultAzureCredential } = require("@azure/identity");
|
|
128
129
|
|
|
129
130
|
const client = new DocumentAnalysisClient("<endpoint>", new DefaultAzureCredential());
|
|
130
131
|
```
|
|
@@ -135,9 +136,10 @@ const client = new DocumentAnalysisClient("<endpoint>", new DefaultAzureCredenti
|
|
|
135
136
|
|
|
136
137
|
`DocumentAnalysisClient` provides operations for analyzing input documents using custom and prebuilt models. It has three methods:
|
|
137
138
|
|
|
138
|
-
- `
|
|
139
|
-
- `beginExtractLayout`, which uses the "prebuilt-layout" model to extract only the basic layout (OCR) information from the input documents, such as pages and their contents, tables, and observed text styles. This method provides a stronger TypeScript type for the layout result than the `
|
|
140
|
-
- `
|
|
139
|
+
- `beginAnalyzeDocument`, which extracts data from an input document using a custom or prebuilt model given by its model ID. For information about the prebuilt models supported in all resources and their model IDs/outputs, please see [the service's documentation of the models][fr-models].
|
|
140
|
+
- `beginExtractLayout`, which uses the "prebuilt-layout" model to extract only the basic layout (OCR) information from the input documents, such as pages and their contents, tables, and observed text styles. This method provides a stronger TypeScript type for the layout result than the `beginAnalyzeDocument` method.
|
|
141
|
+
- `beginExtractGeneralDocument`, which uses the "prebuilt-document" model to extract key-value pairs and entities in addition to the properties of the prebuilt layout model. This method also provides a stronger TypeScript type for the general document result than the `beginAnalyzeDocument` method.
|
|
142
|
+
- `beginReadDocument`, which uses the "prebuilt-read" model to extract textual elements, such as page words and lines in addition to text language information.
|
|
141
143
|
|
|
142
144
|
### `DocumentModelAdministrationClient`
|
|
143
145
|
|
|
@@ -145,7 +147,7 @@ const client = new DocumentAnalysisClient("<endpoint>", new DefaultAzureCredenti
|
|
|
145
147
|
|
|
146
148
|
- `beginBuildModel` starts an operation to create a new document model from your own training data set. The created model can extract fields according to a custom schema. The training data are expected to be located in an Azure Storage container and organized according to a particular convention. See the [service's documentation on creating a training data set][fr-build-training-set] for a more detailed explanation of applying labels to a training data set.
|
|
147
149
|
- `beginComposeModel` starts an operation to compose multiple models into a single model. When used for custom form recognition, the new composed model will first perform a classification of the input documents to determine which of its submodels is most appropriate.
|
|
148
|
-
- `
|
|
150
|
+
- `beginCopyModelTo` starts an operation to copy a custom model from one Form Recognizer resource to another (or even to the same Form Recognizer resource). It requires a `CopyAuthorization` from the target Form Recognizer resource, which can be generated using the `getCopyAuthorization` method.
|
|
149
151
|
- `getInfo` retrieves information about the resource's limits, such as the number of custom models and the maximum number of models the resource can support.
|
|
150
152
|
- `getModel`, `listModels`, and `deleteModel` enable managing models in the resource.
|
|
151
153
|
- `getOperation` and `listOperations` enable viewing the status of model creation operations, even those operations that are ongoing or that have failed. Operations are retained for 24 hours.
|
|
@@ -166,17 +168,17 @@ The following section provides several JavaScript code snippets illustrating com
|
|
|
166
168
|
|
|
167
169
|
- [Analyze a Document with a Model](#analyze-a-document-with-a-model)
|
|
168
170
|
- [Extract Layout](#extract-layout)
|
|
169
|
-
- [Extract
|
|
171
|
+
- [Extract General Document](#extract-general-document)
|
|
170
172
|
- [Use Prebuilt Models](#using-prebuilt-models)
|
|
171
173
|
- [Build a Model](#build-a-model)
|
|
172
174
|
- [Manage Models](#manage-models)
|
|
173
175
|
|
|
174
176
|
### Analyze a Document with a Model
|
|
175
177
|
|
|
176
|
-
The `
|
|
178
|
+
The `beginAnalyzeDocument` method can extract fields and table data from documents. These models are trained with your own data, so they're tailored to your particular documents. A custom model should only be used with documents of the same structure as one of the document types in the model (there may be multiple, such as in a composed model).
|
|
177
179
|
|
|
178
180
|
```javascript
|
|
179
|
-
|
|
181
|
+
const { DocumentAnalysisClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
|
|
180
182
|
|
|
181
183
|
const fs = require("fs");
|
|
182
184
|
|
|
@@ -189,7 +191,7 @@ async function main() {
|
|
|
189
191
|
const readStream = fs.createReadStream(path);
|
|
190
192
|
|
|
191
193
|
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
|
|
192
|
-
const poller = await client.
|
|
194
|
+
const poller = await client.beginAnalyzeDocument(modelId, readStream, {
|
|
193
195
|
onProgress: ({ status }) => {
|
|
194
196
|
console.log(`status: ${status}`);
|
|
195
197
|
},
|
|
@@ -231,10 +233,13 @@ As an alternative to providing a readable stream, a publicly-accessible URL can
|
|
|
231
233
|
|
|
232
234
|
### Extract Layout
|
|
233
235
|
|
|
236
|
+
<a id="beginextractlayout-deprecation"></a>
|
|
237
|
+
**Deprecation Warning** ⚠️: The built in `beginExtractLayout` method is deprecated as of version `4.0.0-beta.4`. Prior to a stable release of version 4.0.0, we will remove it and provide a solution that uses the `beginAnalyzeDocument` method instead. This will align the `"prebuilt-layout"` model with the other prebuilt models, enabling us to continue to provide timely updates and ensure stability as the number of supported prebuilt models increases and as their capabilities are enhanced.
|
|
238
|
+
|
|
234
239
|
The `beginExtractLayout` method extracts only the basic elements of the document, such as pages, (which consist of text words/lines and selection marks), tables, and visual text styles along with their bounding regions and spans within the text content of the input documents.
|
|
235
240
|
|
|
236
241
|
```javascript
|
|
237
|
-
|
|
242
|
+
const { DocumentAnalysisClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
|
|
238
243
|
|
|
239
244
|
const fs = require("fs");
|
|
240
245
|
|
|
@@ -266,14 +271,17 @@ main().catch((err) => {
|
|
|
266
271
|
});
|
|
267
272
|
```
|
|
268
273
|
|
|
269
|
-
_Note_: you may also use the `
|
|
274
|
+
_Note_: you may also use the `beginAnalyzeDocument` method to extract layout information using the prebuilt layout model by providing the model ID `"prebuilt-layout"`. This method provides a weaker TypeScript type for the layout analysis result, but will produce the same information. The `beginExtractLayout` method is available for your convenience.
|
|
275
|
+
|
|
276
|
+
### Extract General Document
|
|
270
277
|
|
|
271
|
-
|
|
278
|
+
<a id="beginextractgeneraldocument-deprecation"></a>
|
|
279
|
+
**Deprecation Warning** ⚠️: The built in `beginExtractGeneralDocument` method is deprecated as of version `4.0.0-beta.4`. Prior to a stable release of version 4.0.0, we will remove it and provide a solution that uses the `beginAnalyzeDocument` method instead. This will align the `"prebuilt-document"` model with the other prebuilt models, enabling us to continue to provide timely updates and ensure stability as the number of supported prebuilt models increases and as their capabilities are enhanced.
|
|
272
280
|
|
|
273
|
-
The `
|
|
281
|
+
The `beginExtractGeneralDocument` method extracts information about key-value pairs and entities in addition to the properties produced by the layout extraction method. This prebuilt (general) document model provides similar functionality to the custom models trained without label information in previous iterations of the Form Recognizer service, but it is now provided as a prebuilt model that works with a wide variety of documents.
|
|
274
282
|
|
|
275
283
|
```javascript
|
|
276
|
-
|
|
284
|
+
const { DocumentAnalysisClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
|
|
277
285
|
|
|
278
286
|
const fs = require("fs");
|
|
279
287
|
|
|
@@ -285,7 +293,7 @@ async function main() {
|
|
|
285
293
|
const readStream = fs.createReadStream(path);
|
|
286
294
|
|
|
287
295
|
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
|
|
288
|
-
const poller = await client.
|
|
296
|
+
const poller = await client.beginExtractGeneralDocument(readStream);
|
|
289
297
|
|
|
290
298
|
// `pages`, `tables` and `styles` are also available as in the "layout" example above, but for the sake of this
|
|
291
299
|
// example we won't show them here.
|
|
@@ -320,16 +328,89 @@ main().catch((err) => {
|
|
|
320
328
|
});
|
|
321
329
|
```
|
|
322
330
|
|
|
323
|
-
_Note_: you may also use the `
|
|
331
|
+
_Note_: you may also use the `beginAnalyzeDocument` method to extract general document information using the prebuilt document model by providing the model ID `"prebuilt-document"`. This method provides a weaker TypeScript type for the layout analysis result, but will produce the same information. The `beginExtractGeneralDocument` method is available for your convenience.
|
|
332
|
+
|
|
333
|
+
### Read Document
|
|
334
|
+
|
|
335
|
+
<a id="beginreaddocument-deprecation"></a>
|
|
336
|
+
**Deprecation Warning** ⚠️: The built in `beginReadDocument` method is deprecated as of version `4.0.0-beta.4`. Prior to a stable release of version 4.0.0, we will remove it and provide a solution that uses the `beginAnalyzeDocument` method instead. This will align the `"prebuilt-document"` model with the other prebuilt models, enabling us to continue to provide timely updates and ensure stability as the number of supported prebuilt models increases and as their capabilities are enhanced.
|
|
337
|
+
|
|
338
|
+
```javascript
|
|
339
|
+
const { DocumentAnalysisClient, AzureKeyCredential } = require("@azure/ai-form-recognizer");
|
|
340
|
+
const fs = require("fs");
|
|
341
|
+
|
|
342
|
+
async function main() {
|
|
343
|
+
const endpoint = "<cognitive services endpoint>";
|
|
344
|
+
const apiKey = "<api key>";
|
|
345
|
+
const path = "<path to a document>"; // pdf/jpeg/png/tiff formats
|
|
346
|
+
|
|
347
|
+
const readStream = fs.createReadStream(path);
|
|
348
|
+
|
|
349
|
+
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
|
|
350
|
+
const poller = await client.beginReadDocument(readStream);
|
|
351
|
+
|
|
352
|
+
// The "prebuilt-read" model (`beginReadDocument` method) only extracts information about the textual content of the
|
|
353
|
+
// document, such as page text elements, text styles, and information about the language of the text.
|
|
354
|
+
const { content, pages, languages, styles } = await poller.pollUntilDone();
|
|
355
|
+
|
|
356
|
+
if (pages.length <= 0) {
|
|
357
|
+
console.log("No pages were extracted from the document.");
|
|
358
|
+
} else {
|
|
359
|
+
console.log("Pages:");
|
|
360
|
+
for (const page of pages) {
|
|
361
|
+
console.log("- Page", page.pageNumber, `(unit: ${page.unit})`);
|
|
362
|
+
console.log(` ${page.width}x${page.height}, angle: ${page.angle}`);
|
|
363
|
+
console.log(` ${page.lines.length} lines, ${page.words.length} words`);
|
|
364
|
+
|
|
365
|
+
if (page.lines.length > 0) {
|
|
366
|
+
console.log(" Lines:");
|
|
367
|
+
|
|
368
|
+
for (const line of page.lines) {
|
|
369
|
+
console.log(` - "${line.content}"`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (languages.length <= 0) {
|
|
376
|
+
console.log("No language spans were extracted from the document.");
|
|
377
|
+
} else {
|
|
378
|
+
console.log("Languages:");
|
|
379
|
+
for (const languageEntry of languages) {
|
|
380
|
+
console.log(
|
|
381
|
+
`- Found language: ${languageEntry.languageCode} (confidence: ${languageEntry.confidence})`
|
|
382
|
+
);
|
|
383
|
+
for (const text of getTextOfSpans(content, languageEntry.spans)) {
|
|
384
|
+
const escapedText = text.replace(/\r?\n/g, "\\n").replace(/"/g, '\\"');
|
|
385
|
+
console.log(` - "${escapedText}"`);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
main().catch((error) => {
|
|
392
|
+
console.error("An error occurred:", error);
|
|
393
|
+
process.exit(1);
|
|
394
|
+
});
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
_Note_: you may also use the `beginAnalyzeDocument` method to read document information using the "read" model by providing the model ID `"prebuilt-read"`. This method provides a weaker TypeScript type for the read result, but will produce the same information. The `beginReadDocument` method is available for your convenience.
|
|
324
398
|
|
|
325
399
|
### Using Prebuilt Models
|
|
326
400
|
|
|
327
|
-
The `
|
|
401
|
+
The `beginAnalyzeDocument` method also supports extracting fields from certain types of common documents such as receipts, invoices, business cards, and identity documents using prebuilt models provided by the Form Recognizer service. The prebuilt models may be provided either as model ID strings (the same as custom document models) or using a `DocumentModel` object. When using a `DocumentModel`, the Form Recognizer SDK for JavaScript provides a much stronger TypeScript type for the resulting extracted documents based on the model's schema, and it will be converted to use JavaScript naming conventions.
|
|
402
|
+
|
|
403
|
+
<a id="prebuiltmodels-deprecation"></a>
|
|
404
|
+
**Deprecation Warning** ⚠️: The built in `PrebuiltModels` are deprecated as of version `4.0.0-beta.4`. Prior to a stable release of version 4.0.0, we will replace `PrebuiltModels` with an out-of-tree solution that provides the same strongly-typed functionality. This will enable us to continue to provide timely updates and ensure stability as the number of supported prebuilt models increases and as their capabilities are enhanced.
|
|
328
405
|
|
|
329
406
|
For example, the following code shows how to use `PrebuiltModels.Receipt` to extract a strongly-typed receipt object from an input.
|
|
330
407
|
|
|
331
408
|
```javascript
|
|
332
|
-
|
|
409
|
+
const {
|
|
410
|
+
DocumentAnalysisClient,
|
|
411
|
+
PrebuiltModels,
|
|
412
|
+
AzureKeyCredential,
|
|
413
|
+
} = require("@azure/ai-form-recognizer");
|
|
333
414
|
|
|
334
415
|
const fs = require("fs");
|
|
335
416
|
|
|
@@ -343,33 +424,32 @@ async function main() {
|
|
|
343
424
|
const client = new DocumentAnalysisClient(endpoint, new AzureKeyCredential(apiKey));
|
|
344
425
|
|
|
345
426
|
// The PrebuiltModels.Receipt `DocumentModel` encodes both the model ID and a stronger return type for the operation
|
|
346
|
-
const poller = await client.
|
|
347
|
-
onProgress: ({status}
|
|
427
|
+
const poller = await client.beginAnalyzeDocument(PrebuiltModels.Receipt, readStream, {
|
|
428
|
+
onProgress: ({ status }) => {
|
|
348
429
|
console.log(`status: ${status}`);
|
|
349
|
-
}
|
|
430
|
+
},
|
|
350
431
|
});
|
|
351
432
|
|
|
352
|
-
const {
|
|
433
|
+
const {
|
|
434
|
+
documents: [receiptDocument],
|
|
435
|
+
} = await poller.pollUntilDone();
|
|
436
|
+
|
|
437
|
+
// The fields of the document constitute the extracted receipt data.
|
|
438
|
+
const receipt = receiptDocument.fields;
|
|
353
439
|
|
|
354
440
|
if (receipt === undefined) {
|
|
355
441
|
throw new Error("Expected at least one receipt in analysis result.");
|
|
356
442
|
}
|
|
357
443
|
|
|
358
|
-
// The fields of the document constitute the extracted receipt data.
|
|
359
|
-
const receipt = receiptDocument.fields;
|
|
360
|
-
|
|
361
444
|
console.log(`Receipt data (${receiptDocument.docType})`);
|
|
362
|
-
console.log(" Type:", receipt.receiptType?.value);
|
|
363
445
|
console.log(" Merchant Name:", receipt.merchantName?.value);
|
|
364
|
-
console.log(" Transaction Date:", receipt.transactionDate?.value);
|
|
365
446
|
|
|
366
447
|
// The items of the receipt are an example of a `DocumentArrayValue`
|
|
367
448
|
if (receipt.items !== undefined) {
|
|
368
|
-
console.log("
|
|
369
|
-
for (const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
console.log(" - Name:", item.name?.value, `(confidence ${itemField.confidence})`);
|
|
449
|
+
console.log("Items:");
|
|
450
|
+
for (const { properties: item } of receipt.items.values) {
|
|
451
|
+
console.log("- Description:", item.description?.value);
|
|
452
|
+
console.log(" Total Price:", item.totalPrice?.value);
|
|
373
453
|
}
|
|
374
454
|
}
|
|
375
455
|
|
|
@@ -387,10 +467,15 @@ Alternatively, as mentioned above, instead of using `PrebuiltDocuments.Receipt`,
|
|
|
387
467
|
|
|
388
468
|
You are not limited to receipts! There are a few prebuilt models to choose from, with more on the way. Each prebuilt model has its own set of supported fields:
|
|
389
469
|
|
|
390
|
-
- Receipts, using `PrebuiltModels.Receipt` or the prebuilt receipt model ID `"prebuilt-receipt"
|
|
391
|
-
- Business cards, using `PrebuiltModels.BusinessCard` or its model ID `"prebuilt-businessCard"
|
|
392
|
-
- Invoices, using `PrebuiltModels.Invoice` or its model ID `"prebuilt-invoice"
|
|
393
|
-
- Identity Documents (such as driver licenses and passports), using `PrebuiltModels.IdentityDocument` or its model ID `"prebuilt-idDocument"
|
|
470
|
+
- Receipts, using `PrebuiltModels.Receipt` or the prebuilt receipt model ID `"prebuilt-receipt"`.
|
|
471
|
+
- Business cards, using `PrebuiltModels.BusinessCard` or its model ID `"prebuilt-businessCard"`.
|
|
472
|
+
- Invoices, using `PrebuiltModels.Invoice` or its model ID `"prebuilt-invoice"`.
|
|
473
|
+
- Identity Documents (such as driver licenses and passports), using `PrebuiltModels.IdentityDocument` or its model ID `"prebuilt-idDocument"`.
|
|
474
|
+
- W2 Tax Forms (United States), using `PrebuiltModels.TaxUsW2` or its model ID `"prebuilt-tax.us.w2"`.
|
|
475
|
+
- Health Insurance Cards (United States), using `PrebuiltModels.HealthInsuranceCardUs` or its model ID `"prebuilt-healthInsuranceCard.us"`.
|
|
476
|
+
- Vaccination Cards (currently supports US COVID-19 vaccination cards), using `PrebuiltModels.VaccinationCard` or its model ID `"prebuilt-vaccinationCard"`.
|
|
477
|
+
|
|
478
|
+
For information about the fields of these models, see [the service's documentation of the available prebuilt models](https://aka.ms/azsdk/formrecognizer/models).
|
|
394
479
|
|
|
395
480
|
The fields of all prebuilt document models may also be accessed programmatically using the `getModel` method (by their model IDs) of `DocumentModelAdministrationClient` and inspecting the `docTypes` field in the result.
|
|
396
481
|
|
|
@@ -401,7 +486,10 @@ The SDK also supports creating models, using `DocumentModelAdministrationClient`
|
|
|
401
486
|
For example, the following program builds a custom document model using a SAS-encoded URL to a pre-existing Azure Storage container:
|
|
402
487
|
|
|
403
488
|
```javascript
|
|
404
|
-
|
|
489
|
+
const {
|
|
490
|
+
DocumentModelAdministrationClient,
|
|
491
|
+
AzureKeyCredential,
|
|
492
|
+
} = require("@azure/ai-form-recognizer");
|
|
405
493
|
|
|
406
494
|
async function main() {
|
|
407
495
|
const endpoint = "<cognitive services endpoint>";
|
|
@@ -415,7 +503,10 @@ async function main() {
|
|
|
415
503
|
|
|
416
504
|
// You must provide the model ID. It can be any text that does not start with "prebuilt-".
|
|
417
505
|
// For example, you could provide a randomly generated GUID using the "uuid" package.
|
|
418
|
-
|
|
506
|
+
// The second parameter is the SAS-encoded URL to an Azure Storage container with the training documents.
|
|
507
|
+
// The third parameter is the build mode: one of "template" (the only mode prior to 4.0.0-beta.3) or "neural".
|
|
508
|
+
// See https://aka.ms/azsdk/formrecognizer/buildmode for more information about build modes.
|
|
509
|
+
const poller = await trainingClient.beginBuildModel("<model ID>", containerSasUrl, "template", {
|
|
419
510
|
// The model description is optional and can be any text.
|
|
420
511
|
description: "This is my new model!",
|
|
421
512
|
onProgress: ({ status }) => {
|
|
@@ -458,7 +549,10 @@ main().catch((err) => {
|
|
|
458
549
|
`DocumentModelAdministrationClient` also provides several methods for managing models. The following example shows how to iterate through the models in a Form Recognizer resource (this will include both custom models in the resource as well as prebuilt models that are common to all resources), get a model by ID, and delete a model.
|
|
459
550
|
|
|
460
551
|
```javascript
|
|
461
|
-
|
|
552
|
+
const {
|
|
553
|
+
DocumentModelAdministrationClient,
|
|
554
|
+
AzureKeyCredential,
|
|
555
|
+
} = require("@azure/ai-form-recognizer");
|
|
462
556
|
|
|
463
557
|
async function main() {
|
|
464
558
|
const endpoint = "<cognitive services endpoint>";
|
|
@@ -511,7 +605,7 @@ For information about the error messages and codes produced by the Form Recogniz
|
|
|
511
605
|
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
|
|
512
606
|
|
|
513
607
|
```javascript
|
|
514
|
-
|
|
608
|
+
const { setLogLevel } = require("@azure/logger");
|
|
515
609
|
|
|
516
610
|
setLogLevel("info");
|
|
517
611
|
```
|