azure - 向 Azure 认知 API 发送批量请求以进行 TEXT-OCR

标签 azure ocr batch-processing azure-cognitive-services

我正在调用 Azure 认知 API 进行 OCR 文本识别,并且同时传递 10 个图像(因为下面的代码一次只接受一个图像 - 即 10 个独立的请求并行),从处理的角度来看,这对我来说效率不高,因为我需要使用额外的模块,即:Celery 和多处理。

那么,有没有办法在单个请求中发送所有 10 张图像并立即获取输出,然后进行后期处理?

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)

任何帮助将不胜感激。

最佳答案

我猜您正在寻找 Batch Read File

public class BatchReadFileSample
    {
        public static async Task RunAsync(string endpoint, string key)
        {
            ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };
            const int numberOfCharsInOperationId = 36;

            string localImagePath = @"Images\handwritten_text.jpg";  // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any appropriate image on your machine.
            string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/printed_text.jpg";

            Console.WriteLine("Text being batch read ...");
            await BatchReadFileFromStreamAsync(computerVision, localImagePath, numberOfCharsInOperationId); 
            await BatchReadFileFromUrlAsync(computerVision, remoteImageUrl, numberOfCharsInOperationId);
        }

        // Read text from a remote image
        private static async Task BatchReadFileFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
                return;
            }

            // Start the async process to read the text
            BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageUrl);
            await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
        }

        // Recognize text from a local image
        private static async Task BatchReadFileFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
                return;
            }

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                // Start the async process to recognize the text
                BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync(imageStream);
                await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
            }
        }

这是Full Code

关于azure - 向 Azure 认知 API 发送批量请求以进行 TEXT-OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632715/

相关文章:

Windows Azure SDK for Node.js 表存储批量更新

ocr - 哪种OCR引擎更好:Tesseract或OCRopus?

linux - 将文件夹中的所有文件名传递给 bash 脚本,添加特殊附录

azure - 使用 TCP 协议(protocol)与 ESP8266 无操作系统设备连接到 Azure IOT 集线器

scala - 使用azure databricks scala将数据从blob存储加载到sql数据仓库

c++ - 如何将 Tesseract OCR 库集成到 C++ 程序中

python - 使用python和opencv检测图像中的文本区域

java - 在 perl 脚本中执行批处理文件时需要提供参数的帮助

export - 有没有办法通过 API 或脚本在 Jive 中批量下载文件?

sql-server - SQL Azure - 具有 nvarchar(max) 列的外部数据表性能低下