Search in sources :

Example 1 with Table

use of com.amplifyframework.predictions.models.Table in project amplify-android by aws-amplify.

the class TextractResultTransformersTest method testTableConversion.

/**
 * Tests that the graph of related blocks from Textract is properly
 * converted to an Amplify table item.
 */
@Test
public void testTableConversion() {
    Block cellTextBlock = new Block().withId(RandomString.string()).withText(RandomString.string());
    Block cellSelectionBlock = new Block().withId(RandomString.string()).withSelectionStatus(SelectionStatus.SELECTED);
    Block cellBlock = new Block().withId(RandomString.string()).withBlockType(BlockType.CELL).withConfidence(random.nextFloat()).withGeometry(randomGeometry()).withRowIndex(random.nextInt()).withColumnIndex(random.nextInt()).withRelationships(new Relationship().withIds(cellTextBlock.getId(), cellSelectionBlock.getId()));
    Block tableBlock = new Block().withId(RandomString.string()).withBlockType(BlockType.TABLE).withConfidence(random.nextFloat()).withGeometry(randomGeometry()).withRelationships(new Relationship().withIds(cellBlock.getId()));
    // Construct a map to act as a graph
    Map<String, Block> blockMap = new HashMap<>();
    Observable.fromArray(cellTextBlock, cellSelectionBlock, cellBlock, tableBlock).blockingForEach(block -> blockMap.put(block.getId(), block));
    // Test table block conversion
    Table table = TextractResultTransformers.fetchTable(tableBlock, blockMap);
    assertEquals(1, table.getCells().size());
    assertEquals(tableBlock.getConfidence(), table.getConfidence(), DELTA);
    assertEquals(1, table.getRowSize());
    assertEquals(1, table.getColumnSize());
    // Test cell block conversion
    Cell cell = table.getCells().iterator().next();
    assertEquals(cellTextBlock.getText(), cell.getText());
    assertEquals(cellBlock.getConfidence(), cell.getConfidence(), DELTA);
    assertTrue(cell.isSelected());
    assertEquals(cellBlock.getRowIndex() - 1, cell.getRow());
    assertEquals(cellBlock.getColumnIndex() - 1, cell.getColumn());
}
Also used : Table(com.amplifyframework.predictions.models.Table) HashMap(java.util.HashMap) Relationship(com.amazonaws.services.textract.model.Relationship) Block(com.amazonaws.services.textract.model.Block) RandomString(com.amplifyframework.testutils.random.RandomString) Cell(com.amplifyframework.predictions.models.Cell) Test(org.junit.Test)

Example 2 with Table

use of com.amplifyframework.predictions.models.Table in project amplify-android by aws-amplify.

the class AWSTextractService method processTextractBlocks.

private IdentifyDocumentTextResult processTextractBlocks(List<Block> blocks) {
    StringBuilder fullTextBuilder = new StringBuilder();
    List<String> rawLineText = new ArrayList<>();
    List<IdentifiedText> words = new ArrayList<>();
    List<IdentifiedText> lines = new ArrayList<>();
    List<Selection> selections = new ArrayList<>();
    List<Table> tables = new ArrayList<>();
    List<BoundedKeyValue> keyValues = new ArrayList<>();
    List<Block> tableBlocks = new ArrayList<>();
    List<Block> keyValueBlocks = new ArrayList<>();
    Map<String, Block> blockMap = new HashMap<>();
    for (Block block : blocks) {
        // This is the map that will be used for traversing the graph.
        // Each block can contain "relationships", which point to other blocks by ID.
        String id = block.getId();
        blockMap.put(id, block);
        BlockType type = BlockType.fromValue(block.getBlockType());
        switch(type) {
            case LINE:
                rawLineText.add(block.getText());
                lines.add(TextractResultTransformers.fetchIdentifiedText(block));
                continue;
            case WORD:
                fullTextBuilder.append(block.getText()).append(" ");
                words.add(TextractResultTransformers.fetchIdentifiedText(block));
                continue;
            case SELECTION_ELEMENT:
                selections.add(TextractResultTransformers.fetchSelection(block));
                continue;
            case TABLE:
                tableBlocks.add(block);
                continue;
            case KEY_VALUE_SET:
                keyValueBlocks.add(block);
                continue;
            default:
        }
    }
    for (Block tableBlock : tableBlocks) {
        Table table = TextractResultTransformers.fetchTable(tableBlock, blockMap);
        if (table != null) {
            tables.add(table);
        }
    }
    for (Block keyValueBlock : keyValueBlocks) {
        BoundedKeyValue keyValue = TextractResultTransformers.fetchKeyValue(keyValueBlock, blockMap);
        if (keyValue != null) {
            keyValues.add(keyValue);
        }
    }
    return IdentifyDocumentTextResult.builder().fullText(fullTextBuilder.toString().trim()).rawLineText(rawLineText).lines(lines).words(words).selections(selections).tables(tables).keyValues(keyValues).build();
}
Also used : BoundedKeyValue(com.amplifyframework.predictions.models.BoundedKeyValue) IdentifiedText(com.amplifyframework.predictions.models.IdentifiedText) Table(com.amplifyframework.predictions.models.Table) HashMap(java.util.HashMap) Selection(com.amplifyframework.predictions.models.Selection) ArrayList(java.util.ArrayList) BlockType(com.amazonaws.services.textract.model.BlockType) Block(com.amazonaws.services.textract.model.Block)

Example 3 with Table

use of com.amplifyframework.predictions.models.Table 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)

Aggregations

Table (com.amplifyframework.predictions.models.Table)3 Block (com.amazonaws.services.textract.model.Block)2 Cell (com.amplifyframework.predictions.models.Cell)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Bitmap (android.graphics.Bitmap)1 BlockType (com.amazonaws.services.textract.model.BlockType)1 Relationship (com.amazonaws.services.textract.model.Relationship)1 BoundedKeyValue (com.amplifyframework.predictions.models.BoundedKeyValue)1 IdentifiedText (com.amplifyframework.predictions.models.IdentifiedText)1 Selection (com.amplifyframework.predictions.models.Selection)1 IdentifyDocumentTextResult (com.amplifyframework.predictions.result.IdentifyDocumentTextResult)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 ArrayList (java.util.ArrayList)1