gahanmakwana commited on
Commit
37e0465
·
1 Parent(s): e302e06

Fixed file upload and OCR functionality

Browse files
Files changed (3) hide show
  1. app.py +4 -1
  2. static/style.css +33 -32
  3. templates/index.html +14 -13
app.py CHANGED
@@ -9,6 +9,7 @@ UPLOAD_FOLDER = 'uploads'
9
  if not os.path.exists(UPLOAD_FOLDER):
10
  os.makedirs(UPLOAD_FOLDER)
11
 
 
12
  ocr = PaddleOCR(lang='en')
13
 
14
  @app.route('/', methods=['GET', 'POST'])
@@ -20,7 +21,7 @@ def upload_file():
20
  file = request.files['file']
21
  if file.filename == '':
22
  return render_template('index.html', error='No file selected')
23
-
24
  if file:
25
  # Save the file to uploads directory
26
  img_path = os.path.join(UPLOAD_FOLDER, file.filename)
@@ -34,10 +35,12 @@ def upload_file():
34
  for line in result[0]:
35
  text += line[1][0] + "\n"
36
 
 
37
  return render_template('index.html', text=text, filename=file.filename)
38
 
39
  return render_template('index.html')
40
 
 
41
  @app.route('/uploads/<filename>')
42
  def uploaded_file(filename):
43
  return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
 
9
  if not os.path.exists(UPLOAD_FOLDER):
10
  os.makedirs(UPLOAD_FOLDER)
11
 
12
+ # Initialize the OCR model
13
  ocr = PaddleOCR(lang='en')
14
 
15
  @app.route('/', methods=['GET', 'POST'])
 
21
  file = request.files['file']
22
  if file.filename == '':
23
  return render_template('index.html', error='No file selected')
24
+
25
  if file:
26
  # Save the file to uploads directory
27
  img_path = os.path.join(UPLOAD_FOLDER, file.filename)
 
35
  for line in result[0]:
36
  text += line[1][0] + "\n"
37
 
38
+ # Return the rendered template with the extracted text and image
39
  return render_template('index.html', text=text, filename=file.filename)
40
 
41
  return render_template('index.html')
42
 
43
+ # Serve the uploaded file
44
  @app.route('/uploads/<filename>')
45
  def uploaded_file(filename):
46
  return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
