azure - 如何访问 Azure 图像分析 4.0 API?

标签 azure image analysis caption

如何访问 4.0 图像分析 API?尽管我的资源位于美国东部,但我仍然收到 404“资源未找到错误”?谢谢!

引用:https://azure.microsoft.com/en-us/blog/image-analysis-40-with-new-api-endpoint-and-ocr-model-in-preview/

我尝试通过 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 端点。

根据Call the Image Analysis 4.0 Analyze API (preview) ,正确的 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()

这样我就能得到结果: enter image description here

对于Python SDK,你可以查看这个documentation 它将使用 Model version: 2023-02-01-preview默认用于图像分析。 enter image description here

关于azure - 如何访问 Azure 图像分析 4.0 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76871951/

相关文章:

Azure 存储帐户 - 容器级访问和 ACL

azure - 使用图形 api 获取 Azure AD 自定义域列表

matlab - 显示目录中的随机图像对

java - 查找并消除轮廓opencv

image - 查找图像中对象的坐标的最简单方法是什么?

algorithm - 寻找中位数的下限

java - 为什么我的快速排序性能比合并排序差?

Azure Service Fabric 日志记录到 Azure 诊断不起作用

azure - Terraform - Azure 标签格式化问题

CSS:如果大于窗口,则缩小背景图像,否则保持 100%