Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending Batch request to azure cognitive API for TEXT-OCR #32

Open
gr8Adakron opened this issue Jun 14, 2019 · 2 comments
Open

Sending Batch request to azure cognitive API for TEXT-OCR #32

gr8Adakron opened this issue Jun 14, 2019 · 2 comments

Comments

@gr8Adakron
Copy link

I am calling azure cognitive API for OCR text-recognization and I am passing 10-images at the same time simultaneously (as the code below only accepts one image at a time-- that is 10-independent requests in parallel) which is not efficient to me, from processing point of view, as I need to use extra modules i.e: Celery and multiprocessing.

So, is there a way to send all the 10-images in a single request and get the output at once then do post processing?

import time
from io import BytesIO

import cv2
import requests
from PIL import Image as PILImage
from PIL import Image
file_list = []

headers = {
    "Ocp-Apim-Subscription-Key": "<API-KEY>",
    'Content-Type': 'application/octet-stream'}

p = "symbol_sample.jpg"
print(p,"p")

def recognise_text(p):
    p = cv2.imread(p)
    cropped_image = PILImage.fromarray(p)
    buffer = BytesIO()
    cropped_image.save(buffer, format="JPEG")
    image_bytes = buffer.getvalue()
    try:
        response = requests.post(
            "https://centralindia.api.cognitive.microsoft.com/vision/v2.0/recognizeText?mode=Printed",
            headers=headers,
            data=image_bytes
        )
        header_link = str(response.headers['Operation-Location'])
        while (True):
            headers_get = {
                "Ocp-Apim-Subscription-Key": "<API-KEY>"",
                'Content-Type': 'application/json'
            }
            result = requests.get(
                url=header_link,
                headers=headers_get
            )
            response_r = result.json()
            if response_r["status"] == "Succeeded":
                return response_r
            else:
                time.sleep(4)
    except Exception as e:
        print(e)
        return ""
    
image1="symbol_sample.jpg"
o = recognise_text(image1)
print(o)

Any help would be really appreciated.

@gr8Adakron
Copy link
Author

@jdhuntington @chad @berberich @paulbatum Any help, please.

@wiazur
Copy link
Collaborator

wiazur commented Feb 15, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants