public async Task <ReadOperationResult> ParseFileInternal(Stream file) { var response = await _client.ReadInStreamAsync(file); const int NumberOfCharsInOperationId = 36; string operationId = response.OperationLocation.Substring(response.OperationLocation.Length - NumberOfCharsInOperationId); ReadOperationResult result; do { result = await _client.GetReadResultAsync(Guid.Parse(operationId)); }while (result.Status == OperationStatusCodes.Running || result.Status == OperationStatusCodes.NotStarted); return(result); }
public async Task ReadFileLocal(ComputerVisionClient client, string imageFile) { Console.WriteLine("-----------------------------------------------"); Console.WriteLine("Read Image from file"); Console.WriteLine(); var textHeaders = await client.ReadInStreamAsync(File.OpenRead(imageFile), language : "en"); string operationLocation = textHeaders.OperationLocation; Thread.Sleep(2000); const int numberOfCharsInOperationId = 36; string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId); ReadOperationResult results; Console.WriteLine($"Reading text from local file {Path.GetFileName(imageFile)}..."); Console.WriteLine(); do { results = await client.GetReadResultAsync(Guid.Parse(operationId)); } while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); SaveOutputToFile(results.AnalyzeResult.ReadResults, imageFile); }
static async System.Threading.Tasks.Task Main(string[] args) { var cv = new ComputerVisionClient(new ApiKeyServiceClientCredentials("")) { Endpoint = "" }; using (var image = System.IO.File.OpenRead(@"D:\Data\inf103ss\Documents\voorbeeldkaart.jpg")) { var textHeaders = await cv.ReadInStreamAsync(image); string operationLocation = textHeaders.OperationLocation; string operationId = operationLocation.Substring(operationLocation.Length - 36); // Extract the text ReadOperationResult results; do { results = await cv.GetReadResultAsync(Guid.Parse(operationId)); }while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); Console.WriteLine(); var textUrlFileResults = results.AnalyzeResult.ReadResults; foreach (ReadResult page in textUrlFileResults) { foreach (Line line in page.Lines) { Console.WriteLine(line.Text); } } Console.WriteLine(); } }
// Receives the image path and returns the businesscard public override async Task <Businesscard> getCard(string imagePath) { try { using (var image = File.OpenRead(imagePath)) { Businesscard card = MakeEmptyCard(); Debug.WriteLine("AZUREVISION - Received image"); var textHeaders = await cv.ReadInStreamAsync(image); string operationLocation = textHeaders.OperationLocation; string operationId = operationLocation.Substring(operationLocation.Length - 36); // Extract the text ReadOperationResult results = null; Debug.WriteLine("AZUREVISION - sending image to Azure Service"); do { results = await cv.GetReadResultAsync(Guid.Parse(operationId)); }while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); Debug.WriteLine("AZUREVISION - received information from Azure Service"); card = await analyzeText(formatResults(results)); PrintCard(card); return(card); } } catch (Exception ex) { throw new BadRequestException(ex.ToString()); } }
public List <string> ExtractOCRTextFromLocalFile(string localFile, string language = "en") { var textHeaders = _client.ReadInStreamAsync(File.OpenRead(localFile), language: language).Result; // After the request, get the operation location (operation ID) string operationLocation = textHeaders.OperationLocation; Thread.Sleep(2000); // 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 ReadOperationResult results; do { results = _client.GetReadResultAsync(Guid.Parse(operationId)).Result; }while (results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted); List <string> foundTextSnippets = new List <string>(); var textUrlFileResults = results.AnalyzeResult.ReadResults; foreach (ReadResult page in textUrlFileResults) { foreach (Line line in page.Lines) { foundTextSnippets.Add(line.Text); } } return(foundTextSnippets); }
public override async Task RunCommand(object sender) { var engine = (IAutomationEngineInstance)sender; var vAPIKey = (string)await v_APIKey.EvaluateCode(engine); var vEndpoint = (string)await v_Endpoint.EvaluateCode(engine); Stream imageStream = null; if (v_ImageType == "Filepath") { imageStream = new FileStream((string)await v_FilePath.EvaluateCode(engine), FileMode.Open); } else { Bitmap vBitmap = (Bitmap)await v_Bitmap.EvaluateCode(engine); imageStream = new FileStream(engine.EngineContext.ProjectPath + "\\tempOCRBitmap.bmp", FileMode.OpenOrCreate); vBitmap.Save(imageStream, ImageFormat.Bmp); imageStream.Close(); imageStream = new FileStream(engine.EngineContext.ProjectPath + "\\tempOCRBitmap.bmp", FileMode.Open); } ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(vAPIKey)) { Endpoint = vEndpoint }; var textHeaders = await client.ReadInStreamAsync(imageStream); string operationLocation = textHeaders.OperationLocation; Thread.Sleep(2000); const int numberOfCharsInOperationId = 36; string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId); ReadOperationResult results; do { results = await client.GetReadResultAsync(Guid.Parse(operationId)); }while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); string readText = ""; var textUrlFileResults = results.AnalyzeResult.ReadResults; foreach (ReadResult page in textUrlFileResults) { foreach (Line line in page.Lines) { readText += line.Text + "\n"; } } readText.SetVariableValue(engine, v_OutputUserVariableName); }
/// <summary> /// Sends an image to the Azure Cognitive Vision and returns the rendered text. /// More information available at https://westcentralus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-1-ga/operations/5d986960601faab4bf452005 /// </summary> /// <param name="filename"></param> /// <returns></returns> public async Task <List <DocumentText> > ReadImage(Stream stream) { try { var streamHeaders = await ComputerVisionClient.ReadInStreamAsync(stream, "en"); var operationLocation = streamHeaders.OperationLocation; const int numberOfCharsInOperationId = 36; string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId); await Task.Delay(1000); ReadOperationResult readOperationResult; do { readOperationResult = await ComputerVisionClient.GetReadResultAsync(Guid.Parse(operationId)); } while (readOperationResult.Status == OperationStatusCodes.Running || readOperationResult.Status == OperationStatusCodes.NotStarted); var listOfDocumentText = new List <DocumentText>(); var arrayOfReadResults = readOperationResult.AnalyzeResult.ReadResults; foreach (var page in arrayOfReadResults) { foreach (var line in page.Lines) { var boundBox = new BoundingBox() { Left = line.BoundingBox[0], Top = line.BoundingBox[1], Right = line.BoundingBox[4], Bottom = line.BoundingBox[5] }; var documentText = new DocumentText() { BoundingBox = boundBox, Text = line.Text }; listOfDocumentText.Add(documentText); } } return(listOfDocumentText); } catch (Exception e) { Logger.LogError(e, $"failed to analyze file"); return(null); } }
private async Task <IList <Line> > ExtractLinesFromImage(string filePath, string writeToJsonPath = null) { IList <ReadResult> textResults; if (filePath.EndsWith("json")) { using (StreamReader r = new StreamReader(filePath)) { string json = r.ReadToEnd(); textResults = JsonConvert.DeserializeObject <IList <ReadResult> >(json); } } else { ReadInStreamHeaders textHeaders; try { textHeaders = await _client.ReadInStreamAsync(File.OpenRead(filePath), language : "en"); } catch (Exception ex) { Log.Error(ex, "Error using Azure's ReadInStreamAsync method"); return(null); } string operationLocation = textHeaders.OperationLocation; //Thread.Sleep(2000); // This is in the github examples // 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); ReadOperationResult results; do { results = await _client.GetReadResultAsync(Guid.Parse(operationId)); }while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); textResults = results.AnalyzeResult.ReadResults; if (writeToJsonPath != null) { string json = JsonConvert.SerializeObject(textResults, Formatting.Indented); System.IO.File.WriteAllText(writeToJsonPath, json); } } return(textResults[0].Lines); }
// </snippet_readfileurl_3> /* * END - READ FILE - URL */ // <snippet_read_local> /* * READ FILE - LOCAL */ public static async Task ReadFileLocal(ComputerVisionClient client, string localFile) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("READ FILE FROM LOCAL"); Console.WriteLine(); // Read text from URL // language code = null, pages = null, and specify model-version. var textHeaders = await client.ReadInStreamAsync(File.OpenRead(localFile), null, null, "2022-01-30-preview"); // After the request, get the operation location (operation ID) string operationLocation = textHeaders.OperationLocation; Thread.Sleep(2000); // <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 ReadOperationResult results; Console.WriteLine($"Reading text from local file {Path.GetFileName(localFile)}..."); Console.WriteLine(); do { results = await client.GetReadResultAsync(Guid.Parse(operationId)); }while ((results.Status == OperationStatusCodes.Running || results.Status == OperationStatusCodes.NotStarted)); // </snippet_extract_response> // <snippet_extract_display> // Display the found text. Console.WriteLine(); var textUrlFileResults = results.AnalyzeResult.ReadResults; foreach (ReadResult page in textUrlFileResults) { foreach (Line line in page.Lines) { Console.WriteLine(line.Text); } } Console.WriteLine(); }
private async static void getTexto(ComputerVisionClient cliente, string url) { var streamImg = new FileStream(url, FileMode.Open, FileAccess.Read, FileShare.Read); var text = await cliente.ReadInStreamAsync(streamImg, language : "es"); var operacion = text.OperationLocation; Thread.Sleep(2000); const int numero = 36; Console.WriteLine("Operation Location"); Console.WriteLine(operacion.Substring(operacion.Length - numero)); var operationId = operacion.Substring(operacion.Length - numero); //extraer el texto ReadOperationResult results; Console.WriteLine("Extrayendo de url: "); Console.WriteLine(); do { results = await cliente.GetReadResultAsync(Guid.Parse(operationId)); } while ((results.Status == OperationStatusCodes.Running) || (results.Status == OperationStatusCodes.NotStarted)); //Deplegando texto encontrado en imagen Console.WriteLine(); var textUrl = results.AnalyzeResult.ReadResults; foreach (var res in textUrl) { foreach (var linea in res.Lines) { Console.WriteLine(linea.Text); } } Console.WriteLine(); }
public static async Task <ReadOperationResult> ReadFileLocal(ComputerVisionClient client, string localFile) { Console.WriteLine("----------------------------------------------------------"); Console.WriteLine("READ FILE FROM LOCAL"); Console.WriteLine(); // Read text from URL var textHeaders = await client.ReadInStreamAsync(File.OpenRead(localFile), language : "en"); // After the request, get the operation location (operation ID) string operationLocation = textHeaders.OperationLocation; Thread.Sleep(2000); // </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); return(await GetReadOperationResult(client, operationId)); }
private async Task <ReadOperationResult> ComputerVisionReadByStreamAsync(Stream imageStream, ReadLanguage readLanguage) { var readRequestHeaders = await _computerVisionClient.ReadInStreamAsync(imageStream, language : readLanguage.ToString()); return(await GetReadOperationResultAsync(readRequestHeaders.OperationLocation)); }
public async Task <List <DocumentText> > ReadImage(string filename) { try { using (var fileStream = File.OpenRead(filename)) { var streamHeaders = await ComputerVisionClient.ReadInStreamAsync(fileStream, "en"); var operationLocation = streamHeaders.OperationLocation; const int numberOfCharsInOperationId = 36; string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId); await Task.Delay(1500); ReadOperationResult readOperationResult; do { readOperationResult = await ComputerVisionClient.GetReadResultAsync(Guid.Parse(operationId)); } while (readOperationResult.Status == OperationStatusCodes.Running || readOperationResult.Status == OperationStatusCodes.NotStarted); var listOfDocumentText = new List <DocumentText>(); var arrayOfReadResults = readOperationResult.AnalyzeResult.ReadResults; foreach (var page in arrayOfReadResults) { foreach (var line in page.Lines) { var boundBox = new BoundingBox() { x1 = line.BoundingBox[0], y1 = line.BoundingBox[1], x2 = line.BoundingBox[2], y2 = line.BoundingBox[3] }; var documentText = new DocumentText() { BoundingBox = boundBox, Text = line.Text }; listOfDocumentText.Add(documentText); } } return(listOfDocumentText); } } catch (FileNotFoundException e) { Logger.LogInformation($"file not found: {filename}"); throw e; } catch (Exception e) { Logger.LogError(e, $"failed to analyze file: {filename}"); return(null); } }