【问题标题】:Trigger notification after Computer Vision OCR extraction is complete计算机视觉 OCR 提取完成后触发通知
【发布时间】:2020-08-28 15:10:23
【问题描述】:

我正在探索 Microsoft Computer Vision 的读取 API (asyncBatchAnalyze) 以从图像中提取文本。我在 Microsoft 网站上找到了一些示例代码,用于从图像中异步提取文本。它的工作方式如下:

1) 将图像提交到 asyncBatchAnalyze API。 2) 此 API 接受请求并返回一个 URI。 3) 我们需要轮询这个 URI 来获取提取的数据。

当 asyncBatchAnalyze 完成图像分析时,有什么方法可以触发某些通知(例如在 AWS SQS 或类似服务中发布通知)?


public class MicrosoftOCRAsyncReadText {

    private static final String SUBSCRIPTION_KEY = “key”;
    private static final String ENDPOINT = "https://computervision.cognitiveservices.azure.com";
    private static final String URI_BASE = ENDPOINT + "/vision/v2.1/read/core/asyncBatchAnalyze";


    public static void main(String[] args) {

    CloseableHttpClient httpTextClient = HttpClientBuilder.create().build();
        CloseableHttpClient httpResultClient = HttpClientBuilder.create().build();;

        try {
            URIBuilder builder = new URIBuilder(URI_BASE);

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);

            request.setHeader("Content-Type", "application/octet-stream");
            request.setHeader("Ocp-Apim-Subscription-Key", SUBSCRIPTION_KEY);


            String image = "/Users/xxxxx/Documents/img1.jpg";
            File file = new File(image);       
            FileEntity reqEntity = new FileEntity(file);
            request.setEntity(reqEntity);

            HttpResponse response = httpTextClient.execute(request);

            if (response.getStatusLine().getStatusCode() != 202) {
                HttpEntity entity = response.getEntity();
                String jsonString = EntityUtils.toString(entity);
                JSONObject json = new JSONObject(jsonString);
                System.out.println("Error:\n");
                System.out.println(json.toString(2));
                return;
            }

            String operationLocation = null;

            Header[] responseHeaders = response.getAllHeaders();
            for (Header header : responseHeaders) {
                if (header.getName().equals("Operation-Location")) {
                    operationLocation = header.getValue();
                    break;
                }
            }

            if (operationLocation == null) {
                System.out.println("\nError retrieving Operation-Location.\nExiting.");
                System.exit(1);
            }

            /* Wait for asyncBatchAnalyze to complete. In place of this wait, can we trigger any notification from Computer Vision when the extract text operation is complete?
*/
            Thread.sleep(5000);

            // Call the second REST API method and get the response.
            HttpGet resultRequest = new HttpGet(operationLocation);
            resultRequest.setHeader("Ocp-Apim-Subscription-Key", SUBSCRIPTION_KEY);

            HttpResponse resultResponse = httpResultClient.execute(resultRequest);
            HttpEntity responseEntity = resultResponse.getEntity();

            if (responseEntity != null) {
                String jsonString = EntityUtils.toString(responseEntity);
                JSONObject json = new JSONObject(jsonString);
                System.out.println(json.toString(2));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

【问题讨论】:

    标签: azure ocr azure-cognitive-services


    【解决方案1】:

    这些异步操作没有通知/webhook 机制。

    我唯一能正确知道的是通过使用 while 条件来更改您提到的实现,该条件定期检查结果是否存在(以及取消等待的机制 - 基于最大等待时间或数量重试次数)。

    请参阅 Microsoft 文档here 中的示例,尤其是这部分:

    // If the first REST API method completes successfully, the second 
    // REST API method retrieves the text written in the image.
    //
    // Note: The response may not be immediately available. Text
    // recognition is an asynchronous operation that can take a variable
    // amount of time depending on the length of the text.
    // You may need to wait or retry this operation.
    //
    // This example checks once per second for ten seconds.
    string contentString;
    int i = 0;
    do
    {
        System.Threading.Thread.Sleep(1000);
        response = await client.GetAsync(operationLocation);
        contentString = await response.Content.ReadAsStringAsync();
        ++i;
    }
    while (i < 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1);
    
    if (i == 10 && contentString.IndexOf("\"status\":\"Succeeded\"") == -1)
    {
        Console.WriteLine("\nTimeout error.\n");
        return;
    }
    
    // Display the JSON response.
    Console.WriteLine("\nResponse:\n\n{0}\n",
        JToken.Parse(contentString).ToString());
    

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2021-10-26
      • 2018-11-07
      • 1970-01-01
      • 2011-10-18
      相关资源
      最近更新 更多