简体   繁体   中英

Azure Computer Vision SDK read_in_stream always returns Bad Request

I'm finding myself unable to use the read_in_stream API of the Azure Computer Vision SDK in Python. The SDK always returns a

File "<path>/azure/cognitiveservices/vision/computervision/operations/_computer_vision_client_operations.py", line 1567, in read_in_stream raise models.ComputerVisionErrorException(self._deserialize, response) azure.cognitiveservices.vision.computervision.models._models_py3.ComputerVisionErrorException: Operation returned an invalid status code 'Bad Request'

I've tried using the standard read API, providing it a file URL rather than a local one, and it works fine, so the client is properly authenticated.

The file does have read permissions, and reading it into a pillow image works fine. It's a fairly small PNG file.

from azure.cognitiveservices.vision import computervision
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

from array import array
import os
from PIL import Image
import sys
import time

subscription_key = "<key from azure portal>"
endpoint = "<endpoint from azure portal>"


def ocr(filePath):

    computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

    with open(filePath, "rb") as local_image_printed_text:
        recognize_handw_results = computervision_client.read_in_stream(local_image_printed_text, raw=True)

You mentioned that your PNG file is a small one, could you please check if its dimension is larger than 50 50? Based on official API reference : 在此处输入图像描述 I can repro your issue if a use a PNG file that its dimension is less than 50 50: 在此处输入图像描述 在此处输入图像描述

But when I changed to a bigger one,it works as excepted:

在此处输入图像描述

在此处输入图像描述

This is my test code below:

from azure.cognitiveservices.vision import computervision
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import OperationStatusCodes
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes
from msrest.authentication import CognitiveServicesCredentials

from array import array
import os
from PIL import Image
import sys
import time
import requests

subscription_key = ""
endpoint = ""


def ocr(filePath):

    computervision_client = ComputerVisionClient(endpoint, CognitiveServicesCredentials(subscription_key))

    with open(filePath, "rb") as local_image_printed_text:
        recognize_handw_results = computervision_client.read_in_stream(local_image_printed_text, raw=True)
        
        time.sleep(5)
        result_url = recognize_handw_results.headers.get('Operation-Location')
        result = requests.get(result_url,headers = {"Ocp-Apim-Subscription-Key":subscription_key})
        print(result.text)

ocr("<png local path>")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM