gahanmakwana commited on
Commit
0bd0f00
ยท
1 Parent(s): 4cca3cd

Updated app.py and index.html for file upload functionality

Browse files
Files changed (2) hide show
  1. app.py +28 -26
  2. templates/index.html +25 -18
app.py CHANGED
@@ -1,40 +1,42 @@
1
- import os
2
- from flask import Flask, render_template, request, send_from_directory
3
  from paddleocr import PaddleOCR
 
4
 
5
  app = Flask(__name__)
 
 
6
  UPLOAD_FOLDER = 'uploads'
7
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
 
8
 
9
- ocr = PaddleOCR(use_angle_cls=True, lang='en')
10
 
11
  @app.route('/', methods=['GET', 'POST'])
12
  def upload_file():
13
- text = None
14
- filename = None
15
  if request.method == 'POST':
 
 
 
16
  file = request.files['file']
 
 
 
17
  if file:
18
- filename = file.filename
19
- img_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
20
  file.save(img_path)
21
-
22
- # Run OCR
23
- result = ocr.ocr(img_path, cls=True)
24
- extracted_text = ""
25
- for line in result:
26
- for word_info in line:
27
- extracted_text += word_info[1][0] + " "
28
-
29
- text = extracted_text
30
-
31
- return render_template('index.html', text=text, filename=filename)
32
-
33
- @app.route('/uploads/<filename>')
34
- def uploaded_file(filename):
35
- return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
36
 
37
  if __name__ == '__main__':
38
- port = int(os.environ.get("PORT", 5000)) # Corrected indentation here
39
- # Run the app, binding to all IP addresses and the correct port
40
- app.run(host="0.0.0.0", port=port)
 
1
+ from flask import Flask, render_template, request
 
2
  from paddleocr import PaddleOCR
3
+ import os
4
 
5
  app = Flask(__name__)
6
+
7
+ # Create uploads directory if it doesn't exist
8
  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'])
15
  def upload_file():
 
 
16
  if request.method == 'POST':
17
+ if 'file' not in request.files:
18
+ return render_template('index.html', error='No file selected')
19
+
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)
27
  file.save(img_path)
28
+
29
+ # Perform OCR
30
+ result = ocr.ocr(img_path)
31
+
32
+ # Extract text from OCR result
33
+ text = ""
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
  if __name__ == '__main__':
42
+ app.run(host='0.0.0.0', port=5000)
 
 
templates/index.html CHANGED
@@ -2,36 +2,43 @@
2
  <html lang="en">
3
  <head>
4
  <meta charset="UTF-8">
 
5
  <title>OCR App</title>
6
- <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
7
  </head>
8
  <body>
 
 
9
 
10
- <div class="container">
11
- <h1>๐Ÿ“ OCR Text Recognition</h1>
12
-
13
- <form method="POST" enctype="multipart/form-data">
14
- <label for="file-upload" class="custom-file-upload">
15
- ๐Ÿ“‚ Choose an image
16
- </label>
17
- <input id="file-upload" type="file" name="file" onchange="this.form.submit()" />
18
  </form>
19
 
20
- {% if filename %}
21
- <div class="image-preview">
22
- <h2>๐Ÿ–ผ Uploaded Image:</h2>
23
- <img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
24
- </div>
25
  {% endif %}
26
 
 
27
  {% if text %}
28
- <div class="output-box">
29
- <h2>๐Ÿ“‹ Extracted Text:</h2>
30
- <p>{{ text }}</p>
31
- </div>
32
  {% endif %}
33
 
 
 
 
 
34
  </div>
35
 
 
 
36
  </body>
37
  </html>
 
2
  <html lang="en">
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
  </head>
9
  <body>
10
+ <div class="container mt-5">
11
+ <h1>OCR Application</h1>
12
 
13
+ <!-- Form to upload file -->
14
+ <form method="post" enctype="multipart/form-data">
15
+ <div class="mb-3">
16
+ <label for="file" class="form-label">Choose a file</label>
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="alert alert-danger mt-3" role="alert">
25
+ {{ error }}
26
+ </div>
27
  {% endif %}
28
 
29
+ <!-- Display OCR Result -->
30
  {% if text %}
31
+ <h3 class="mt-4">Extracted Text:</h3>
32
+ <pre>{{ text }}</pre>
 
 
33
  {% endif %}
34
 
35
+ {% if filename %}
36
+ <h4>Uploaded Image: {{ filename }}</h4>
37
+ <img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image" class="img-fluid mt-3">
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>