【问题标题】:Computer Vision Quickstart not working for Java计算机视觉快速入门不适用于 Java
【发布时间】:2021-11-21 21:13:08
【问题描述】:

我是计算机视觉的新手,我正在尝试实现以下 (example),但它总是向我发送相同的错误:

出了点问题:无法从 操作地点

/**
 * OCR with READ : Performs a Read Operation on a local image
 * @param client instantiated vision client
 * @param localFilePath local file path from which to perform the read operation against
 */
private static void ReadFromFile(ComputerVisionClient client) {
    System.out.println("-----------------------------------------------");
    
    String localFilePath = "C:\\main\\resources\\file.pdf";
    System.out.println("Read with local file: " + localFilePath);


    try {
        File rawImage = new File(localFilePath);
        byte[] localImageBytes = Files.readAllBytes(rawImage.toPath());

        // Cast Computer Vision to its implementation to expose the required methods
        ComputerVisionImpl vision = (ComputerVisionImpl) client.computerVision();

        // Read in remote image and response header
        ReadInStreamHeaders responseHeader =
                vision.readInStreamWithServiceResponseAsync(localImageBytes, null, null)
                    .toBlocking()
                    .single()
                    .headers();
   
        String operationLocation = responseHeader.operationLocation();
        System.out.println("Operation Location:" + operationLocation);

        getAndPrintReadResult(vision, operationLocation);

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
/**
 * Polls for Read result and prints results to console
 * @param vision Computer Vision instance
 * @return operationLocation returned in the POST Read response header
 */
private static void getAndPrintReadResult(ComputerVision vision, String operationLocation) throws InterruptedException {
    System.out.println("Polling for Read results ...");

    // Extract OperationId from Operation Location
    String operationId = extractOperationIdFromOpLocation(operationLocation);

    boolean pollForResult = true;
    ReadOperationResult readResults = null;

    while (pollForResult) {
        // Poll for result every second
        Thread.sleep(1000);
        readResults = vision.getReadResult(UUID.fromString(operationId));

        // The results will no longer be null when the service has finished processing the request.
        if (readResults != null) {
            // Get request status
            OperationStatusCodes status = readResults.status();

            if (status == OperationStatusCodes.FAILED || status == OperationStatusCodes.SUCCEEDED) {
                pollForResult = false;
            }
        }
    }
  
    // Print read results, page per page
    for (ReadResult pageResult : readResults.analyzeResult().readResults()) {
        System.out.println("");
        System.out.println("Printing Read results for page " + pageResult.page());
        StringBuilder builder = new StringBuilder();

        for (Line line : pageResult.lines()) {
            builder.append(line.text());
            builder.append("\n");
        }

        System.out.println(builder.toString());
    }
}


//The error marks it in this method since it cannot extract the operation id


 /**
 * Extracts the OperationId from a Operation-Location returned by the POST Read operation
 * @param operationLocation
 * @return operationId
 */
private static String extractOperationIdFromOpLocation(String operationLocation) {
    if (operationLocation != null && !operationLocation.isEmpty()) {
        String[] splits = operationLocation.split("/");

        if (splits != null && splits.length > 0) {
            return splits[splits.length - 1];
        }
    }
    throw new IllegalStateException("Something went wrong: Couldn't extract the operation id from the operation location");
}
  

如果你能帮我看看我的错误是什么或我做错了什么,我将不胜感激。

【问题讨论】:

  • 请编辑您的问题以包括细节。如所写,您没有包含任何代码、任何引发此错误的代码的特定区域、输入和输出等。您只包含指向外部存储库的链接,这需要有人去那里,克隆存储库,浏览整个示例,并尝试重新创建您的错误,但不知道您是如何设置的。请提供尽可能多的详细信息以帮助其他人帮助您。
  • 感谢cmets,我已经编辑了问题,希望可以理解

标签: java azure computer-vision


【解决方案1】:

您似乎只遵循快速入门中的一部分。在此之前,您还应该设置环境并设置您的主要方法、订阅 ID 和密钥。请关注全文:https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/client-library?tabs=visual-studio&pivots=programming-language-java

【讨论】:

  • 我已经实现了整个示例,我将这些方法放在了它向我抛出错误的部分中extractOperationIdFromOpLocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2021-08-17
  • 2011-02-01
  • 2012-02-01
  • 2021-10-26
  • 2018-11-07
  • 2019-11-24
相关资源
最近更新 更多