private async Task <string> GetOcrResults(string operationLocation)
        {
            string operationId = operationLocation.Substring(operationLocation.LastIndexOf('/') + 1);

            ReadOperationResult result = await client.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)
            {
                await Task.Delay(1000);

                result = await client.GetReadOperationResultAsync(operationId);
            }

            var overallResults = string
                                 .Join(
                separator,
                result.RecognitionResults.Select(
                    r => string.Join(
                        separator,
                        r.Lines.Select(l => l.Text).ToArray()))
                .ToArray());

            return(CleanupText(overallResults));
        }
        // Retrieve the recognized text
        private static async Task <string> GetTextAsync(ComputerVisionClient computerVision, string operationLocation)
        {
            var numberOfCharsInOperationId = 36;

            var operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            var result = await computerVision.GetReadOperationResultAsync(operationId);

            var maxRetries = 10;

            for (var i = 0; i < maxRetries; i++)
            {
                if (result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted)
                {
                    await Task.Delay(1000);
                }
                else
                {
                    break;
                }
                result = await computerVision.GetReadOperationResultAsync(operationId);
            }

            return(result.RecognitionResults.SelectMany(recResult => recResult.Lines)
                   .Aggregate("", (current, line) => current + $"{line.Text}\n"));
        }
Esempio n. 3
0
        // Retrieve the recognized text
        private static async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation, int numberOfCharsInOperationId)
        {
            // Retrieve the URI where the recognized text will be
            // stored from the Operation-Location header
            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            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;

            foreach (TextRecognitionResult recResult in recResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    Console.WriteLine(line.Text);
                }
            }
            Console.WriteLine();
        }
Esempio n. 4
0
                    //Retriving the recognized text
                    private async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation)
                    {
                        //Retrieve the URI where the recognized text will be stored from the Operation-Location header
                        string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

                        //Calling GetReadOperationResultAsync
                        ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);

                        //Waiting for the operation to complete
                        int i          = 0;
                        int maxRetries = 10;

                        while ((result.Status == TextOperationStatusCodes.Running ||
                                result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
                        {
                            await Task.Delay(1000);

                            result = await computerVision.GetReadOperationResultAsync(operationId);
                        }

                        //Displaying the results
                        var recResults = result.RecognitionResults;


                        foreach (TextRecognitionResult recResult in recResults)
                        {
                            foreach (Line line in recResult.Lines)
                            {
                                OCRList.Add(line.Text);
                                OcrResult += line.Text + " <br> ";
                            }
                        }
                    }
Esempio n. 5
0
        /// <summary>
        /// Gets the text visible in the specified image file by using
        /// the Computer Vision REST API.
        /// </summary>
        /// <param name="imageFilePath">The image file with printed text.</param>
        static async Task <IList <TextRecognitionResult> > MakeOcrRequestAsync(string imageFilePath)
        {
            const string subscriptionKey = "";
            var          computerVision  = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey), new DelegatingHandler[] { });

            // You must use the same region as you used to get your subscription
            // keys. For example, if you got your subscription keys from westus,
            // replace "westcentralus" with "westus".
            //
            // Free trial subscription keys are generated in the westcentralus
            // region. If you use a free trial subscription key, you shouldn't
            // need to change the region.
            // Specify the Azure region
            computerVision.Endpoint = "https://westcentralus.api.cognitive.microsoft.com";
            BatchReadFileInStreamHeaders headers;

            //RecognizeTextInStreamHeaders headers;
            using (var image = File.OpenRead(imageFilePath))
            {
                headers = await computerVision.BatchReadFileInStreamAsync(image);

                //headers = await computerVision.RecognizeTextInStreamAsync(image, TextRecognitionMode.Printed);
            }

            var operationId = headers.OperationLocation.Substring(headers.OperationLocation.Length - numberOfCharsInOperationId);

            var result = await computerVision.GetReadOperationResultAsync(operationId);

            // Wait for the operation to complete
            var i          = 0;
            var 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;

            foreach (TextRecognitionResult recResult in recResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    Console.WriteLine(line.Text);
                }
            }

            return(recResults);
        }
Esempio n. 6
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;
            }
        }
Esempio n. 7
0
        /*
         *	EXTRACT TEXT - URL IMAGE
         */
        public async Task ExtractTextFromURL(string url)
        {
            const int numberOfCharsInOperationId = 36;

            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("EXTRACT TEXT - URL");
            Console.WriteLine();

            Console.WriteLine($"Extracting text from url {url}...");
            Console.WriteLine();

            var headers = await _client.BatchReadFileWithHttpMessagesAsync(url);

            string operationLocation = headers.Headers.OperationLocation;

            // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
            // We only need the ID and not the full URL

            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            // Extract the text
            // Delay is between iterations and tries a maximum of 10 times.
            int i          = 0;
            int maxRetries = 10;
            ReadOperationResult results;

            do
            {
                results = await _client.GetReadOperationResultAsync(operationId);

                Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                await Task.Delay(1000);
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

            // Display the found text.
            Console.WriteLine();
            var recognitionResults = results.RecognitionResults;

            foreach (TextRecognitionResult result in recognitionResults)
            {
                foreach (var line in result.Lines)
                {
                    if (_reNumberPlate.IsMatch(line.Text))
                    {
                        Console.WriteLine(line.Text);
                    }
                }
            }
            Console.WriteLine();
        }
Esempio n. 8
0
        private static async Task RunAsync()
        {
            string endpoint =; // insert your endpoint
            string key      =; // insert your key

            var cvc = new ComputerVisionClient(
                new ApiKeyServiceClientCredentials(key))
            {
                Endpoint = endpoint
            };

            var file = "image.PNG";

            using (var imageStream = File.OpenRead(file))
            {
                Console.WriteLine("Beginning processing...");
                // Start the async process to recognize the text
                var textHeaders = await cvc.BatchReadFileInStreamAsync(imageStream);

                const int numberOfCharsInOperationId = 36;
                var       operationId = textHeaders.OperationLocation.Substring(
                    textHeaders.OperationLocation.Length - numberOfCharsInOperationId);

                var result = await cvc.GetReadOperationResultAsync(operationId);

                // Wait for the operation to complete
                var i          = 0;
                var 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 cvc.GetReadOperationResultAsync(operationId);
                }

                foreach (var item in result.RecognitionResults)
                {
                    Console.WriteLine($"Result: {item.Width} {item.Height} {item.Lines.Count}");

                    foreach (Line line in item.Lines)
                    {
                        line.BoundingBox.ToList().ForEach(bbi => Console.Write($"{bbi} "));
                        Console.WriteLine($"{line.Text}");
                    }
                }
            }
        }
        public static async Task <string> ExtractUrlLocal(ComputerVisionClient client, string localImage)
        {
            const int numberOfCharsInOperationId = 36;

            using (Stream imageStream = File.OpenRead(localImage))
            {
                BatchReadFileInStreamHeaders localFileTextHeaders = await client.BatchReadFileInStreamAsync(imageStream);

                string operationLocation = localFileTextHeaders.OperationLocation;
                string operationId       = operationLocation.Substring
                                               (operationLocation.Length - numberOfCharsInOperationId);
                int i          = 0;
                int maxRetries = 10;
                ReadOperationResult results;
                do
                {
                    results = await client.GetReadOperationResultAsync(operationId);

                    await Task.Delay(1000);
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);
                StringBuilder sb = new StringBuilder();
                var           recognitionResults = results.RecognitionResults;
                foreach (TextRecognitionResult result in recognitionResults)
                {
                    foreach (Line line in result.Lines)
                    {
                        sb.Append($"{line.Text} ");
                    }
                }
                return(sb.ToString());
            }
        }
        static async Task GetTextFromImageAsync(ComputerVisionClient visionClient, string url)
        {
            BatchReadFileHeaders textHeaders = await visionClient.BatchReadFileAsync(url);

            string operationLocation     = textHeaders.OperationLocation;
            int    maxCharsInOperationId = 36;
            string operationId           = operationLocation.Substring(operationLocation.Length - maxCharsInOperationId);
            int    i                    = 0;
            int    maxRetries           = 10;
            ReadOperationResult results = null;

            do
            {
                results = await visionClient.GetReadOperationResultAsync(operationId);

                Console.WriteLine($"Статус сервера {results.Status}, выполняется {i} c");
                await Task.Delay(1000);

                if (i == 9)
                {
                    Console.WriteLine("Сервер вызвал таймаут");
                }
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);
            Console.WriteLine();
            var textRecognitionResults = results.RecognitionResults;

            foreach (TextRecognitionResult textRecognitionResult in results.RecognitionResults)
            {
                foreach (var line in textRecognitionResult.Lines)
                {
                    Console.WriteLine(line.Text);
                }
            }
        }
