|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +# [START aiplatform_sdk_predict_image_classification_sample] |
| 17 | +import base64 |
| 18 | + |
| 19 | +from typing import List |
| 20 | + |
| 21 | +from google.cloud import aiplatform |
| 22 | + |
| 23 | + |
| 24 | +def predict_image_classification_sample( |
| 25 | + project: str, |
| 26 | + location: str, |
| 27 | + endpoint_name: str, |
| 28 | + images: List, |
| 29 | +): |
| 30 | + ''' |
| 31 | + Args |
| 32 | + project: Your project ID or project number. |
| 33 | + location: Region where Endpoint is located. For example, 'us-central1'. |
| 34 | + endpoint_name: A fully qualified endpoint name or endpoint ID. Example: "projects/123/locations/us-central1/endpoints/456" or |
| 35 | + "456" when project and location are initialized or passed. |
| 36 | + images: A list of one or more images to return a prediction for. |
| 37 | + ''' |
| 38 | + aiplatform.init(project=project, location=location) |
| 39 | + |
| 40 | + endpoint = aiplatform.Endpoint(endpoint_name) |
| 41 | + |
| 42 | + instances = [] |
| 43 | + for image in images: |
| 44 | + with open(image, "rb") as f: |
| 45 | + content = f.read() |
| 46 | + instances.append({"content": base64.b64encode(content).decode("utf-8")}) |
| 47 | + |
| 48 | + response = endpoint.predict(instances=instances) |
| 49 | + |
| 50 | + for prediction_ in response.predictions: |
| 51 | + print(prediction_) |
| 52 | + |
| 53 | + |
| 54 | +# [END aiplatform_sdk_predict_image_classification_sample] |
0 commit comments