要使用Python调用谷歌识图API,您可以遵循以下步骤:
获取API密钥
访问Google Cloud Platform控制台,创建一个新的项目。
启用Google Vision API。
从API和服务页面创建一个API密钥。
安装必要的库
使用pip安装`google-cloud-vision`库:
pip install --upgrade google-cloud-vision
调用API
导入所需的库:
from google.cloud import visionfrom google.cloud.vision import ImageAnnotatorClient
设置API密钥:
用你的API密钥替换这里的YOUR_API_KEYos.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/your/keyfile.json"

调用识图API:
def detect_labels(image_path):client = ImageAnnotatorClient()with open(image_path, "rb") as image_file:content = image_file.read()image = vision.Image(content=content)response = client.object_localization(image=image)localized_object_annotations = response.localized_object_annotationsfor object_ in localized_object_annotations:print(f"{object_.name} (Confidence: {object_.score})")替换为你的图片路径detect_labels("path/to/image.jpg")
请确保替换`path/to/your/keyfile.json`和`path/to/image.jpg`为实际的密钥文件路径和要识别的图片路径。
以上步骤可以帮助您使用Python调用谷歌识图API。