static/style.css CHANGED
@@ -8,53 +8,38 @@ body {
8
  .container {
9
  background: white;
10
  border-radius: 16px;
11
- box-shadow: 0px 10px 30px rgba(0,0,0,0.2);
12
  padding: 40px;
13
  display: inline-block;
14
  max-width: 700px;
15
- animation: fadeIn 1s ease-in;
16
  }
17
 
18
  h1 {
19
  margin-bottom: 20px;
20
  color: #333;
21
- font-size: 2.5em;
22
  }
23
 
24
- form {
25
- margin-bottom: 30px;
26
- }
27
-
28
- .custom-file-upload {
29
- background-color: #007bff;
30
- border: none;
31
- color: white;
32
- padding: 14px 28px;
33
- border-radius: 8px;
34
  cursor: pointer;
35
- font-size: 18px;
36
  transition: background 0.3s;
37
  }
38
 
39
- .custom-file-upload:hover {
40
- background-color: #0056b3;
41
  }
42
 
43
- input[type="file"] {
44
  display: none;
45
  }
46
 
47
- .image-preview {
48
- margin-top: 30px;
49
- }
50
-
51
- .image-preview img {
52
- width: 100%;
53
- max-height: 400px;
54
- object-fit: contain;
55
- border-radius: 12px;
56
- margin-top: 10px;
57
- box-shadow: 0 5px 15px rgba(0,0,0,0.2);
58
  }
59
 
60
  .output-box {
@@ -66,11 +51,27 @@ input[type="file"] {
66
  word-wrap: break-word;
67
  }
68
 
69
- .output-box p {
70
  white-space: pre-wrap;
71
  }
72
 
73
- @keyframes fadeIn {
74
- from { opacity: 0; transform: translateY(30px); }
75
- to { opacity: 1; transform: translateY(0); }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  }
 
8
  .container {
9
  background: white;
10
  border-radius: 16px;
11
+ box-shadow: 0px 10px 30px rgba(0, 0, 0, 0.2);
12
  padding: 40px;
13
  display: inline-block;
14
  max-width: 700px;
 
15
  }
16
 
17
  h1 {
18
  margin-bottom: 20px;
19
  color: #333;
 
20
  }
21
 
22
+ .upload-box {
23
+ background-color: #f8f9fa;
24
+ padding: 40px;
25
+ border-radius: 12px;
26
+ border: 2px dashed #007bff;
 
 
 
 
 
27
  cursor: pointer;
 
28
  transition: background 0.3s;
29
  }
30
 
31
+ .upload-box:hover {
32
+ background-color: #e9ecef;
33
  }
34
 
35
+ .file-input {
36
  display: none;
37
  }
38
 
39
+ .file-label {
40
+ font-size: 16px;
41
+ font-weight: 600;
42
+ color: #007bff;
 
 
 
 
 
 
 
43
  }
44
 
45
  .output-box {
 
51
  word-wrap: break-word;
52
  }
53
 
54
+ .output-box pre {
55
  white-space: pre-wrap;
56
  }
57
 
58
+ img {
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
+ button {
66
+ background-color: #007bff;
67
+ border: none;
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
  }
templates/index.html CHANGED
@@ -3,21 +3,21 @@
3
  <head>
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>OCR App</title>
7
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/css/bootstrap.min.css">
8
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
9
  </head>
10
  <body>
11
  <div class="container mt-5">
12
- <h1>OCR Application</h1>
13
 
14
  <!-- Form to upload file -->
15
- <form method="post" enctype="multipart/form-data">
16
- <div class="mb-3">
17
- <label for="file" class="form-label">Choose a file</label>
18
- <input type="file" class="form-control" name="file" id="file" required>
19
  </div>
20
- <button type="submit" class="btn btn-primary">Upload</button>
21
  </form>
22
 
23
  <!-- Error Message Display -->
@@ -30,18 +30,19 @@
30
  <!-- Display OCR Result -->
31
  {% if text %}
32
  <h3 class="mt-4">Extracted Text:</h3>
33
- <pre>{{ text }}</pre>
 
 
34
  {% endif %}
35
 
36
- <!-- Display Image -->
37
  {% if filename %}
38
- <h4>Uploaded Image: {{ filename }}</h4>
39
- <div class="image-preview">
40
- <img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
41
- </div>
42
  {% endif %}
43
  </div>
44
 
 
45
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
46
  </body>
47
  </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="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/css/bootstrap.min.css">
8
  <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
9
  </head>
10
  <body>
11
  <div class="container mt-5">
12
+ <h1 class="text-center">OCR Application</h1>
13
 
14
  <!-- Form to upload file -->
15
+ <form method="post" enctype="multipart/form-data" class="upload-form">
16
+ <div class="upload-box">
17
+ <label for="file" class="file-label">Choose an image</label>
18
+ <input type="file" name="file" id="file" required class="file-input">
19
  </div>
20
+ <button type="submit" class="btn btn-primary mt-3">Upload</button>
21
  </form>
22
 
23
  <!-- Error Message Display -->
 
30
  <!-- Display OCR Result -->
31
  {% if text %}
32
  <h3 class="mt-4">Extracted Text:</h3>
33
+ <div class="output-box">
34
+ <pre>{{ text }}</pre>
35
+ </div>
36
  {% endif %}
37
 
38
+ <!-- Display Uploaded Image -->
39
  {% if filename %}
40
+ <h4 class="mt-3">Uploaded Image: {{ filename }}</h4>
41
+ <img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image" class="img-fluid mt-3">
 
 
42
  {% endif %}
43
  </div>
44
 
45
+ <!-- Optional: Bootstrap JavaScript for interaction -->
46
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.0/js/bootstrap.bundle.min.js"></script>
47
  </body>
48
  </html>