gahanmakwana commited on
Commit
932e485
·
1 Parent(s): 88f28f0
Files changed (2) hide show
  1. app.py +56 -47
  2. static/style.css +1 -0
app.py CHANGED
@@ -1,52 +1,61 @@
 
 
1
  import os
2
- from flask import Flask, request, jsonify
3
  from paddleocr import PaddleOCR
 
4
 
5
  app = Flask(__name__)
 
 
 
6
 
7
- # Global variable to hold the OCR instance
8
- ocr = None
9
-
10
- # Function to initialize the OCR model
11
- def get_ocr():
12
- global ocr
13
- if ocr is None:
14
- # Ensure the /tmp/ocr_models directory exists
15
- os.makedirs('/tmp/ocr_models', exist_ok=True)
16
-
17
- # Initialize the OCR model
18
- ocr = PaddleOCR(
19
- use_angle_cls=False,
20
- use_gpu=False,
21
- lang='en',
22
- det_model_dir='/tmp/ocr_models/det',
23
- rec_model_dir='/tmp/ocr_models/rec',
24
- cls_model_dir='/tmp/ocr_models/cls'
25
- )
26
- return ocr
27
-
28
- @app.route('/')
29
- def home():
30
- return "OCR App is running!"
31
-
32
- @app.route('/extract-text', methods=['POST'])
33
- def extract_text():
34
- # Get OCR model
35
- ocr_model = get_ocr()
36
-
37
- # Get the image file from the request
38
- image_file = request.files.get('image')
39
- if image_file:
40
- # Perform OCR on the uploaded image
41
- result = ocr_model.ocr(image_file, cls=False)
42
-
43
- # Extract text from OCR result
44
- text = '\n'.join([line[1][0] for line in result[0]])
45
-
46
- return jsonify({"extracted_text": text})
47
-
48
- return jsonify({"error": "No image uploaded!"}), 400
49
-
50
- if __name__ == '__main__':
51
- port = int(os.environ.get('PORT', 5000))
52
- app.run(host='0.0.0.0', port=port, threaded=False)
 
 
 
 
 
1
+ # app.py
2
+ from flask import Flask, render_template, request, redirect, flash, url_for
3
  import os
 
4
  from paddleocr import PaddleOCR
5
+ from werkzeug.utils import secure_filename
6
 
7
  app = Flask(__name__)
8
+ app.secret_key = os.environ.get('SECRET_KEY', 'change-this') # Replace in production
9
+ UPLOAD_FOLDER = os.path.join('static', 'uploads')
10
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
11
 
12
+ # Ensure upload directory exists
13
+ os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
14
+
15
+ # Initialize PaddleOCR once (CPU mode, English; angle_cls=False speeds up simple text)
16
+ import os
17
+ from paddleocr import PaddleOCR
18
+
19
+ # create /tmp/ocr_models directory if it doesn't exist
20
+ os.makedirs('/tmp/ocr_models', exist_ok=True)
21
+
22
+ ocr = PaddleOCR(
23
+ use_angle_cls=False,
24
+ use_gpu=False,
25
+ lang='en',
26
+ det_model_dir='/tmp/ocr_models/det',
27
+ rec_model_dir='/tmp/ocr_models/rec',
28
+ cls_model_dir='/tmp/ocr_models/cls'
29
+ )
30
+
31
+ @app.route('/', methods=['GET', 'POST'])
32
+ def index():
33
+ extracted_text = None
34
+ image_file = None
35
+
36
+ if request.method == 'POST':
37
+ # Check file in request
38
+ if 'image' not in request.files:
39
+ flash('No file part in the request.')
40
+ return redirect(request.url)
41
+ file = request.files['image']
42
+ if file.filename == '':
43
+ flash('No image selected.')
44
+ return redirect(request.url)
45
+
46
+ # Save uploaded file
47
+ filename = secure_filename(file.filename)
48
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
49
+ file.save(file_path)
50
+
51
+ # Run PaddleOCR on the saved image (CPU mode)
52
+ result = ocr.ocr(file_path, cls=False)
53
+ # Collect recognized text lines
54
+ lines = []
55
+ for res_line in result:
56
+ for box, (txt, prob) in res_line:
57
+ lines.append(txt)
58
+ extracted_text = "\n".join(lines)
59
+ image_file = filename
60
+
61
+ return render_template('index.html', extracted_text=extracted_text, image_file=image_file)
static/style.css CHANGED
@@ -104,6 +104,7 @@ button:hover {
104
  z-index: 1000;
105
  } */
106
  /* static/style.css */
 
107
  body {
108
  background: #f0f2f5;
109
  font-family: 'Segoe UI', Tahoma, sans-serif;
 
104
  z-index: 1000;
105
  } */
106
  /* static/style.css */
107
+ /* static/style.css */
108
  body {
109
  background: #f0f2f5;
110
  font-family: 'Segoe UI', Tahoma, sans-serif;