public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "Photos",
                 collectionName: "Text",
                 ConnectionStringSetting = "CosmosDBConnection")]  IAsyncCollector <NumberItem> items,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var data = await req.ReadAsStringAsync();

            dynamic obj        = JsonConvert.DeserializeObject(data);
            string  photo      = obj?.Photo;
            var     imageBytes = Convert.FromBase64String(photo);

            using (var ms = new MemoryStream(imageBytes))
            {
                var result = await computerVision.RecognizeTextInStreamAsync(ms, TextRecognitionMode.Handwritten);

                var text = await GetTextAsync(result.OperationLocation);

                log.LogInformation($"Text read = {text}");

                if (double.TryParse(text, out var d))
                {
                    await items.AddAsync(new NumberItem { Id = Guid.NewGuid().ToString(), Number = d });
                }

                return(new OkResult());
            }
        }
예제 #2
0
        // Recognize text from a local image
        private static async Task <String> ExtractLocalTextAsync(
            ComputerVisionClient computerVision, string imagePath)
        {
            String finalOutCome = "";
            String finalresult  = "";

            String[] imagePathArray = imagePath.Split("|");

            for (int i = 0; i < imagePathArray.Length; i++)
            {
                if (!System.IO.File.Exists(imagepath + imagePathArray[i].Replace("\n", string.Empty).Replace("\r", string.Empty)))
                {
                    return("ERROr " + imagepath + imagePathArray[i].Replace("\n", string.Empty).Replace("\r", string.Empty));
                }
                using (Stream imageStream = System.IO.File.OpenRead(imagepath + imagePathArray[i]))
                {
                    // Start the async process to recognize the text
                    RecognizeTextInStreamHeaders textHeaders =
                        await computerVision.RecognizeTextInStreamAsync(
                            imageStream, textRecognitionMode);

                    String result = await GetTextAsync(computerVision, textHeaders.OperationLocation);

                    finalOutCome = finalOutCome + "{\"id\": \"" + i + 1 + "\",\"language\": \"en\",\"text\":\"" + result + "\"},";
                }
                if (System.IO.File.Exists(imagepath + imagePathArray[i]))
                {
                    System.IO.File.Delete(imagepath + imagePathArray[i]);
                }
            }
            finalresult = "{\"documents\":[" + finalOutCome.Substring(0, finalOutCome.Length - 1) + "]}";
            // return finalresult;
            return(await GetTextAsync(finalresult));
        }
예제 #3
0
        public static async Task <IEnumerable <Line> > ExtractLocalTextAsync(string imagePath)
        {
            if (!File.Exists(imagePath))
            {
                throw new Exception();
            }

            var computerVision = new ComputerVisionClient(
                new ApiKeyServiceClientCredentials(AppSettings.MicrosoftVisionApiKey))
            {
                Endpoint = "https://westeurope.api.cognitive.microsoft.com"
            };

            RecognizeTextInStreamHeaders textHeaders = null;

            while (textHeaders == null)
            {
                try
                {
                    using (Stream imageStream = File.Open(imagePath, FileMode.OpenOrCreate))
                    {
                        textHeaders = await computerVision.RecognizeTextInStreamAsync(imageStream, TextRecognitionMode.Handwritten);
                    }
                }
                catch
                {
                    Thread.Sleep(5000);
                }
            }

            return(await GetTextAsync(computerVision, textHeaders.OperationLocation));
        }
예제 #4
0
        // Recognize text from a local image
        public static async Task <IList <Line> > ExtractLocalTextAsync(byte [] uploadedImage
                                                                       )
        {
            ComputerVisionClient computerVision = new ComputerVisionClient(
                new ApiKeyServiceClientCredentials(subscriptionKey),
                new System.Net.Http.DelegatingHandler[] { });


            // Specify the Azure region
            computerVision.Endpoint = "https://westeurope.api.cognitive.microsoft.com/";
            try
            {
                using (Stream uploadedImageStream = new MemoryStream(uploadedImage))
                {
                    // Start the async process to recognize the text
                    RecognizeTextInStreamHeaders textHeaders =
                        await computerVision.RecognizeTextInStreamAsync(
                            uploadedImageStream, textRecognitionMode);

                    return(await GetTextAsync(computerVision, textHeaders.OperationLocation));
                }
            }
            catch (Exception ex)
            {
                // logger.LogError(ex, "Problem getting text from computer vision " + ex.Message);
                return(new List <Line> {
                    new Line(new List <int> {
                        0
                    }, "Error processing message")
                });
            }
        }
예제 #5
0
        public async Task <IList <Line> > ExtractTextAsync(Stream image, TextRecognitionMode recognitionMode)
        {
            RecognizeTextInStreamHeaders headers = await _client.RecognizeTextInStreamAsync(image, recognitionMode);

            IList <Line> detectedLines = await GetTextAsync(_client, headers.OperationLocation);

            return(detectedLines);
        }
