Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added NodeJS samples #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions nodejs/01-detect-text-local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*Example showing processing a document on local machine.*/

const aws = require('aws-sdk');
/*Initializing Textract from AWS SDK JS*/
const textract = new aws.Textract({ apiVersion: "2018-06-27" });
/*File system package for importing images and converting the image data to ByteArray */
const fs = require('fs');

exports.handler = async(event, context) => {
let textractParams = {
Document: {
Bytes: fs.readFileSync("simple-document-image.jpg")
},
};
try {
let response = await textract.detectDocumentText(params).promise();
console.log(JSON.stringify(response),null,2)
}catch(e){
console.log("Error: ",e)
}
}
24 changes: 24 additions & 0 deletions nodejs/02-detect-text-s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*Example showing processing a document on AWS S3 bucket.*/

const aws = require('aws-sdk');
/*Initializing Textract from AWS SDK JS*/
const textract = new aws.Textract({ apiVersion: "2018-06-27" });
const s3BucketName = "ki-textract-demo-docs"
const documentName = "simple-document-image.jpg"

exports.handler = async (event, context) => {
let textractParams = {
Document: {
S3Object: {
Bucket: s3BucketName,
Name: documentName,
}
},
};
try {
let response = await textract.detectDocumentText(params).promise();
console.log(JSON.stringify(response),null,2)
} catch (e) {
console.log("Error: ", e)
}
}
21 changes: 21 additions & 0 deletions nodejs/03-analyze-expense-local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*Example showing analyzing expense with document on local machine.*/

const aws = require('aws-sdk');
/*Initializing Textract from AWS SDK JS*/
const textract = new aws.Textract({ apiVersion: "2018-06-27" });
/*File system package for importing images and converting the image data to ByteArray */
const fs = require('fs');

exports.handler = async(event, context) => {
let textractParams = {
Document: {
Bytes: fs.readFileSync("receipt.jfif")
},
};
try {
let response = await textract.analyzeExpense(params).promise();
console.log(JSON.stringify(response),null,2)
}catch(e){
console.log("Error: ",e)
}
}
24 changes: 24 additions & 0 deletions nodejs/04-analyze-expense-s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*Example showing analyzing expense with document on AWS S3 bucket.*/

const aws = require('aws-sdk');
/*Initializing Textract from AWS SDK JS*/
const textract = new aws.Textract({ apiVersion: "2018-06-27" });
const s3BucketName = "ki-textract-demo-docs"
const documentName = "receipt.jfif"

exports.handler = async (event, context) => {
let textractParams = {
Document: {
S3Object: {
Bucket: s3BucketName,
Name: documentName,
}
},
};
try {
let response = await textract.analyzeExpense(params).promise();
console.log(JSON.stringify(response),null,2)
} catch (e) {
console.log("Error: ", e)
}
}
6 changes: 6 additions & 0 deletions nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Textract NodeJS Examples

| Argument | Description |
| ----------------------------------------------------------- | ---------------------------------------------------------- |
| [01-detect-text-local.js](./01-detect-text-local.js) | Example showing processing a document on local machine. |
| [02-detect-text-s3.js](./02-detect-text-s3.js) | Example showing processing a document in Amazon S3 bucket. |
Binary file added nodejs/receipt.jfif
Binary file not shown.
Binary file added nodejs/simple-document-image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.