Azure Computer Vision - OCR API - Bad or unrecognizable request JSON or binary file

Ammar Mohanna 26 Reputation points
2023-01-13T09:27:44.7666667+00:00

Hello,

I am trying to use Azure OCR from python. I created an instance of the 'Cognitive Services | Computer vision' and I have my endpoint URL and keys.

It works well when I use it from postman but when I try to run it from python I get the following error:

b'{"error":{"code":"BadArgument","message":"Bad or unrecognizable request JSON or binary file."}}'

My code is the following:

import cv2, json, base64
import http.client, urllib.request, urllib.parse, urllib.error

headers = {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': 'XXX',
}

params = urllib.parse.urlencode({
    'language': 'ar',
    'pages': 'all',
    # 'readingOrder': '{string}',
    # 'model-version': '{string}',
})

try:
    conn = http.client.HTTPSConnection('YYY.cognitiveservices.azure.com')

    with open("C:/Users/User/Downloads/Old/Numbers+LettersArabic.jpg", "rb") as image_file:
        body = base64.b64encode(image_file.read())
        body = '{body}'

    conn.request("POST", "/vision/v3.2/read/analyze?%s" % params, body, headers)
    response = conn.getresponse()
    data = response.read()
    print(data)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
316 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,416 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. romungi-MSFT 42,311 Reputation points Microsoft Employee
    2023-01-13T11:35:24.9933333+00:00

    @Ammar Mohanna I think the body is not passed correctly in the above case. Try this snippet and it should provide you with the operation location that can be used to get the end result of the analyze operation using Read Result API.

    
    import  json, base64
    import http.client, urllib.request, urllib.parse, urllib.error
    
    headers = {
        'Content-Type': 'application/octet-stream',
        'Ocp-Apim-Subscription-Key': 'key',
    }
    
    params = urllib.parse.urlencode({
        'language': 'en',
        'pages': 'all',
        # 'readingOrder': '{string}',
        # 'model-version': '{string}',
    })
    
    
    with open("C:\\Users\\user\\Pictures\\image.jpg", "rb") as image_file:
        body = image_file.read()
    
    try:
        conn = http.client.HTTPSConnection('your_region.cognitiveservices.azure.com')
        conn.request("POST", "/vision/v3.2/read/analyze?%s" % params, body, headers)
        response = conn.getresponse()
        #data = response.read()
        print(response.headers)
        conn.close()
    except Exception as e:
        print("[Errno {0}] {1}".format(e.errno, e.strerror))
    
    

    Response:User's image