Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template
|
| 2 |
+
import pytesseract
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
# Define the path for uploaded files
|
| 9 |
+
UPLOAD_FOLDER = 'uploads'
|
| 10 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 11 |
+
|
| 12 |
+
# Make sure the upload folder exists
|
| 13 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
# Route to home page with form
|
| 16 |
+
@app.route('/')
|
| 17 |
+
def index():
|
| 18 |
+
return render_template('index.html')
|
| 19 |
+
|
| 20 |
+
# Route to handle file upload and invoice extraction
|
| 21 |
+
@app.route('/upload', methods=['POST'])
|
| 22 |
+
def upload_file():
|
| 23 |
+
if 'invoice_image' not in request.files:
|
| 24 |
+
return 'No file part'
|
| 25 |
+
file = request.files['invoice_image']
|
| 26 |
+
if file.filename == '':
|
| 27 |
+
return 'No selected file'
|
| 28 |
+
if file:
|
| 29 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
| 30 |
+
file.save(file_path)
|
| 31 |
+
|
| 32 |
+
# Use pytesseract to extract text from the image
|
| 33 |
+
img = Image.open(file_path)
|
| 34 |
+
extracted_text = pytesseract.image_to_string(img)
|
| 35 |
+
|
| 36 |
+
# Return the extracted text
|
| 37 |
+
return render_template('index.html', extracted_text=extracted_text)
|
| 38 |
+
|
| 39 |
+
if __name__ == '__main__':
|
| 40 |
+
app.run(debug=True)
|