Search in sources :

Example 76 with Image

use of com.google.cloud.vision.v1p3beta1.Image in project java-docs-samples by GoogleCloudPlatform.

the class QuickstartSample method main.

public static void main(String... args) throws Exception {
    // Instantiates a client
    try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {
        // The path to the image file to annotate
        String fileName = "./resources/wakeupcat.jpg";
        // Reads the image file into memory
        Path path = Paths.get(fileName);
        byte[] data = Files.readAllBytes(path);
        ByteString imgBytes = ByteString.copyFrom(data);
        // Builds the image annotation request
        List<AnnotateImageRequest> requests = new ArrayList<>();
        Image img = Image.newBuilder().setContent(imgBytes).build();
        Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
        AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
        requests.add(request);
        // Performs label detection on the image file
        BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
        List<AnnotateImageResponse> responses = response.getResponsesList();
        for (AnnotateImageResponse res : responses) {
            if (res.hasError()) {
                System.out.printf("Error: %s\n", res.getError().getMessage());
                return;
            }
            for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                annotation.getAllFields().forEach((k, v) -> System.out.printf("%s : %s\n", k, v.toString()));
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ByteString(com.google.protobuf.ByteString) ImageAnnotatorClient(com.google.cloud.vision.v1.ImageAnnotatorClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) AnnotateImageRequest(com.google.cloud.vision.v1.AnnotateImageRequest) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) EntityAnnotation(com.google.cloud.vision.v1.EntityAnnotation) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 77 with Image

use of com.google.cloud.vision.v1p3beta1.Image in project spring-cloud-gcp by spring-cloud.

the class CloudVisionTemplate method analyzeImage.

/**
 * Analyze an image and extract the features of the image specified by
 * {@code featureTypes}.
 * <p>A feature describes the kind of Cloud Vision analysis one wishes to perform on an
 * image, such as text detection, image labelling, facial detection, etc. A full list of
 * feature types can be found in {@link Feature.Type}.
 * @param imageResource the image one wishes to analyze. The Cloud Vision APIs support
 *     image formats described here: https://cloud.google.com/vision/docs/supported-files
 * @param imageContext the image context used to customize the Vision API request
 * @param featureTypes the types of image analysis to perform on the image
 * @return the results of image analyses
 * @throws CloudVisionException if the image could not be read or if a malformed response
 *     is received from the Cloud Vision APIs
 */
public AnnotateImageResponse analyzeImage(Resource imageResource, ImageContext imageContext, Feature.Type... featureTypes) {
    ByteString imgBytes;
    try {
        imgBytes = ByteString.readFrom(imageResource.getInputStream());
    } catch (IOException ex) {
        throw new CloudVisionException("Failed to read image bytes from provided resource.", ex);
    }
    Image image = Image.newBuilder().setContent(imgBytes).build();
    List<Feature> featureList = Arrays.stream(featureTypes).map((featureType) -> Feature.newBuilder().setType(featureType).build()).collect(Collectors.toList());
    BatchAnnotateImagesRequest request = BatchAnnotateImagesRequest.newBuilder().addRequests(AnnotateImageRequest.newBuilder().addAllFeatures(featureList).setImageContext(imageContext).setImage(image)).build();
    BatchAnnotateImagesResponse batchResponse = this.imageAnnotatorClient.batchAnnotateImages(request);
    List<AnnotateImageResponse> annotateImageResponses = batchResponse.getResponsesList();
    if (!annotateImageResponses.isEmpty()) {
        return annotateImageResponses.get(0);
    } else {
        throw new CloudVisionException("Failed to receive valid response Vision APIs; empty response received.");
    }
}
Also used : Arrays(java.util.Arrays) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) Type(com.google.cloud.vision.v1.Feature.Type) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Feature(com.google.cloud.vision.v1.Feature) ByteString(com.google.protobuf.ByteString) List(java.util.List) 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) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) Code(com.google.rpc.Code) Resource(org.springframework.core.io.Resource) Assert(org.springframework.util.Assert) BatchAnnotateImagesRequest(com.google.cloud.vision.v1.BatchAnnotateImagesRequest) ByteString(com.google.protobuf.ByteString) AnnotateImageResponse(com.google.cloud.vision.v1.AnnotateImageResponse) IOException(java.io.IOException) Image(com.google.cloud.vision.v1.Image) Feature(com.google.cloud.vision.v1.Feature) BatchAnnotateImagesResponse(com.google.cloud.vision.v1.BatchAnnotateImagesResponse)

