Search in sources :

Example 1 with ImageClassificationPredictionInstance

use of com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance in project java-aiplatform by googleapis.

the class PredictImageClassificationSample method predictImageClassification.

static void predictImageClassification(String project, String fileName, String endpointId) throws IOException {
    PredictionServiceSettings settings = PredictionServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create(settings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        byte[] contents = Base64.encodeBase64(Files.readAllBytes(Paths.get(fileName)));
        String content = new String(contents, StandardCharsets.UTF_8);
        ImageClassificationPredictionInstance predictionInstance = ImageClassificationPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(predictionInstance));
        ImageClassificationPredictionParams predictionParams = ImageClassificationPredictionParams.newBuilder().setConfidenceThreshold((float) 0.5).setMaxPredictions(5).build();
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(predictionParams));
        System.out.println("Predict Image Classification Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            ClassificationPredictionResult.Builder resultBuilder = ClassificationPredictionResult.newBuilder();
            // Display names and confidences values correspond to
            // IDs in the ID list.
            ClassificationPredictionResult result = (ClassificationPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            int counter = 0;
            for (Long id : result.getIdsList()) {
                System.out.printf("Label ID: %d\n", id);
                System.out.printf("Label: %s\n", result.getDisplayNames(counter));
                System.out.printf("Confidence: %.4f\n", result.getConfidences(counter));
                counter++;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) ImageClassificationPredictionParams(com.google.cloud.aiplatform.v1.schema.predict.params.ImageClassificationPredictionParams) Value(com.google.protobuf.Value) ImageClassificationPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance) ClassificationPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)

Aggregations

EndpointName (com.google.cloud.aiplatform.v1.EndpointName)1 PredictResponse (com.google.cloud.aiplatform.v1.PredictResponse)1 PredictionServiceClient (com.google.cloud.aiplatform.v1.PredictionServiceClient)1 PredictionServiceSettings (com.google.cloud.aiplatform.v1.PredictionServiceSettings)1 ImageClassificationPredictionInstance (com.google.cloud.aiplatform.v1.schema.predict.instance.ImageClassificationPredictionInstance)1 ImageClassificationPredictionParams (com.google.cloud.aiplatform.v1.schema.predict.params.ImageClassificationPredictionParams)1 ClassificationPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.ClassificationPredictionResult)1 Value (com.google.protobuf.Value)1 ArrayList (java.util.ArrayList)1