File size: 1,150 Bytes
9a6f90b |
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 32 33 34 35 36 37 38 39 40 41 |
from flask import Flask, request, render_template
import pytesseract
from PIL import Image
import os
app = Flask(__name__)
# Define the path for uploaded files
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Make sure the upload folder exists
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Route to home page with form
@app.route('/')
def index():
return render_template('index.html')
# Route to handle file upload and invoice extraction
@app.route('/upload', methods=['POST'])
def upload_file():
if 'invoice_image' not in request.files:
return 'No file part'
file = request.files['invoice_image']
if file.filename == '':
return 'No selected file'
if file:
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
# Use pytesseract to extract text from the image
img = Image.open(file_path)
extracted_text = pytesseract.image_to_string(img)
# Return the extracted text
return render_template('index.html', extracted_text=extracted_text)
if __name__ == '__main__':
app.run(debug=True)
|