1616import base64
1717
1818from google .cloud import aiplatform
19- from google .cloud .aiplatform .schema import predict
19+ from google .protobuf import json_format
20+ from google .protobuf .struct_pb2 import Value
2021
2122
2223def predict_image_classification_sample (
@@ -36,29 +37,25 @@ def predict_image_classification_sample(
3637
3738 # The format of each instance should conform to the deployed model's prediction input schema.
3839 encoded_content = base64 .b64encode (file_content ).decode ("utf-8" )
40+ instance_dict = {"content" : encoded_content }
3941
40- instance_obj = predict .instance .ImageClassificationPredictionInstance (
41- content = encoded_content )
42-
43- instance_val = instance_obj .to_value ()
44- instances = [instance_val ]
45-
46- params_obj = predict .params .ImageClassificationPredictionParams (
47- confidence_threshold = 0.5 , max_predictions = 5 )
48-
42+ instance = json_format .ParseDict (instance_dict , Value ())
43+ instances = [instance ]
44+ # See gs://google-cloud-aiplatform/schema/predict/params/image_classification_1.0.0.yaml for the format of the parameters.
45+ parameters_dict = {"confidenceThreshold" : 0.5 , "maxPredictions" : 5 }
46+ parameters = json_format .ParseDict (parameters_dict , Value ())
4947 endpoint = client .endpoint_path (
5048 project = project , location = location , endpoint = endpoint_id
5149 )
5250 response = client .predict (
53- endpoint = endpoint , instances = instances , parameters = params_obj
51+ endpoint = endpoint , instances = instances , parameters = parameters
5452 )
5553 print ("response" )
56- print ("\t deployed_model_id :" , response .deployed_model_id )
54+ print (" deployed_model_id :" , response .deployed_model_id )
5755 # See gs://google-cloud-aiplatform/schema/predict/prediction/classification.yaml for the format of the predictions.
5856 predictions = response .predictions
59- for prediction_ in predictions :
60- prediction_obj = predict .prediction .ClassificationPredictionResult .from_map (prediction_ )
61- print (prediction_obj )
57+ for prediction in predictions :
58+ print (" prediction:" , dict (prediction ))
6259
6360
6461# [END aiplatform_predict_image_classification_sample]
0 commit comments