封装成http服务进行调用
import threading
from flask import Flask, request, jsonify
from paddleocr import PaddleOCR
app = Flask(__name__)
# Initialize 5 PaddleOCR instances and locks
ocr_instances = [PaddleOCR(enable_mkldnn=True, use_angle_cls=True, lang='en') for _ in range(5)]
locks = [threading.Lock() for _ in range(5)]
@app.route('/ocr', methods=['POST'])
def ocr_handler():
if 'img' not in request.files:
return jsonify({'error': 'No image file provided'})
image_file = request.files['img']
image_path = 'C:\\Users\\dukun\\Downloads\\ppocr_img\\ppocr_img\\imgs_en\\' + image_file.filename
# 创建一个空白的图像文件
with open(image_path, 'wb') as f:
# 仅写入一个字节作为占位符
f.write(b'\x00')
# Specify the path to save the image
image_file.save(image_path)
# image_path = 'C:\\Users\\dukun\\AppData\\Local\\Temp\\16853451072921665941938794632449408042page_2.jpg'
# Get the index of the OCR instance to use
model_index = id(request._get_current_object()) % len(ocr_instances)
ocr = ocr_instances[model_index]
# Acquire the lock for the OCR instance
lock = locks[model_index]
lock.acquire()
print("model_index", model_index)
try:
result = ocr.ocr(image_path, cls=True)
# Process the OCR result
ocr_result = []
for res in result:
for line in res:
ocr_result.append(line)
return jsonify({'result': ocr_result})
finally:
# Release the lock after OCR processing is complete
lock.release()
if __name__ == '__main__':
app.run()