0

Not able to pass the image which is converted to bytes to request. Due to which i am unable to use detectdocumenttextresult and detectdocumenttextresponse

This is the java code i have tried to convert in c#

string document = "input.png";

ByteBuffer imageBytes;
using (Stream inputStream = new FileStream(document, FileMode.Open, FileAccess.Read)) {
    imageBytes = ByteBuffer.wrap(IOUtils.toByteArray(inputStream));
}
AmazonTextract client = AmazonTextractClientBuilder.defaultClient();

DetectDocumentTextRequest request = (new DetectDocumentTextRequest()).withDocument(new Document()
                    .withBytes(imageBytes));

DetectDocumentTextResult result = client.detectDocumentText(request);

/* this is the c# code i am not able to pass the data to request*/

AmazonTextractClient Atc = new AmazonTextractClient(credentials, config);
Image img = Image.FromFile("D:\\Images\\1.Jpeg");
byte[] ImageBytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));
DetectDocumentTextRequest request = new DetectDocumentTextRequest();
request.Document.Bytes.Read(ImageBytes, 0 , ImageBytes.Length);
DetectDocumentTextResponse res = Atc.DetectDocumentText(request);
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470

1 Answers1

0

Even though the property says bytes, it wants a raw memory stream. Photo is the file location of your image. Client is your AmazonTextractClient client however you want to instantiate it.

var client = new AmazonTextractClient("[KEY ID]", "[ACCESS KEY]", Amazon.RegionEndpoint.USEast1); 

Document MyDocument;
using (Image image = Image.FromFile(photo))
{
    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        MyDocument = new Document()
        {
            Bytes = m
        };
    }
}

Then for DetectDocumentTextRequest()

var request = new DetectDocumentTextRequest()
{
    Document = MyDocument
};

var response = client.DetectDocumentText(request);

AnalyzeDocumentRequest() also works

var DocRequest = new AnalyzeDocumentRequest()
{
    Document = MyDocument,
    FeatureTypes = new List<string> { FeatureType.FORMS, FeatureType.TABLES }
};

var response =  client.AnalyzeDocument(DocRequest);
JAZ
  • 1,050
  • 6
  • 15
  • Thanks:-) can you please tell me how to parse the data from key value pair and table from the analyse document response? – rahul shetty May 23 '19 at 04:44
  • You just need to loop through and parse the Blocks property of the response. For each Block there is a BlockType and you can test for Key/Values/Entity Type or a Table or Cells of the table. – JAZ May 23 '19 at 18:05