Search in sources :

Example 1 with IdentifyDocumentTextResult

use of com.amplifyframework.predictions.result.IdentifyDocumentTextResult in project amplify-android by aws-amplify.

the class AWSPredictionsIdentifyTextTest method testIdentifyTables.

/**
 * Assert table detection works.
 * @throws PredictionsException if prediction fails
 */
@Test
public void testIdentifyTables() throws PredictionsException {
    final Bitmap image = Assets.readAsBitmap("sample-table.png");
    // Identify the text inside given image and assert non-null result.
    IdentifyDocumentTextResult result = (IdentifyDocumentTextResult) predictions.identify(TextFormatType.TABLE, image);
    assertNotNull(result);
    // Assert that one table is detected.
    List<Table> tables = result.getTables();
    assertFalse(Empty.check(tables));
    assertEquals(1, tables.size());
    // Assert that table has correct dimensions.
    Table table = tables.get(0);
    assertEquals(2, table.getRowSize());
    assertEquals(2, table.getColumnSize());
    // Assert that table has correct cells.
    List<Cell> cells = table.getCells();
    assertEquals(4, cells.size());
    FeatureAssert.assertContains("Name", cells);
    FeatureAssert.assertContains("Address", cells);
    FeatureAssert.assertContains("Ana Carolina", cells);
    FeatureAssert.assertContains("123 Any Town", cells);
}
Also used : Bitmap(android.graphics.Bitmap) Table(com.amplifyframework.predictions.models.Table) IdentifyDocumentTextResult(com.amplifyframework.predictions.result.IdentifyDocumentTextResult) Cell(com.amplifyframework.predictions.models.Cell) Test(org.junit.Test)

Example 2 with IdentifyDocumentTextResult

use of com.amplifyframework.predictions.result.IdentifyDocumentTextResult in project amplify-android by aws-amplify.

the class AWSPredictionsIdentifyTextTest method testIdentifyForms.

/**
 * Assert form detection works.
 * @throws PredictionsException if prediction fails
 */
@Test
public void testIdentifyForms() throws PredictionsException {
    final Bitmap image = Assets.readAsBitmap("sample-form.png");
    // Identify the text inside given image and assert non-null result.
    IdentifyDocumentTextResult result = (IdentifyDocumentTextResult) predictions.identify(TextFormatType.FORM, image);
    // Assert that four key-values are detected.
    List<BoundedKeyValue> keyValues = result.getKeyValues();
    assertFalse(Empty.check(keyValues));
    assertEquals(4, keyValues.size());
    // Assert that key-value pairs have correct values.
    FeatureAssert.assertContains(Pair.create("Name:", "Jane Doe"), keyValues);
    FeatureAssert.assertContains(Pair.create("Address:", "123 Any Street, Anytown, USA"), keyValues);
    FeatureAssert.assertContains(Pair.create("Birth date:", "12-26-1980"), keyValues);
// FeatureAssert.assertContains(Pair.create("Male:", "true"), keyValues); // Selection not supported
}
Also used : BoundedKeyValue(com.amplifyframework.predictions.models.BoundedKeyValue) Bitmap(android.graphics.Bitmap) IdentifyDocumentTextResult(com.amplifyframework.predictions.result.IdentifyDocumentTextResult) Test(org.junit.Test)

Example 3 with IdentifyDocumentTextResult

use of com.amplifyframework.predictions.result.IdentifyDocumentTextResult in project amplify-android by aws-amplify.

the class RxPredictionsBindingTest method testSuccessfulImageIdentification.

/**
 * When the delegate of {@link RxPredictionsBinding#identify(IdentifyAction, Bitmap)} succeeds,
 * the result should be propagated via the returned {@link Single}.
 * @throws InterruptedException If interrupted while test observer is awaiting terminal event
 */
@Test
public void testSuccessfulImageIdentification() throws InterruptedException {
    Bitmap bitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ALPHA_8);
    IdentifyDocumentTextResult result = IdentifyDocumentTextResult.builder().fullText(RandomString.string()).keyValues(Collections.emptyList()).lines(Collections.emptyList()).rawLineText(Collections.emptyList()).selections(Collections.emptyList()).tables(Collections.emptyList()).words(Collections.emptyList()).build();
    doAnswer(invocation -> {
        // 0 = type, 1 = bitmap, 2 = result, 3 = error
        final int indexOfResultConsumer = 2;
        Consumer<IdentifyDocumentTextResult> onResult = invocation.getArgument(indexOfResultConsumer);
        onResult.accept(result);
        return mock(IdentifyOperation.class);
    }).when(delegate).identify(eq(IdentifyActionType.DETECT_TEXT), eq(bitmap), anyConsumer(), anyConsumer());
    TestObserver<IdentifyResult> observer = rxPredictions.identify(IdentifyActionType.DETECT_TEXT, bitmap).test();
    observer.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
    observer.assertValue(result);
}
Also used : Bitmap(android.graphics.Bitmap) IdentifyResult(com.amplifyframework.predictions.result.IdentifyResult) IdentifyDocumentTextResult(com.amplifyframework.predictions.result.IdentifyDocumentTextResult) Test(org.junit.Test)

Aggregations

Bitmap (android.graphics.Bitmap)3 IdentifyDocumentTextResult (com.amplifyframework.predictions.result.IdentifyDocumentTextResult)3 Test (org.junit.Test)3 BoundedKeyValue (com.amplifyframework.predictions.models.BoundedKeyValue)1 Cell (com.amplifyframework.predictions.models.Cell)1 Table (com.amplifyframework.predictions.models.Table)1 IdentifyResult (com.amplifyframework.predictions.result.IdentifyResult)1