使用python进行图像描述的代码
时间: 2024-06-10 14:06:01 浏览: 52
以下是使用Python进行图像描述的代码:
```python
import json
import requests
# Enter your subscription key and endpoint here
subscription_key = 'your-subscription-key'
endpoint = 'your-endpoint'
# Specify image URL and caption language
image_url = 'https://example.com/image.png'
caption_language = 'en'
# Specify API url and parameters
api_url = f'{endpoint}/vision/v3.1/describe'
headers = {'Ocp-Apim-Subscription-Key': subscription_key, 'Content-Type': 'application/json'}
params = {'maxCandidates': '1', 'language': caption_language}
data = {'url': image_url}
# Call the API and extract the description
response = requests.post(api_url, headers=headers, params=params, json=data)
response.raise_for_status()
description = response.json()['description']['captions'][0]['text']
# Print the description
print(description)
```
注意:在使用此代码之前,需要先在 Azure 认知服务中心创建计算机视觉服务的资源,然后将订阅密钥和终结点替换为您自己的值。
阅读全文