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)); }
// 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)); }
private async Task <TextOperationResult> GetRecognizingOperationResult(RecognizeTextInStreamHeaders headers) { string operationId = headers.OperationLocation.Substring(headers.OperationLocation.Length - 36); int i = 0; do { var result = await _client.GetTextOperationResultAsync(operationId); if (result.Status == TextOperationStatusCodes.Failed) { throw new ComputerVisionErrorException("Text recognition operation failed."); } if (result.Status == TextOperationStatusCodes.Succeeded) { return(result); } i++; await Task.Delay(1000); } while (i < 10); throw new TimeoutException("Cognitive Services did not respond properly in estimated time."); }
// 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") }); } }
public void RecognizeTextInStreamTest() { using (MockContext context = MockContext.Start(this.GetType().FullName)) { HttpMockServer.Initialize(this.GetType().FullName, "RecognizeTextInStreamTest"); using (IComputerVisionClient client = GetComputerVisionClient(HttpMockServer.CreateInstance())) using (FileStream stream = new FileStream(GetTestImagePath("whiteboard.jpg"), FileMode.Open)) { RecognizeTextInStreamHeaders headers = client.RecognizeTextInStreamAsync(stream, TextRecognitionMode.Handwritten).Result; Assert.NotNull(headers.OperationLocation); TextRecognitionResult recognitionResult = GetRecognitionResultWithPolling(client, headers.OperationLocation); Assert.NotNull(recognitionResult); Assert.Equal( new string[] { "You must be the change", "you want to see in the world!!" }, recognitionResult.Lines.Select(line => line.Text)); Assert.Equal(2, recognitionResult.Lines.Count); Assert.Equal(5, recognitionResult.Lines[0].Words.Count); Assert.Equal(7, recognitionResult.Lines[1].Words.Count); } } }
public static async Task <RecognitionResult> MakeTextRequest(MediaFile file) { using (var stream = file.GetStream()) { var client = CreateClient(); RecognizeTextInStreamHeaders headers = await client.RecognizeTextInStreamAsync(stream, TextRecognitionMode.Handwritten); if (headers?.OperationLocation == null) { return(null); } // Extract the operation id from the url string operationId = headers.OperationLocation.Substring(headers.OperationLocation.Length - 36); TextOperationResult result = await client.GetTextOperationResultAsync(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.GetTextOperationResultAsync(operationId); } return(result.RecognitionResult); } }
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); }
// 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)); }
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); } }
// 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); } }
// 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); } }
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); } }
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); } }
private async Task <List <string> > GetData(Stream streamImage) { RecognizeTextInStreamHeaders headers = await _cvs.RecognizeTextInStreamAsync(streamImage, TextRecognitionMode.Printed); string operationId = headers.OperationLocation.Substring( headers.OperationLocation.Length - numberOfCharsInOperationId); TextOperationResult result = await _cvs.GetTextOperationResultAsync(operationId); int i = 0; int maxRetries = 10; while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries) { await Task.Delay(1000); result = await _cvs.GetTextOperationResultAsync(operationId); } return(result.RecognitionResult.Lines.Select(l => l.Text).ToList()); }
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); }
public static async Task <TextOperationResult> RecognizeTextAsync(Func <Task <Stream> > imageStreamCallback, TextRecognitionMode textRecognitionMode, bool detectOrientation = true) { RecognizeTextInStreamHeaders textHeaders = await client.RecognizeTextInStreamAsync(await imageStreamCallback(), textRecognitionMode); return(await GetTextRecognitionResultAsync(client, textHeaders.OperationLocation)); }
// This method is adapted from the ExtractLocalTextAsync() and // GetTextAsync() methods from the following tutorial: // https://docs.microsoft.com/en-us/azure/cognitive-services/Computer-vision/quickstarts-sdk/csharp-hand-text-sdk private async Task GetTextAsync() { // If computerVision is not initialized, display an error message // prompting the user to enter the relevant credentials (i.e. API key // and/or Endpoint). if (computerVision == null) { lblError.Content = "Computer Vision API Client not initialized. Please enter your API key and/or Endpoint if you haven't already done so."; lblError.Visibility = Visibility.Visible; return; } // If the user has not uploaded an image, display an error message // prompting the user to do so. if (String.IsNullOrEmpty(filePath)) { lblError.Content = "Please upload an image."; lblError.Visibility = Visibility.Visible; return; } lblError.Visibility = Visibility.Hidden; imageDescriptionStatusBar.Text = "Extracting text..."; using (Stream imageStream = File.OpenRead(filePath)) { // Start the async process to recognize the text RecognizeTextInStreamHeaders textHeaders = await computerVision.RecognizeTextInStreamAsync( imageStream, textRecognitionMode); string operationLocation = textHeaders.OperationLocation; // Retrieve the URI where the recognized text will be // stored from the Operation-Location header string operationId = operationLocation.Substring( operationLocation.Length - numberOfCharsInOperationId); TextOperationResult result = await computerVision.GetTextOperationResultAsync(operationId); // Wait for the operation to complete. // // This ensures that the operation fully completes and returns // the necessary information before the program can proceed. // // While the Text Extraction operation has not fully completed, // the program will wait until: // // 1. The operation has fully completed, or // // 2. The timeout threshold has been reached (i.e. the counter // variable i reaches the value of maxRetries). int i = 0; int maxRetries = 10; while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) /*&& i++ < maxRetries*/) { await Task.Delay(1000); result = await computerVision.GetTextOperationResultAsync(operationId); } // If operation timeout has occurred, display an error message. if (i == maxRetries) { imageDescriptionStatusBar.Text = ""; lblError.Content = "API timeout. Unable to extract text."; lblError.Visibility = Visibility.Visible; return; } // Display the results var lines = result.RecognitionResult.Lines; imageDescriptionStatusBar.Text = "Text:\n"; if (lines.Count == 0) { imageDescriptionStatusBar.Text += "None"; } else { foreach (Line line in lines) { imageDescriptionStatusBar.Text += line.Text + "\n"; } } DrawTextRectangles(lines); } }
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); }