Spaces:
Running
Running
Commit
·
88f28f0
1
Parent(s):
d0f81fb
changed app.py for mem
Browse files
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 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
@app.route('/'
|
31 |
-
def
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|