예제 #1
0
        // Recognize text from a local image
        private async Task ExtractLocalTextAsync(
            ComputerVisionClient computerVision, string imagePath)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine(
                    "\nUnable to open or read localImagePath:\n{0} \n", imagePath);
                return;
            }

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                // Start the async process to recognize the text
                try
                {
                    BatchReadFileInStreamHeaders textHeaders =
                        await computerVision.BatchReadFileInStreamAsync(
                            imageStream, textRecognitionMode);

                    await GetTextAsync(computerVision, textHeaders.OperationLocation);
                }
                catch (Exception err)
                {
                    Global.ProcessStatus = ProcessStatus.Error.ToString();
                    //Thread.Sleep(100);
                    OnReadDone?.Invoke(err, EventArgs.Empty);
                }
            }

            //DO FILTER
        }
예제 #2
0
        // Retrieve the recognized text
        private async Task GetTextAsync(
            ComputerVisionClient computerVision, string operationLocation)
        {
            try
            {
                // Retrieve the URI where the recognized text will be
                // stored from the Operation-Location header
                string operationId = operationLocation.Substring(
                    operationLocation.Length - numberOfCharsInOperationId);

                Console.WriteLine("\nCalling GetReadOperationResultAsync()");
                ReadOperationResult result =
                    await computerVision.GetReadOperationResultAsync(operationId);

                // Wait for the operation to complete
                int i          = 0;
                int maxRetries = 10;
                while ((result.Status == TextOperationStatusCodes.Running ||
                        result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
                {
                    Console.WriteLine(
                        "Server status: {0}, waiting {1} seconds...", result.Status, i);
                    await Task.Delay(1000);

                    result = await computerVision.GetReadOperationResultAsync(operationId);
                }

                // Display the results
                Console.WriteLine();
                var recResults = result.RecognitionResults;

                int lineNo = 0;
                foreach (TextRecognitionResult recResult in recResults)
                {
                    foreach (Line line in recResult.Lines)
                    {
                        lineNo += 1;
                        Console.WriteLine(line.Text);
                        BaiOcrLine ocr = new BaiOcrLine
                        {
                            LineNo  = lineNo,
                            Content = line.Text
                        };
                        RawList.Add(ocr);
                    }
                }
                Console.WriteLine();
                Global.ProcessStatus = ProcessStatus.Ready.ToString();
                OnReadDone?.Invoke(this, EventArgs.Empty);
            }
            catch (Exception err)
            {
                throw err;
            }
        }