Search in sources :

Example 11 with TextAnnotation

use of com.google.cloud.vision.v1p4beta1.TextAnnotation in project kie-wb-common by kiegroup.

the class ArtifactsConverterTest method convertTextAnnotation.

@Test
public void convertTextAnnotation() {
    TextAnnotation element = Bpmn2Factory.eINSTANCE.createTextAnnotation();
    when(typedFactoryManager.newNode(any(), eq(org.kie.workbench.common.stunner.bpmn.definition.TextAnnotation.class))).thenReturn(nodeTextAnnotation);
    when(nodeTextAnnotation.getContent()).thenReturn(contentTextAnnotation);
    when(contentTextAnnotation.getDefinition()).thenReturn(defTextAnnotation);
    when(propertyReaderFactory.of(element)).thenReturn(readerTextAnnotation);
    final Result<BpmnNode> node = tested.convert(element);
    final Node<? extends View<? extends BPMNViewDefinition>, ?> value = node.value().value();
    assertEquals(contentTextAnnotation, value.getContent());
    assertEquals(defTextAnnotation, value.getContent().getDefinition());
}
Also used : BpmnNode(org.kie.workbench.common.stunner.bpmn.backend.converters.tostunner.BpmnNode) TextAnnotation(org.eclipse.bpmn2.TextAnnotation) Test(org.junit.Test)

Example 12 with TextAnnotation

use of com.google.cloud.vision.v1p4beta1.TextAnnotation in project spring-cloud-gcp by spring-cloud.

the class DocumentOcrResultSet method getAllPages.

/**
 * Returns an {@link Iterator} over all the OCR pages of the document.
 *
 * @return iterator of {@link TextAnnotation} describing OCR content of each page in the
 * document.
 */
public Iterator<TextAnnotation> getAllPages() {
    return new Iterator<TextAnnotation>() {

        private final Iterator<OcrPageRange> pageRangeIterator = ocrPageRanges.values().iterator();

        private int offset = 0;

        private List<TextAnnotation> currentPageRange = Collections.EMPTY_LIST;

        @Override
        public boolean hasNext() {
            return pageRangeIterator.hasNext() || offset < currentPageRange.size();
        }

        @Override
        public TextAnnotation next() {
            if (!hasNext()) {
                throw new NoSuchElementException("No more pages left in DocumentOcrResultSet.");
            }
            if (offset >= currentPageRange.size()) {
                OcrPageRange pageRange = pageRangeIterator.next();
                offset = 0;
                try {
                    currentPageRange = pageRange.getPages();
                } catch (InvalidProtocolBufferException e) {
                    throw new RuntimeException("Failed to parse OCR output from JSON output file " + pageRange.getBlob().getName(), e);
                }
            }
            TextAnnotation result = currentPageRange.get(offset);
            offset++;
            return result;
        }
    };
}
Also used : Iterator(java.util.Iterator) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) List(java.util.List) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) NoSuchElementException(java.util.NoSuchElementException)

Example 13 with TextAnnotation

use of com.google.cloud.vision.v1p4beta1.TextAnnotation in project spring-cloud-gcp by spring-cloud.

the class WebController method renderViewDocumentPage.

