Search in sources :

Example 1 with IdentifiedText

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

the class RekognitionResultTransformersTest method testTextDetectionConversion.

/**
 * Tests that the text detection from Rekognition is converted
 * to an Amplify image text feature.
 */
@Test
public void testTextDetectionConversion() {
    TextDetection detection = new TextDetection().withDetectedText(RandomString.string()).withConfidence(random.nextFloat()).withGeometry(randomGeometry());
    // Test text detection conversion
    IdentifiedText text = RekognitionResultTransformers.fromTextDetection(detection);
    assertEquals(detection.getDetectedText(), text.getText());
    assertEquals(detection.getConfidence(), text.getConfidence(), DELTA);
}
Also used : TextDetection(com.amazonaws.services.rekognition.model.TextDetection) IdentifiedText(com.amplifyframework.predictions.models.IdentifiedText) Test(org.junit.Test)

Example 2 with IdentifiedText

use of com.amplifyframework.predictions.models.IdentifiedText 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 3 with IdentifiedText

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

the class AWSRekognitionService method detectPlainText.

private IdentifyTextResult detectPlainText(ByteBuffer imageData) throws PredictionsException {
    DetectTextRequest request = new DetectTextRequest().withImage(new Image().withBytes(imageData));
    // Read text in the given image via Amazon Rekognition
    final DetectTextResult result;
    try {
        result = rekognition.detectText(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting text.", serviceException, "See attached service exception for more details.");
    }
    StringBuilder fullTextBuilder = new StringBuilder();
    List<String> rawLineText = new ArrayList<>();
    List<IdentifiedText> words = new ArrayList<>();
    List<IdentifiedText> lines = new ArrayList<>();
    for (TextDetection detection : result.getTextDetections()) {
        TextTypes type = TextTypes.fromValue(detection.getType());
        switch(type) {
            case LINE:
                rawLineText.add(detection.getDetectedText());
                lines.add(RekognitionResultTransformers.fromTextDetection(detection));
                continue;
            case WORD:
                fullTextBuilder.append(detection.getDetectedText()).append(" ");
                words.add(RekognitionResultTransformers.fromTextDetection(detection));
                continue;
            default:
        }
    }
    return IdentifyTextResult.builder().fullText(fullTextBuilder.toString().trim()).rawLineText(rawLineText).lines(lines).words(words).build();
}
Also used : IdentifiedText(com.amplifyframework.predictions.models.IdentifiedText) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) Image(com.amazonaws.services.rekognition.model.Image) DetectTextResult(com.amazonaws.services.rekognition.model.DetectTextResult) TextTypes(com.amazonaws.services.rekognition.model.TextTypes) TextDetection(com.amazonaws.services.rekognition.model.TextDetection) DetectTextRequest(com.amazonaws.services.rekognition.model.DetectTextRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 4 with IdentifiedText

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

Aggregations

IdentifiedText (com.amplifyframework.predictions.models.IdentifiedText)4 TextDetection (com.amazonaws.services.rekognition.model.TextDetection)2 Block (com.amazonaws.services.textract.model.Block)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 AmazonClientException (com.amazonaws.AmazonClientException)1 DetectTextRequest (com.amazonaws.services.rekognition.model.DetectTextRequest)1 DetectTextResult (com.amazonaws.services.rekognition.model.DetectTextResult)1 Image (com.amazonaws.services.rekognition.model.Image)1 TextTypes (com.amazonaws.services.rekognition.model.TextTypes)1 BlockType (com.amazonaws.services.textract.model.BlockType)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 BoundedKeyValue (com.amplifyframework.predictions.models.BoundedKeyValue)1 Selection (com.amplifyframework.predictions.models.Selection)1 Table (com.amplifyframework.predictions.models.Table)1 HashMap (java.util.HashMap)1