Search in sources :

Example 56 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-automl by googleapis.

the class ModelApi method createModel.

// [START automl_vision_create_model]
/**
 * Demonstrates using the AutoML client to create a model.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param dataSetId the Id of the dataset to which model is created.
 * @param modelName the Name of the model.
 * @param trainBudget the Budget for training the model.
 */
static void createModel(String projectId, String computeRegion, String dataSetId, String modelName, String trainBudget) {
    // Instantiates a client
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, computeRegion);
        // Set model metadata.
        ImageClassificationModelMetadata imageClassificationModelMetadata = Long.valueOf(trainBudget) == 0 ? ImageClassificationModelMetadata.newBuilder().build() : ImageClassificationModelMetadata.newBuilder().setTrainBudget(Long.valueOf(trainBudget)).build();
        // Set model name and model metadata for the image dataset.
        Model myModel = Model.newBuilder().setDisplayName(modelName).setDatasetId(dataSetId).setImageClassificationModelMetadata(imageClassificationModelMetadata).build();
        // Create a model with the model metadata in the region.
        OperationFuture<Model, OperationMetadata> response = client.createModelAsync(projectLocation, myModel);
        System.out.println(String.format("Training operation name: %s", response.getInitialFuture().get().getName()));
        System.out.println("Training started...");
    } catch (IOException | ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : ImageClassificationModelMetadata(com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata) Model(com.google.cloud.automl.v1beta1.Model) IOException(java.io.IOException) OperationMetadata(com.google.cloud.automl.v1beta1.OperationMetadata) ExecutionException(java.util.concurrent.ExecutionException) AutoMlClient(com.google.cloud.automl.v1beta1.AutoMlClient) LocationName(com.google.cloud.automl.v1beta1.LocationName)

Example 57 with Image

use of com.google.cloud.automl.v1beta1.Image in project java-automl by googleapis.

the class PredictionApi method predict.

// [START automl_vision_predict]
/**
 * Demonstrates using the AutoML client to predict an image.
 *
 * @param projectId the Id of the project.
 * @param computeRegion the Region name.
 * @param modelId the Id of the model which will be used for text classification.
 * @param filePath the Local text file path of the content to be classified.
 * @param scoreThreshold the Confidence score. Only classifications with confidence score above
 *     scoreThreshold are displayed.
 */
static void predict(String projectId, String computeRegion, String modelId, String filePath, String scoreThreshold) throws IOException {
    // Instantiate client for prediction service.
    try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, computeRegion, modelId);
        // Read the image and assign to payload.
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload examplePayload = ExamplePayload.newBuilder().setImage(image).build();
        // Additional parameters that can be provided for prediction e.g. Score Threshold
        Map<String, String> params = new HashMap<>();
        if (scoreThreshold != null) {
            params.put("score_threshold", scoreThreshold);
        }
        // Perform the AutoML Prediction request
        PredictResponse response = predictionClient.predict(name, examplePayload, params);
        System.out.println("Prediction results:");
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.println("Predicted class name :" + annotationPayload.getDisplayName());
            System.out.println("Predicted class score :" + annotationPayload.getClassification().getScore());
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) ByteString(com.google.protobuf.ByteString) Image(com.google.cloud.automl.v1.Image) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload)

Example 58 with Image

use of com.google.cloud.automl.v1beta1.Image in project openstack4j by ContainX.

the class ImageV2Tests method testCreateImage.

public void testCreateImage() throws IOException {
    respondWith(IMAGE_JSON);
    String id = "8a2ea42d-06b5-42c2-a54d-97105420f2bb";
    String name = "amphora-x64-haproxy";
    ContainerFormat cf = ContainerFormat.BARE;
    DiskFormat df = DiskFormat.QCOW2;
    Long mindisk = 0L;
    Long minram = 0L;
    Image.ImageVisibility vis = Image.ImageVisibility.PUBLIC;
    String key1 = "test-key1";
    String key2 = "test-key2";
    String key3 = "id";
    String value1 = "test-value1";
    String value2 = "test-value2";
    String value3 = "test-value3";
    Image im = Builders.imageV2().id(id).name(name).containerFormat(cf).diskFormat(df).minDisk(mindisk).minRam(minram).visibility(vis).additionalProperty(key1, value1).additionalProperty(key2, value2).additionalProperty(key3, value3).build();
    Image image = osv3().imagesV2().create(im);
    assertNotNull(image);
    assertEquals(image.getId(), id);
    assertEquals(image.getName(), name);
    assertEquals(image.getContainerFormat(), cf);
    assertEquals(image.getDiskFormat(), df);
    assertEquals(image.getVisibility(), vis);
    assertEquals(image.getMinDisk(), mindisk);
    assertEquals(image.getMinRam(), minram);
    assertEquals(image.getAdditionalPropertyValue(key1), value1);
    assertEquals(image.getAdditionalPropertyValue(key2), value2);
    assertNull(image.getAdditionalPropertyValue(key3));
}
Also used : ContainerFormat(org.openstack4j.model.image.v2.ContainerFormat) Image(org.openstack4j.model.image.v2.Image) DiskFormat(org.openstack4j.model.image.v2.DiskFormat)

Example 59 with Image

use of com.google.cloud.automl.v1beta1.Image in project openstack4j by ContainX.

the class ImageV2Tests method testGetImageWithLocations.

public void testGetImageWithLocations() throws IOException {
    respondWith(IMAGE_WIHT_LOCATION_JSON);
    String id = "c73056d6-c583-4d6c-9f70-04f3bfd8dff4";
    Image image = osv3().imagesV2().get(id);
    assertNotNull(image);
    assertNotNull(image.getId());
    assertEquals(image.getId(), id);
    assertEquals(2, image.getLocations().size());
}
Also used : Image(org.openstack4j.model.image.v2.Image)

Example 60 with Image

use of com.google.cloud.automl.v1beta1.Image in project openstack4j by ContainX.

the class ImageServiceImpl method update.

/**
 * {@inheritDoc}
 */
@Override
public Image update(Image image) {
    checkNotNull(image);
    ObjectMapper objectMapper = new ObjectMapper();
    Image origImage = get(image.getId());
    ObjectNode origJson;
    ObjectNode newJson;
    try {
        String oImg = objectMapper.writeValueAsString(origImage);
        origJson = (ObjectNode) objectMapper.readTree(oImg);
        String img = objectMapper.writeValueAsString(image);
        newJson = (ObjectNode) objectMapper.readTree(img);
        JsonNode jsonDiff = JsonDiff.asJson(origJson, newJson);
        GlanceImageUpdate giu = new GlanceImageUpdate(jsonDiff);
        return update(image.getId(), giu);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) GlanceImageUpdate(org.openstack4j.openstack.image.v2.domain.GlanceImageUpdate) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) GlanceImage(org.openstack4j.openstack.image.v2.domain.GlanceImage) CachedImage(org.openstack4j.model.image.v2.CachedImage) Image(org.openstack4j.model.image.v2.Image) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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)21 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