gahanmakwana commited on
Commit
88f28f0
·
1 Parent(s): d0f81fb

changed app.py for mem

Browse files
Files changed (1) hide show
  1. app.py +48 -56
app.py CHANGED
@@ -1,60 +1,52 @@
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
-
17
-
18
- # create /tmp/ocr_models directory if it doesn't exist
19
- os.makedirs('/tmp/ocr_models', exist_ok=True)
20
-
21
- ocr = PaddleOCR(
22
- use_angle_cls=False,
23
- use_gpu=False,
24
- lang='en',
25
- det_model_dir='/tmp/ocr_models/det',
26
- rec_model_dir='/tmp/ocr_models/rec',
27
- cls_model_dir='/tmp/ocr_models/cls'
28
- )
29
-
30
- @app.route('/', methods=['GET', 'POST'])
31
- def index():
32
- extracted_text = None
33
- image_file = None
34
-
35
- if request.method == 'POST':
36
- # Check file in request
37
- if 'image' not in request.files:
38
- flash('No file part in the request.')
39
- return redirect(request.url)
40
- file = request.files['image']
41
- if file.filename == '':
42
- flash('No image selected.')
43
- return redirect(request.url)
44
-
45
- # Save uploaded file
46
- filename = secure_filename(file.filename)
47
- file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
48
- file.save(file_path)
49
-
50
- # Run PaddleOCR on the saved image (CPU mode)
51
- result = ocr.ocr(file_path, cls=False)
52
- # Collect recognized text lines
53
- lines = []
54
- for res_line in result:
55
- for box, (txt, prob) in res_line:
56
- lines.append(txt)
57
- extracted_text = "\n".join(lines)
58
- image_file = filename
59
-
60
- return render_template('index.html', extracted_text=extracted_text, image_file=image_file)
 
 
 
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)