Esempio n. 11
0
        async Task <List <string> > GetLines()
        {
            var photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions { PhotoSize = PhotoSize.Full });

            if (photo != null)
            {
                var computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(AppConstants.OcrKey))
                {
                    Endpoint = AppConstants.OcrEndpoint
                };

                var results = await computerVision.BatchReadFileInStreamAsync(photo.GetStream());

                var loc = results.OperationLocation;

                var operationId = loc.Substring(loc.Length - 36);


                var result = await computerVision.GetReadOperationResultAsync(operationId);

                // Wait for the operation to complete
                var i          = 0;
                var maxRetries = 10;
                while ((result.Status == TextOperationStatusCodes.Running ||
                        result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
                {
                    await Task.Delay(100);

                    result = await computerVision.GetReadOperationResultAsync(operationId);
                }

                if (result.Status == TextOperationStatusCodes.Running ||
                    result.Status == TextOperationStatusCodes.NotStarted)
                {
                    return(new List <string>());
                }

                var all = result.RecognitionResults.SelectMany(l => l.Lines);

                return(all.Select(l => l.Text).ToList());
            }

            return(new List <string>());
        }
        /*
         * END - GENERATE THUMBNAIL
         */

        // <snippet_extract_call>

        /*
         * BATCH READ FILE - URL IMAGE
         * Recognizes handwritten text.
         * This API call offers an improvement of results over the Recognize Text calls.
         */
        public static async Task BatchReadFileUrl(ComputerVisionClient client, string urlImage)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("BATCH READ FILE - URL IMAGE");
            Console.WriteLine();

            // Read text from URL
            BatchReadFileHeaders textHeaders = await client.BatchReadFileAsync(urlImage);

            // After the request, get the operation location (operation ID)
            string operationLocation = textHeaders.OperationLocation;
            // </snippet_extract_call>

            // <snippet_extract_response>
            // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
            // We only need the ID and not the full URL
            const int numberOfCharsInOperationId = 36;
            string    operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            // Extract the text
            // Delay is between iterations and tries a maximum of 10 times.
            int i          = 0;
            int maxRetries = 10;
            ReadOperationResult results;

            Console.WriteLine($"Extracting text from URL image {Path.GetFileName(urlImage)}...");
            Console.WriteLine();
            do
            {
                results = await client.GetReadOperationResultAsync(operationId);

                Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                await Task.Delay(1000);

                if (maxRetries == 9)
                {
                    Console.WriteLine("Server timed out.");
                }
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);
            // </snippet_extract_response>

            // <snippet_extract_display>
            // Display the found text.
            Console.WriteLine();
            var textRecognitionLocalFileResults = results.RecognitionResults;

            foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    Console.WriteLine(line.Text);
                }
            }
            Console.WriteLine();
        }
        private static async Task <ReadOperationResult> GetReadResultAsync(ComputerVisionClient computerVision, string operationLocation)
        {
            // Retrieve the URI where the recognized text will be stored from the Operation-Location header
            string operationId = operationLocation.Substring(operationLocation.Length - NumberOfCharsInOperationId);
            var    result      = await computerVision.GetReadOperationResultAsync(operationId);

            // Wait for the operation to complete
            int i = 0;

            while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) &&
                   i++ < MaxRetriesOnTextRecognition)
            {
                await Task.Delay(DelayOnTextRecognition);

                result = await computerVision.GetReadOperationResultAsync(operationId);
            }

            return(result);
        }
