簡體   English   中英

為什么我收到“ComputerVisionOcrErrorException:操作返回無效狀態代碼‘BadRequest’”

[英]Why im getting "ComputerVisionOcrErrorException: Operation returned an invalid status code 'BadRequest'"

我正在嘗試為 .NET 測試計算機視覺 SDK。為此,我在 azure 中創建了計算機視覺資源。

我在 .NET6 中創建一個項目並按照Microsoft 指南實施 api 調用。

但是當我到達代碼行時:

var textHeaders = await client.ReadAsync(urlFile);

我收到異常:ComputerVisionOcrErrorException:操作返回無效狀態代碼“BadRequest”

這是完整的代碼:

public static void ComputerVisionPOC()
        {
            // Add your Computer Vision subscription key and endpoint
            const string subscriptionKey = "xxxxxxx";
            const string endpoint = @"https://xxxx.cognitiveservices.azure.com/";

            const string readTextUrlImage = "https://drive.google.com/file/d/xxxxxxxx/view?usp=sharing";
            var client = Authenticate(endpoint, subscriptionKey);

            // Extract text (OCR) from a URL image using the Read 
            ReadFileUrl(client, readTextUrlImage).Wait();
        }
        
        public static ComputerVisionClient Authenticate(string endpoint, string key)
        {
            var client =
                new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                    { Endpoint = endpoint };
            return client;
        }

        public static async Task ReadFileUrl(ComputerVisionClient client, string urlFile)
        {
            try
            {
                var textHeaders = await client.ReadAsync(urlFile);
                // After the request, get the operation location (operation ID)
                var operationLocation = textHeaders.OperationLocation;
                Thread.Sleep(2000);

                // Retrieve the URI where the extracted text will be stored from the Operation-Location header.
                // We only need the ID and not the full URL
                const int numberOfCharsInOperationId = 36;
                var operationId = operationLocation[^numberOfCharsInOperationId..];

                // Extract the text
                ReadOperationResult results;
                Console.WriteLine($"Extracting text from URL file {Path.GetFileName(urlFile)}...");
                Console.WriteLine();
                do
                {
                    results = await client.GetReadResultAsync(Guid.Parse(operationId));
                } while ((results.Status == OperationStatusCodes.Running ||
                          results.Status == OperationStatusCodes.NotStarted));

                // Display the found text.
                Console.WriteLine();
                var textUrlFileResults = results.AnalyzeResult.ReadResults;
                foreach (var page in textUrlFileResults)
                {
                    foreach (var line in page.Lines)
                    {
                        Console.WriteLine(line.Text);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

我真的很感激任何幫助!

當發送到網站服務器的請求不正確/輸入錯誤或損壞並且接收請求的服務器無法理解它時,會發生錯誤請求錯誤 (400)。

  • 代碼中使用的 URL 有可能是錯誤的/輸入錯誤的。
  • 重新檢查 readTextUrlImage 中的URL並提供有效的。
const string readTextUrlImage = "https://drive.google.com/file/d/xxxxxxxx/view?usp=sharing";
  • 我用有效的 URL 重現了相同的代碼並得到了預期的結果。

Output:在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM