usmanyousaf commited on
Commit
1c6dd8a
·
verified ·
1 Parent(s): 541556f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, redirect, url_for
2
+ import cv2
3
+ import pytesseract
4
+ import os
5
+
6
+ # Initialize Flask app
7
+ app = Flask(__name__)
8
+ UPLOAD_FOLDER = "static/uploads/"
9
+ app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
10
+
11
+ # Inform pytesseract where Tesseract is installed
12
+ pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"
13
+
14
+ # Ensure upload folder exists
15
+ if not os.path.exists(UPLOAD_FOLDER):
16
+ os.makedirs(UPLOAD_FOLDER)
17
+
18
+ # Home route
19
+ @app.route("/", methods=["GET", "POST"])
20
+ def home():
21
+ if request.method == "POST":
22
+ if "image" not in request.files:
23
+ return redirect(request.url)
24
+ file = request.files["image"]
25
+ if file.filename == "":
26
+ return redirect(request.url)
27
+
28
+ # Save the uploaded file
29
+ if file:
30
+ filepath = os.path.join(app.config["UPLOAD_FOLDER"], file.filename)
31
+ file.save(filepath)
32
+ return render_template("index.html", image_url=filepath)
33
+
34
+ return render_template("index.html")
35
+
36
+ # Process image and extract text
37
+ @app.route("/process", methods=["POST"])
38
+ def process_image():
39
+ if "image_path" in request.form:
40
+ image_path = request.form["image_path"]
41
+ img = cv2.imread(image_path)
42
+
43
+ # Check if the image was read successfully
44
+ if img is None:
45
+ error_message = "Error: Unable to read the uploaded image. Please upload a valid image."
46
+ return render_template("index.html", error=error_message)
47
+
48
+ # Extract text from the image
49
+ extracted_text = pytesseract.image_to_string(img)
50
+
51
+ # Pass extracted text to the result template
52
+ return render_template("index.html", image_url=image_path, text=extracted_text)
53
+
54
+ # Handle missing form data case
55
+ error_message = "Error: No image path provided for processing. Please upload an image first."
56
+ return render_template("index.html", error=error_message)
57
+
58
+ # Run Flask app
59
+ if __name__ == "__main__":
60
+ app.run(debug=True)