use of com.google.cloud.automl.v1.Image in project java-docs-samples by GoogleCloudPlatform.
the class ListImages method listImages.
// [START compute_images_list]
// Prints a list of all non-deprecated image names available in given project.
public static void listImages(String project) throws IOException {
// safely clean up any remaining background resources.
try (ImagesClient imagesClient = ImagesClient.create()) {
// Listing only non-deprecated images to reduce the size of the reply.
ListImagesRequest imagesRequest = ListImagesRequest.newBuilder().setProject(project).setMaxResults(100).setFilter("deprecated.state != DEPRECATED").build();
// Although the `setMaxResults` parameter is specified in the request, the iterable returned
// by the `list()` method hides the pagination mechanic. The library makes multiple
// requests to the API for you, so you can simply iterate over all the images.
int imageCount = 0;
for (Image image : imagesClient.list(imagesRequest).iterateAll()) {
imageCount++;
System.out.println(image.getName());
}
System.out.printf("Image count in %s is: %s", project, imageCount);
}
}
use of com.google.cloud.automl.v1.Image in project java-docs-samples by GoogleCloudPlatform.
the class ImageMagick method accept.
// [END functions_imagemagick_setup]
// [START functions_imagemagick_analyze]
@Override
public // Blurs uploaded images that are flagged as Adult or Violence.
void accept(GcsEvent event, Context context) {
// Validate parameters
if (event.getBucket() == null || event.getName() == null) {
logger.severe("Error: Malformed GCS event.");
return;
}
BlobInfo blobInfo = BlobInfo.newBuilder(event.getBucket(), event.getName()).build();
// Construct URI to GCS bucket and file.
String gcsPath = String.format("gs://%s/%s", event.getBucket(), event.getName());
logger.info(String.format("Analyzing %s", event.getName()));
// Construct request.
ImageSource imgSource = ImageSource.newBuilder().setImageUri(gcsPath).build();
Image img = Image.newBuilder().setSource(imgSource).build();
Feature feature = Feature.newBuilder().setType(Type.SAFE_SEARCH_DETECTION).build();
AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feature).setImage(img).build();
List<AnnotateImageRequest> requests = List.of(request);
// Send request to the Vision API.
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
List<AnnotateImageResponse> responses = response.getResponsesList();
for (AnnotateImageResponse res : responses) {
if (res.hasError()) {
logger.info(String.format("Error: %s", res.getError().getMessage()));
return;
}
// Get Safe Search Annotations
SafeSearchAnnotation annotation = res.getSafeSearchAnnotation();
if (annotation.getAdultValue() == 5 || annotation.getViolenceValue() == 5) {
logger.info(String.format("Detected %s as inappropriate.", event.getName()));
blur(blobInfo);
} else {
logger.info(String.format("Detected %s as OK.", event.getName()));
}
}
} catch (IOException e) {
logger.log(Level.SEVERE, "Error with Vision API: " + e.getMessage(), e);
}
}
use of com.google.cloud.automl.v1.Image in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method createImage.
private static Image createImage(Disk srcDisk) throws IOException, InterruptedException, ExecutionException {
try (ImagesClient imagesClient = ImagesClient.create()) {
Image image = Image.newBuilder().setName("test-img-" + UUID.randomUUID()).setSourceDisk(srcDisk.getSelfLink()).build();
OperationFuture<Operation, Operation> operation = imagesClient.insertAsync(PROJECT_ID, image);
operation.get();
return imagesClient.get(PROJECT_ID, image.getName());
}
}
use of com.google.cloud.automl.v1.Image in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method getAllImagesAsRepoImages.
public List<RepoImage> getAllImagesAsRepoImages(Integer listSize, Integer totalListSize) {
ArrayList<RepoImage> repoImages = new ArrayList<>();
long currentTime = System.currentTimeMillis();
Images images = null;
do {
OpenStackRequest<Images> listRequest = getClient().images().list(true).queryParam("limit", listSize).queryParam("sort_key", "name").queryParam("sort_dir", "asc");
if (images != null) {
listRequest.queryParam("marker", images.getList().get(images.getList().size() - 1).getId());
}
images = listRequest.execute();
for (Image glanceImage : images) {
RepoImage repoImage = imageToRepoImage(glanceImage);
repoImage.setLastRefreshed(currentTime);
repoImages.add(repoImage);
}
} while (images.getList().size() >= listSize && totalListSize != null && repoImages.size() < totalListSize);
return repoImages;
}
use of com.google.cloud.automl.v1.Image in project ovirt-engine by oVirt.
the class OpenStackImageProviderProxy method createImageFromDiskImage.
public String createImageFromDiskImage(DiskImage diskImage) {
Image glanceImage = new Image();
glanceImage.setName(diskImage.getDiskAlias());
if (diskImage.getVolumeFormat() == VolumeFormat.RAW) {
glanceImage.setDiskFormat(GlanceImageFormat.RAW.getValue());
} else if (diskImage.getVolumeFormat() == VolumeFormat.COW) {
glanceImage.setDiskFormat(GlanceImageFormat.COW.getValue());
} else {
throw new OpenStackImageException(OpenStackImageException.ErrorType.UNSUPPORTED_DISK_FORMAT, "Unknown disk format: " + diskImage.getVolumeFormat());
}
glanceImage.setContainerFormat(GlanceImageContainer.BARE.getValue());
Image retGlanceImage = getClient().images().create(glanceImage).execute();
return retGlanceImage.getId();
}
Aggregations