예제 #6
0
        // Recognize text from a local image
        private async Task <string[]> ExtractLocalTextAsync(
            Stream imageStream)
        {
            // Start the async process to recognize the text
            RecognizeTextInStreamHeaders textHeaders =
                await computerVision.RecognizeTextInStreamAsync(
                    imageStream, textRecognitionMode);

            return(await GetTextAsync(computerVision, textHeaders.OperationLocation));
        }
예제 #7
0
        private static async Task ExtractTextAsync(ComputerVisionClient client, string image)
        {
            // The SDK uses the 'async' POST/GET pattern. For a regular POST pattern, please see the Recognize-TextHttp sample
            using (var imageStream = File.OpenRead(image))
            {
                // Submit POST request
                var textHeaders = await client.RecognizeTextInStreamAsync(imageStream, TextRecognitionMode.Printed);

                // Submit GET requests to poll for result
                await GetTextAsync(client, textHeaders.OperationLocation);
            }
        }
예제 #8
0
 private static async Task ExtractLocalHandTextAsync(ComputerVisionClient computerVision, string imagePath)
 {
     if (!File.Exists(imagePath))
     {
         Console.WriteLine("\nInvalid image:\n{0} \n",
                           imagePath);
         return;
     }
     using (Stream imageStream = File.OpenRead(imagePath))
     {
         RecognizeTextInStreamHeaders textHeaders = await computerVision.RecognizeTextInStreamAsync(imageStream, TextRecognitionMode.Printed);
         await GetTextAsync(computerVision, textHeaders.OperationLocation);
     }
 }
예제 #9
0
        // Recognize text from a local image
        private static async Task RecognizeTextFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId, TextRecognitionMode textRecognitionMode)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
                return;
            }

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                // Start the async process to recognize the text
                RecognizeTextInStreamHeaders textHeaders = await computerVision.RecognizeTextInStreamAsync(imageStream, textRecognitionMode);
                await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
            }
        }
예제 #10
0
        // Recognize text from a local image
        private static async Task ExtractLocalTextAsync(
            ComputerVisionClient computerVision, System.Drawing.Image img)
        {
            using (Stream imageStream = new MemoryStream())
            {
                img.Save(imageStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                imageStream.Seek(0, SeekOrigin.Begin);

                // Start the async process to recognize the text
                RecognizeTextInStreamHeaders textHeaders =
                    await computerVision.RecognizeTextInStreamAsync(
                        imageStream, textRecognitionMode);

                await GetTextAsync(computerVision, textHeaders.OperationLocation);
            }
        }
예제 #11
0
        public async Task <TextOperationResult> RecognizeTextAsync(Stream imageStream, CancellationToken cancellationToken)
        {
            var credentials = new ApiKeyServiceClientCredentials(AppSettings.ComputerVisionKey);

            using (var client = new ComputerVisionClient(credentials)
            {
                Endpoint = AppSettings.ComputerVisionEndpoint
            })
            {
                RecognizeTextInStreamHeaders textHeaders =
                    await client.RecognizeTextInStreamAsync(imageStream, Mode, cancellationToken);

                // Go for the result
                var result = await GetTextAsync(client, textHeaders.OperationLocation, cancellationToken);

                return(result);
            }
        }
예제 #12
0
        public async Task <List <string> > AnalyzeImageForText(MediaFile file)
        {
            List <string> result = new List <string>();

            try {
                using (Stream imageStream = file.GetStream()) {
                    // Start the async process to recognize the text
                    RecognizeTextInStreamHeaders textHeaders = await _computerVision.RecognizeTextInStreamAsync(imageStream, textRecognitionMode);

                    result = await GetTextAsync(_computerVision, textHeaders.OperationLocation);
                }
            }
            catch (Exception ex) {
                Debug.WriteLine($"--ERROR--: {ex.Message}");
                Debugger.Break();
            }

            return(result);
        }
        // Recognize text from a local image
        public static 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
                RecognizeTextInStreamHeaders textHeaders =
                    await computerVision.RecognizeTextInStreamAsync(
                        imageStream, Variables.TextRecognitionMode);

                await GetTextAsync(computerVision, textHeaders.OperationLocation);
            }
        }
예제 #14
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] Stream image,
            ILogger log)
        {
            var mode = TextRecognitionMode.Handwritten;
            var text = string.Empty;

            try
            {
                var result = await visionClient.RecognizeTextInStreamAsync(image, mode);

                text = await GetTextAsync(result.OperationLocation);
            }
            catch (Exception ex)
            {
                log.LogError(ex.ToString());

                return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
            }

            return(new OkObjectResult(text));
        }
