Azure Computer Vision SDK read_in_stream always returns Bad Request

Viewed 1076

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)
1 Answers

You mentioned that your PNG file is a small one, could you please check if its dimension is larger than 5050? Based on official API reference: enter image description here I can repro your issue if a use a PNG file that its dimension is less than 5050: enter image description here enter image description here

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

enter image description here

enter image description here

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>")
Related