计算机视觉 OCR 提取完成后触发通知



我正在探索Microsoft计算机视觉的读取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());
}
}
}

这些异步操作没有通知/网络钩子机制。

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

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

// 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:nn{0}n",
JToken.Parse(contentString).ToString());

最新更新