예제 #15
0
        public static async Task <TextOperationResult> RecognizeText(string filename, string subscriptionKey, string serviceEndpoint)
        {
            ComputerVisionClient vision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey), new DelegatingHandler[] { })
            {
                Endpoint = serviceEndpoint
            };

            TextRecognitionMode mode = TextRecognitionMode.Printed;

            using (Stream stream = File.OpenRead(filename))
            {
                try
                {
                    RecognizeTextInStreamHeaders headers = await vision.RecognizeTextInStreamAsync(stream, mode);

                    string id = headers.OperationLocation.Substring(headers.OperationLocation.LastIndexOf('/') + 1);

                    int count = 0;
                    TextOperationResult result;
                    do
                    {
                        result = await vision.GetTextOperationResultAsync(id);

                        if (result.Status == TextOperationStatusCodes.Succeeded || result.Status == TextOperationStatusCodes.Failed)
                        {
                            return(result);
                        }
                        await Task.Delay(DelayBetweenRetriesInMilliseconds);
                    }while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && count++ < MaximumNumberOfRetries);
                }
                catch
                {
                    // TODO: handle exception here
                }
            }
            return(null);
        }
예제 #16
0
        // Recognize the text in a Stream with OneOCR
        private static async Task <TextOperationResult> RecognizeTextInStreamAsync(Stream stream, TraceWriter log, string logHeader)
        {
            var subscriptionKey = ConfigurationManager.AppSettings["OneOCRSubscriptionKey"];

            using (var computerVision = new ComputerVisionClient(
                       new ApiKeyServiceClientCredentials(subscriptionKey),
                       new System.Net.Http.DelegatingHandler[] { })
            {
                // E.g. Docker Container running in localhost: http://localhost:5000, Service running in West US region: https://westus.api.cognitive.microsoft.com
                Endpoint = ConfigurationManager.AppSettings["OneOCREndpoint"]
            })
            {
                log.Info($"{logHeader} Starting text recognition with OneOCR...");

                // Start the async process to recognize the text
                var textHeaders = await computerVision.RecognizeTextInStreamAsync(stream, TextRecognitionMode.Printed);

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

                // Retrieve the recognized text
                return(await GetTextOperationResultAsync(computerVision, operationId, log, logHeader));
            }
        }
예제 #17
0
        public async Task <LabelImageAdded> ObterIngredientes(string pathFile)
        {
            _logs.AppendLine("Iniciando método de ObterIngredientes");

            try
            {
                List <string> ingredientes = new List <string>();
                StringBuilder concat       = new StringBuilder();
                bool          entrar       = false;

                _logs.AppendLine($"Lendo o arquivo: {pathFile} ");
                using (var imgStream = new FileStream(pathFile, FileMode.Open))
                {
                    RecognizeTextInStreamHeaders results = await _client.RecognizeTextInStreamAsync(imgStream, TextRecognitionMode.Printed);

                    Thread.Sleep(2000);
                    string idImagem = results.OperationLocation.Split('/').Last();

                    var resultText = await _client.GetTextOperationResultAsync(idImagem);

                    var lines = resultText.RecognitionResult.Lines;

                    _logs.AppendLine($"Número de linhas encontradas: {lines.Count} ");

                    if (lines.Count > 0)
                    {
                        foreach (Line line in lines)
                        {
                            if (line.Text.IndexOf("INGREDIENTE") >= 0 || entrar)
                            {
                                entrar = true;
                                concat.Append(line.Text);
                            }
                        }

                        if (concat.ToString().Length > 0)
                        {
                            var resultado = Regex.Replace(concat.ToString(), "[^A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ, -]", "");
                            resultado = resultado.Replace("INGREDIENTES", "");
                            resultado = resultado.Replace("INGREDIENTE", "");

                            _logs.AppendLine($"Retorno dos ingredientes: {resultado}");

                            LabelImageAdded labelImageAdded = new LabelImageAdded();
                            labelImageAdded.ItemName    = pathFile;
                            labelImageAdded.Ingredients = resultado.Split(',');

                            _logs.AppendLine($"LabelImageAdded serializado: {JsonConvert.SerializeObject(labelImageAdded)}");
                            _logger.LogInformation(_logs.ToString());
                            return(labelImageAdded);
                        }
                        else
                        {
                            _logs.AppendLine("Não foi encontrado ingredientes");
                        }
                    }
                    else
                    {
                        _logs.AppendLine("Não foi encontrado ingredientes");
                    }
                }
            }
            catch (Exception ex)
            {
                _logs.AppendLine($"Ocorreu um erro: {ex.Message}");
                _logger.LogError(ex, _logs.ToString());
            }
            finally
            {
                _logger.LogInformation(_logs.ToString());

                Log _log = new Log();
                _log.DataHora = DateTime.Now;
                _log.Mensagem = _logs.ToString();

                LogService _logService = new LogService();
                await _logService.MainAsync(_log);
            }
            return(null);
        }