@GetMapping("/viewDocument")
public ModelAndView renderViewDocumentPage(@RequestParam("gcsDocumentUrl") String gcsDocumentUrl, @RequestParam("pageNumber") int pageNumber, ModelMap map) throws ExecutionException, InterruptedException, InvalidProtocolBufferException {
    TextAnnotation textAnnotation = ocrStatusReporter.getDocumentOcrStatuses().get(gcsDocumentUrl).getResultSet().getPage(pageNumber);
    String[] firstWordsTokens = textAnnotation.getText().split(" ", 50);
    map.put("pageNumber", pageNumber);
    map.put("gcsDocumentUrl", gcsDocumentUrl);
    map.put("text", String.join(" ", firstWordsTokens));
    return new ModelAndView("viewDocument", map);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 14 with TextAnnotation

use of com.google.cloud.vision.v1p4beta1.TextAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectDocumentText.

// [START vision_detect_document]
/**
 * Performs document text detection on a local image file.
 *
 * @param filePath The path to the local file to detect document text on.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectDocumentText(String filePath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
    Image img = Image.newBuilder().setContent(imgBytes).build();
    Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            TextAnnotation annotation = res.getFullTextAnnotation();
            for (Page page : annotation.getPagesList()) {
                String pageText = "";
                for (Block block : page.getBlocksList()) {
                    String blockText = "";
                    for (Paragraph para : block.getParagraphsList()) {
                        String paraText = "";
                        for (Word word : para.getWordsList()) {
                            String wordText = "";
                            for (Symbol symbol : word.getSymbolsList()) {
                                wordText = wordText + symbol.getText();
                                out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), symbol.getConfidence());
                            }
                            out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        out.println("\nParagraph: \n" + paraText);
                        out.format("Paragraph Confidence: %f\n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            out.println("\nComplete annotation:");
            out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1.Word) ByteString(com.google.protobuf.ByteString) Symbol(com.google.cloud.vision.v1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) Paragraph(com.google.cloud.vision.v1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Block(com.google.cloud.vision.v1.Block) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 15 with TextAnnotation

use of com.google.cloud.vision.v1p4beta1.TextAnnotation in project java-docs-samples by GoogleCloudPlatform.

the class Detect method detectDocumentTextGcs.

// [END vision_detect_document]
// [START vision_detect_document_uri]
/**
 * Performs document text detection on a remote image on Google Cloud Storage.
 *
 * @param gcsPath The path to the remote file on Google Cloud Storage to detect document text on.
 * @param out A {@link PrintStream} to write the results to.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
public static void detectDocumentTextGcs(String gcsPath, PrintStream out) throws Exception, IOException {
    List<AnnotateImageRequest> requests = new ArrayList<>();
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.DOCUMENT_TEXT_DETECTION).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    requests.add(request);
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            // For full list of available annotations, see http://g.co/cloud/vision/docs
            TextAnnotation annotation = res.getFullTextAnnotation();
            for (Page page : annotation.getPagesList()) {
                String pageText = "";
                for (Block block : page.getBlocksList()) {
                    String blockText = "";
                    for (Paragraph para : block.getParagraphsList()) {
                        String paraText = "";
                        for (Word word : para.getWordsList()) {
                            String wordText = "";
                            for (Symbol symbol : word.getSymbolsList()) {
                                wordText = wordText + symbol.getText();
                                out.format("Symbol text: %s (confidence: %f)\n", symbol.getText(), symbol.getConfidence());
                            }
                            out.format("Word text: %s (confidence: %f)\n\n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        out.println("\nParagraph: \n" + paraText);
                        out.format("Paragraph Confidence: %f\n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            out.println("\nComplete annotation:");
            out.println(annotation.getText());
        }
    }
}
Also used : Word(com.google.cloud.vision.v1.Word) Symbol(com.google.cloud.vision.v1.Symbol) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) WebPage(com.google.cloud.vision.v1.WebDetection.WebPage) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) WebImage(com.google.cloud.vision.v1.WebDetection.WebImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) Paragraph(com.google.cloud.vision.v1.Paragraph) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Block(com.google.cloud.vision.v1.Block) ImageSource(com.google.cloud.vision.v1.ImageSource) TextAnnotation(com.google.cloud.vision.v1.TextAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Aggregations

ArrayList (java.util.ArrayList)15 TextAnnotation (com.google.cloud.vision.v1.TextAnnotation)12 Test (org.junit.Test)11 ByteString (com.google.protobuf.ByteString)9 TextAnnotation (com.google.cloud.videointelligence.v1.TextAnnotation)8 VideoAnnotationResults (com.google.cloud.videointelligence.v1.VideoAnnotationResults)8 List (java.util.List)7 TextAnnotation (org.kie.workbench.common.dmn.api.definition.v1_1.TextAnnotation)7 FreeTextAnnotation (org.opencastproject.metadata.mpeg7.FreeTextAnnotation)7 TextAnnotation (org.opencastproject.metadata.mpeg7.TextAnnotation)7 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)6 Duration (com.google.protobuf.Duration)6 AnnotateVideoProgress (com.google.cloud.videointelligence.v1.AnnotateVideoProgress)4 AnnotateVideoRequest (com.google.cloud.videointelligence.v1.AnnotateVideoRequest)4 AnnotateVideoResponse (com.google.cloud.videointelligence.v1.AnnotateVideoResponse)4 NormalizedVertex (com.google.cloud.videointelligence.v1.NormalizedVertex)4 TextFrame (com.google.cloud.videointelligence.v1.TextFrame)4 TextSegment (com.google.cloud.videointelligence.v1.TextSegment)4 VideoIntelligenceServiceClient (com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient)4 VideoSegment (com.google.cloud.videointelligence.v1.VideoSegment)4