Example 78 with Image

use of com.google.cloud.vision.v1p3beta1.Image in project openj9 by eclipse.

the class XMLIndexReader method _createCoreImageAfterParseError.

/**
 * This is like the setJ9DumpData method but is to be used when we fail to parse the file.  It is meant to try to construct the
 * Image object with what it was able to get
 *
 * @param e The cause of the error
 */
private void _createCoreImageAfterParseError(Exception e) {
    Builder builder = null;
    if (_stream == null) {
        // extract directly from the file
        builder = new Builder(_coreFile, _reader, 0, _fileResolvingAgent);
    } else {
        // extract using the data stream
        builder = new Builder(_coreFile, _stream, 0, _fileResolvingAgent);
    }
    _coreFile.extract(builder);
    String osType = builder.getOSType();
    String cpuType = builder.getCPUType();
    String cpuSubType = builder.getCPUSubType();
    long creationTime = builder.getCreationTime();
    _coreImage = new Image(osType, null, cpuType, cpuSubType, 0, 0, creationTime);
    Iterator spaces = builder.getAddressSpaces();
    while (spaces.hasNext()) {
        ImageAddressSpace addressSpace = (ImageAddressSpace) spaces.next();
        // Set all processes as having invalid runtimes
        for (Iterator processes = addressSpace.getProcesses(); processes.hasNext(); ) {
            ImageProcess process = (ImageProcess) processes.next();
            process.runtimeExtractionFailed(e);
        }
        _coreImage.addAddressSpace(addressSpace);
    }
}
Also used : ImageAddressSpace(com.ibm.dtfj.image.j9.ImageAddressSpace) ImageProcess(com.ibm.dtfj.image.j9.ImageProcess) Builder(com.ibm.dtfj.image.j9.Builder) Iterator(java.util.Iterator) Image(com.ibm.dtfj.image.j9.Image)

Example 79 with Image

use of com.google.cloud.vision.v1p3beta1.Image in project java-docs-samples by GoogleCloudPlatform.

the class InstancesAdvancedIT method createImage.

private static Image createImage(Disk srcDisk) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    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(3, TimeUnit.MINUTES);
        return imagesClient.get(PROJECT_ID, image.getName());
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image)

Example 80 with Image

use of com.google.cloud.vision.v1p3beta1.Image in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstancesAdvanced method createWithSnapshottedDataDisk.

// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_snapshot_disk]
/**
 * Create a new VM instance with Debian 10 operating system and data disk created from snapshot.
 *
 * @param project project ID or project number of the Cloud project you want to use.
 * @param zone name of the zone to create the instance in. For example: "us-west3-b"
 * @param instanceName name of the new virtual machine (VM) instance.
 * @param snapshotName link to the snapshot you want to use as the source of your data disk in the
 * form of: "projects/{project_name}/global/snapshots/{snapshot_name}"
 * @return Instance object.
 */
public static Instance createWithSnapshottedDataDisk(String project, String zone, String instanceName, String snapshotName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
        Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
        String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
        Vector<AttachedDisk> disks = new Vector<>();
        disks.add(diskFromImage(diskType, 10, true, image.getSelfLink()));
        disks.add(diskFromSnapshot(diskType, 11, false, snapshotName));
        return createWithDisks(project, zone, instanceName, disks, "n1-standard-1", "global/networks/default", null);
    }
}
Also used : AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Vector(java.util.Vector)

Aggregations

AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)75 Image (com.google.cloud.vision.v1.Image)75 Feature (com.google.cloud.vision.v1.Feature)73 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)72 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)69 ArrayList (java.util.ArrayList)69 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)66 ByteString (com.google.protobuf.ByteString)52 ImageSource (com.google.cloud.vision.v1.ImageSource)41 FileInputStream (java.io.FileInputStream)32 EntityAnnotation (com.google.cloud.vision.v1.EntityAnnotation)28 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)26 IOException (java.io.IOException)20 ImageContext (com.google.cloud.vision.v1.ImageContext)14 SafeSearchAnnotation (com.google.cloud.vision.v1.SafeSearchAnnotation)11 WebDetection (com.google.cloud.vision.v1.WebDetection)11 LocationInfo (com.google.cloud.vision.v1.LocationInfo)10 Arrays (java.util.Arrays)10 Image (com.google.cloud.compute.v1.Image)9 ImagesClient (com.google.cloud.compute.v1.ImagesClient)9