Esempio n. 14
0
        // </snippet_extract_display>

        /*
         * END - BATCH READ FILE - URL IMAGE
         */

        /*
         * BATCH READ FILE - LOCAL IMAGE
         * This API call offers an improvement of results over the Recognize Text calls.
         */
        public static async Task BatchReadFileLocal(ComputerVisionClient client, string localImage)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("BATCH READ FILE - LOCAL IMAGE");
            Console.WriteLine();

            // Helps calucalte starting index to retrieve operation ID
            const int numberOfCharsInOperationId = 36;

            Console.WriteLine($"Extracting text from local image {Path.GetFileName(localImage)}...");
            Console.WriteLine();
            using (Stream imageStream = File.OpenRead(localImage))
            {
                // Read the text from the local image
                BatchReadFileInStreamHeaders localFileTextHeaders = await client.BatchReadFileInStreamAsync(imageStream);

                // Get the operation location (operation ID)
                string operationLocation = localFileTextHeaders.OperationLocation;

                // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

                // Extract text, wait for it to complete.
                int i          = 0;
                int maxRetries = 10;
                ReadOperationResult results;
                do
                {
                    results = await client.GetReadOperationResultAsync(operationId);

                    Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                    await Task.Delay(1000);

                    if (maxRetries == 9)
                    {
                        Console.WriteLine("Server timed out.");
                    }
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

                // Display the found text.
                Console.WriteLine();
                var textRecognitionLocalFileResults = results.RecognitionResults;
                foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
                {
                    foreach (Line line in recResult.Lines)
                    {
                        Console.WriteLine(line.Text);
                    }
                }
                Console.WriteLine();
            }
        }
Esempio n. 15
0
        private static async Task <string> GetTextAsync(ComputerVisionClient computerVision, string operationLocation)
        {
            const int numberOfCharsInOperationId = 36;
            string    operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);


            ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);

            int i          = 0;
            int maxRetries = 10;

            while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
            {
                await Task.Delay(1000);

                result = await computerVision.GetReadOperationResultAsync(operationId);
            }

            var lines      = result.RecognitionResults;
            var outputtext = new System.Text.StringBuilder();

            foreach (TextRecognitionResult recResult in lines)
            {
                foreach (Line line in recResult.Lines)
                {
                    outputtext.AppendLine(line.Text);
                }
            }

            if (!string.IsNullOrEmpty(outputtext.ToString()))
            {
                return(await Task.FromResult(outputtext.ToString()));

                // await TextToSepach(outputtext.ToString());
            }
            else
            {
                return(string.Empty);
            }
        }
Esempio n. 16
0
        //-------------------------------------------------------------



        public async Task BatchReadFileLocal(ComputerVisionClient client, string localImage)
        {
            // Helps calucalte starting index to retrieve operation ID
            const int numberOfCharsInOperationId = 36;

            addDebugMessage($"Extracting text from local image {Path.GetFileName(localImage)}...");
            Console.WriteLine();
            using (Stream imageStream = File.OpenRead(localImage))
            {
                // Read the text from the local image
                BatchReadFileInStreamHeaders localFileTextHeaders = await client.BatchReadFileInStreamAsync(imageStream);

                addDebugMessage("Read text from local image.");
                // Get the operation location (operation ID)
                string operationLocation = localFileTextHeaders.OperationLocation;
                addDebugMessage("Got the operation location.");
                // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
                addDebugMessage("Got the URI.");
                // Extract text, wait for it to complete.
                int i          = 0;
                int maxRetries = 10;
                ReadOperationResult results;
                do
                {
                    results = await client.GetReadOperationResultAsync(operationId);

                    await Task.Delay(1000);

                    if (i == 9)
                    {
                        addDebugMessage("Server timed out.");
                    }
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

                // Display the found text.
                Console.WriteLine();
                var textRecognitionLocalFileResults = results.RecognitionResults;
                foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
                {
                    foreach (Line line in recResult.Lines)
                    {
                        textBox2.AppendText(line.Text + Environment.NewLine);
                    }
                }
            }

            button1.Enabled = true;
            button2.Enabled = true;
            button2.Text    = "Detect";
        }
        private static async Task <string> GetTextAsync(ComputerVisionClient computerVision,
                                                        string operationLocation)
        {
            int numberOfCharsInOperationId = 36;
            // Retrieve the URI where the recognized text will be
            // stored from the Operation-Location header
            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            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);
            }

            //TODO: Improve Error Handling - Case where Max Tries exceed for Result.STatus is failure

            // Display the results
            var           recResults = result.RecognitionResults;
            StringBuilder imageText  = new StringBuilder();

            foreach (TextRecognitionResult recResult in recResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    imageText.Append(line.Text);
                    imageText.Append(" ");
                }
            }
            return(imageText.ToString());
        }
