Search in sources :

Example 1 with Block

use of com.amazonaws.services.textract.model.Block in project aws-doc-sdk-examples by awsdocs.

the class DocumentText method paintComponent.

// Draws the image and text bounding box.
public void paintComponent(Graphics g) {
    int height = image.getHeight(this);
    int width = image.getWidth(this);
    // Create a Java2D version of g.
    Graphics2D g2d = (Graphics2D) g;
    // Draw the image.
    g2d.drawImage(image, 0, 0, image.getWidth(this), image.getHeight(this), this);
    // Iterate through blocks and display polygons around lines of detected text.
    List<Block> blocks = result.getBlocks();
    for (Block block : blocks) {
        DisplayBlockInfo(block);
        if ((block.getBlockType()).equals("LINE")) {
            ShowPolygon(height, width, block.getGeometry().getPolygon(), g2d);
        /*
                  ShowBoundingBox(height, width, block.getGeometry().getBoundingBox(), g2d);
                 */
        } else {
            // its a word, so just show vertical lines.
            ShowPolygonVerticals(height, width, block.getGeometry().getPolygon(), g2d);
        }
    }
}
Also used : Block(com.amazonaws.services.textract.model.Block) Point(com.amazonaws.services.textract.model.Point)

Example 2 with Block

use of com.amazonaws.services.textract.model.Block in project amplify-android by aws-amplify.

the class TextractResultTransformers method doForEachRelatedBlock.

private static void doForEachRelatedBlock(Block block, Map<String, Block> blockMap, Consumer<Block> forEach) {
    if (block == null || block.getRelationships() == null) {
        return;
    }
    for (Relationship relationship : block.getRelationships()) {
        for (String id : relationship.getIds()) {
            Block relatedBlock = blockMap.get(id);
            if (relatedBlock == null) {
                continue;
            }
            forEach.accept(relatedBlock);
        }
    }
}
Also used : Relationship(com.amazonaws.services.textract.model.Relationship) Block(com.amazonaws.services.textract.model.Block)

Example 3 with Block

use of com.amazonaws.services.textract.model.Block in project amplify-android by aws-amplify.

the class TextractResultTransformersTest method testIdentifiedTextConversion.

/**
 * Tests that the individual block from Textract is converted to
 * an Amplify image text feature.
 */
@Test
public void testIdentifiedTextConversion() {
    Block block = new Block().withText(RandomString.string()).withConfidence(random.nextFloat()).withGeometry(randomGeometry()).withPage(random.nextInt());
    // Test block conversion
    IdentifiedText text = TextractResultTransformers.fetchIdentifiedText(block);
    assertEquals(block.getText(), text.getText());
    assertEquals(block.getConfidence(), text.getConfidence(), DELTA);
    assertEquals((int) block.getPage(), text.getPage());
}
Also used : IdentifiedText(com.amplifyframework.predictions.models.IdentifiedText) Block(com.amazonaws.services.textract.model.Block) Test(org.junit.Test)

Example 4 with Block

use of com.amazonaws.services.textract.model.Block in project amplify-android by aws-amplify.

the class TextractResultTransformersTest method testSelectionConversion.

/**
 * Tests that the individual block from Textract is properly
 * converted to an Amplify selection item.
 */
@Test
public void testSelectionConversion() {
    Block block;
    Selection selection;
    // Assert that SELECTED sets it to selected
    block = new Block().withSelectionStatus(SelectionStatus.SELECTED).withGeometry(randomGeometry());
    selection = TextractResultTransformers.fetchSelection(block);
    assertTrue(selection.isSelected());
    // Assert that NOT_SELECTED sets it to not selected
    block = new Block().withSelectionStatus(SelectionStatus.NOT_SELECTED).withGeometry(randomGeometry());
    selection = TextractResultTransformers.fetchSelection(block);
    assertFalse(selection.isSelected());
}
Also used : Selection(com.amplifyframework.predictions.models.Selection) Block(com.amazonaws.services.textract.model.Block) Test(org.junit.Test)

Example 5 with Block

use of com.amazonaws.services.textract.model.Block 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)

Aggregations

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