Spaces:
Running
Running
Commit
·
44154a0
1
Parent(s):
98bb570
Fixed file upload and OCR functionality
Browse files- app.py +25 -30
- static/style.css +50 -46
- templates/index.html +20 -25
app.py
CHANGED
@@ -4,44 +4,39 @@ import os
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
#
|
8 |
UPLOAD_FOLDER = 'uploads'
|
9 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
10 |
if not os.path.exists(UPLOAD_FOLDER):
|
11 |
os.makedirs(UPLOAD_FOLDER)
|
12 |
|
13 |
-
# Initialize
|
14 |
-
ocr = PaddleOCR(lang='en')
|
15 |
|
16 |
@app.route('/', methods=['GET', 'POST'])
|
17 |
def upload_file():
|
|
|
|
|
18 |
if request.method == 'POST':
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
# Return the rendered template with the extracted text and image
|
40 |
-
return render_template('index.html', text=text, filename=file.filename)
|
41 |
-
|
42 |
-
return render_template('index.html')
|
43 |
-
|
44 |
-
# Serve the uploaded file (from the uploads directory)
|
45 |
@app.route('/uploads/<filename>')
|
46 |
def uploaded_file(filename):
|
47 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
# Upload folder
|
8 |
UPLOAD_FOLDER = 'uploads'
|
9 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
10 |
if not os.path.exists(UPLOAD_FOLDER):
|
11 |
os.makedirs(UPLOAD_FOLDER)
|
12 |
|
13 |
+
# Initialize OCR
|
14 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
15 |
|
16 |
@app.route('/', methods=['GET', 'POST'])
|
17 |
def upload_file():
|
18 |
+
text = None
|
19 |
+
filename = None
|
20 |
if request.method == 'POST':
|
21 |
+
file = request.files.get('file')
|
22 |
+
if not file or file.filename == '':
|
23 |
+
return render_template('index.html', error="No file selected")
|
24 |
+
|
25 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
|
26 |
+
file.save(filepath)
|
27 |
+
|
28 |
+
# Run OCR
|
29 |
+
result = ocr.ocr(filepath, cls=True)
|
30 |
+
extracted_text = ""
|
31 |
+
for line in result:
|
32 |
+
for word_info in line:
|
33 |
+
extracted_text += word_info[1][0] + " "
|
34 |
+
|
35 |
+
text = extracted_text
|
36 |
+
filename = file.filename
|
37 |
+
|
38 |
+
return render_template('index.html', text=text, filename=filename)
|
39 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
@app.route('/uploads/<filename>')
|
41 |
def uploaded_file(filename):
|
42 |
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
|
static/style.css
CHANGED
@@ -1,77 +1,81 @@
|
|
1 |
body {
|
2 |
font-family: 'Poppins', sans-serif;
|
3 |
-
background: linear-gradient(135deg, #74ebd5, #
|
4 |
-
|
5 |
-
padding:
|
|
|
|
|
|
|
|
|
6 |
}
|
7 |
|
8 |
.container {
|
9 |
-
background:
|
10 |
-
border-radius: 16px;
|
11 |
-
box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.2);
|
12 |
padding: 40px;
|
13 |
-
|
|
|
14 |
max-width: 700px;
|
|
|
|
|
15 |
}
|
16 |
|
17 |
h1 {
|
18 |
margin-bottom: 20px;
|
19 |
color: #333;
|
|
|
20 |
}
|
21 |
|
22 |
-
.upload-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
cursor: pointer;
|
28 |
-
transition: background 0.3s;
|
29 |
}
|
30 |
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
33 |
}
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
}
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
font-weight: 600;
|
42 |
-
color: #007bff;
|
43 |
}
|
44 |
|
45 |
-
.
|
46 |
-
|
47 |
-
|
48 |
-
padding: 20px;
|
49 |
border-radius: 10px;
|
50 |
-
|
51 |
-
word-wrap: break-word;
|
52 |
}
|
53 |
|
54 |
-
.output
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
}
|
57 |
|
58 |
-
|
59 |
-
border-radius: 8px;
|
60 |
-
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
|
61 |
-
max-width: 100%;
|
62 |
margin-top: 20px;
|
|
|
|
|
63 |
}
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
color: white;
|
69 |
-
padding: 12px 30px;
|
70 |
-
border-radius: 5px;
|
71 |
-
cursor: pointer;
|
72 |
-
transition: background 0.3s;
|
73 |
-
}
|
74 |
-
|
75 |
-
button:hover {
|
76 |
-
background-color: #0056b3;
|
77 |
}
|
|
|
1 |
body {
|
2 |
font-family: 'Poppins', sans-serif;
|
3 |
+
background: linear-gradient(135deg, #74ebd5, #ACB6E5);
|
4 |
+
margin: 0;
|
5 |
+
padding: 0;
|
6 |
+
min-height: 100vh;
|
7 |
+
display: flex;
|
8 |
+
justify-content: center;
|
9 |
+
align-items: center;
|
10 |
}
|
11 |
|
12 |
.container {
|
13 |
+
background: #ffffff;
|
|
|
|
|
14 |
padding: 40px;
|
15 |
+
border-radius: 20px;
|
16 |
+
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
|
17 |
max-width: 700px;
|
18 |
+
width: 90%;
|
19 |
+
animation: fadeIn 1s ease-in;
|
20 |
}
|
21 |
|
22 |
h1 {
|
23 |
margin-bottom: 20px;
|
24 |
color: #333;
|
25 |
+
font-weight: 600;
|
26 |
}
|
27 |
|
28 |
+
.upload-form {
|
29 |
+
display: flex;
|
30 |
+
flex-direction: column;
|
31 |
+
gap: 20px;
|
32 |
+
margin-bottom: 30px;
|
|
|
|
|
33 |
}
|
34 |
|
35 |
+
input[type="file"] {
|
36 |
+
padding: 10px;
|
37 |
+
background: #f5f5f5;
|
38 |
+
border: 2px dashed #ccc;
|
39 |
+
border-radius: 10px;
|
40 |
}
|
41 |
|
42 |
+
button {
|
43 |
+
padding: 12px;
|
44 |
+
background: #007bff;
|
45 |
+
color: white;
|
46 |
+
border: none;
|
47 |
+
border-radius: 10px;
|
48 |
+
font-size: 16px;
|
49 |
+
cursor: pointer;
|
50 |
+
transition: background 0.3s;
|
51 |
}
|
52 |
|
53 |
+
button:hover {
|
54 |
+
background: #0056b3;
|
|
|
|
|
55 |
}
|
56 |
|
57 |
+
.preview img {
|
58 |
+
max-width: 100%;
|
59 |
+
max-height: 400px;
|
|
|
60 |
border-radius: 10px;
|
61 |
+
margin-top: 20px;
|
|
|
62 |
}
|
63 |
|
64 |
+
.output {
|
65 |
+
margin-top: 30px;
|
66 |
+
background: #f9f9f9;
|
67 |
+
padding: 20px;
|
68 |
+
border-radius: 10px;
|
69 |
+
word-break: break-word;
|
70 |
}
|
71 |
|
72 |
+
.error {
|
|
|
|
|
|
|
73 |
margin-top: 20px;
|
74 |
+
color: red;
|
75 |
+
font-weight: bold;
|
76 |
}
|
77 |
|
78 |
+
@keyframes fadeIn {
|
79 |
+
from { opacity: 0; transform: translateY(30px);}
|
80 |
+
to { opacity: 1; transform: translateY(0);}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
templates/index.html
CHANGED
@@ -3,42 +3,37 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
-
<title>OCR
|
7 |
-
<link rel="stylesheet" href="
|
|
|
8 |
</head>
|
9 |
<body>
|
10 |
-
<div class="container
|
11 |
-
<h1>
|
12 |
|
13 |
-
<!-- Form
|
14 |
-
<form method="
|
15 |
-
<
|
16 |
-
|
17 |
-
<input type="file" class="form-control" name="file" id="file" required>
|
18 |
-
</div>
|
19 |
-
<button type="submit" class="btn btn-primary">Upload</button>
|
20 |
</form>
|
21 |
|
22 |
-
<!-- Error Message Display -->
|
23 |
{% if error %}
|
24 |
-
<div class="
|
25 |
-
{{ error }}
|
26 |
-
</div>
|
27 |
{% endif %}
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
{% endif %}
|
34 |
|
35 |
-
{% if
|
36 |
-
<
|
37 |
-
|
|
|
|
|
38 |
{% endif %}
|
39 |
</div>
|
40 |
-
|
41 |
-
<!-- Optional: Bootstrap JavaScript for interaction (such as form validation) -->
|
42 |
-
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
|
43 |
</body>
|
44 |
</html>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
6 |
+
<title>OCR Application</title>
|
7 |
+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
|
8 |
+
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
|
9 |
</head>
|
10 |
<body>
|
11 |
+
<div class="container">
|
12 |
+
<h1>Image Text Extractor</h1>
|
13 |
|
14 |
+
<!-- Upload Form -->
|
15 |
+
<form method="POST" enctype="multipart/form-data" class="upload-form">
|
16 |
+
<input type="file" name="file" id="file" required>
|
17 |
+
<button type="submit">Upload Image</button>
|
|
|
|
|
|
|
18 |
</form>
|
19 |
|
|
|
20 |
{% if error %}
|
21 |
+
<div class="error">{{ error }}</div>
|
|
|
|
|
22 |
{% endif %}
|
23 |
|
24 |
+
{% if filename %}
|
25 |
+
<div class="preview">
|
26 |
+
<h2>Uploaded Image</h2>
|
27 |
+
<img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
|
28 |
+
</div>
|
29 |
{% endif %}
|
30 |
|
31 |
+
{% if text %}
|
32 |
+
<div class="output">
|
33 |
+
<h2>Extracted Text</h2>
|
34 |
+
<p>{{ text }}</p>
|
35 |
+
</div>
|
36 |
{% endif %}
|
37 |
</div>
|
|
|
|
|
|
|
38 |
</body>
|
39 |
</html>
|