Skip to content

Instantly share code, notes, and snippets.

@SumindaD
Last active June 27, 2019 10:23
Show Gist options
  • Save SumindaD/462f4d74721b2335866dc88d7df461af to your computer and use it in GitHub Desktop.
Save SumindaD/462f4d74721b2335866dc88d7df461af to your computer and use it in GitHub Desktop.
import json
import boto3
import base64
def lambda_handler(event, context):
eventBody = json.loads(json.dumps(event))['body']
imageBase64 = json.loads(eventBody)['Image']
# Amazon Textract client
textract = boto3.client('textract')
# Call Amazon Textract
response = textract.detect_document_text(
Document={
'Bytes': base64.b64decode(imageBase64)
})
detectedText = ''
# Print detected text
for item in response['Blocks']:
if item['BlockType'] == 'LINE':
detectedText += item['Text'] + '\n'
return {
'statusCode': 200,
'body': json.dumps(detectedText)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment