usmanyousaf commited on
Commit
665ac31
·
verified ·
1 Parent(s): 47b9341

Rename app.py to main.py

Browse files
Files changed (2) hide show
  1. app.py +0 -60
  2. main.py +89 -0
app.py DELETED
@@ -1,60 +0,0 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, Form
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import cv2
5
+ import pytesseract
6
+ import os
7
+
8
+ # Initialize FastAPI app
9
+ app = FastAPI()
10
+
11
+ # Define upload folder
12
+ UPLOAD_FOLDER = "static/uploads/"
13
+ if not os.path.exists(UPLOAD_FOLDER):
14
+ os.makedirs(UPLOAD_FOLDER)
15
+
16
+ # Inform pytesseract where Tesseract is installed
17
+ pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"
18
+
19
+ # Serve static files (images, etc.)
20
+ app.mount("/static", StaticFiles(directory="static"), name="static")
21
+
22
+ # Home route
23
+ @app.get("/", response_class=HTMLResponse)
24
+ async def home():
25
+ html_content = """
26
+ <!DOCTYPE html>
27
+ <html lang="en">
28
+ <head>
29
+ <meta charset="UTF-8">
30
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
31
+ <title>Image to Text Converter</title>
32
+ <style>
33
+ body { font-family: Arial, sans-serif; background-color: #000000; color: #333; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh; }
34
+ .container { text-align: center; background: #fff; padding: 30px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); width: 90%; max-width: 600px; }
35
+ h1 { font-size: 24px; color: #4CAF50; }
36
+ p { color: #666; }
37
+ .upload-box { margin: 20px auto; padding: 30px; border: 2px dashed #ccc; border-radius: 10px; background-color: #f9f9f9; cursor: pointer; position: relative; }
38
+ .upload-box:hover { background-color: #f4f4f4; }
39
+ .upload-box span { color: #888; font-size: 14px; display: block; }
40
+ .upload-box input[type="file"] { position: absolute; width: 100%; height: 100%; top: 0; left: 0; opacity: 0; cursor: pointer; }
41
+ .process-button { background-color: #4CAF50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; }
42
+ .process-button:hover { background-color: #45a049; }
43
+ .result-box { margin-top: 20px; text-align: left; }
44
+ .result-box img { max-width: 100%; border-radius: 5px; margin-top: 10px; }
45
+ .actions { margin-top: 20px; }
46
+ .actions button { margin: 5px; background-color: #4CAF50; color: white; padding: 8px 16px; border: none; border-radius: 5px; cursor: pointer; font-size: 14px; }
47
+ .actions button:hover { background-color: #45a049; }
48
+ pre { background-color: #f4f4f4; padding: 10px; border-radius: 5px; white-space: pre-wrap; word-wrap: break-word; }
49
+ .error { color: red; font-weight: bold; margin-bottom: 20px; }
50
+ </style>
51
+ </head>
52
+ <body>
53
+ <div class="container">
54
+ <h1>Image to Text Converter</h1>
55
+ <p>Quickly extract text from your uploaded images!</p>
56
+ <form action="/upload_image/" method="POST" enctype="multipart/form-data">
57
+ <div class="upload-box">
58
+ <span>Drag & Drop the Images<br>Or Click to Browse</span>
59
+ <input type="file" name="image" accept="image/*" id="image-file">
60
+ </div>
61
+ <button class="process-button" type="submit">Upload Image</button>
62
+ </form>
63
+ </div>
64
+ </body>
65
+ </html>
66
+ """
67
+ return HTMLResponse(content=html_content)
68
+
69
+ # Upload image route
70
+ @app.post("/upload_image/")
71
+ async def upload_image(image: UploadFile = File(...)):
72
+ file_location = f"{UPLOAD_FOLDER}/{image.filename}"
73
+ with open(file_location, "wb") as file:
74
+ file.write(await image.read())
75
+
76
+ return {"image_url": f"/static/uploads/{image.filename}"}
77
+
78
+ # Process image and extract text
79
+ @app.post("/process_image/")
80
+ async def process_image(image_path: str = Form(...)):
81
+ img = cv2.imread(image_path)
82
+
83
+ if img is None:
84
+ return {"error": "Unable to read the uploaded image. Please upload a valid image."}
85
+
86
+ # Extract text from the image
87
+ extracted_text = pytesseract.image_to_string(img)
88
+
89
+ return {"extracted_text": extracted_text}