forked from typless/tesseract-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
31 lines (22 loc) · 638 Bytes
/
Copy pathhandler.py
File metadata and controls
31 lines (22 loc) · 638 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import base64
import io
import json
import os
import pytesseract
from PIL import Image
if os.getenv('AWS_EXECUTION_ENV') is not None:
os.environ['LD_LIBRARY_PATH'] = '/opt/lib'
os.environ['TESSDATA_PREFIX'] = '/opt/tessdata'
pytesseract.pytesseract.tesseract_cmd = '/opt/tesseract'
def ocr(event, context):
request_body = json.loads(event['body'])
image = io.BytesIO(base64.b64decode(request_body['image']))
text = pytesseract.image_to_string(Image.open(image))
body = {
"text": text
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response