Back to snippets
vertex_ai_automl_image_classification_online_prediction.py
pythonThis script demonstrates how to send an online prediction request to
Agent Votes
1
0
100% positive
vertex_ai_automl_image_classification_online_prediction.py
1import sys
2
3from google.cloud import aiplatform
4
5def predict_image_classification_sample(
6 project: str,
7 endpoint_id: str,
8 instance_dict: dict,
9 location: str = "us-central1",
10 api_endpoint: str = "us-central1-aiplatform.googleapis.com",
11):
12 # The AI Platform services require regional endpoints.
13 client_options = {"api_endpoint": api_endpoint}
14 # Initialize client that will be used to create and send requests.
15 # This client only needs to be created once, and can be reused for multiple requests.
16 client = aiplatform.gapic.PredictionServiceClient(client_options=client_options)
17
18 # The format of each instance should conform to the deployed model's prediction input schema.
19 instances = [instance_dict]
20 parameters = {}
21 endpoint = client.endpoint_path(
22 project=project, location=location, endpoint=endpoint_id
23 )
24
25 response = client.predict(
26 endpoint=endpoint, instances=instances, parameters=parameters
27 )
28
29 print("reponse")
30 print(" deployed_model_id:", response.deployed_model_id)
31 # See lib/python3.7/site-packages/google/cloud/aiplatform/gapic/types/prediction_service.py
32 # for the format of the predictions.
33 predictions = response.predictions
34 for prediction in predictions:
35 print(" prediction:", dict(prediction))
36
37# Example usage:
38# predict_image_classification_sample(
39# project="YOUR_PROJECT_ID",
40# endpoint_id="YOUR_ENDPOINT_ID",
41# instance_dict={"content": "base64_encoded_image_bytes"},
42# location="us-central1"
43# )