public List <string> GetLines(DetectDocumentTextResponse result) { var lines = new List <string>(); result.Blocks.FindAll(block => block.BlockType == "LINE").ForEach(block => lines.Add(block.Text)); return(lines); }
public void Print(DetectDocumentTextResponse response) { if (response != null) { this.Print(response.Blocks); } }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { DetectDocumentTextResponse response = new DetectDocumentTextResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("Blocks", targetDepth)) { var unmarshaller = new ListUnmarshaller <Block, BlockUnmarshaller>(BlockUnmarshaller.Instance); response.Blocks = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("DocumentMetadata", targetDepth)) { var unmarshaller = DocumentMetadataUnmarshaller.Instance; response.DocumentMetadata = unmarshaller.Unmarshall(context); continue; } } return(response); }
public async static void GetTextFromS3(IAmazonTextract textractClient, string bucketName, string keyName) { List <Block> result = null; DetectDocumentTextRequest detectTextRequest = new DetectDocumentTextRequest() { Document = new Document { S3Object = new S3Object { Bucket = bucketName, Name = keyName } } }; try { Task <DetectDocumentTextResponse> detectTask = textractClient.DetectDocumentTextAsync(detectTextRequest); DetectDocumentTextResponse detectTextResponse = await detectTask; result = detectTextResponse.Blocks; //PrintBlockDetails(result); } catch (AmazonTextractException textractException) { Console.WriteLine(textractException.Message, textractException.InnerException); } }
public async Task <List <Block> > GetTextFromStream(IAmazonTextract textractClient, MemoryStream stream) { List <Block> result = null; DetectDocumentTextRequest detectTextRequest = new DetectDocumentTextRequest() { Document = new Document { Bytes = stream } }; try { Task <DetectDocumentTextResponse> detectTask = textractClient.DetectDocumentTextAsync(detectTextRequest); DetectDocumentTextResponse detectTextResponse = await detectTask; result = detectTextResponse.Blocks; //PrintBlockDetails(result); } catch (AmazonTextractException textractException) { Console.WriteLine(textractException.Message, textractException.InnerException); } return(result); }
public async Task <DetectDocumentTextResponse> DetectTextS3(string bucketName, string key) { var result = new DetectDocumentTextResponse(); var s3Object = new S3Object { Bucket = bucketName, Name = key }; var request = new DetectDocumentTextRequest(); request.Document = new Document { S3Object = s3Object }; return(await this.textract.DetectDocumentTextAsync(request)); }
public async Task <DetectDocumentTextResponse> DetectTextLocal(string localPath) { var result = new DetectDocumentTextResponse(); if (File.Exists(localPath)) { var request = new DetectDocumentTextRequest(); request.Document = new Document { Bytes = new MemoryStream(File.ReadAllBytes(localPath)) }; return(await this.textract.DetectDocumentTextAsync(request)); } Console.WriteLine("File: " + localPath + " doesn't exist"); return(result); }
public override async Task RunCommand(object sender) { var engine = (IAutomationEngineInstance)sender; var vAccessKey = (string)await v_AccessKey.EvaluateCode(engine); var vSecretKey = (string)await v_SecretKey.EvaluateCode(engine); var ocrRequest = new DetectDocumentTextRequest(); MemoryStream memStream = new MemoryStream(); if (v_ImageType == "Filepath") { string vFilePath = (string)await v_FilePath.EvaluateCode(engine); FileStream image = File.OpenRead(vFilePath); image.CopyTo(memStream); } else { Bitmap vBitmap = (Bitmap)await v_Bitmap.EvaluateCode(engine); vBitmap.Save(memStream, ImageFormat.Jpeg); } ocrRequest.Document = new Document(); ocrRequest.Document.Bytes = memStream; AmazonTextractConfig config = new AmazonTextractConfig(); config.RegionEndpoint = Amazon.RegionEndpoint.GetBySystemName(v_AWSRegion); AmazonTextractClient client = new AmazonTextractClient(vAccessKey, vSecretKey, config); DetectDocumentTextResponse results = client.DetectDocumentText(ocrRequest); string readText = ""; if (results.HttpStatusCode == System.Net.HttpStatusCode.OK) { foreach (Block block in results.Blocks) { if (block.BlockType.Value == "LINE") { readText += block.Text + "\n"; } } } else { throw new Exception("Call to AWS textract failed"); } readText.SetVariableValue(engine, v_OutputUserVariableName); }