【问题标题】:Project oxford vision API ocr exceptionProject oxford vision API ocr异常
【发布时间】:2017-03-24 09:24:27
【问题描述】:

牛津视觉 API 项目出现问题。 project oxford git 中的示例可以正常工作并识别图像上的文本。但是我的代码抛出异常:

引发了“Microsoft.ProjectOxford.Vision.ClientException”类型的异常。 在 Microsoft.ProjectOxford.Vision.VisionServiceClient.HandleException(异常异常) 在 Microsoft.ProjectOxford.Vision.VisionServiceClient.b__39_1[TRequest,TResponse](异常 e) 在 System.AggregateException.Handle(Func2 predicate) at Microsoft.ProjectOxford.Vision.VisionServiceClient.<SendAsync>d__392.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 Microsoft.ProjectOxford.Vision.VisionServiceClient.d__32.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at ..OcrWorker.<UploadAndRecognizeImageAsync>d__15.MoveNext() in ..\\OcrWorker.cs:line 165\r\n --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() 在 ..OcrWorker.d__14.MoveNext() 在 ..\OcrWorker.cs:127 行

类代码:

public string SubscriptionKey { get; set; }
    public string OcrResultText
    {
        get
        {
            if (FullOcrResult == null)
            {
                FullOcrResult = new StringBuilder();
            }
            string response = string.Empty;
            if (OcrDone)
            {
                response = FullOcrResult.ToString();
            }
            else
            {
                response = null;
            }
            return response;
        }
    }
    private bool OcrDone = true;
    public bool IsOcrDone { get { return OcrDone; } }
    private StringBuilder FullOcrResult;

    public OcrWorker(string appKey)
    {
        SubscriptionKey = appKey;
        FullOcrResult = new StringBuilder();
    }

    public string DoWorkSync(List<Bitmap> images)
    {
        if (OcrDone)
        {
            List<IterationItem> iteartionItems = new List<IterationItem>();
            int i = 0;
            OcrDone = false;
            foreach (var image in images)
            {
                IterationItem ocrIterationItem = new IterationItem();
                try
                {
                    Task<IterationItem> o = DoWorkForIterationAsync(image, i);
                    o.Wait();
                    ocrIterationItem = o.Result;
                }
                catch (Exception ex)
                {
                    var a = ex.GetBaseException();
                }
                iteartionItems.Add(ocrIterationItem);
                i++;
            }
            GetOcrResultFromIterations(iteartionItems);
            OcrDone = true;
        }
        return OcrResultText;
    }

    public void WriteResultToFile(string path)
    {
        if (OcrDone)
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.AppendAllText(path, OcrResultText);
        }
    }

    private void GetOcrResultFromIterations(List<IterationItem> iterationResults)
    {
        iterationResults = iterationResults.OrderBy(item => item.Number).ToList();
        foreach (var iterationItem in iterationResults)
        {
            var results = iterationItem.OcrResult;
            FullOcrResult.AppendLine();
            foreach (var item in results.Regions)
            {
                foreach (var line in item.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        FullOcrResult.Append(word.Text);
                        FullOcrResult.Append(" ");
                    }
                    FullOcrResult.AppendLine();
                }
                FullOcrResult.AppendLine();
            }
        }
    }

    /// <summary>
    /// Perform the work for this scenario
    /// </summary>
    /// <param name="imageUri">The URI of the image to run against the scenario</param>
    /// <param name="upload">Upload the image to Project Oxford if [true]; submit the Uri as a remote url if [false];</param>
    /// <returns></returns>
    private async Task<IterationItem> DoWorkForIterationAsync(Bitmap image, int iterationNumber)
    {
        var _status = "Performing OCR...";

        //
        // Upload an image
        //
        OcrResults ocrResult = await UploadAndRecognizeImageAsync(image, RecognizeLanguage.ShortCode);
        _status = "OCR Done";

        //
        // Log analysis result in the log window
        //
        return new IterationItem()
        {
            Number = iterationNumber,
            OcrResult = ocrResult
        };
    }

    /// <summary>
    /// Uploads the image to Project Oxford and performs OCR
    /// </summary>
    /// <param name="imageFilePath">The image file path.</param>
    /// <param name="language">The language code to recognize for</param>
    /// <returns></returns>
    private async Task<OcrResults> UploadAndRecognizeImageAsync(Bitmap image, string language)
    {
        // -----------------------------------------------------------------------
        // KEY SAMPLE CODE STARTS HERE
        // -----------------------------------------------------------------------

        //
        // Create Project Oxford Vision API Service client
        //
        VisionServiceClient VisionServiceClient = new VisionServiceClient(SubscriptionKey);
        Log("VisionServiceClient is created");

        using (Stream imageMemoryStream = new MemoryStream())
        {
            image.Save(imageMemoryStream, ImageFormat.Bmp);
            //
            // Upload an image and perform OCR
            //
            Log("Calling VisionServiceClient.RecognizeTextAsync()...");
            OcrResults ocrResult = await VisionServiceClient.RecognizeTextAsync(imageMemoryStream, language);
            return ocrResult;
        }

        // -----------------------------------------------------------------------
        // KEY SAMPLE CODE ENDS HERE
        // -----------------------------------------------------------------------
    }

    //get ocred text
    class IterationItem
    {
        public int Number { get; set; }
        public OcrResults OcrResult { get; set; }
    }
    public static class RecognizeLanguage
    {
        public static string ShortCode { get { return "en"; } }
        public static string LongName { get { return "English"; } }
    }

有没有人遇到同样的问题,我该如何解决?

【问题讨论】:

  • 解决了!对于正确的工作,您应该使用:UploadAndRecognizeImageAsync 方法中的 File.OpenRead(imageFilePath) 而不是:创建 MemoryStream 并将位图复制到它所以不要将内存流传递给 SDK 的 RecognizeTextAsync 方法
  • FileStream 解决方案很好,但如果您希望 MemoryStream 出于任何原因工作(例如,您需要在调用 OCR 之前裁剪或操作图像),您需要做的就是'rewind ' 首先通过调用imageMemoryStream.Position = 0 流。否则,您在 MemoryStream 上的“读取”指针位于image.Save() 之后。

标签: c# .net ocr microsoft-cognitive


【解决方案1】:

解决了!对于正确的工作,您应该使用 imageMemoryStream.Seek(0, SeekOrigin.Begin);从图像复制流后

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2023-03-02
    • 2016-07-31
    相关资源
    最近更新 更多