OCR using Pytesseract + Flask Server and base64 Image String in Python

Sidhartha Mallick
1 min readNov 11, 2020
  1. Create a basic Flask Server .
  2. Create a virtual environment using env.
  3. Install dependencies like opencv-python and Pytesseract using pip/pip3.
  4. Add the OCR function to your code.
  5. Game Onnn !!

Define Flask path parameters:

export FLASK_ENV=development
export FLASK_APP=app.py

Create Environment using venv :

python3 -m venv venv
. venv/bin/activate

Define the OCR function in your app.py/server.py/index.py :

# [data] parameter is a dictionary with a key "photo" which contains # the base64 image stringdef ocr(data):
with open("UploadFiles/ss.jpeg", "wb") as fh:
fh.write(base64.b64decode(data["photo"]))
img = cv2.imread("UploadFiles/ss.jpeg")
custom_config = r'--oem 3 --psm 6'
text = pytesseract.image_to_string(img, config=custom_config)
return text

Define the API endpoint

@app.route('/ocr', methods=['POST'])
def ExtractTextFromOcr():
data = json.loads(request.data)
result = ocr(data)
print(result)
return result

Dev Dependencies :

flask
opencv-python
Pytesseract
python-dotenv

Use pip install to install all the dependencies

pip install -r requirements.txt

Checkout the Github repository : Link. Follow the readme well.

Note: Don’t use Heroku or similar hosting platforms for tesseract based applications. Better use, AWS or Google Cloud.

!! Happy Coding !!

--

--

Sidhartha Mallick
Sidhartha Mallick

Written by Sidhartha Mallick

building investorsync.in/, iOS@Gojek, Ex- Intern@BrowserStack

No responses yet