Search in sources :

Example 76 with Feature

use of com.google.cloud.vision.v1p3beta1.Feature in project java-vision by googleapis.

the class DetectWebEntitiesIncludeGeoResultsGcs method detectWebEntitiesIncludeGeoResultsGcs.

// Find web entities given the remote image on Google Cloud Storage.
public static void detectWebEntitiesIncludeGeoResultsGcs(String gcsPath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        // Set the image source to the given gs uri
        ImageSource imageSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
        // Build the image
        Image image = Image.newBuilder().setSource(imageSource).build();
        // Enable `IncludeGeoResults`
        WebDetectionParams webDetectionParams = WebDetectionParams.newBuilder().setIncludeGeoResults(true).build();
        // Set the parameters for the image
        ImageContext imageContext = ImageContext.newBuilder().setWebDetectionParams(webDetectionParams).build();
        // Create the request with the image, imageContext, and the specified feature: web detection
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)).setImage(image).setImageContext(imageContext).build();
        // Perform the request
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(Arrays.asList(request));
        // Display the results
        response.getResponsesList().stream().forEach(r -> r.getWebDetection().getWebEntitiesList().stream().forEach(entity -> {
            System.out.format("Description: %s%n", entity.getDescription());
            System.out.format("Score: %f%n", entity.getScore());
        }));
    }
}
Also used : WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) WebDetectionParams(com.google.cloud.vision.v1.WebDetectionParams) Arrays(java.util.Arrays) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageContext(com.google.cloud.vision.v1.ImageContext) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) IOException(java.io.IOException) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ImageSource(com.google.cloud.vision.v1.ImageSource) Image(com.google.cloud.vision.v1.Image) ImageContext(com.google.cloud.vision.v1.ImageContext) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 77 with Feature

use of com.google.cloud.vision.v1p3beta1.Feature in project java-vision by googleapis.

the class Detect method detectDocumentTextGcs.

// [END vision_fulltext_detection]
/**
 * 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.
 * @throws Exception on errors while closing the client.
 * @throws IOException on Input/Output errors.
 */
// [START vision_fulltext_detection_gcs]
public static void detectDocumentTextGcs(String gcsPath) throws 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);
    // the "close" method on the client to safely clean up any remaining background resources.
    try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
        BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        client.close();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.format("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();
                                System.out.format("Symbol text: %s (confidence: %f)%n", symbol.getText(), symbol.getConfidence());
                            }
                            System.out.format("Word text: %s (confidence: %f)%n%n", wordText, word.getConfidence());
                            paraText = String.format("%s %s", paraText, wordText);
                        }
                        // Output Example using Paragraph:
                        System.out.println("%nParagraph: %n" + paraText);
                        System.out.format("Paragraph Confidence: %f%n", para.getConfidence());
                        blockText = blockText + paraText;
                    }
                    pageText = pageText + blockText;
                }
            }
            System.out.println("%nComplete annotation:");
            System.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) Page(com.google.cloud.vision.v1.Page) ByteString(com.google.protobuf.ByteString) 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)

Example 78 with Feature

use of com.google.cloud.vision.v1p3beta1.Feature in project java-vision by googleapis.

the class ITSystemTest method detectWebEntitiesGcsTest.

@Test
public void detectWebEntitiesGcsTest() throws IOException {
    ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + "landmark/pofa.jpg").build();
    Image img = Image.newBuilder().setSource(imgSource).build();
    Feature feat = Feature.newBuilder().setType(Type.WEB_DETECTION).setMaxResults(MAX_RESULTS).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    List<AnnotateImageResponse> responses = response.getResponsesList();
    List<String> actual = new ArrayList<>();
    for (AnnotateImageResponse imgResponse : responses) {
        for (WebDetection.WebEntity entity : imgResponse.getWebDetection().getWebEntitiesList()) {
            actual.add(entity.getDescription());
        }
    }
    assertThat(actual).contains("The Palace Of Fine Arts");
}
Also used : WebDetection(com.google.cloud.vision.v1.WebDetection) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) ArrayList(java.util.ArrayList) ImageSource(com.google.cloud.vision.v1.ImageSource) ByteString(com.google.protobuf.ByteString) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse) Test(org.junit.Test)

