Skip to content

Commit

Permalink
Added Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dinurp committed May 27, 2020
1 parent fa246ca commit e3e5c27
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
19 changes: 19 additions & 0 deletions logger-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: 1
formatters:
simple:
# format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
format: '%(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
tesseract-in-cf:
level: DEBUG
handlers: [console]
propagate: no
root:
level: DEBUG
handlers: [console]
1 change: 1 addition & 0 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ applications:
command: python server.py
env:
TESSDATA_PREFIX: ../deps/0/apt/usr/share/tesseract-ocr/4.00/tessdata
FLASK_ENV: development
buildpacks:
- https://github.com/cloudfoundry/apt-buildpack
- https://github.com/cloudfoundry/python-buildpack
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pytesseract==0.3.0
Flask==1.0.2
PyYAML==5.1
16 changes: 13 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import json

from flask import Flask, request, make_response, send_file, Response
from flask import json
from time import time

import pytesseract

import logging
logger = logging.getLogger('tesseract-in-cf')
app = Flask(__name__)

#health check
Expand All @@ -23,6 +24,7 @@ def ocr_test():
file = request.args.get('image','sample/fox.png')
lang = request.args.get('lang', 'eng')
config = request.args.get('config', '--psm 11')
logger.info("pytesseract.image_to_string({},lang={},config={})".format(file,lang,config))
text = pytesseract.image_to_string(file,lang=lang,config=config)
response = make_response(text)
response.headers['Content-Type'] = 'text/plain'
Expand All @@ -43,10 +45,11 @@ def ocr_files(folder=None):
uploaded_files = request.files.getlist('image')
saved_files = []
for file in uploaded_files:
print("Posted file: {}".format(file))
logger.info("Posted file: {}".format(file))
if file.filename.split('.')[-1] in ['jpeg','png','jpg','pdf','gif','jfif']:
saved_file = getSavedFilepath(folder,file.filename)
file.save( saved_file )
logger.info("pytesseract.image_to_string({},lang={},config={})".format(saved_file,lang,config))
text = pytesseract.image_to_string(saved_file,lang=lang,config=config)
saved_files.append({"file":file.filename,"saved_as":saved_file, "text":text})
response = make_response(json.dumps(saved_files))
Expand All @@ -55,6 +58,13 @@ def ocr_files(folder=None):


if __name__ == '__main__':
import logging.config
import yaml

with open('logger-config.yaml', 'r') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)

port = int(os.environ.get('PORT', 3000))
host = os.environ.get('VCAP_APP_HOST', '0.0.0.0')
app.run(host=host, port=port, debug=True)
app.run(host=host, port=port)

0 comments on commit e3e5c27

Please sign in to comment.