Esempio n. 18
0
        public async Task <ExtractTextFromPictureResult> ExtractTextFromPicture(Stream pictureStream)
        {
            try
            {
                BatchReadFileInStreamHeaders textHeaders = await _computerVisionClient.BatchReadFileInStreamAsync(pictureStream);

                string operationLocation = textHeaders.OperationLocation;

                // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
                // We only need the ID and not the full URL
                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

                // Extract the text
                // Delay is between iterations and tries a maximum of 10 times.
                int numberOfRetries = 0;
                ReadOperationResult results;

                do
                {
                    results = await _computerVisionClient.GetReadOperationResultAsync(operationId);

                    await Task.Delay(1000);

                    if (numberOfRetries == 9)
                    {
                        return(new ExtractTextFromPictureResult {
                            ErrorMessage = "Server timed out."
                        });
                    }
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && numberOfRetries++ < maxNumberOfRetries);

                var            textRecognitionLocalFileResults = results.RecognitionResults;
                IList <string> textLines = new List <string>();
                foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
                {
                    foreach (Line line in recResult.Lines)
                    {
                        textLines.Add(line.Text);
                    }
                }

                return(new ExtractTextFromPictureResult(textLines.ToArray()));
            }
            catch (Exception e)
            {
                return(new ExtractTextFromPictureResult {
                    ErrorMessage = e.Message
                });
            }
        }
Esempio n. 19
0
        public static async Task <IList <TextRecognitionResult> > GetTextAsync(Stream imageToAnalyze)
        {
            BatchReadFileInStreamHeaders textHeaders = await _computerVisionClient.BatchReadFileInStreamAsync(imageToAnalyze);

            string operationLocation = textHeaders.OperationLocation;
            string operationId       = operationLocation.Substring(operationLocation.Length - NumberOfCharsInOperationId);

            ReadOperationResult result = await _computerVisionClient.GetReadOperationResultAsync(operationId);

            int i          = 0;
            int maxRetries = 10;

            while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
            {
                await Task.Delay(1000);

                result = await _computerVisionClient.GetReadOperationResultAsync(operationId);
            }

            IList <TextRecognitionResult> recResults = result.RecognitionResults;

            return(recResults);
        }
        private async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation, int numberOfCharsInOperationId)
        {
            string operationId         = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
            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
            var recResults = result.RecognitionResults;

            foreach (TextRecognitionResult recResult in recResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    ProcessValue(line.Text);
                }
            }

            Console.WriteLine($"NIT:{NITValue}");
            Console.WriteLine($"NIT:{IVAValue}");
            Console.WriteLine($"NIT:{InvoiceNumber}");
            Console.WriteLine($"NIT:{Subtotal}");
            Console.WriteLine($"NIT:{InvoiceDate}");

            Console.ReadLine();
        }
Esempio n. 21
0
        // public static ComputerVisionClient Authenticate(string endpoint, string key)
        // {
        //     ComputerVisionClient client =
        //       new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
        //       { Endpoint = endpoint };
        //     return client;
        // }

        public async Task <List <string> > BatchReadFileUrl(ComputerVisionClient client, Stream imgStream)
        {
            String fullTextResultList = "";

            // Read text from URL

            var textHeaders = await client.BatchReadFileInStreamAsync(imgStream);

            // After the request, get the operation location (operation ID)
            string operationLocation = textHeaders.OperationLocation;

            const int numberOfCharsInOperationId = 36;
            string    operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            // Extract the text
            // Delay is between iterations and tries a maximum of 10 times.
            int i          = 0;
            int maxRetries = 10;
            ReadOperationResult results;

            do
            {
                results = await client.GetReadOperationResultAsync(operationId);

                Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                await Task.Delay(1000);

                if (i == 9)
                {
                    Console.WriteLine("Server timed out.");
                }
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

            var textRecognitionLocalFileResults = results.RecognitionResults;

            foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
            {
                foreach (Line line in recResult.Lines)
                {
                    fullTextResultList += line.Text + " ";
                }
            }
            var cleanResults = resultCleanUp(fullTextResultList);

            return(cleanResults);
        }
        public static async Task <string> ExtractText(ComputerVisionClient client, string urlImage)
        {
            // Read text from URL
            BatchReadFileHeaders textHeaders = await client.BatchReadFileAsync(urlImage);

            // After the request, get the operation location (operation ID)
            string operationLocation = textHeaders.OperationLocation;

            // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
            // We only need the ID and not the full URL
            const int numberOfCharsInOperationId = 36;
            string    operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

            // Extract the text
            // Delay is between iterations and tries a maximum of 10 times.
            int i          = 0;
            int maxRetries = 10;
            ReadOperationResult results;

            Console.WriteLine($"Extracting text from URL image {System.IO.Path.GetFileName(urlImage)}...");
            Console.WriteLine();
            do
            {
                results = await client.GetReadOperationResultAsync(operationId);

                Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                await Task.Delay(1000);
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

            // Display the found text.
            Console.WriteLine();
            var    recognitionResults = results.RecognitionResults;
            string content            = string.Empty;

            foreach (TextRecognitionResult result in recognitionResults)
            {
                foreach (Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.Line line in result.Lines)
                {
                    Console.WriteLine(line.Text);
                    content += line.Text + Environment.NewLine;
                }
            }
            Console.WriteLine();
            return(content);
        }
        public static async Task ExtractTextLocal(ComputerVisionClient client, string localImage)
        {
            // Helps calucalte starting index to retrieve operation ID
            const int numberOfCharsInOperationId = 36;

            using (Stream imageStream = File.OpenRead(localImage))
            {
                // Read the text from the local image
                BatchReadFileInStreamHeaders localFileTextHeaders = await client.BatchReadFileInStreamAsync(imageStream);

                // Get the operation location (operation ID)
                string operationLocation = localFileTextHeaders.OperationLocation;
                // Retrieve the URI where the recognized text will be stored from the Operation-Location header.
                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
                // Extract text, wait for it to complete.
                int i          = 0;
                int maxRetries = 10;
                ReadOperationResult results;
                do
                {
                    results = await client.GetReadOperationResultAsync(operationId);

                    await Task.Delay(1000);

                    if (maxRetries == 9)
                    {
                        throw new Exception("Azure API timed out");
                    }
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);
                // Display the found text.
                var textRecognitionLocalFileResults = results.RecognitionResults;
                foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
                {
                    using (StreamWriter sw = new StreamWriter(@"C:\Users\35385\source\repos\BookingSystem\BookingSystem\surveytest.txt"))
                    {
                        foreach (Line line in recResult.Lines)
                        {
                            sw.WriteLine(line.Text);
                        }
                    }
                }
            }
        }
Esempio n. 24
0
        private async Task <ReadOperationResult> ReadDocumentScanResult(string operationId)
        {
            int i          = 0;
            int maxRetries = 20;
            ReadOperationResult results;

            do
            {
                results = await _computerVisionClient.GetReadOperationResultAsync(operationId);

                _log.LogInformation("Server status: {0}, waiting {1} seconds...", results.Status, i);
                await Task.Delay(2000);

                if (i == 9)
                {
                    _log.LogError("Server timed out. Cannot get OCR scanning result.");
                }
            }while ((results.Status == TextOperationStatusCodes.Running ||
                     results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

            return(results);
        }
Esempio n. 25
0
        private String BatchReadFileLocal(ComputerVisionClient client, String localImage)
        {
            StringBuilder stringBuilder = new StringBuilder();
            const Int32   numberOfCharsInOperationId = 36;

            using (Stream imageStream = System.IO.File.OpenRead(localImage))
            {
                BatchReadFileInStreamHeaders localFileTextHeaders = client.BatchReadFileInStreamAsync(imageStream).Result;
                string operationLocation = localFileTextHeaders.OperationLocation;

                string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

                int i          = 0;
                int maxRetries = 10;
                ReadOperationResult results;
                do
                {
                    results = client.GetReadOperationResultAsync(operationId).Result;
                    Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
                    Task.Delay(1000);
                    if (i == 9)
                    {
                        return(String.Empty);
                    }
                }while ((results.Status == TextOperationStatusCodes.Running ||
                         results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

                var textRecognitionLocalFileResults = results.RecognitionResults;
                foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
                {
                    foreach (Line line in recResult.Lines)
                    {
                        stringBuilder.Append(line.Text);
                    }
                }
            }

            return(stringBuilder.ToString());
        }
Esempio n. 26
0
        static void Main(string[] args)
        {
            string endpoint = "https://westeurope.api.cognitive.microsoft.com/";
            string key      = "c50bac85176f4da384d8e2d06bf17d41";

            var credentials = new ApiKeyServiceClientCredentials(key);
            var client      = new ComputerVisionClient(credentials);

            client.Endpoint = endpoint;

            //Detección de objetos

            var files = Directory.GetFiles(Environment.CurrentDirectory, "*.jpg");

            if (!files.Any())
            {
                System.Console.WriteLine("No hay imágenes");
                return;
            }

            var features = new List <VisualFeatureTypes>();

            features.Add(VisualFeatureTypes.Objects);
            features.Add(VisualFeatureTypes.Description);
            features.Add(VisualFeatureTypes.Tags);

            foreach (var file in files)
            {
                using (Stream stream = File.OpenRead(file)){
                    var analysis = client.AnalyzeImageInStreamAsync(stream, features).Result;

                    System.Console.WriteLine($"Archivo: {file}");
                    System.Console.WriteLine(analysis.Description?.Captions?.FirstOrDefault().Text);
                    System.Console.WriteLine(analysis.Objects?.FirstOrDefault()?.ObjectProperty);
                    System.Console.WriteLine();
                    System.Console.WriteLine();
                }
            }

            //Detección de texto

            files = Directory.GetFiles(Environment.CurrentDirectory, "texto*.jpg");

            foreach (var file in files)
            {
                using (Stream stream = File.OpenRead(file))
                {
                    var analysis = client.BatchReadFileInStreamAsync(stream, TextRecognitionMode.Printed).Result;

                    string operationId = analysis.OperationLocation.Substring(analysis.OperationLocation.Length - 36);

                    var result = client.GetReadOperationResultAsync(operationId).Result;

                    while (result.Status == TextOperationStatusCodes.Running ||
                           result.Status == TextOperationStatusCodes.NotStarted)
                    {
                        System.Console.WriteLine("Esperando...");
                        Task.Delay(1000).Wait();

                        result = client.GetReadOperationResultAsync(operationId).Result;
                    }


                    if (result.RecognitionResults == null)
                    {
                        continue;
                    }

                    foreach (var recognition in result.RecognitionResults)
                    {
                        foreach (var line in recognition?.Lines)
                        {
                            System.Console.WriteLine(line.Text);
                        }
                    }
                }
            }
        }
Esempio n. 27
0
                        //Retriving the recognized text
                        public async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation)
                        {
                            //Retrieve the URI where the recognized text will be stored from the Operation-Location header
                            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

                            //Calling GetReadOperationResultAsync
                            ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);

                            //Waiting for the operation to complete
                            int i          = 0;
                            int maxRetries = 10;

                            while ((result.Status == TextOperationStatusCodes.Running ||
                                    result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
                            {
                                await Task.Delay(1000);

                                result = await computerVision.GetReadOperationResultAsync(operationId);
                            }

                            //Displaying the results
                            var recResults = result.RecognitionResults;


                            foreach (TextRecognitionResult recResult in recResults)
                            {
                                foreach (Line line in recResult.Lines)
                                {
                                    OCRList.Add(line.Text);
                                    OcrResult += line.Text + " <br> ";
                                }
                            }


                            //Loop
                            for (int j = 0; j < OCRList.Count; j++)
                            {
                                //Calling LUIS
                                var           client   = new RestClient(ConfigurationManager.AppSettings["LUIS_EndPoint"] + ConfigurationManager.AppSettings["LUIS_AppID"] + "?verbose=true&timezoneOffset=-360&subscription-key=" + ConfigurationManager.AppSettings["LUIS_Key"] + "&q=" + OCRList[j]);
                                var           request  = new RestRequest(Method.GET);
                                IRestResponse response = client.Execute(request);
                                dynamic       jObject  = JObject.Parse(response.Content);

                                JArray luislenobj = JArray.Parse(jObject.entities.ToString());

                                if (luislenobj.Count > 0)
                                {
                                    for (int k = 0; k < luislenobj.Count; k++)
                                    {
                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Contract Details" && jObject["entities"][k]["type"].ToString() == "Contract Date")
                                        {
                                            ContractDate = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }
                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Contract Details" && jObject["entities"][k]["type"].ToString() == "Client Name")
                                        {
                                            ClientName = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }
                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Contract Details" && jObject["entities"][k]["type"].ToString() == "Vendor Name")
                                        {
                                            VendorName += jObject["entities"][k]["entity"].ToString();
                                        }


                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Services" && jObject["entities"][k]["type"].ToString() == "Services")
                                        {
                                            Services += " " + jObject["entities"][k]["entity"].ToString();
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Contract Value" && jObject["entities"][k]["type"].ToString() == "Contract Value")
                                        {
                                            ContractValue = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Contract End Date" && jObject["entities"][k]["type"].ToString() == "End Date")
                                        {
                                            EndDate = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Penalty" && jObject["entities"][k]["type"].ToString() == "builtin.percentage")
                                        {
                                            PenaltyValue = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Jurisdiction" && jObject["entities"][k]["type"].ToString() == "Jurisdiction Place")
                                        {
                                            JurisdictionPlace = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Vendor Details" && jObject["entities"][k]["type"].ToString() == "builtin.email")
                                        {
                                            VendorEmail = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }
                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Vendor Details" && jObject["entities"][k]["type"].ToString() == "builtin.phonenumber")
                                        {
                                            VendorPhone = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }

                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Client Details" && jObject["entities"][k]["type"].ToString() == "builtin.email")
                                        {
                                            ClientEmail = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }
                                        if (jObject["topScoringIntent"]["intent"].ToString() == "Discover Client Details" && jObject["entities"][k]["type"].ToString() == "builtin.phonenumber")
                                        {
                                            ClientPhone = jObject["entities"][k]["entity"].ToString();
                                            break;
                                        }
                                    }
                                }
                            }

                            FinalResult = "<br>" + "Contract Date : " + ContractDate + "<br>" + "Vendor Name : " + VendorName + "<br>" + "Client Name : " + ClientName + "<br>" + "Service Description : " + Services + "<br>" + "Contract Value : " + ContractValue + "<br>" + "End Date : " + EndDate + "<br>" + "Penalty Value : " + PenaltyValue + "<br>" + "Jurisdiction Place : " + JurisdictionPlace + "<br>" + "Vendor Email : " + VendorEmail + "<br>" + "Vendor Phone : " + VendorPhone + "<br>" + "Client Email : " + ClientEmail + "<br>" + "Client Phone : " + ClientPhone + "<br>";
                        }
        public async Task <ConvertResponse> ExtractTextBatchRead(ConvertResponse response)
        {
            try
            {
                var key      = _config["computerVision:key"];
                var endpoint = _config["computerVision:endpoint"];
                ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                {
                    Endpoint = endpoint
                };

                var operationInfo = await computerVision.BatchReadFileAsync(response.Request.UploadBlobUrl + _config["storage:sas"]);

                var result      = new ReadOperationResult();
                var operationId = operationInfo.OperationLocation.Split('/').Last();

                while (result.Status != TextOperationStatusCodes.Failed && result.Status != TextOperationStatusCodes.Succeeded)
                {
                    await Task.Delay(500);

                    result = await computerVision.GetReadOperationResultAsync(operationId);
                }

                if (result.Status == TextOperationStatusCodes.Failed)
                {
                    response.ErrorMessage = $"Text translation failed.";
                    return(response);
                }

                var text = new StringBuilder();
                foreach (var page in result.RecognitionResults)
                {
                    Line lastLine = null;
                    foreach (var line in page.Lines)
                    {
                        // if (lastLine?.Words.Count >= 4)
                        // {
                        //  text.Append($" {line.Text}");
                        // }
                        // else
                        // {
                        text.Append(Environment.NewLine + line.Text);
                        // }

                        lastLine = line;
                    }
                }

                Console.WriteLine();
                Console.WriteLine(text.ToString());

                var transcriptBlobName = Path.GetFileNameWithoutExtension(response.Request.BlobName) + ".txt";

                var blobClient = new BlobStorageClient(_config);
                var textBlob   = blobClient.GetBlobBlock("transcripts", transcriptBlobName);

                response.TranscriptBlobUrl = textBlob.Uri.AbsoluteUri;
                response.Transcript        = text.ToString().Trim();

                await textBlob.UploadTextAsync(response.Transcript);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine(ex.Message);
                response.ErrorMessage = ex.ToString();
            }

            return(response);
        }