File size: 1,391 Bytes
9f7a699
88f28f0
6c59b39
93c87da
 
88f28f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from flask import Flask, request, jsonify
from paddleocr import PaddleOCR

app = Flask(__name__)

# Global variable to hold the OCR instance
ocr = None

# Function to initialize the OCR model
def get_ocr():
    global ocr
    if ocr is None:
        # Ensure the /tmp/ocr_models directory exists
        os.makedirs('/tmp/ocr_models', exist_ok=True)

        # Initialize the OCR model
        ocr = PaddleOCR(
            use_angle_cls=False,
            use_gpu=False,
            lang='en',
            det_model_dir='/tmp/ocr_models/det',
            rec_model_dir='/tmp/ocr_models/rec',
            cls_model_dir='/tmp/ocr_models/cls'
        )
    return ocr

@app.route('/')
def home():
    return "OCR App is running!"

@app.route('/extract-text', methods=['POST'])
def extract_text():
    # Get OCR model
    ocr_model = get_ocr()

    # Get the image file from the request
    image_file = request.files.get('image')
    if image_file:
        # Perform OCR on the uploaded image
        result = ocr_model.ocr(image_file, cls=False)

        # Extract text from OCR result
        text = '\n'.join([line[1][0] for line in result[0]])

        return jsonify({"extracted_text": text})

    return jsonify({"error": "No image uploaded!"}), 400

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port, threaded=False)