use of com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient in project java-vision by googleapis.
the class DetectWebEntitiesGcs method detectWebEntitiesGcs.
// Find web entities given the remote image on Google Cloud Storage.
public static void detectWebEntitiesGcs(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();
// Create the request with the image and the specified feature: web detection
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Feature.Type.WEB_DETECTION)).setImage(image).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());
}));
}
}
use of com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient in project java-vision by googleapis.
the class DetectWebEntitiesIncludeGeoResults method detectWebEntitiesIncludeGeoResults.
// Find web entities given a local image.
public static void detectWebEntitiesIncludeGeoResults(String filePath) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
// Read in the local image
ByteString contents = ByteString.readFrom(new FileInputStream(filePath));
// Build the image
Image image = Image.newBuilder().setContent(contents).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(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());
}));
}
}
use of com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient in project java-vision by googleapis.
the class AsyncBatchAnnotateImages method asyncBatchAnnotateImages.
public static void asyncBatchAnnotateImages(String inputImageUri, String outputUri) throws IOException, ExecutionException, InterruptedException {
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
// You can send multiple images to be annotated, this sample demonstrates how to do this with
// one image. If you want to use multiple images, you have to create a `AnnotateImageRequest`
// object for each image that you want annotated.
// First specify where the vision api can find the image
ImageSource source = ImageSource.newBuilder().setImageUri(inputImageUri).build();
Image image = Image.newBuilder().setSource(source).build();
// Set the type of annotation you want to perform on the image
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
Feature feature = Feature.newBuilder().setType(Feature.Type.LABEL_DETECTION).build();
// Build the request object for that one image. Note: for additional images you have to create
// additional `AnnotateImageRequest` objects and store them in a list to be used below.
AnnotateImageRequest imageRequest = AnnotateImageRequest.newBuilder().setImage(image).addFeatures(feature).build();
// Set where to store the results for the images that will be annotated.
GcsDestination gcsDestination = GcsDestination.newBuilder().setUri(outputUri).build();
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).setBatchSize(// The max number of responses to output in each JSON file
2).build();
// Add each `AnnotateImageRequest` object to the batch request and add the output config.
AsyncBatchAnnotateImagesRequest request = AsyncBatchAnnotateImagesRequest.newBuilder().addRequests(imageRequest).setOutputConfig(outputConfig).build();
// Make the asynchronous batch request.
AsyncBatchAnnotateImagesResponse response = imageAnnotatorClient.asyncBatchAnnotateImagesAsync(request).get();
// The output is written to GCS with the provided output_uri as prefix
String gcsOutputUri = response.getOutputConfig().getGcsDestination().getUri();
System.out.format("Output written to GCS with prefix: %s%n", gcsOutputUri);
}
}
use of com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient in project java-vision by googleapis.
the class BatchAnnotateFiles method batchAnnotateFiles.
public static void batchAnnotateFiles(String filePath) throws IOException {
// the "close" method on the client to safely clean up any remaining background resources.
try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
// You can send multiple files to be annotated, this sample demonstrates how to do this with
// one file. If you want to use multiple files, you have to create a `AnnotateImageRequest`
// object for each file that you want annotated.
// First read the files contents
Path path = Paths.get(filePath);
byte[] data = Files.readAllBytes(path);
ByteString content = ByteString.copyFrom(data);
// Specify the input config with the file's contents and its type.
// Supported mime_type: application/pdf, image/tiff, image/gif
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#inputconfig
InputConfig inputConfig = InputConfig.newBuilder().setMimeType("application/pdf").setContent(content).build();
// Set the type of annotation you want to perform on the file
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.Feature.Type
Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();
// Build the request object for that one file. Note: for additional file you have to create
// additional `AnnotateFileRequest` objects and store them in a list to be used below.
// Since we are sending a file of type `application/pdf`, we can use the `pages` field to
// specify which pages to process. The service can process up to 5 pages per document file.
// https://cloud.google.com/vision/docs/reference/rpc/google.cloud.vision.v1#google.cloud.vision.v1.AnnotateFileRequest
AnnotateFileRequest fileRequest = AnnotateFileRequest.newBuilder().setInputConfig(inputConfig).addFeatures(feature).addPages(// Process the first page
1).addPages(// Process the second page
2).addPages(// Process the last page
-1).build();
// Add each `AnnotateFileRequest` object to the batch request.
BatchAnnotateFilesRequest request = BatchAnnotateFilesRequest.newBuilder().addRequests(fileRequest).build();
// Make the synchronous batch request.
BatchAnnotateFilesResponse response = imageAnnotatorClient.batchAnnotateFiles(request);
// sample.
for (AnnotateImageResponse imageResponse : response.getResponsesList().get(0).getResponsesList()) {
System.out.format("Full text: %s%n", imageResponse.getFullTextAnnotation().getText());
for (Page page : imageResponse.getFullTextAnnotation().getPagesList()) {
for (Block block : page.getBlocksList()) {
System.out.format("%nBlock confidence: %s%n", block.getConfidence());
for (Paragraph par : block.getParagraphsList()) {
System.out.format("\tParagraph confidence: %s%n", par.getConfidence());
for (Word word : par.getWordsList()) {
System.out.format("\t\tWord confidence: %s%n", word.getConfidence());
for (Symbol symbol : word.getSymbolsList()) {
System.out.format("\t\t\tSymbol: %s, (confidence: %s)%n", symbol.getText(), symbol.getConfidence());
}
}
}
}
}
}
}
}
use of com.google.cloud.vision.v1p4beta1.ImageAnnotatorClient in project java-vision by googleapis.
the class SetEndpoint method setEndpoint.
// Change your endpoint
public static void setEndpoint() throws IOException {
// [START vision_set_endpoint]
ImageAnnotatorSettings settings = ImageAnnotatorSettings.newBuilder().setEndpoint("eu-vision.googleapis.com:443").build();
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
ImageAnnotatorClient client = ImageAnnotatorClient.create(settings);
// [END vision_set_endpoint]
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri("gs://cloud-samples-data/vision/text/screen.jpg").build();
Image image = Image.newBuilder().setSource(imgSource).build();
Feature feature = Feature.newBuilder().setType(Feature.Type.TEXT_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(image).build();
List<AnnotateImageRequest> requests = new ArrayList<>();
requests.add(request);
BatchAnnotateImagesResponse batchResponse = client.batchAnnotateImages(requests);
for (AnnotateImageResponse response : batchResponse.getResponsesList()) {
for (EntityAnnotation annotation : response.getTextAnnotationsList()) {
System.out.format("Text: %s%n", annotation.getDescription());
System.out.println("Position:");
System.out.format("%s%n", annotation.getBoundingPoly());
}
}
client.close();
}
Aggregations