如何访问4.0图像分析API?尽管我的资源位于美国东部,但我仍然收到 404“资源未找到错误”?谢谢!
我尝试通过 Python 和 REST API URL 进行访问。这是我的代码(用于分析屏幕内容):
import os
import requests
import pyautogui
from PIL import Image
from io import BytesIO
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials
# Azure Configuration
SUBSCRIPTION_KEY = "..."
ENDPOINT = "..."
# OpenAI Configuration
OPENAI_KEY = "..."
def take_screenshot():
screenshot = pyautogui.screenshot()
output = BytesIO()
screenshot.save(output, format="PNG")
output.seek(0) # Rewind the stream back to the beginning before reading from it
return output
def analyze_image(image_stream):
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY,
}
params = {
'visualFeatures': 'Objects,Tags,Description,Faces,ImageType,Color,Adult',
'language': 'en',
}
response = requests.post(ENDPOINT + 'vision/v4.0/analyze', headers=headers, params=params, data=image_stream)
print(f"Status code: {response.status_code}")
print(f"Response text: {response.text}")
response.raise_for_status()
return response.json()
def query_openai(azure_result):
headers = {
'Authorization': f'Bearer {OPENAI_KEY}',
'Content-Type': 'application/json',
}
# Format the Azure result into a string
result_string = "Description: {}. Tags: {}. Objects: {}. Adult content: {}. Racy content: {}.".format(
azure_result['description']['captions'][0]['text'] if azure_result['description']['captions'] else "None",
', '.join([tag['name'] for tag in azure_result['tags']]),
', '.join([obj['objectProperty'] for obj in azure_result['objects']]) if 'objects' in azure_result and azure_result['objects'] else "None",
azure_result['adult']['isAdultContent'],
azure_result['adult']['isRacyContent']
)
print(f"Azure details sent to OpenAI: {result_string}") # Print the Azure details
data = {
'model': 'gpt-4',
'messages': [
{"role": "system", "content": "Translate the following Azure Image Analysis result to a boolean value."},
{"role": "user", "content": f"{result_string}. Does the image contain an apple?"}
]
}
response = requests.post('https://api.openai.com/v1/chat/completions', headers=headers, json=data)
return response.json()['choices'][0]['message']['content'].strip().lower() == 'true'
if __name__ == "__main__":
image_data = take_screenshot()
azure_result = analyze_image(image_data)
is_apple_present = query_openai(azure_result)
print('Does the screen have an apple on it?', is_apple_present)
这是我得到的回复:
Status code: 404
Response text: {"error":{"code":"404","message": "Resource not found"}}
Traceback (most recent call last):
File "C:\Users\...\Desktop\ScreenAnalysisApp\app.py", line 69, in <module>
azure_result = analyze_image(image_data)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\...\Desktop\ScreenAnalysisApp\app.py", line 36, in analyze_image
response.raise_for_status()
File "C:\Users\...\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\models.py", line 1021, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 404 Client Error: Resource Not Found for url: https://purefocus.cognitiveservices.azure.com/vision/v4.0/analyze?visualFeatures=Objects%2CTags%2CDescription%2CFaces%2CImageType%2CColor%2CAdult&language=en
根据提供的信息,您似乎使用了错误的 API 端点。
根据调用图像分析4.0分析API(预览版),正确的API端点应采用以下格式:
https://<endpoint>/computervision/imageanalysis:analyze&api-version=2023-02-01-preview
。
我在下面的代码片段中使用了上述格式:
def read_image_file(file_path):
with open(file_path, 'rb') as f:
image_data = f.read()
return BytesIO(image_data)
def analyze_image(image_stream):
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY,
}
params = {
'api-version': '2023-02-01-preview',
'features': 'tags,read,caption,denseCaptions,smartCrops,objects,people',
}
response = requests.post(ENDPOINT + 'computervision/imageanalysis:analyze', headers=headers, params=params, data=image_stream)
print(f"Status code: {response.status_code}")
print(f"Response text: {response.text}")
response.raise_for_status()
return response.json()
def main():
image_file_path = "8CtXD.jpg"
image_data = read_image_file(image_file_path)
azure_result = analyze_image(image_data)
print(azure_result)
if __name__ == "__main__":
main()
这样我就能得到结果:
对于Python SDK,你可以查看这个文档 默认情况下将使用
Model version: 2023-02-01-preview
进行图像分析。