Search in sources :

Example 1 with FeatureType

use of software.amazon.awssdk.services.textract.model.FeatureType in project aws-doc-sdk-examples by awsdocs.

the class StartDocumentAnalysis method startDocAnalysisS3.

// snippet-start:[textract.java2._start_doc_analysis.main]
public static String startDocAnalysisS3(TextractClient textractClient, String bucketName, String docName) {
    try {
        List<FeatureType> myList = new ArrayList<FeatureType>();
        myList.add(FeatureType.TABLES);
        myList.add(FeatureType.FORMS);
        S3Object s3Object = S3Object.builder().bucket(bucketName).name(docName).build();
        DocumentLocation location = DocumentLocation.builder().s3Object(s3Object).build();
        StartDocumentAnalysisRequest documentAnalysisRequest = StartDocumentAnalysisRequest.builder().documentLocation(location).featureTypes(myList).build();
        StartDocumentAnalysisResponse response = textractClient.startDocumentAnalysis(documentAnalysisRequest);
        // Get the job ID
        String jobId = response.jobId();
        return jobId;
    } catch (TextractException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : FeatureType(software.amazon.awssdk.services.textract.model.FeatureType) DocumentLocation(software.amazon.awssdk.services.textract.model.DocumentLocation) StartDocumentAnalysisRequest(software.amazon.awssdk.services.textract.model.StartDocumentAnalysisRequest) TextractException(software.amazon.awssdk.services.textract.model.TextractException) ArrayList(java.util.ArrayList) S3Object(software.amazon.awssdk.services.textract.model.S3Object) StartDocumentAnalysisResponse(software.amazon.awssdk.services.textract.model.StartDocumentAnalysisResponse)

Example 2 with FeatureType

use of software.amazon.awssdk.services.textract.model.FeatureType in project aws-doc-sdk-examples by awsdocs.

the class AnalyzeDocument method analyzeDoc.

// snippet-start:[textract.java2._analyze_doc.main]
public static void analyzeDoc(TextractClient textractClient, String sourceDoc) {
    try {
        InputStream sourceStream = new FileInputStream(new File(sourceDoc));
        SdkBytes sourceBytes = SdkBytes.fromInputStream(sourceStream);
        // Get the input Document object as bytes
        Document myDoc = Document.builder().bytes(sourceBytes).build();
        List<FeatureType> featureTypes = new ArrayList<FeatureType>();
        featureTypes.add(FeatureType.FORMS);
        featureTypes.add(FeatureType.TABLES);
        AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder().featureTypes(featureTypes).document(myDoc).build();
        AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest);
        List<Block> docInfo = analyzeDocument.blocks();
        Iterator<Block> blockIterator = docInfo.iterator();
        while (blockIterator.hasNext()) {
            Block block = blockIterator.next();
            System.out.println("The block type is " + block.blockType().toString());
        }
    } catch (TextractException | FileNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : FeatureType(software.amazon.awssdk.services.textract.model.FeatureType) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TextractException(software.amazon.awssdk.services.textract.model.TextractException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Document(software.amazon.awssdk.services.textract.model.Document) AnalyzeDocumentResponse(software.amazon.awssdk.services.textract.model.AnalyzeDocumentResponse) FileInputStream(java.io.FileInputStream) SdkBytes(software.amazon.awssdk.core.SdkBytes) Block(software.amazon.awssdk.services.textract.model.Block) File(java.io.File) AnalyzeDocumentRequest(software.amazon.awssdk.services.textract.model.AnalyzeDocumentRequest)

Example 3 with FeatureType

use of software.amazon.awssdk.services.textract.model.FeatureType in project aws-doc-sdk-examples by awsdocs.

the class TextractService method analyzeDoc.

public String analyzeDoc(byte[] bytes) {
    List myList = new ArrayList<String>();
    try {
        Region region = Region.US_EAST_2;
        TextractClient textractClient = TextractClient.builder().region(region).build();
        SdkBytes sourceBytes = SdkBytes.fromByteArray(bytes);
        // Get the input Document object as bytes
        Document myDoc = Document.builder().bytes(sourceBytes).build();
        List<FeatureType> featureTypes = new ArrayList<FeatureType>();
        featureTypes.add(FeatureType.FORMS);
        featureTypes.add(FeatureType.TABLES);
        AnalyzeDocumentRequest analyzeDocumentRequest = AnalyzeDocumentRequest.builder().featureTypes(featureTypes).document(myDoc).build();
        AnalyzeDocumentResponse analyzeDocument = textractClient.analyzeDocument(analyzeDocumentRequest);
        List<Block> docInfo = analyzeDocument.blocks();
        Iterator<Block> blockIterator = docInfo.iterator();
        while (blockIterator.hasNext()) {
            Block block = blockIterator.next();
            myList.add("The block type is " + block.blockType().toString());
        }
        return convertToString(toXml(myList));
    } catch (TextractException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : FeatureType(software.amazon.awssdk.services.textract.model.FeatureType) TextractClient(software.amazon.awssdk.services.textract.TextractClient) TextractException(software.amazon.awssdk.services.textract.model.TextractException) ArrayList(java.util.ArrayList) Document(software.amazon.awssdk.services.textract.model.Document) AnalyzeDocumentResponse(software.amazon.awssdk.services.textract.model.AnalyzeDocumentResponse) SdkBytes(software.amazon.awssdk.core.SdkBytes) Region(software.amazon.awssdk.regions.Region) Block(software.amazon.awssdk.services.textract.model.Block) ArrayList(java.util.ArrayList) List(java.util.List) AnalyzeDocumentRequest(software.amazon.awssdk.services.textract.model.AnalyzeDocumentRequest)

Aggregations

ArrayList (java.util.ArrayList)3 FeatureType (software.amazon.awssdk.services.textract.model.FeatureType)3 TextractException (software.amazon.awssdk.services.textract.model.TextractException)3 SdkBytes (software.amazon.awssdk.core.SdkBytes)2 AnalyzeDocumentRequest (software.amazon.awssdk.services.textract.model.AnalyzeDocumentRequest)2 AnalyzeDocumentResponse (software.amazon.awssdk.services.textract.model.AnalyzeDocumentResponse)2 Block (software.amazon.awssdk.services.textract.model.Block)2 Document (software.amazon.awssdk.services.textract.model.Document)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Region (software.amazon.awssdk.regions.Region)1 TextractClient (software.amazon.awssdk.services.textract.TextractClient)1 DocumentLocation (software.amazon.awssdk.services.textract.model.DocumentLocation)1 S3Object (software.amazon.awssdk.services.textract.model.S3Object)1 StartDocumentAnalysisRequest (software.amazon.awssdk.services.textract.model.StartDocumentAnalysisRequest)1 StartDocumentAnalysisResponse (software.amazon.awssdk.services.textract.model.StartDocumentAnalysisResponse)1