Example 79 with Feature

use of com.google.cloud.vision.v1p3beta1.Feature in project java-vision by googleapis.

the class ITSystemTest method getResponsesList.

private static List<AnnotateImageResponse> getResponsesList(String image, Type type, boolean isGcs) throws IOException {
    ImageSource imgSource;
    Image img;
    if (isGcs) {
        imgSource = ImageSource.newBuilder().setGcsImageUri(SAMPLE_BUCKET + image).build();
        img = Image.newBuilder().setSource(imgSource).build();
    } else {
        ByteString imgBytes = ByteString.readFrom(new FileInputStream(RESOURCES + image));
        img = Image.newBuilder().setContent(imgBytes).build();
    }
    Feature feat = Feature.newBuilder().setType(type).build();
    AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
    BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(ImmutableList.of(request));
    return response.getResponsesList();
}
Also used : AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) ByteString(com.google.protobuf.ByteString) ImageSource(com.google.cloud.vision.v1.ImageSource) ReferenceImage(com.google.cloud.vision.v1.ReferenceImage) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) FileInputStream(java.io.FileInputStream) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 80 with Feature

use of com.google.cloud.vision.v1p3beta1.Feature in project google-cloud-java by GoogleCloudPlatform.

the class VideoIntelligenceServiceClientTest method annotateVideoTest.

@Test
@SuppressWarnings("all")
public void annotateVideoTest() throws Exception {
    AnnotateVideoResponse expectedResponse = AnnotateVideoResponse.newBuilder().build();
    Operation resultOperation = Operation.newBuilder().setName("annotateVideoTest").setDone(true).setResponse(Any.pack(expectedResponse)).build();
    mockVideoIntelligenceService.addResponse(resultOperation);
    String inputUri = "inputUri1707300727";
    List<Feature> features = new ArrayList<>();
    VideoContext videoContext = VideoContext.newBuilder().build();
    String outputUri = "outputUri-1273518802";
    String locationId = "locationId552319461";
    AnnotateVideoResponse actualResponse = client.annotateVideoAsync(inputUri, features, videoContext, outputUri, locationId).get();
    Assert.assertEquals(expectedResponse, actualResponse);
    List<GeneratedMessageV3> actualRequests = mockVideoIntelligenceService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    AnnotateVideoRequest actualRequest = (AnnotateVideoRequest) actualRequests.get(0);
    Assert.assertEquals(inputUri, actualRequest.getInputUri());
    Assert.assertEquals(features, actualRequest.getFeaturesList());
    Assert.assertEquals(videoContext, actualRequest.getVideoContext());
    Assert.assertEquals(outputUri, actualRequest.getOutputUri());
    Assert.assertEquals(locationId, actualRequest.getLocationId());
}
Also used : AnnotateVideoRequest(com.google.cloud.videointelligence.v1beta1.AnnotateVideoRequest) ArrayList(java.util.ArrayList) VideoContext(com.google.cloud.videointelligence.v1beta1.VideoContext) Operation(com.google.longrunning.Operation) Feature(com.google.cloud.videointelligence.v1beta1.Feature) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) AnnotateVideoResponse(com.google.cloud.videointelligence.v1beta1.AnnotateVideoResponse) Test(org.junit.Test)

Aggregations

Feature (org.osate.aadl2.Feature)86 ArrayList (java.util.ArrayList)82 Feature (com.google.cloud.vision.v1.Feature)74 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)71 Image (com.google.cloud.vision.v1.Image)71 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)68 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)68 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)65 ByteString (com.google.protobuf.ByteString)48 ImageSource (com.google.cloud.vision.v1.ImageSource)39 Subcomponent (org.osate.aadl2.Subcomponent)31 FileInputStream (java.io.FileInputStream)30 Classifier (org.osate.aadl2.Classifier)29 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 NamedElement (org.osate.aadl2.NamedElement)24 ComponentClassifier (org.osate.aadl2.ComponentClassifier)22 FeatureGroup (org.osate.aadl2.FeatureGroup)20 FeatureGroupType (org.osate.aadl2.FeatureGroupType)19 FeatureInstance (org.osate.aadl2.instance.FeatureInstance)19