Search in sources :

Example 1 with ImageObjectDetectionPredictionParams

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

the class PredictImageObjectDetectionSample method predictImageObjectDetection.

static void predictImageObjectDetection(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);
        ImageObjectDetectionPredictionParams params = ImageObjectDetectionPredictionParams.newBuilder().setConfidenceThreshold((float) (0.5)).setMaxPredictions(5).build();
        ImageObjectDetectionPredictionInstance instance = ImageObjectDetectionPredictionInstance.newBuilder().setContent(content).build();
        List<Value> instances = new ArrayList<>();
        instances.add(ValueConverter.toValue(instance));
        PredictResponse predictResponse = predictionServiceClient.predict(endpointName, instances, ValueConverter.toValue(params));
        System.out.println("Predict Image Object Detection Response");
        System.out.format("\tDeployed Model Id: %s\n", predictResponse.getDeployedModelId());
        System.out.println("Predictions");
        for (Value prediction : predictResponse.getPredictionsList()) {
            ImageObjectDetectionPredictionResult.Builder resultBuilder = ImageObjectDetectionPredictionResult.newBuilder();
            ImageObjectDetectionPredictionResult result = (ImageObjectDetectionPredictionResult) ValueConverter.fromValue(resultBuilder, prediction);
            for (int i = 0; i < result.getIdsCount(); i++) {
                System.out.printf("\tDisplay name: %s\n", result.getDisplayNames(i));
                System.out.printf("\tConfidences: %f\n", result.getConfidences(i));
                System.out.printf("\tIDs: %d\n", result.getIds(i));
                System.out.printf("\tBounding boxes: %s\n", result.getBboxes(i));
            }
        }
    }
}
Also used : ImageObjectDetectionPredictionInstance(com.google.cloud.aiplatform.v1.schema.predict.instance.ImageObjectDetectionPredictionInstance) ArrayList(java.util.ArrayList) PredictResponse(com.google.cloud.aiplatform.v1.PredictResponse) ImageObjectDetectionPredictionParams(com.google.cloud.aiplatform.v1.schema.predict.params.ImageObjectDetectionPredictionParams) PredictionServiceClient(com.google.cloud.aiplatform.v1.PredictionServiceClient) PredictionServiceSettings(com.google.cloud.aiplatform.v1.PredictionServiceSettings) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) Value(com.google.protobuf.Value) ImageObjectDetectionPredictionResult(com.google.cloud.aiplatform.v1.schema.predict.prediction.ImageObjectDetectionPredictionResult)

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 ImageObjectDetectionPredictionInstance (com.google.cloud.aiplatform.v1.schema.predict.instance.ImageObjectDetectionPredictionInstance)1 ImageObjectDetectionPredictionParams (com.google.cloud.aiplatform.v1.schema.predict.params.ImageObjectDetectionPredictionParams)1 ImageObjectDetectionPredictionResult (com.google.cloud.aiplatform.v1.schema.predict.prediction.ImageObjectDetectionPredictionResult)1 Value (com.google.protobuf.Value)1 ArrayList (java.util.ArrayList)1