public async Task <IActionResult> LabelLoaderIngredients(AddLabelImage addLabelImage) { try { _logger.LogInformation($"Iniciando extração de ingredientes item {addLabelImage.ItemName}," + $" imagem {addLabelImage.ItemName }"); if (!ValidFile(addLabelImage.File)) { return(NotFound(":(")); } LabelImageAdded labelImageAdded = new LabelImageAdded() { ItemName = addLabelImage.ItemName, Ingredients = await _extractIngredientsService.GetIngredients(addLabelImage.File) }; _sendIngredientsService.SendIngredients(labelImageAdded); _logger.LogInformation("Extração finalizada"); return(Ok()); } catch (Exception ex) { _logger.LogError($"Falha no processo de extração de ingredientes {ex.ToString()}"); return(NotFound()); } }
private async Task MainAsync(LabelImageAdded imagemString) { try { managementClient = new ManagementClient(Configuration.GetSection("AzureServiceBusConfig")["connectionString"]); await managementClient.GetTopicAsync(TopicName); } catch (MessagingEntityNotFoundException) { await managementClient.CreateTopicAsync(new TopicDescription(TopicName) { EnablePartitioning = true }); } topicClient = new TopicClient(Configuration.GetSection("AzureServiceBusConfig")["connectionString"], TopicName); await SendMessagesAsync(imagemString); await topicClient.CloseAsync(); var notRead = $"{Environment.CurrentDirectory}{Configuration.GetSection("Files")["NotRead"]}"; var read = $"{Environment.CurrentDirectory}{Configuration.GetSection("Files")["Read"]}"; if (!Directory.Exists(notRead)) { Directory.CreateDirectory(notRead); } if (!Directory.Exists(read)) { Directory.CreateDirectory(read); } Directory.Move(notRead, read); }
private async Task SendMessagesAsync(LabelImageAdded imagemString) { try { { var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(imagemString))); await topicClient.SendAsync(message); } } catch (Exception exception) { _logs.AppendLine($"{DateTime.Now} :: Exception: {exception.Message}"); } 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); } }
public void SendIngredients(LabelImageAdded labelImageAdded) { try { string messageBody = JsonConvert.SerializeObject(labelImageAdded); _logger.LogInformation("Imagem serializada"); SendMessagesAsync(messageBody).Wait(); } catch (Exception ex) { _logger.LogError($"Falha ao enviar ingredientes - {ex.Message}"); throw new Exception("Falha ao enviar ingredientes!"); } }
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); }