use of com.google.cloud.vision.v1p3beta1.ImageSource in project java-vision by googleapis.
the class DetectLandmarksUrl method detectLandmarksUrl.
// Detects landmarks in the specified URI.
public static void detectLandmarksUrl(String uri) throws IOException {
List<AnnotateImageRequest> requests = new ArrayList<>();
ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_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();
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
for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
LocationInfo info = annotation.getLocationsList().listIterator().next();
System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
}
}
}
}
use of com.google.cloud.vision.v1p3beta1.ImageSource in project java-vision by googleapis.
the class DetectBeta method detectLocalizedObjectsGcs.
// [END vision_localize_objects_beta]
// [START vision_localize_objects_gcs_beta]
/**
* Detects localized objects in a remote image on Google Cloud Storage.
*
* @param gcsPath The path to the remote file on Google Cloud Storage to detect localized objects
* on.
* @param out A {@link PrintStream} to write detected objects to.
* @throws Exception on errors while closing the client.
* @throws IOException on Input/Output errors.
*/
public static void detectLocalizedObjectsGcs(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();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(Feature.newBuilder().setType(Type.OBJECT_LOCALIZATION)).setImage(img).build();
requests.add(request);
// Perform the request
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
client.close();
// Display the results
for (AnnotateImageResponse res : responses) {
for (LocalizedObjectAnnotation entity : res.getLocalizedObjectAnnotationsList()) {
out.format("Object name: %s\n", entity.getName());
out.format("Confidence: %s\n", entity.getScore());
out.format("Normalized Vertices:\n");
entity.getBoundingPoly().getNormalizedVerticesList().forEach(vertex -> out.format("- (%s, %s)\n", vertex.getX(), vertex.getY()));
}
}
}
}
use of com.google.cloud.vision.v1p3beta1.ImageSource in project java-vision by googleapis.
the class ProductSearch method getSimilarProductsGcs.
// [END vision_product_search_get_similar_products]
// [START vision_product_search_get_similar_products_gcs]
/**
* Search similar products to image in Google Cloud Storage.
*
* @param projectId - Id of the project.
* @param computeRegion - Region name.
* @param productSetId - Id of the product set.
* @param productCategory - Category of the product.
* @param gcsUri - GCS file path of the image to be searched
* @param filter - Condition to be applied on the labels. Example for filter: (color = red OR
* color = blue) AND style = kids It will search on all products with the following labels:
* color:red AND style:kids color:blue AND style:kids
* @throws Exception - on errors.
*/
public static void getSimilarProductsGcs(String projectId, String computeRegion, String productSetId, String productCategory, String gcsUri, String filter) throws Exception {
try (ImageAnnotatorClient queryImageClient = ImageAnnotatorClient.create()) {
// Get the full path of the product set.
String productSetPath = ProductSetName.of(projectId, computeRegion, productSetId).toString();
// Get the image from Google Cloud Storage
ImageSource source = ImageSource.newBuilder().setGcsImageUri(gcsUri).build();
// Create annotate image request along with product search feature.
Feature featuresElement = Feature.newBuilder().setType(Type.PRODUCT_SEARCH).build();
Image image = Image.newBuilder().setSource(source).build();
ImageContext imageContext = ImageContext.newBuilder().setProductSearchParams(ProductSearchParams.newBuilder().setProductSet(productSetPath).addProductCategories(productCategory).setFilter(filter)).build();
AnnotateImageRequest annotateImageRequest = AnnotateImageRequest.newBuilder().addFeatures(featuresElement).setImage(image).setImageContext(imageContext).build();
List<AnnotateImageRequest> requests = Arrays.asList(annotateImageRequest);
// Search products similar to the image.
BatchAnnotateImagesResponse response = queryImageClient.batchAnnotateImages(requests);
List<Result> similarProducts = response.getResponses(0).getProductSearchResults().getResultsList();
System.out.println("Similar Products: ");
for (Result product : similarProducts) {
System.out.println(String.format("\nProduct name: %s", product.getProduct().getName()));
System.out.println(String.format("Product display name: %s", product.getProduct().getDisplayName()));
System.out.println(String.format("Product description: %s", product.getProduct().getDescription()));
System.out.println(String.format("Score(Confidence): %s", product.getScore()));
System.out.println(String.format("Image name: %s", product.getImage()));
}
}
}
use of com.google.cloud.vision.v1p3beta1.ImageSource in project java-docs-samples by GoogleCloudPlatform.
the class Detect method detectWebDetectionsGcs.
// [END vision_detect_web]
// [START vision_detect_web_uri]
/**
* Detects whether the remote image on Google Cloud Storage has features you would want to
* moderate.
*
* @param gcsPath The path to the remote on Google Cloud Storage file to detect web annotations.
* @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 detectWebDetectionsGcs(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.WEB_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();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
out.printf("Error: %s\n", res.getError().getMessage());
return;
}
// Search the web for usages of the image. You could use these signals later
// for user input moderation or linking external references.
// For a full list of available annotations, see http://g.co/cloud/vision/docs
WebDetection annotation = res.getWebDetection();
out.println("Entity:Id:Score");
out.println("===============");
for (WebEntity entity : annotation.getWebEntitiesList()) {
out.println(entity.getDescription() + " : " + entity.getEntityId() + " : " + entity.getScore());
}
for (WebLabel label : annotation.getBestGuessLabelsList()) {
out.format("\nBest guess label: %s", label.getLabel());
}
out.println("\nPages with matching images: Score\n==");
for (WebPage page : annotation.getPagesWithMatchingImagesList()) {
out.println(page.getUrl() + " : " + page.getScore());
}
out.println("\nPages with partially matching images: Score\n==");
for (WebImage image : annotation.getPartialMatchingImagesList()) {
out.println(image.getUrl() + " : " + image.getScore());
}
out.println("\nPages with fully matching images: Score\n==");
for (WebImage image : annotation.getFullMatchingImagesList()) {
out.println(image.getUrl() + " : " + image.getScore());
}
out.println("\nPages with visually similar images: Score\n==");
for (WebImage image : annotation.getVisuallySimilarImagesList()) {
out.println(image.getUrl() + " : " + image.getScore());
}
}
}
}
use of com.google.cloud.vision.v1p3beta1.ImageSource in project java-docs-samples by GoogleCloudPlatform.
the class Detect method detectWebEntitiesGcs.
/**
* Find web entities given the remote image on Google Cloud Storage.
* @param gcsPath The path to the remote file on Google Cloud Storage to detect web entities.
* @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 detectWebEntitiesGcs(String gcsPath, PrintStream out) throws Exception, IOException {
// Instantiates a client
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(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());
}));
}
}
Aggregations