diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index e69559f2d7d6..9698024a7db5 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -147,10 +147,10 @@ so they're tailored to your forms. You should only recognize forms of the same f ```java String analyzeFilePath = "{file_source_url}"; String modelId = "{custom_trained_model_id}"; -SyncPoller> recognizeFormPoller = +SyncPoller> recognizeFormPoller = formRecognizerClient.beginRecognizeCustomFormsFromUrl(analyzeFilePath, modelId); -IterableStream recognizedForms = recognizeFormPoller.getFinalResult(); +List recognizedForms = recognizeFormPoller.getFinalResult(); recognizedForms.forEach(form -> { System.out.println("----------- Recognized Form -----------"); @@ -169,10 +169,10 @@ Recognize text and table structures, along with their bounding box coordinates, ```java String analyzeFilePath = "{file_source_url}"; -SyncPoller> recognizeLayoutPoller = +SyncPoller> recognizeLayoutPoller = formRecognizerClient.beginRecognizeContentFromUrl(analyzeFilePath); -IterableStream layoutPageResults = recognizeLayoutPoller.getFinalResult(); +List layoutPageResults = recognizeLayoutPoller.getFinalResult(); layoutPageResults.forEach(formPage -> { // Table information @@ -197,9 +197,9 @@ Recognize data from a USA sales receipts using a prebuilt model. ```java String receiptSourceUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/media" + "/contoso-allinone.jpg"; -SyncPoller> syncPoller = +SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptSourceUrl); -IterableStream receiptPageResults = syncPoller.getFinalResult(); +List receiptPageResults = syncPoller.getFinalResult(); receiptPageResults.forEach(recognizedReceipt -> { USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java index 8d82265d60d7..00aba80e359a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClient.java @@ -22,7 +22,6 @@ import com.azure.core.http.HttpPipeline; import com.azure.core.http.rest.SimpleResponse; import com.azure.core.util.CoreUtils; -import com.azure.core.util.IterableStream; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.polling.LongRunningOperationStatus; import com.azure.core.util.polling.PollResponse; @@ -99,7 +98,7 @@ public FormRecognizerServiceVersion getServiceVersion() { * or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeCustomFormsFromUrl(String fileSourceUrl, String modelId) { return beginRecognizeCustomFormsFromUrl(fileSourceUrl, modelId, false, null); } @@ -123,11 +122,11 @@ public FormRecognizerServiceVersion getServiceVersion() { * or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeCustomFormsFromUrl(String fileSourceUrl, String modelId, boolean includeTextDetails, Duration pollInterval) { final Duration interval = pollInterval != null ? pollInterval : DEFAULT_DURATION; - return new PollerFlux>( + return new PollerFlux>( interval, analyzeFormActivationOperation(fileSourceUrl, modelId, includeTextDetails), createAnalyzeFormPollOperation(modelId), @@ -156,7 +155,7 @@ public FormRecognizerServiceVersion getServiceVersion() { * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeCustomForms(Flux data, String modelId, long length, FormContentType formContentType) { return beginRecognizeCustomForms(data, modelId, length, formContentType, false, null); } @@ -185,11 +184,11 @@ public FormRecognizerServiceVersion getServiceVersion() { * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeCustomForms(Flux data, String modelId, long length, FormContentType formContentType, boolean includeTextDetails, Duration pollInterval) { final Duration interval = pollInterval != null ? pollInterval : DEFAULT_DURATION; - return new PollerFlux>( + return new PollerFlux>( interval, analyzeFormStreamActivationOperation(data, modelId, length, formContentType, includeTextDetails), createAnalyzeFormPollOperation(modelId), @@ -212,7 +211,7 @@ public FormRecognizerServiceVersion getServiceVersion() { * or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> beginRecognizeContentFromUrl(String fileSourceUrl) { + public PollerFlux> beginRecognizeContentFromUrl(String fileSourceUrl) { return beginRecognizeContentFromUrl(fileSourceUrl, null); } @@ -233,10 +232,10 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeContentFromUrl(String sourceUrl, Duration pollInterval) { final Duration interval = pollInterval != null ? pollInterval : DEFAULT_DURATION; - return new PollerFlux>(interval, + return new PollerFlux>(interval, contentAnalyzeActivationOperation(sourceUrl), extractContentPollOperation(), (activationResponse, context) -> monoError(logger, @@ -264,7 +263,7 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> beginRecognizeContent( + public PollerFlux> beginRecognizeContent( Flux data, long length, FormContentType formContentType) { return beginRecognizeContent(data, length, formContentType, null); } @@ -291,7 +290,7 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> beginRecognizeContent( + public PollerFlux> beginRecognizeContent( Flux data, long length, FormContentType formContentType, Duration pollInterval) { return new PollerFlux<>( pollInterval != null ? pollInterval : DEFAULT_DURATION, @@ -317,7 +316,7 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeReceiptsFromUrl(String sourceUrl) { return beginRecognizeReceiptsFromUrl(sourceUrl, false, null); } @@ -340,10 +339,10 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> + public PollerFlux> beginRecognizeReceiptsFromUrl(String sourceUrl, boolean includeTextDetails, Duration pollInterval) { final Duration interval = pollInterval != null ? pollInterval : DEFAULT_DURATION; - return new PollerFlux>(interval, + return new PollerFlux>(interval, receiptAnalyzeActivationOperation(sourceUrl, includeTextDetails), extractReceiptPollOperation(), (activationResponse, context) -> monoError(logger, @@ -371,7 +370,7 @@ public PollerFlux> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> beginRecognizeReceipts( + public PollerFlux> beginRecognizeReceipts( Flux data, long length, FormContentType formContentType) { return beginRecognizeReceipts(data, length, formContentType, false, null); } @@ -399,7 +398,7 @@ public PollerFlux> beginRecog * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public PollerFlux> beginRecognizeReceipts( + public PollerFlux> beginRecognizeReceipts( Flux data, long length, FormContentType formContentType, boolean includeTextDetails, Duration pollInterval) { @@ -466,7 +465,7 @@ private Function, Mono> receipt }; } - private Function, Mono>> + private Function, Mono>> fetchExtractReceiptResult(boolean includeTextDetails) { return (pollingContext) -> { try { @@ -474,8 +473,7 @@ private Function, Mono> receipt return service.getAnalyzeReceiptResultWithResponseAsync(resultUid) .map(modelSimpleResponse -> { throwIfAnalyzeStatusInvalid(modelSimpleResponse); - return toReceipt(modelSimpleResponse.getValue().getAnalyzeResult(), - includeTextDetails); + return toReceipt(modelSimpleResponse.getValue().getAnalyzeResult(), includeTextDetails); }); } catch (RuntimeException ex) { return monoError(logger, ex); @@ -536,7 +534,7 @@ private Function, Mono> content }; } - private Function, Mono>> + private Function, Mono>> fetchExtractContentResult() { return (pollingContext) -> { try { @@ -544,8 +542,7 @@ private Function, Mono> content return service.getAnalyzeLayoutResultWithResponseAsync(resultUid) .map(modelSimpleResponse -> { throwIfAnalyzeStatusInvalid(modelSimpleResponse); - return new IterableStream<>( - toRecognizedLayout(modelSimpleResponse.getValue().getAnalyzeResult(), true)); + return toRecognizedLayout(modelSimpleResponse.getValue().getAnalyzeResult(), true); }); } catch (RuntimeException ex) { return monoError(logger, ex); @@ -553,7 +550,7 @@ private Function, Mono> content }; } - private Function, Mono>> + private Function, Mono>> fetchAnalyzeFormResultOperation(String modelId, boolean includeTextDetails) { return (pollingContext) -> { try { @@ -563,8 +560,7 @@ private Function, Mono> content return service.getAnalyzeFormResultWithResponseAsync(modelUid, resultUid) .map(modelSimpleResponse -> { throwIfAnalyzeStatusInvalid(modelSimpleResponse); - return new IterableStream<>(toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(), - includeTextDetails)); + return toRecognizedForm(modelSimpleResponse.getValue().getAnalyzeResult(), includeTextDetails); }); } catch (RuntimeException ex) { return monoError(logger, ex); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java index 9b85bc90f2c0..609d1f3676b8 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/FormRecognizerClient.java @@ -12,13 +12,13 @@ import com.azure.core.annotation.ReturnType; import com.azure.core.annotation.ServiceClient; import com.azure.core.annotation.ServiceMethod; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; import reactor.core.publisher.Flux; import java.io.InputStream; import java.nio.ByteBuffer; import java.time.Duration; +import java.util.List; /** * This class provides a synchronous client that contains all the operations that apply to Azure Form Recognizer. @@ -60,7 +60,7 @@ public final class FormRecognizerClient { * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeCustomFormsFromUrl(String fileSourceUrl, String modelId) { return beginRecognizeCustomFormsFromUrl(fileSourceUrl, modelId, false, null); } @@ -84,7 +84,7 @@ public final class FormRecognizerClient { * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeCustomFormsFromUrl(String fileSourceUrl, String modelId, boolean includeTextDetails, Duration pollInterval) { return client.beginRecognizeCustomFormsFromUrl(fileSourceUrl, modelId, includeTextDetails, pollInterval) @@ -109,7 +109,7 @@ public final class FormRecognizerClient { * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeCustomForms(InputStream data, String modelId, long length, FormContentType formContentType) { return beginRecognizeCustomForms(data, modelId, length, formContentType, false, null); } @@ -135,7 +135,7 @@ public final class FormRecognizerClient { * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeCustomForms(InputStream data, String modelId, long length, FormContentType formContentType, boolean includeTextDetails, Duration pollInterval) { Flux buffer = Utility.toFluxByteBuffer(data); @@ -158,7 +158,7 @@ public final class FormRecognizerClient { * or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> beginRecognizeContentFromUrl(String fileSourceUrl) { + public SyncPoller> beginRecognizeContentFromUrl(String fileSourceUrl) { return beginRecognizeContentFromUrl(fileSourceUrl, null); } @@ -178,7 +178,7 @@ public SyncPoller> beginRecognizeConte * failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeContentFromUrl(String sourceUrl, Duration pollInterval) { return client.beginRecognizeContentFromUrl(sourceUrl, pollInterval).getSyncPoller(); } @@ -199,7 +199,7 @@ public SyncPoller> beginRecognizeConte * been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeContent(InputStream data, long length, FormContentType formContentType) { return beginRecognizeContent(data, length, formContentType, null); } @@ -223,7 +223,7 @@ public SyncPoller> beginRecognizeConte * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeContent(InputStream data, long length, FormContentType formContentType, Duration pollInterval) { // TODO: #9248 should be able to infer form content type Flux buffer = Utility.toFluxByteBuffer(data); @@ -246,7 +246,7 @@ public SyncPoller> beginRecognizeConte * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceiptsFromUrl(String sourceUrl) { return beginRecognizeReceiptsFromUrl(sourceUrl, false, null); } @@ -269,7 +269,7 @@ public SyncPoller> beginRecognizeConte * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceiptsFromUrl(String sourceUrl, boolean includeTextDetails, Duration pollInterval) { return client.beginRecognizeReceiptsFromUrl(sourceUrl, includeTextDetails, pollInterval).getSyncPoller(); } @@ -291,7 +291,7 @@ public SyncPoller> beginRecognizeConte * has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceipts(InputStream data, long length, FormContentType formContentType) { return beginRecognizeReceipts(data, length, formContentType, false, null); } @@ -316,7 +316,7 @@ public SyncPoller> beginRecognizeConte * has completed, has failed, or has been cancelled. */ @ServiceMethod(returns = ReturnType.COLLECTION) - public SyncPoller> + public SyncPoller> beginRecognizeReceipts(InputStream data, long length, FormContentType formContentType, boolean includeTextDetails, Duration pollInterval) { Flux buffer = Utility.toFluxByteBuffer(data); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java index 34522ccddaf9..45dd9bbe1afe 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/Transforms.java @@ -26,7 +26,6 @@ import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.core.util.CoreUtils; -import com.azure.core.util.IterableStream; import com.azure.core.util.logging.ClientLogger; import java.util.ArrayList; @@ -57,7 +56,7 @@ private Transforms() { * @param analyzeResult The service returned result for analyze custom forms. * @param includeTextDetails Boolean to indicate if to set reference elements data on fields. * - * @return The IterableStream of {@code RecognizedForm}. + * @return The List of {@code RecognizedForm}. */ static List toRecognizedForm(AnalyzeResult analyzeResult, boolean includeTextDetails) { List readResults = analyzeResult.getReadResults(); @@ -84,8 +83,7 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea extractedFieldMap, documentResultItem.getDocType(), pageRange, - new IterableStream<>(formPages.subList(pageRange.getStartPageNumber() - 1, - pageRange.getEndPageNumber())))); + formPages.subList(pageRange.getStartPageNumber() - 1, pageRange.getEndPageNumber()))); } } else { extractedFormList = new ArrayList<>(); @@ -103,7 +101,7 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea extractedFieldMap, formType.toString(), new PageRange(pageNumber, pageNumber), - new IterableStream<>(Collections.singletonList(formPages.get(index))))); + Collections.singletonList(formPages.get(index)))); })); } return extractedFormList; @@ -115,14 +113,13 @@ static List toRecognizedForm(AnalyzeResult analyzeResult, boolea * @param analyzeResult The service returned result for analyze receipts. * @param includeTextDetails Boolean to indicate if to set reference elements data on fields. * - * @return The IterableStream of {@code RecognizedReceipt}. + * @return The List of {@code RecognizedReceipt}. */ - static IterableStream toReceipt(AnalyzeResult analyzeResult, boolean includeTextDetails) { - return new IterableStream<>( - toRecognizedForm(analyzeResult, includeTextDetails).stream() - .map(recognizedForm -> - new RecognizedReceipt("en-US", recognizedForm)) - .collect(Collectors.toList())); + static List toReceipt(AnalyzeResult analyzeResult, boolean includeTextDetails) { + return toRecognizedForm(analyzeResult, includeTextDetails) + .stream() + .map(recognizedForm -> new RecognizedReceipt("en-US", recognizedForm)) + .collect(Collectors.toList()); } /** @@ -131,7 +128,7 @@ static IterableStream toReceipt(AnalyzeResult analyzeResult, * @param analyzeResult The service returned result for analyze layouts. * @param includeTextDetails Boolean to indicate if to set reference elements data on fields. * - * @return The IterableStream of {@code FormPage}. + * @return The List of {@code FormPage}. */ static List toRecognizedLayout(AnalyzeResult analyzeResult, boolean includeTextDetails) { List readResults = analyzeResult.getReadResults(); @@ -173,7 +170,8 @@ static List getPageTables(PageResult pageResultItem, List return pageResultItem.getTables().stream() .map(dataTable -> new FormTable(dataTable.getRows(), dataTable.getColumns(), - new IterableStream<>(dataTable.getCells().stream() + dataTable.getCells() + .stream() .map(dataTableCell -> new FormTableCell( dataTableCell.getRowIndex(), dataTableCell.getColumnIndex(), dataTableCell.getRowSpan(), dataTableCell.getColumnSpan(), @@ -182,7 +180,7 @@ static List getPageTables(PageResult pageResultItem, List dataTableCell.isHeader() == null ? false : dataTableCell.isHeader(), dataTableCell.isFooter() == null ? false : dataTableCell.isFooter(), pageNumber, setReferenceElements(dataTableCell.getElements(), readResults, pageNumber))) - .collect(Collectors.toList())))) + .collect(Collectors.toList()))) .collect(Collectors.toList()); } @@ -221,7 +219,7 @@ private static Map> getUnlabeledFieldMap(DocumentResult doc if (fieldValue != null) { Integer pageNumber = fieldValue.getPage(); FieldText labelText = new FieldText(key, null, pageNumber, null); - IterableStream formContentList = null; + List formContentList = null; if (includeTextDetails) { formContentList = setReferenceElements(fieldValue.getElements(), readResults, pageNumber); } @@ -361,8 +359,8 @@ private static FormPage getFormPage(ReadResult readResultItem, List p readResultItem.getAngle(), DimensionUnit.fromString(readResultItem.getUnit().toString()), readResultItem.getWidth(), - new IterableStream(perPageLineList), - new IterableStream(perPageTableList) + perPageLineList, + perPageTableList ); } @@ -383,8 +381,8 @@ private static Map> getLabeledFieldMap(boolean includeTextD Map> formFieldMap = new TreeMap<>(); List keyValuePairs = pageResultItem.getKeyValuePairs(); forEachWithIndex(keyValuePairs, ((index, keyValuePair) -> { - IterableStream formKeyContentList = null; - IterableStream formValueContentList = null; + List formKeyContentList = null; + List formValueContentList = null; if (includeTextDetails) { formKeyContentList = setReferenceElements(keyValuePair.getKey().getElements(), readResults, pageNumber); formValueContentList = setReferenceElements(keyValuePair.getValue().getElements(), readResults, @@ -409,10 +407,10 @@ private static Map> getLabeledFieldMap(boolean includeTextD * * @return The list if referenced elements. */ - private static IterableStream setReferenceElements(List elements, + private static List setReferenceElements(List elements, List readResults, Integer pageNumber) { if (CoreUtils.isNullOrEmpty(elements)) { - return IterableStream.of(null); + return new ArrayList(); } List formContentList = new ArrayList<>(); elements.forEach(elementString -> { @@ -441,7 +439,7 @@ private static IterableStream setReferenceElements(List ele formContentList.add(lineElement); } }); - return new IterableStream<>(formContentList); + return formContentList; } /** @@ -452,14 +450,14 @@ private static IterableStream setReferenceElements(List ele * * @return The list of {@code FormWord words}. */ - private static IterableStream toWords(List words, Integer pageNumber) { - return new IterableStream<>(words.stream() + private static List toWords(List words, Integer pageNumber) { + return words.stream() .map(textWord -> new FormWord( textWord.getText(), toBoundingBox(textWord.getBoundingBox()), pageNumber, setDefaultConfidenceValue(textWord.getConfidence())) - ).collect(Collectors.toList())); + ).collect(Collectors.toList()); } /** diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldText.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldText.java index 7c795bdbb3a9..e012f907dee1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldText.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FieldText.java @@ -4,7 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; + +import java.util.List; /** * The FieldText model. @@ -15,7 +16,7 @@ public final class FieldText extends FormContent { /** * The list of text element references for the field value. */ - private final IterableStream textContent; + private final List textContent; /** * Creates raw OCR FieldText item. @@ -26,7 +27,7 @@ public final class FieldText extends FormContent { * @param textContent The list of text element references when includeTextDetails is set to true. */ public FieldText(String text, BoundingBox boundingBox, Integer pageNumber, - final IterableStream textContent) { + final List textContent) { super(text, boundingBox, pageNumber, null); this.textContent = textContent; } @@ -60,7 +61,7 @@ public Integer getPageNumber() { * * @return The list of reference elements. */ - public IterableStream getTextContent() { + public List getTextContent() { return this.textContent; } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormLine.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormLine.java index bd6b8bcbc12d..7afb256317ef 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormLine.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormLine.java @@ -4,7 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; + +import java.util.List; /** * The FormLine model. @@ -15,7 +16,7 @@ public final class FormLine extends FormContent { /* * List of words in the text line. */ - private final IterableStream formWords; + private final List formWords; /** * Creates raw OCR item. @@ -27,7 +28,7 @@ public final class FormLine extends FormContent { * @param formWords The list of word element references. */ public FormLine(String text, BoundingBox boundingBox, Integer pageNumber, - final IterableStream formWords) { + final List formWords) { super(text, boundingBox, pageNumber, TextContentType.LINE); this.formWords = formWords; } @@ -37,7 +38,7 @@ public FormLine(String text, BoundingBox boundingBox, Integer pageNumber, * * @return the words value. */ - public IterableStream getFormWords() { + public List getFormWords() { return this.formWords; } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormPage.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormPage.java index 0e4aa2f0a388..bbef3824781c 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormPage.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormPage.java @@ -4,7 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; + +import java.util.List; /** * The FormPage model. @@ -20,12 +21,12 @@ public final class FormPage { /* * When includeTextDetails is set to true, a list of recognized text lines. */ - private final IterableStream lines; + private final List lines; /* * List of data tables extracted from the page. */ - private final IterableStream tables; + private final List tables; /* * The general orientation of the text in clockwise direction, measured in @@ -55,13 +56,13 @@ public final class FormPage { * @param tables List of data tables extracted from the page. */ public FormPage(final float height, final float textAngle, final DimensionUnit unit, - final float width, final IterableStream lines, final IterableStream tables) { + final float width, final List lines, final List tables) { this.height = height; this.textAngle = textAngle; this.unit = unit; this.width = width; - this.lines = IterableStream.of(lines); - this.tables = IterableStream.of(tables); + this.lines = lines; + this.tables = tables; } /** @@ -80,7 +81,7 @@ public float getHeight() { * * @return the lines value. */ - public IterableStream getLines() { + public List getLines() { return this.lines; } @@ -89,7 +90,7 @@ public IterableStream getLines() { * * @return the tables value. */ - public IterableStream getTables() { + public List getTables() { return this.tables; } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTable.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTable.java index cd6489111ea1..31bf99048f62 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTable.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTable.java @@ -4,7 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; + +import java.util.List; /** * The FormTable model. @@ -25,7 +26,7 @@ public final class FormTable { /* * List of cells contained in the table. */ - private final IterableStream cells; + private final List cells; /** * Constructs a FormTable object. @@ -34,7 +35,7 @@ public final class FormTable { * @param columnCount Number of columns. * @param cells ist of cells contained in the table. */ - public FormTable(final int rowCount, final int columnCount, final IterableStream cells) { + public FormTable(final int rowCount, final int columnCount, final List cells) { this.rowCount = rowCount; this.columnCount = columnCount; this.cells = cells; @@ -63,7 +64,7 @@ public int getColumnCount() { * * @return the cells value. */ - public IterableStream getCells() { + public List getCells() { return this.cells; } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTableCell.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTableCell.java index fdddfe7f1ca9..f9b9ad13508a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTableCell.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/FormTableCell.java @@ -4,7 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; + +import java.util.List; /** * The FormTableCell model. @@ -41,7 +42,7 @@ public final class FormTableCell extends FormContent { * When includeTextDetails is set to true, a list of references to the text * elements constituting this table cell. */ - private final IterableStream elements; + private final List elements; /* * Is the current cell a header cell? @@ -71,7 +72,7 @@ public final class FormTableCell extends FormContent { public FormTableCell(final int rowIndex, final int columnIndex, final Integer rowSpan, final Integer columnSpan, final String text, final BoundingBox boundingBox, final float confidence, final boolean isHeader, final boolean isFooter, final int pageNumber, - final IterableStream textContent) { + final List textContent) { super(text, boundingBox, pageNumber, null); this.rowIndex = rowIndex; this.columnIndex = columnIndex; @@ -159,7 +160,7 @@ public Integer getColumnSpan() { * * @return the elements value. */ - public IterableStream getElements() { + public List getElements() { return this.elements; } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java index bfa2954fbfaf..323bdcdef80e 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/main/java/com/azure/ai/formrecognizer/models/RecognizedForm.java @@ -4,8 +4,8 @@ package com.azure.ai.formrecognizer.models; import com.azure.core.annotation.Immutable; -import com.azure.core.util.IterableStream; +import java.util.List; import java.util.Map; /** @@ -32,7 +32,7 @@ public final class RecognizedForm { /* * List of extracted pages from the form. */ - private final IterableStream pages; + private final List pages; /** * Constructs a RecognizedForm object. @@ -43,7 +43,7 @@ public final class RecognizedForm { * @param pages List of extracted pages from the form. */ public RecognizedForm(final Map> fields, final String formType, final PageRange pageRange, - final IterableStream pages) { + final List pages) { this.fields = fields; this.formType = formType; this.pageRange = pageRange; @@ -82,7 +82,7 @@ public PageRange getPageRange() { * * @return the pages value. */ - public IterableStream getPages() { + public List getPages() { return this.pages; } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java index 383ac95ea3b7..6e362721560f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledData.java @@ -6,11 +6,11 @@ import com.azure.ai.formrecognizer.models.FormContentType; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.List; /** * Sample to show the differences in output that arise when RecognizeCustomForms @@ -36,10 +36,10 @@ public static void main(String[] args) throws IOException { File analyzeFile = new File("../formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/" + "forms/Invoice_6.pdf"); - IterableStream formsWithLabeledModel = + List formsWithLabeledModel = client.beginRecognizeCustomForms(new FileInputStream(analyzeFile), "{labeled_model_Id}", analyzeFile.length(), FormContentType.APPLICATION_PDF, true, null).getFinalResult(); - IterableStream formsWithUnlabeledModel = + List formsWithUnlabeledModel = client.beginRecognizeCustomForms(new FileInputStream(analyzeFile), "{unlabeled_model_Id}", analyzeFile.length(), FormContentType.APPLICATION_PDF).getFinalResult(); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java index 6ea47c7df417..b5afe4a0885a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/AdvancedDiffLabeledUnlabeledDataAsync.java @@ -7,7 +7,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; @@ -15,6 +14,7 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.util.List; import java.util.concurrent.TimeUnit; import static com.azure.ai.formrecognizer.implementation.Utility.toFluxByteBuffer; @@ -44,12 +44,12 @@ public static void main(String[] args) throws IOException { + "forms/Invoice_6.pdf"); byte[] fileContent = Files.readAllBytes(analyzeFile.toPath()); - PollerFlux> labeledCustomFormPoller = + PollerFlux> labeledCustomFormPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(new ByteArrayInputStream(fileContent)), "{labeled_model_Id}", analyzeFile.length(), FormContentType.APPLICATION_PDF, true, null); - PollerFlux> unlabeledCustomFormPoller = + PollerFlux> unlabeledCustomFormPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(new ByteArrayInputStream(fileContent)), "{unlabeled_model_Id}", analyzeFile.length(), FormContentType.APPLICATION_PDF); - Mono> labeledDataResult = labeledCustomFormPoller + Mono> labeledDataResult = labeledCustomFormPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -61,7 +61,7 @@ public static void main(String[] args) throws IOException { } }); - Mono> unlabeledDataResult = unlabeledCustomFormPoller + Mono> unlabeledDataResult = unlabeledCustomFormPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -134,22 +134,4 @@ public static void main(String[] args) throws IOException { e.printStackTrace(); } } - - private static void printFieldData(IterableStream recognizedForms) { - recognizedForms.forEach(labeledForm -> labeledForm.getFields().forEach((label, formField) -> { - // With your labeled custom model, you will not get back label data but will get back value data - // This is because your custom model didn't have to use any machine learning to deduce the label, - // the label was directly provided to it. - final StringBuilder boundingBoxStr = new StringBuilder(); - if (formField.getValueText().getBoundingBox() != null) { - formField.getValueText().getBoundingBox().getPoints().forEach(point -> - boundingBoxStr.append(String.format("[%2f, %.2f]", point.getX(), point.getY()))); - } - // The unlabeled custom model will also include data about your labels - System.out.printf("Field %s has value %s based on %s within bounding box %s with a confidence score " - + "of %.2f.%n", - label, formField.getFieldValue(), formField.getValueText().getText(), boundingBoxStr, - formField.getConfidence()); - })); - } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java index 81beb2d11e09..4601a26421c1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxes.java @@ -8,9 +8,10 @@ import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.TextContentType; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; +import java.util.List; + /* * Sample to get detailed information to visualize the outlines of form content and fields, * which can be used for manual validation and drawing UI as part of an application. @@ -31,14 +32,15 @@ public static void main(String[] args) { String modelId = "{model_Id}"; String filePath = "{analyze_file_path}"; - SyncPoller> trainingPoller = + SyncPoller> trainingPoller = client.beginRecognizeCustomFormsFromUrl(filePath, modelId, true, null); - IterableStream recognizedForms = trainingPoller.getFinalResult(); + List recognizedForms = trainingPoller.getFinalResult(); System.out.println("--------RECOGNIZING FORM --------"); - recognizedForms.forEach(recognizedForm -> { - System.out.printf("Form has type: %s%n", recognizedForm.getFormType()); + for (int i = 0; i < recognizedForms.size(); i++) { + final RecognizedForm recognizedForm = recognizedForms.get(i); + System.out.printf("Form %s has type: %s%n", i, recognizedForm.getFormType()); // each field is of type FormField // The value of the field can also be a FormField, or a list of FormFields // In our sample, it is not. @@ -80,6 +82,6 @@ public static void main(String[] args) { System.out.println(); }); }); - }); + } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java index f4525add2062..40bb0b02817b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/GetBoundingBoxesAsync.java @@ -8,10 +8,10 @@ import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.ai.formrecognizer.models.TextContentType; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; +import java.util.List; import java.util.concurrent.TimeUnit; /* @@ -34,10 +34,10 @@ public static void main(String[] args) { String modelId = "{model_Id}"; String filePath = "{file_source_url}"; - PollerFlux> recognizeFormPoller = + PollerFlux> recognizeFormPoller = client.beginRecognizeCustomFormsFromUrl(filePath, modelId, true, null); - Mono> recognizeFormResult = recognizeFormPoller + Mono> recognizeFormResult = recognizeFormPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -50,52 +50,55 @@ public static void main(String[] args) { }); System.out.println("--------RECOGNIZING FORM --------"); - recognizeFormResult.subscribe(recognizedForms -> recognizedForms.forEach(recognizedForm -> { - System.out.printf("Form has type: %s%n", recognizedForm.getFormType()); - // each field is of type FormField - // The value of the field can also be a FormField, or a list of FormFields - // In our sample, it is not. - recognizedForm.getFields().forEach((fieldText, fieldValue) -> { - System.out.printf("Field %s has value %s based on %s with a confidence score " - + "of %.2f.%n", - fieldText, fieldValue.getFieldValue(), fieldValue.getValueText().getText(), - fieldValue.getConfidence()); - }); + recognizeFormResult.subscribe(recognizedForms -> { + for (int i = 0; i < recognizedForms.size(); i++) { + final RecognizedForm recognizedForm = recognizedForms.get(i); + System.out.printf("Form %s has type: %s%n", i, recognizedForm.getFormType()); + // each field is of type FormField + // The value of the field can also be a FormField, or a list of FormFields + // In our sample, it is not. + recognizedForm.getFields().forEach((fieldText, fieldValue) -> { + System.out.printf("Field %s has value %s based on %s with a confidence score " + + "of %.2f.%n", + fieldText, fieldValue.getFieldValue(), fieldValue.getValueText().getText(), + fieldValue.getConfidence()); + }); - // Page Information - recognizedForm.getPages().forEach(formPage -> { - System.out.printf("-------Recognizing Page %s of Form -------%n", 1); - System.out.printf("Has width %s , angle %s, height %s %n", formPage.getWidth(), - formPage.getTextAngle(), formPage.getHeight()); - // Table information - System.out.println("Recognized Tables: "); - formPage.getTables().forEach(formTable -> { - formTable.getCells().forEach(formTableCell -> { - System.out.printf("Cell text %s has following words: %n", formTableCell.getText()); - // text_content only exists if you set include_text_content to True in your - // function call to recognize_custom_forms - // It is also a list of FormWords and FormLines, but in this example, we only deal with - // FormWords - formTableCell.getElements().forEach(formContent -> { - if (formContent.getTextContentType().equals(TextContentType.WORD)) { - FormWord formWordElement = (FormWord) (formContent); - final StringBuilder boundingBoxStr = new StringBuilder(); - if (formWordElement.getBoundingBox() != null) { - formWordElement.getBoundingBox().getPoints().forEach(point -> { - boundingBoxStr.append(String.format("[%.2f, %.2f]", point.getX(), - point.getY())); - }); + // Page Information + recognizedForm.getPages().forEach(formPage -> { + System.out.printf("-------Recognizing Page %s of Form -------%n", 1); + System.out.printf("Has width %s , angle %s, height %s %n", formPage.getWidth(), + formPage.getTextAngle(), formPage.getHeight()); + // Table information + System.out.println("Recognized Tables: "); + formPage.getTables().forEach(formTable -> { + formTable.getCells().forEach(formTableCell -> { + System.out.printf("Cell text %s has following words: %n", formTableCell.getText()); + // text_content only exists if you set include_text_content to True in your + // function call to recognize_custom_forms + // It is also a list of FormWords and FormLines, but in this example, we only deal with + // FormWords + formTableCell.getElements().forEach(formContent -> { + if (formContent.getTextContentType().equals(TextContentType.WORD)) { + FormWord formWordElement = (FormWord) (formContent); + final StringBuilder boundingBoxStr = new StringBuilder(); + if (formWordElement.getBoundingBox() != null) { + formWordElement.getBoundingBox().getPoints().forEach(point -> { + boundingBoxStr.append(String.format("[%.2f, %.2f]", point.getX(), + point.getY())); + }); + } + System.out.printf("Word '%s' within bounding box %s with a confidence of %.2f.%n", + formWordElement.getText(), boundingBoxStr, + formWordElement.getConfidence()); } - System.out.printf("Word '%s' within bounding box %s with a confidence of %.2f.%n", - formWordElement.getText(), boundingBoxStr, - formWordElement.getConfidence()); - } + }); }); + System.out.println(); }); - System.out.println(); }); - }); - })); + } + }); // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep // the thread so the program does not end before the send operation is complete. Using .block() instead of diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java index b4e5d013497c..3c14f091b35b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/ReadmeSamples.java @@ -16,9 +16,9 @@ import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.http.rest.PagedIterable; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** @@ -58,10 +58,10 @@ public void rotatingAzureKeyCredential() { public void recognizeCustomForm() { String analyzeFilePath = "{file_source_url}"; String modelId = "{custom_trained_model_id}"; - SyncPoller> recognizeFormPoller = + SyncPoller> recognizeFormPoller = formRecognizerClient.beginRecognizeCustomFormsFromUrl(analyzeFilePath, modelId); - IterableStream recognizedForms = recognizeFormPoller.getFinalResult(); + List recognizedForms = recognizeFormPoller.getFinalResult(); recognizedForms.forEach(form -> { System.out.println("----------- Recognized Form -----------"); @@ -77,10 +77,10 @@ public void recognizeCustomForm() { public void recognizeContent() { String analyzeFilePath = "{file_source_url}"; - SyncPoller> recognizeLayoutPoller = + SyncPoller> recognizeLayoutPoller = formRecognizerClient.beginRecognizeContentFromUrl(analyzeFilePath); - IterableStream layoutPageResults = recognizeLayoutPoller.getFinalResult(); + List layoutPageResults = recognizeLayoutPoller.getFinalResult(); layoutPageResults.forEach(formPage -> { // Table information @@ -102,9 +102,9 @@ public void recognizeContent() { public void recognizeReceipt() { String receiptSourceUrl = "https://docs.microsoft.com/en-us/azure/cognitive-services/form-recognizer/media" + "/contoso-allinone.jpg"; - SyncPoller> syncPoller = + SyncPoller> syncPoller = formRecognizerClient.beginRecognizeReceiptsFromUrl(receiptSourceUrl); - IterableStream receiptPageResults = syncPoller.getFinalResult(); + List receiptPageResults = syncPoller.getFinalResult(); receiptPageResults.forEach(recognizedReceipt -> { USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContent.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContent.java index abd803df8ddc..4c44a9a04af8 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContent.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContent.java @@ -7,7 +7,6 @@ import com.azure.ai.formrecognizer.models.FormPage; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; import java.io.ByteArrayInputStream; @@ -15,6 +14,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.List; /** * Sample for recognizing content information from a document given through a file. @@ -41,14 +41,15 @@ public static void main(final String[] args) throws IOException { byte[] fileContent = Files.readAllBytes(sourceFile.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); - SyncPoller> recognizeLayoutPoller = + SyncPoller> recognizeLayoutPoller = client.beginRecognizeContent(targetStream, sourceFile.length(), FormContentType.IMAGE_JPEG); - IterableStream layoutPageResults = recognizeLayoutPoller.getFinalResult(); + List contentPageResults = recognizeLayoutPoller.getFinalResult(); - layoutPageResults.forEach(formPage -> { + for (int i = 0; i < contentPageResults.size(); i++) { + final FormPage formPage = contentPageResults.get(i); + System.out.printf("----Recognizing content for page %s ----", i); // Table information - System.out.println("----Recognizing content ----"); System.out.printf("Has width: %s and height: %s, measured with unit: %s%n", formPage.getWidth(), formPage.getHeight(), formPage.getUnit()); @@ -66,6 +67,6 @@ public static void main(final String[] args) throws IOException { }); System.out.println(); }); - }); + } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentAsync.java index 149a09ddf71c..938ef815f481 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeContentAsync.java @@ -6,10 +6,10 @@ import com.azure.ai.formrecognizer.models.FormPage; import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; +import java.util.List; import java.util.concurrent.TimeUnit; /** @@ -30,10 +30,10 @@ public static void main(final String[] args) { .endpoint("https://{endpoint}.cognitiveservices.azure.com/") .buildAsyncClient(); - PollerFlux> recognizeLayoutPoller = + PollerFlux> recognizeContentPoller = client.beginRecognizeContentFromUrl("https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/forms/layout1.jpg"); - Mono> layoutPageResults = recognizeLayoutPoller + Mono> contentPageResults = recognizeContentPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -45,27 +45,30 @@ public static void main(final String[] args) { } }); - layoutPageResults.subscribe(formPages -> formPages.forEach(formPage -> { - // Table information - System.out.println("----Recognizing content ----"); - System.out.printf("Has width: %s and height: %s, measured with unit: %s%n", formPage.getWidth(), - formPage.getHeight(), - formPage.getUnit()); - formPage.getTables().forEach(formTable -> { - System.out.printf("Table has %s rows and %s columns.%n", formTable.getRowCount(), - formTable.getColumnCount()); - formTable.getCells().forEach(formTableCell -> { - final StringBuilder boundingBoxStr = new StringBuilder(); - if (formTableCell.getBoundingBox() != null) { - formTableCell.getBoundingBox().getPoints().forEach(point -> - boundingBoxStr.append(String.format("[%.2f, %.2f]", point.getX(), point.getY()))); - } - System.out.printf("Cell has text %s, within bounding box %s.%n", formTableCell.getText(), - boundingBoxStr); + contentPageResults.subscribe(formPages -> { + for (int i = 0; i < formPages.size(); i++) { + final FormPage formPage = formPages.get(i); + System.out.printf("----Recognizing content for page %s ----", i); + System.out.printf("Has width: %s and height: %s, measured with unit: %s%n", formPage.getWidth(), + formPage.getHeight(), + formPage.getUnit()); + // Table information + formPage.getTables().forEach(formTable -> { + System.out.printf("Table has %s rows and %s columns.%n", formTable.getRowCount(), + formTable.getColumnCount()); + formTable.getCells().forEach(formTableCell -> { + final StringBuilder boundingBoxStr = new StringBuilder(); + if (formTableCell.getBoundingBox() != null) { + formTableCell.getBoundingBox().getPoints().forEach(point -> + boundingBoxStr.append(String.format("[%.2f, %.2f]", point.getX(), point.getY()))); + } + System.out.printf("Cell has text %s, within bounding box %s.%n", formTableCell.getText(), + boundingBoxStr); + }); + System.out.println(); }); - System.out.println(); - }); - })); + } + }); // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep // the thread so the program does not end before the send operation is complete. Using .block() instead of diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomForms.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomForms.java index 103d306b01ff..9ac1619704ed 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomForms.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomForms.java @@ -6,9 +6,10 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; +import java.util.List; + /** * Sample to analyze a form from a document with a custom trained model. To learn how to train your own models, * look at TrainModelWithoutLabels.java and TrainModelWithLabels.java. @@ -30,13 +31,14 @@ public static void main(String[] args) { String analyzeFilePath = "{file_source_url}"; String modelId = "{custom_trained_model_id}"; - SyncPoller> recognizeFormPoller = + SyncPoller> recognizeFormPoller = client.beginRecognizeCustomFormsFromUrl(analyzeFilePath, modelId); - IterableStream recognizedForms = recognizeFormPoller.getFinalResult(); + List recognizedForms = recognizeFormPoller.getFinalResult(); - recognizedForms.forEach(form -> { - System.out.println("----------- Recognized Form -----------"); + for (int i = 0; i < recognizedForms.size(); i++) { + final RecognizedForm form = recognizedForms.get(i); + System.out.printf("----------- Recognized Form page %s -----------", i); System.out.printf("Form type: %s%n", form.getFormType()); form.getFields().forEach((label, formField) -> { System.out.printf("Field %s has value %s with confidence score of %.2f.%n", label, @@ -44,6 +46,6 @@ public static void main(String[] args) { formField.getConfidence()); }); System.out.print("-----------------------------------"); - }); + } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsAsync.java index a8e648d23bdf..9362824296ca 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeCustomFormsAsync.java @@ -7,7 +7,6 @@ import com.azure.ai.formrecognizer.models.OperationResult; import com.azure.ai.formrecognizer.models.RecognizedForm; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; @@ -16,6 +15,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.List; import java.util.concurrent.TimeUnit; import static com.azure.ai.formrecognizer.implementation.Utility.toFluxByteBuffer; @@ -47,12 +47,12 @@ public static void main(String[] args) throws IOException { InputStream targetStream = new ByteArrayInputStream(fileContent); String modelId = "{modelId}"; - PollerFlux> recognizeFormPoller = + PollerFlux> recognizeFormPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(targetStream), modelId, sourceFile.length(), FormContentType.APPLICATION_PDF); - Mono> recognizeFormResult = recognizeFormPoller + Mono> recognizeFormResult = recognizeFormPoller .last() .flatMap(recognizeFormPollOperation -> { if (recognizeFormPollOperation.getStatus().isComplete()) { @@ -64,9 +64,10 @@ public static void main(String[] args) throws IOException { } }); - recognizeFormResult.subscribe(recognizedForms -> - recognizedForms.forEach(form -> { - System.out.println("----------- Recognized Form -----------"); + recognizeFormResult.subscribe(recognizedForms -> { + for (int i = 0; i < recognizedForms.size(); i++) { + final RecognizedForm form = recognizedForms.get(i); + System.out.printf("----------- Recognized Form page %s -----------", i); System.out.printf("Form type: %s%n", form.getFormType()); form.getFields().forEach((label, formField) -> { System.out.printf("Field %s has value %s with confidence score of %.2f.%n", label, @@ -74,7 +75,8 @@ public static void main(String[] args) throws IOException { formField.getConfidence()); }); System.out.print("-----------------------------------"); - })); + } + }); // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep // the thread so the program does not end before the send operation is complete. Using .block() instead of diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java index 6cdc48c3f3a7..b63c795f773f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceipts.java @@ -8,7 +8,6 @@ import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.models.USReceipt; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; import java.io.ByteArrayInputStream; @@ -16,6 +15,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.List; /** * Sample for recognizing US receipt information using file source URL. @@ -40,35 +40,44 @@ public static void main(final String[] args) throws IOException { byte[] fileContent = Files.readAllBytes(sourceFile.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); - SyncPoller> analyzeReceiptPoller = + SyncPoller> analyzeReceiptPoller = client.beginRecognizeReceipts(targetStream, sourceFile.length(), FormContentType.IMAGE_JPEG); - IterableStream receiptPageResults = analyzeReceiptPoller.getFinalResult(); + List receiptPageResults = analyzeReceiptPoller.getFinalResult(); - receiptPageResults.forEach(recognizedReceipt -> { - System.out.println("----------- Recognized Receipt -----------"); + for (int i = 0; i < receiptPageResults.size(); i++) { + final RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); + System.out.printf("----------- Recognized Receipt page %s -----------", i); USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); System.out.printf("Page Number: %s%n", usReceipt.getMerchantName().getPageNumber()); - System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), usReceipt.getMerchantName().getConfidence()); - System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), usReceipt.getMerchantAddress().getConfidence()); - System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); - System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), usReceipt.getTotal().getConfidence()); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), + usReceipt.getMerchantName().getConfidence()); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), + usReceipt.getMerchantAddress().getConfidence()); + System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", + usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); + System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), + usReceipt.getTotal().getConfidence()); System.out.printf("Receipt Items: %n"); usReceipt.getReceiptItems().forEach(receiptItem -> { if (receiptItem.getName() != null) { - System.out.printf("Name: %s, confidence: %.2fs%n", receiptItem.getName().getFieldValue(), receiptItem.getName().getConfidence()); + System.out.printf("Name: %s, confidence: %.2fs%n", receiptItem.getName().getFieldValue(), + receiptItem.getName().getConfidence()); } if (receiptItem.getQuantity() != null) { - System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), receiptItem.getQuantity().getConfidence()); + System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), + receiptItem.getQuantity().getConfidence()); } if (receiptItem.getPrice() != null) { - System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), receiptItem.getPrice().getConfidence()); + System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), + receiptItem.getPrice().getConfidence()); } if (receiptItem.getTotalPrice() != null) { - System.out.printf("Total Price: %s, confidence: %.2f%n", receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); + System.out.printf("Total Price: %s, confidence: %.2f%n", + receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); } }); System.out.print("-----------------------------------"); - }); + } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java index 57504460fd3a..c06771f13217 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsAsync.java @@ -8,7 +8,6 @@ import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.models.USReceipt; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; @@ -17,6 +16,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; +import java.util.List; import java.util.concurrent.TimeUnit; import static com.azure.ai.formrecognizer.implementation.Utility.toFluxByteBuffer; @@ -44,11 +44,11 @@ public static void main(final String[] args) throws IOException { byte[] fileContent = Files.readAllBytes(sourceFile.toPath()); InputStream targetStream = new ByteArrayInputStream(fileContent); - PollerFlux> analyzeReceiptPoller = + PollerFlux> analyzeReceiptPoller = client.beginRecognizeReceipts(toFluxByteBuffer(targetStream), sourceFile.length(), FormContentType.IMAGE_JPEG); - IterableStream receiptPageResults = analyzeReceiptPoller + List receiptPageResults = analyzeReceiptPoller .last() .flatMap(recognizeReceiptPollOperation -> { if (recognizeReceiptPollOperation.getStatus().isComplete()) { @@ -61,31 +61,40 @@ public static void main(final String[] args) throws IOException { } }).block(); - receiptPageResults.forEach(recognizedReceipt -> { - System.out.println("----------- Recognized Receipt -----------"); + for (int i = 0; i < receiptPageResults.size(); i++) { + final RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); + System.out.printf("----------- Recognized Receipt for page %s -----------", i); USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); System.out.printf("Page Number: %s%n", usReceipt.getMerchantName().getPageNumber()); - System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), usReceipt.getMerchantName().getConfidence()); - System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), usReceipt.getMerchantAddress().getConfidence()); - System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); - System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), usReceipt.getTotal().getConfidence()); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), + usReceipt.getMerchantName().getConfidence()); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), + usReceipt.getMerchantAddress().getConfidence()); + System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", + usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); + System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), + usReceipt.getTotal().getConfidence()); System.out.printf("Receipt Items: %n"); usReceipt.getReceiptItems().forEach(receiptItem -> { if (receiptItem.getName() != null) { - System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), receiptItem.getName().getConfidence()); + System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), + receiptItem.getName().getConfidence()); } if (receiptItem.getQuantity() != null) { - System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), receiptItem.getQuantity().getConfidence()); + System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), + receiptItem.getQuantity().getConfidence()); } if (receiptItem.getPrice() != null) { - System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), receiptItem.getPrice().getConfidence()); + System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), + receiptItem.getPrice().getConfidence()); } if (receiptItem.getTotalPrice() != null) { - System.out.printf("Total Price: %s, confidence: %.2f%n", receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); + System.out.printf("Total Price: %s, confidence: %.2f%n", + receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); } }); System.out.print("-----------------------------------"); - }); + } // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep // the thread so the program does not end before the send operation is complete. Using .block() instead of diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java index 2e9ce0a1276a..ae65ddc5da0b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrl.java @@ -7,9 +7,10 @@ import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.models.USReceipt; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; +import java.util.List; + /** * Sample for recognizing US receipt information using file source URL. */ @@ -28,35 +29,44 @@ public static void main(final String[] args) { .buildClient(); String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; - SyncPoller> recognizeReceiptPoller = + SyncPoller> recognizeReceiptPoller = client.beginRecognizeReceiptsFromUrl(receiptUrl); - IterableStream receiptPageResults = recognizeReceiptPoller.getFinalResult(); + List receiptPageResults = recognizeReceiptPoller.getFinalResult(); - receiptPageResults.forEach(recognizedReceipt -> { - System.out.println("----------- Recognized Receipt -----------"); + for (int i = 0; i < receiptPageResults.size(); i++) { + final RecognizedReceipt recognizedReceipt = receiptPageResults.get(i); + System.out.printf("----------- Recognized Receipt page %s -----------", i); USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); System.out.printf("Page Number: %d%n", usReceipt.getMerchantName().getPageNumber()); - System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), usReceipt.getMerchantName().getConfidence()); - System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), usReceipt.getMerchantAddress().getConfidence()); - System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); - System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), usReceipt.getTotal().getConfidence()); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), + usReceipt.getMerchantName().getConfidence()); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), + usReceipt.getMerchantAddress().getConfidence()); + System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", + usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); + System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), + usReceipt.getTotal().getConfidence()); System.out.printf("Receipt Items: %n"); usReceipt.getReceiptItems().forEach(receiptItem -> { if (receiptItem.getName() != null) { - System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), receiptItem.getName().getConfidence()); + System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), + receiptItem.getName().getConfidence()); } if (receiptItem.getQuantity() != null) { - System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), receiptItem.getQuantity().getConfidence()); + System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), + receiptItem.getQuantity().getConfidence()); } if (receiptItem.getPrice() != null) { - System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), receiptItem.getPrice().getConfidence()); + System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), + receiptItem.getPrice().getConfidence()); } if (receiptItem.getTotalPrice() != null) { - System.out.printf("Total Price: %s, confidence: %.2f%n", receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); + System.out.printf("Total Price: %s, confidence: %.2f%n", + receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); } }); System.out.print("-----------------------------------"); - }); + } } } diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java index c328af9df3d2..e61165c404cd 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/com/azure/ai/formrecognizer/RecognizeReceiptsFromUrlAsync.java @@ -7,10 +7,10 @@ import com.azure.ai.formrecognizer.models.RecognizedReceipt; import com.azure.ai.formrecognizer.models.USReceipt; import com.azure.core.credential.AzureKeyCredential; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.PollerFlux; import reactor.core.publisher.Mono; +import java.util.List; import java.util.concurrent.TimeUnit; /** @@ -31,10 +31,10 @@ public static void main(final String[] args) { .buildAsyncClient(); String receiptUrl = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/sdk/formrecognizer/azure-ai-formrecognizer/src/samples/java/sample-forms/receipts/contoso-allinone.jpg"; - PollerFlux> analyzeReceiptPoller = + PollerFlux> recognizeReceiptPoller = client.beginRecognizeReceiptsFromUrl(receiptUrl); - Mono> receiptPageResults = analyzeReceiptPoller + Mono> receiptPageResults = recognizeReceiptPoller .last() .flatMap(trainingOperationResponse -> { if (trainingOperationResponse.getStatus().isComplete()) { @@ -47,39 +47,43 @@ public static void main(final String[] args) { } }); - receiptPageResults.subscribe(recognizedReceipts -> recognizedReceipts.forEach(recognizedReceipt -> { - System.out.println("----------- Recognized Receipt -----------"); - USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); - System.out.printf("Page Number: %s%n", usReceipt.getMerchantName().getPageNumber()); - System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), - usReceipt.getMerchantName().getConfidence()); - System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), - usReceipt.getMerchantAddress().getConfidence()); - System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", - usReceipt.getMerchantPhoneNumber().getFieldValue(), usReceipt.getMerchantPhoneNumber().getConfidence()); - System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), - usReceipt.getTotal().getConfidence()); - System.out.printf("Receipt Items: %n"); - usReceipt.getReceiptItems().forEach(receiptItem -> { - if (receiptItem.getName() != null) { - System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), - receiptItem.getName().getConfidence()); - } - if (receiptItem.getQuantity() != null) { - System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), - receiptItem.getQuantity().getConfidence()); - } - if (receiptItem.getPrice() != null) { - System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), - receiptItem.getPrice().getConfidence()); - } - if (receiptItem.getTotalPrice() != null) { - System.out.printf("Total Price: %s, confidence: %.2f%n", - receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); - } - }); - System.out.print("-----------------------------------"); - })); + receiptPageResults.subscribe(recognizedReceipts -> { + for (int i = 0; i < recognizedReceipts.size(); i++) { + final RecognizedReceipt recognizedReceipt = recognizedReceipts.get(i); + System.out.printf("----------- Recognized Receipt page %s -----------", i); + USReceipt usReceipt = ReceiptExtensions.asUSReceipt(recognizedReceipt); + System.out.printf("Page Number: %s%n", usReceipt.getMerchantName().getPageNumber()); + System.out.printf("Merchant Name: %s, confidence: %.2f%n", usReceipt.getMerchantName().getFieldValue(), + usReceipt.getMerchantName().getConfidence()); + System.out.printf("Merchant Address: %s, confidence: %.2f%n", usReceipt.getMerchantAddress().getName(), + usReceipt.getMerchantAddress().getConfidence()); + System.out.printf("Merchant Phone Number %s, confidence: %.2f%n", + usReceipt.getMerchantPhoneNumber().getFieldValue(), + usReceipt.getMerchantPhoneNumber().getConfidence()); + System.out.printf("Total: %s confidence: %.2f%n", usReceipt.getTotal().getName(), + usReceipt.getTotal().getConfidence()); + System.out.printf("Receipt Items: %n"); + usReceipt.getReceiptItems().forEach(receiptItem -> { + if (receiptItem.getName() != null) { + System.out.printf("Name: %s, confidence: %.2f%n", receiptItem.getName().getFieldValue(), + receiptItem.getName().getConfidence()); + } + if (receiptItem.getQuantity() != null) { + System.out.printf("Quantity: %s, confidence: %.2f%n", receiptItem.getQuantity().getFieldValue(), + receiptItem.getQuantity().getConfidence()); + } + if (receiptItem.getPrice() != null) { + System.out.printf("Price: %s, confidence: %.2f%n", receiptItem.getPrice().getFieldValue(), + receiptItem.getPrice().getConfidence()); + } + if (receiptItem.getTotalPrice() != null) { + System.out.printf("Total Price: %s, confidence: %.2f%n", + receiptItem.getTotalPrice().getFieldValue(), receiptItem.getTotalPrice().getConfidence()); + } + }); + System.out.print("-----------------------------------"); + } + }); // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep // the thread so the program does not end before the send operation is complete. Using .block() instead of diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java index 1ae44d58823c..f219636b53a7 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerAsyncClientTest.java @@ -17,7 +17,6 @@ import com.azure.core.http.policy.HttpLogDetailLevel; import com.azure.core.http.policy.HttpLogOptions; import com.azure.core.test.TestMode; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -26,6 +25,7 @@ import reactor.test.StepVerifier; import java.time.Duration; +import java.util.List; import static com.azure.ai.formrecognizer.TestUtils.CUSTOM_FORM_FILE_LENGTH; import static com.azure.ai.formrecognizer.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS; @@ -90,7 +90,7 @@ private FormTrainingAsyncClient getFormTrainingAsyncClient(HttpClient httpClient public void recognizeReceiptSourceUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptSourceUrlRunner(sourceUrl -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl).getSyncPoller(); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), false); @@ -107,7 +107,7 @@ public void recognizeReceiptSourceUrlTextDetails(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptSourceUrlRunnerTextDetails((sourceUrl, includeTextDetails) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl, includeTextDetails, null).getSyncPoller(); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), includeTextDetails); @@ -122,7 +122,7 @@ public void recognizeReceiptSourceUrlTextDetails(HttpClient httpClient, public void recognizeReceiptData(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptDataRunner((data) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), RECEIPT_FILE_LENGTH, FormContentType.IMAGE_JPEG, false, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -150,7 +150,7 @@ public void recognizeReceiptDataTextDetailsWithNullData(HttpClient httpClient, public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(getReplayableBufferData(RECEIPT_LOCAL_URL), RECEIPT_FILE_LENGTH, null, false, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -167,7 +167,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie // public void recognizeReceiptDataTextDetails(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { // client = getFormRecognizerAsyncClient(httpClient, serviceVersion); // receiptDataRunnerTextDetails((data, includeTextDetails) -> { -// SyncPoller> syncPoller = +// SyncPoller> syncPoller = // client.beginRecognizeReceipts(toFluxByteBuffer(data), RECEIPT_FILE_LENGTH, FormContentType.IMAGE_JPEG, // includeTextDetails, null).getSyncPoller(); // syncPoller.waitForCompletion(); @@ -194,7 +194,7 @@ public void recognizeReceiptInvalidSourceUrl(HttpClient httpClient, FormRecogniz public void recognizeReceiptAsUSReceipt(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); receiptDataRunnerTextDetails((data, includeTextDetails) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), RECEIPT_FILE_LENGTH, FormContentType.IMAGE_JPEG, includeTextDetails, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -210,7 +210,7 @@ public void recognizeReceiptAsUSReceipt(HttpClient httpClient, FormRecognizerSer public void recognizeContent(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); contentFromDataRunner((data) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(toFluxByteBuffer(data), LAYOUT_FILE_LENGTH, FormContentType.IMAGE_JPEG, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -238,7 +238,7 @@ public void recognizeContentResultWithContentTypeAutoDetection(HttpClient httpCl FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); contentFromDataRunner((data) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(getReplayableBufferData(LAYOUT_LOCAL_URL), LAYOUT_FILE_LENGTH, null, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -254,7 +254,7 @@ public void recognizeContentResultWithContentTypeAutoDetection(HttpClient httpCl public void recognizeContentFromUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); contentFromUrlRunner(sourceUrl -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContentFromUrl(sourceUrl).getSyncPoller(); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -302,7 +302,7 @@ public void recognizeCustomFormLabeledData(HttpClient httpClient, FormRecognizer getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(data), trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, FormContentType.APPLICATION_PDF, true, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -346,7 +346,7 @@ public void recognizeCustomFormLabeledDataWithContentTypeAutoDetection(HttpClien getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomForms(getReplayableBufferData(FORM_LOCAL_URL), trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, null, true, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -366,7 +366,7 @@ public void recognizeCustomFormUnlabeledData(HttpClient httpClient, FormRecogniz getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(data), trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, FormContentType.APPLICATION_PDF, false, null).getSyncPoller(); syncPoller.waitForCompletion(); @@ -383,7 +383,7 @@ public void recognizeCustomFormMultiPageUnlabeled(HttpClient httpClient, FormRec getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, false).getSyncPoller(); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomForms(toFluxByteBuffer(data), trainingPoller.getFinalResult().getModelId(), MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF).getSyncPoller(); syncPoller.waitForCompletion(); @@ -400,7 +400,7 @@ public void recognizeCustomFormUrlMultiPageLabeled(HttpClient httpClient, FormRe getFormTrainingAsyncClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, true).getSyncPoller(); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomFormsFromUrl(fileUrl, trainingPoller.getFinalResult().getModelId()).getSyncPoller(); syncPoller.waitForCompletion(); validateMultiPageDataLabeled(syncPoller.getFinalResult()); @@ -412,7 +412,7 @@ public void recognizeCustomFormUrlMultiPageLabeled(HttpClient httpClient, FormRe public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromUrlRunner(fileUrl -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(fileUrl).getSyncPoller(); syncPoller.waitForCompletion(); validateMultipageReceiptData(syncPoller.getFinalResult()); @@ -424,7 +424,7 @@ public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecogniz public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(toFluxByteBuffer(data), MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF).getSyncPoller(); syncPoller.waitForCompletion(); @@ -437,7 +437,7 @@ public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecogni public void recognizeContentFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromUrlRunner((fileUrl) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContentFromUrl(fileUrl).getSyncPoller(); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -449,7 +449,7 @@ public void recognizeContentFromUrlMultiPage(HttpClient httpClient, FormRecogniz public void recognizeContentFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerAsyncClient(httpClient, serviceVersion); multipageFromDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(toFluxByteBuffer(data), MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF).getSyncPoller(); syncPoller.waitForCompletion(); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java index fcf29535db72..381a2a1c4670 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTest.java @@ -17,11 +17,12 @@ import com.azure.core.http.policy.HttpLogDetailLevel; import com.azure.core.http.policy.HttpLogOptions; import com.azure.core.test.TestMode; -import com.azure.core.util.IterableStream; import com.azure.core.util.polling.SyncPoller; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import java.util.List; + import static com.azure.ai.formrecognizer.TestUtils.CUSTOM_FORM_FILE_LENGTH; import static com.azure.ai.formrecognizer.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS; import static com.azure.ai.formrecognizer.TestUtils.FORM_LOCAL_URL; @@ -77,7 +78,7 @@ private FormTrainingClient getFormTrainingClient(HttpClient httpClient, public void recognizeReceiptSourceUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptSourceUrlRunner((sourceUrl) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), false); @@ -94,7 +95,7 @@ public void recognizeReceiptSourceUrlTextDetails(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptSourceUrlRunnerTextDetails((sourceUrl, includeTextDetails) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(sourceUrl, includeTextDetails, null); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), true); @@ -109,7 +110,7 @@ public void recognizeReceiptSourceUrlTextDetails(HttpClient httpClient, public void recognizeReceiptData(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); receiptDataRunner((data) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(data, RECEIPT_FILE_LENGTH, FormContentType.IMAGE_JPEG, false, null); syncPoller.waitForCompletion(); validateReceiptResultData(syncPoller.getFinalResult(), false); @@ -136,7 +137,7 @@ public void recognizeReceiptDataTextDetailsWithNullData(HttpClient httpClient, public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(getContentDetectionFileData(RECEIPT_LOCAL_URL), RECEIPT_FILE_LENGTH, null, false, null); syncPoller.waitForCompletion(); @@ -155,7 +156,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie // public void recognizeReceiptDataTextDetails(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { // client = getFormRecognizerClient(httpClient, serviceVersion); // receiptDataRunnerTextDetails((data, includeTextDetails) -> { -// SyncPoller> syncPoller = +// SyncPoller> syncPoller = // client.beginRecognizeReceipts(data, RECEIPT_FILE_LENGTH, FormContentType.IMAGE_PNG, // includeTextDetails, null); // syncPoller.waitForCompletion(); @@ -171,7 +172,7 @@ public void recognizeReceiptDataWithContentTypeAutoDetection(HttpClient httpClie public void recognizeContent(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); contentFromDataRunner((data) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(data, LAYOUT_FILE_LENGTH, FormContentType.IMAGE_PNG, null); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -198,7 +199,7 @@ public void recognizeContentResultWithNullData(HttpClient httpClient, FormRecogn public void recognizeContentResultWithContentTypeAutoDetection(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(getContentDetectionFileData(LAYOUT_LOCAL_URL), LAYOUT_FILE_LENGTH, null, null); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -209,7 +210,7 @@ public void recognizeContentResultWithContentTypeAutoDetection(HttpClient httpCl public void recognizeContentFromUrl(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); contentFromUrlRunner(sourceUrl -> { - SyncPoller> syncPoller + SyncPoller> syncPoller = client.beginRecognizeContentFromUrl(sourceUrl); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -262,7 +263,7 @@ public void recognizeCustomFormLabeledData(HttpClient httpClient, FormRecognizer getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller + SyncPoller> syncPoller = client.beginRecognizeCustomForms(data, trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, FormContentType.APPLICATION_PDF, true, null); syncPoller.waitForCompletion(); @@ -308,7 +309,7 @@ public void recognizeCustomFormLabeledDataWithContentTypeAutoDetection(HttpClien getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller + SyncPoller> syncPoller = client.beginRecognizeCustomForms(getContentDetectionFileData(FORM_LOCAL_URL), trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, null, true, null); @@ -330,7 +331,7 @@ public void recognizeCustomFormUnlabeledData(HttpClient httpClient, FormRecogniz getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, useTrainingLabels); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller + SyncPoller> syncPoller = client.beginRecognizeCustomForms(data, trainingPoller.getFinalResult().getModelId(), CUSTOM_FORM_FILE_LENGTH, FormContentType.APPLICATION_PDF, false, null); syncPoller.waitForCompletion(); @@ -343,7 +344,7 @@ public void recognizeCustomFormUnlabeledData(HttpClient httpClient, FormRecogniz public void recognizeContentFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContent(data, MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); @@ -358,7 +359,7 @@ void recognizeCustomFormUrlMultiPageLabeled(HttpClient httpClient, FormRecognize getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, true); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomFormsFromUrl(fileUrl, trainingPoller.getFinalResult().getModelId()); syncPoller.waitForCompletion(); validateMultiPageDataLabeled(syncPoller.getFinalResult()); @@ -375,7 +376,7 @@ public void recognizeCustomFormMultiPageUnlabeled(HttpClient httpClient, getFormTrainingClient(httpClient, serviceVersion).beginTraining(trainingFilesUrl, false); trainingPoller.waitForCompletion(); - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeCustomForms(data, trainingPoller.getFinalResult().getModelId(), MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF); syncPoller.waitForCompletion(); @@ -388,7 +389,7 @@ public void recognizeCustomFormMultiPageUnlabeled(HttpClient httpClient, public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromUrlRunner(fileUrl -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceiptsFromUrl(fileUrl); syncPoller.waitForCompletion(); validateMultipageReceiptData(syncPoller.getFinalResult()); @@ -400,7 +401,7 @@ public void recognizeReceiptFromUrlMultiPage(HttpClient httpClient, FormRecogniz public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromDataRunner(data -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeReceipts(data, MULTIPAGE_INVOICE_FILE_LENGTH, FormContentType.APPLICATION_PDF); syncPoller.waitForCompletion(); validateMultipageReceiptData(syncPoller.getFinalResult()); @@ -412,7 +413,7 @@ public void recognizeReceiptFromDataMultiPage(HttpClient httpClient, FormRecogni public void recognizeContentFromUrlMultiPage(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormRecognizerClient(httpClient, serviceVersion); multipageFromUrlRunner((fileUrl) -> { - SyncPoller> syncPoller = + SyncPoller> syncPoller = client.beginRecognizeContentFromUrl(fileUrl); syncPoller.waitForCompletion(); validateLayoutDataResults(syncPoller.getFinalResult(), false); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java index f0f52ce668d0..c6100adfbfeb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormRecognizerClientTestBase.java @@ -35,19 +35,16 @@ import com.azure.core.test.TestBase; import com.azure.core.test.models.NetworkCallRecord; import com.azure.core.util.Configuration; -import com.azure.core.util.IterableStream; import com.azure.core.util.serializer.SerializerAdapter; import org.junit.jupiter.api.Test; import java.io.ByteArrayInputStream; import java.io.InputStream; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.regex.Pattern; -import java.util.stream.Collectors; import static com.azure.ai.formrecognizer.FormTrainingClientTestBase.AZURE_FORM_RECOGNIZER_API_KEY; import static com.azure.ai.formrecognizer.FormTrainingClientTestBase.AZURE_FORM_RECOGNIZER_ENDPOINT; @@ -71,9 +68,8 @@ public abstract class FormRecognizerClientTestBase extends TestBase { private static final String ITEMIZED_RECEIPT_VALUE = "Itemized"; private static void validateReferenceElementsData(List expectedElements, - IterableStream actualFormContents, List readResults) { - if (expectedElements != null && actualFormContents != null) { - List actualFormContentList = actualFormContents.stream().collect(Collectors.toList()); + List actualFormContentList, List readResults) { + if (expectedElements != null && actualFormContentList != null) { assertEquals(expectedElements.size(), actualFormContentList.size()); for (int i = 0; i < actualFormContentList.size(); i++) { String[] indices = NON_DIGIT_PATTERN.matcher(expectedElements.get(i)).replaceAll(" ").trim().split(" "); @@ -108,8 +104,7 @@ private static void validateReferenceElementsData(List expectedElements, } private static void validateFormTableData(List expectedFormTables, - IterableStream actualFormTables, List readResults, boolean includeTextDetails) { - List actualFormTable = actualFormTables.stream().collect(Collectors.toList()); + List actualFormTable, List readResults, boolean includeTextDetails) { assertEquals(expectedFormTables.size(), actualFormTable.size()); for (int i = 0; i < actualFormTable.size(); i++) { DataTable expectedTable = expectedFormTables.get(i); @@ -121,8 +116,7 @@ private static void validateFormTableData(List expectedFormTables, } private static void validateCellData(List expectedTableCells, - IterableStream actualTableCells, List readResults, boolean includeTextDetails) { - List actualTableCellList = actualTableCells.stream().collect(Collectors.toList()); + List actualTableCellList, List readResults, boolean includeTextDetails) { assertEquals(expectedTableCells.size(), actualTableCellList.size()); for (int i = 0; i < actualTableCellList.size(); i++) { DataTableCell expectedTableCell = expectedTableCells.get(i); @@ -139,8 +133,7 @@ private static void validateCellData(List expectedTableCells, } } - private static void validateFormLineData(List expectedLines, IterableStream actualLines) { - List actualLineList = actualLines.stream().collect(Collectors.toList()); + private static void validateFormLineData(List expectedLines, List actualLineList) { assertEquals(expectedLines.size(), actualLineList.size()); for (int i = 0; i < actualLineList.size(); i++) { TextLine expectedLine = expectedLines.get(i); @@ -152,8 +145,7 @@ private static void validateFormLineData(List expectedLines, IterableS } private static void validateFormWordData(List expectedFormWords, - IterableStream actualFormWords) { - List actualFormWordList = actualFormWords.stream().collect(Collectors.toList()); + List actualFormWordList) { assertEquals(expectedFormWords.size(), actualFormWordList.size()); for (int i = 0; i < actualFormWordList.size(); i++) { @@ -225,8 +217,7 @@ private static void validatePageRangeData(int expectedPageInfo, PageRange actual } private static void validateReceiptItemsData(List expectedReceiptItemList, - List actualReceiptItems, List readResults, boolean includeTextDetails) { - List actualReceiptItemList = new ArrayList<>(actualReceiptItems); + List actualReceiptItemList, List readResults, boolean includeTextDetails) { assertEquals(expectedReceiptItemList.size(), actualReceiptItemList.size()); for (int i = 0; i < expectedReceiptItemList.size(); i++) { FieldValue expectedReceiptItem = expectedReceiptItemList.get(i); @@ -350,11 +341,10 @@ void validateUSReceiptData(USReceipt actualRecognizedReceipt, boolean includeTex actualRecognizedReceipt.getReceiptItems(), readResults, includeTextDetails); } - void validateLayoutDataResults(IterableStream actualFormPages, boolean includeTextDetails) { + void validateLayoutDataResults(List actualFormPageList, boolean includeTextDetails) { AnalyzeResult analyzeResult = getAnalyzeRawResponse().getAnalyzeResult(); final List pageResults = analyzeResult.getPageResults(); final List readResults = analyzeResult.getReadResults(); - List actualFormPageList = actualFormPages.stream().collect(Collectors.toList()); for (int i = 0; i < actualFormPageList.size(); i++) { FormPage actualFormPage = actualFormPageList.get(i); ReadResult readResult = readResults.get(i); @@ -372,9 +362,8 @@ void validateLayoutDataResults(IterableStream actualFormPages, boolean } } - void validateReceiptResultData(IterableStream actualResult, boolean includeTextDetails) { + void validateReceiptResultData(List actualReceiptList, boolean includeTextDetails) { final AnalyzeResult rawResponse = getAnalyzeRawResponse().getAnalyzeResult(); - List actualReceiptList = actualResult.stream().collect(Collectors.toList()); for (int i = 0; i < actualReceiptList.size(); i++) { final RecognizedReceipt actualReceipt = actualReceiptList.get(i); assertEquals("en-US", actualReceipt.getReceiptLocale()); @@ -383,13 +372,12 @@ void validateReceiptResultData(IterableStream actualResult, b } } - void validateRecognizedResult(IterableStream actualForms, boolean includeTextDetails, + void validateRecognizedResult(List actualFormList, boolean includeTextDetails, boolean isLabeled) { final AnalyzeResult rawResponse = getAnalyzeRawResponse().getAnalyzeResult(); List readResults = rawResponse.getReadResults(); List pageResults = rawResponse.getPageResults(); List documentResults = rawResponse.getDocumentResults(); - List actualFormList = actualForms.stream().collect(Collectors.toList()); for (int i = 0; i < actualFormList.size(); i++) { validateLayoutDataResults(actualFormList.get(i).getPages(), includeTextDetails); @@ -515,13 +503,12 @@ private void validateLabeledData(RecognizedForm actualForm, boolean includeTextD }); } - static void validateMultiPageDataLabeled(IterableStream recognizedFormResult) { - List actualRecognizedFormsList = recognizedFormResult.stream().collect(Collectors.toList()); + static void validateMultiPageDataLabeled(List actualRecognizedFormsList) { actualRecognizedFormsList.forEach(recognizedForm -> { assertEquals("custom:form", recognizedForm.getFormType()); assertEquals(1, recognizedForm.getPageRange().getStartPageNumber()); assertEquals(3, recognizedForm.getPageRange().getEndPageNumber()); - assertEquals(3, (int) recognizedForm.getPages().stream().count()); + assertEquals(3, recognizedForm.getPages().size()); recognizedForm.getFields().forEach((label, formField) -> { assertNotNull(formField.getName()); assertNotNull(formField.getFieldValue()); @@ -531,8 +518,7 @@ static void validateMultiPageDataLabeled(IterableStream recogniz }); } - static void validateMultiPageDataUnlabeled(IterableStream recognizedFormResult) { - List actualRecognizedFormsList = recognizedFormResult.stream().collect(Collectors.toList()); + static void validateMultiPageDataUnlabeled(List actualRecognizedFormsList) { actualRecognizedFormsList.forEach(recognizedForm -> { assertNotNull(recognizedForm.getFormType()); assertEquals(1, recognizedForm.getPages().stream().count()); @@ -545,8 +531,7 @@ static void validateMultiPageDataUnlabeled(IterableStream recogn }); } - static void validateMultipageReceiptData(IterableStream actualReceiptResult) { - List recognizedReceipts = actualReceiptResult.stream().collect(Collectors.toList()); + static void validateMultipageReceiptData(List recognizedReceipts) { assertEquals(3, recognizedReceipts.size()); USReceipt receiptPage1 = ReceiptExtensions.asUSReceipt(recognizedReceipts.get(0)); USReceipt receiptPage2 = ReceiptExtensions.asUSReceipt(recognizedReceipts.get(1)); @@ -563,10 +548,10 @@ static void validateMultipageReceiptData(IterableStream actua // Assert no fields, tables and lines on second page assertEquals(0, receiptPage2.getRecognizedForm().getFields().size()); - IterableStream receipt2Pages = receiptPage2.getRecognizedForm().getPages(); - assertEquals(1, receipt2Pages.stream().count()); - assertEquals(0, receipt2Pages.stream().findFirst().get().getTables().stream().count()); - assertEquals(0, receipt2Pages.stream().findFirst().get().getLines().stream().count()); + List receipt2Pages = receiptPage2.getRecognizedForm().getPages(); + assertEquals(1, receipt2Pages.size()); + assertEquals(0, receipt2Pages.stream().findFirst().get().getTables().size()); + assertEquals(0, receipt2Pages.stream().findFirst().get().getLines().size()); assertEquals(2, receiptPage2.getRecognizedForm().getPageRange().getStartPageNumber()); assertEquals(2, receiptPage2.getRecognizedForm().getPageRange().getEndPageNumber()); diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java index 89295e8684ef..cc91fbca2747 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java +++ b/sdk/formrecognizer/azure-ai-formrecognizer/src/test/java/com/azure/ai/formrecognizer/FormTrainingAsyncClientTest.java @@ -181,9 +181,9 @@ public void deleteModelInvalidModelId(HttpClient httpClient, FormRecognizerServi @MethodSource("com.azure.ai.formrecognizer.TestUtils#getTestParameters") public void deleteModelValidModelIdWithResponse(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormTrainingAsyncClient(httpClient, serviceVersion); - beginTrainingLabeledRunner((storageSASUrl, useTrainingLabels) -> { + beginTrainingLabeledRunner((trainingFilesUrl, useTrainingLabels) -> { SyncPoller syncPoller = - client.beginTraining(storageSASUrl, useTrainingLabels).getSyncPoller(); + client.beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); syncPoller.waitForCompletion(); CustomFormModel createdModel = syncPoller.getFinalResult(); @@ -194,7 +194,7 @@ public void deleteModelValidModelIdWithResponse(HttpClient httpClient, FormRecog StepVerifier.create(client.getCustomModelWithResponse(createdModel.getModelId())) .verifyErrorSatisfies(throwable -> - throwable.getMessage().contains(HttpResponseStatus.NOT_FOUND.toString())); + assertTrue(throwable.getMessage().contains("404"))); }); } @@ -233,9 +233,9 @@ public void beginTrainingNullInput(HttpClient httpClient, FormRecognizerServiceV @MethodSource("com.azure.ai.formrecognizer.TestUtils#getTestParameters") public void beginTrainingLabeledResult(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormTrainingAsyncClient(httpClient, serviceVersion); - beginTrainingLabeledRunner((storageSASUrl, useTrainingLabels) -> { + beginTrainingLabeledRunner((trainingFilesUrl, useTrainingLabels) -> { SyncPoller syncPoller = - client.beginTraining(storageSASUrl, useTrainingLabels).getSyncPoller(); + client.beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); syncPoller.waitForCompletion(); validateCustomModelData(syncPoller.getFinalResult(), true); }); @@ -248,9 +248,9 @@ public void beginTrainingLabeledResult(HttpClient httpClient, FormRecognizerServ @MethodSource("com.azure.ai.formrecognizer.TestUtils#getTestParameters") public void beginTrainingUnlabeledResult(HttpClient httpClient, FormRecognizerServiceVersion serviceVersion) { client = getFormTrainingAsyncClient(httpClient, serviceVersion); - beginTrainingUnlabeledRunner((storageSASUrl, useTrainingLabels) -> { + beginTrainingUnlabeledRunner((trainingFilesUrl, useTrainingLabels) -> { SyncPoller syncPoller = - client.beginTraining(storageSASUrl, useTrainingLabels).getSyncPoller(); + client.beginTraining(trainingFilesUrl, useTrainingLabels).getSyncPoller(); syncPoller.waitForCompletion(); validateCustomModelData(syncPoller.getFinalResult(), false); });