File size: 1,995 Bytes
1c6dd8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, render_template, request, redirect, url_for
import cv2
import pytesseract
import os

# Initialize Flask app
app = Flask(__name__)
UPLOAD_FOLDER = "static/uploads/"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER

# Inform pytesseract where Tesseract is installed
pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"

# Ensure upload folder exists
if not os.path.exists(UPLOAD_FOLDER):
    os.makedirs(UPLOAD_FOLDER)

# Home route
@app.route("/", methods=["GET", "POST"])
def home():
    if request.method == "POST":
        if "image" not in request.files:
            return redirect(request.url)
        file = request.files["image"]
        if file.filename == "":
            return redirect(request.url)

        # Save the uploaded file
        if file:
            filepath = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
            file.save(filepath)
            return render_template("index.html", image_url=filepath)

    return render_template("index.html")

# Process image and extract text
@app.route("/process", methods=["POST"])
def process_image():
    if "image_path" in request.form:
        image_path = request.form["image_path"]
        img = cv2.imread(image_path)
        
        # Check if the image was read successfully
        if img is None:
            error_message = "Error: Unable to read the uploaded image. Please upload a valid image."
            return render_template("index.html", error=error_message)

        # Extract text from the image
        extracted_text = pytesseract.image_to_string(img)

        # Pass extracted text to the result template
        return render_template("index.html", image_url=image_path, text=extracted_text)

    # Handle missing form data case
    error_message = "Error: No image path provided for processing. Please upload an image first."
    return render_template("index.html", error=error_message)

# Run Flask app
if __name__ == "__main__":
    app.run(debug=True)