my-ocr-demo / app.py
gahanmakwana's picture
Updated app.py and index.html for file upload functionality
0bd0f00
raw
history blame
1.26 kB
from flask import Flask, render_template, request
from paddleocr import PaddleOCR
import os
app = Flask(__name__)
# Create uploads directory if it doesn't exist
UPLOAD_FOLDER = 'uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
ocr = PaddleOCR(lang='en')
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return render_template('index.html', error='No file selected')
file = request.files['file']
if file.filename == '':
return render_template('index.html', error='No file selected')
if file:
# Save the file to uploads directory
img_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(img_path)
# Perform OCR
result = ocr.ocr(img_path)
# Extract text from OCR result
text = ""
for line in result[0]:
text += line[1][0] + "\n"
return render_template('index.html', text=text, filename=file.filename)
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)