Search in sources :

Example 1 with BoundingPoly

use of com.google.cloud.automl.v1.BoundingPoly in project java-automl by googleapis.

the class VisionObjectDetectionPredict method predict.

static void predict(String projectId, String modelId, String filePath) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (PredictionServiceClient client = PredictionServiceClient.create()) {
        // Get the full path of the model.
        ModelName name = ModelName.of(projectId, "us-central1", modelId);
        ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath)));
        Image image = Image.newBuilder().setImageBytes(content).build();
        ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build();
        PredictRequest predictRequest = PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).putParams("score_threshold", // [0.0-1.0] Only produce results higher than this value
        "0.5").build();
        PredictResponse response = client.predict(predictRequest);
        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName());
            System.out.format("Predicted class score: %.2f\n", annotationPayload.getImageObjectDetection().getScore());
            BoundingPoly boundingPoly = annotationPayload.getImageObjectDetection().getBoundingBox();
            System.out.println("Normalized Vertices:");
            for (NormalizedVertex vertex : boundingPoly.getNormalizedVerticesList()) {
                System.out.format("\tX: %.2f, Y: %.2f\n", vertex.getX(), vertex.getY());
            }
        }
    }
}
Also used : ModelName(com.google.cloud.automl.v1.ModelName) ByteString(com.google.protobuf.ByteString) PredictResponse(com.google.cloud.automl.v1.PredictResponse) BoundingPoly(com.google.cloud.automl.v1.BoundingPoly) ExamplePayload(com.google.cloud.automl.v1.ExamplePayload) Image(com.google.cloud.automl.v1.Image) PredictRequest(com.google.cloud.automl.v1.PredictRequest) PredictionServiceClient(com.google.cloud.automl.v1.PredictionServiceClient) AnnotationPayload(com.google.cloud.automl.v1.AnnotationPayload) NormalizedVertex(com.google.cloud.automl.v1.NormalizedVertex)

Aggregations

AnnotationPayload (com.google.cloud.automl.v1.AnnotationPayload)1 BoundingPoly (com.google.cloud.automl.v1.BoundingPoly)1 ExamplePayload (com.google.cloud.automl.v1.ExamplePayload)1 Image (com.google.cloud.automl.v1.Image)1 ModelName (com.google.cloud.automl.v1.ModelName)1 NormalizedVertex (com.google.cloud.automl.v1.NormalizedVertex)1 PredictRequest (com.google.cloud.automl.v1.PredictRequest)1 PredictResponse (com.google.cloud.automl.v1.PredictResponse)1 PredictionServiceClient (com.google.cloud.automl.v1.PredictionServiceClient)1 ByteString (com.google.protobuf.ByteString)1