前言
最近发现Flask
项目在运行时会报[WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead
警告,意思是不要在生产环境使用这种方案启动程序:
解决
使用WSGI
启动服务即可解决问题(中间可能需要pip3 install gevent
)
原版代码:
from paddleocr import PaddleOCR
from flask import Flask, request
import cv2
import numpy
app = Flask(__name__)
ocr = PaddleOCR(det_model_dir='./inference/small/ch_PP-OCRv2_det_infer/', rec_model_dir='./inference/small/ch_PP-OCRv2_rec_infer/',cls_model_dir='./inference/small/ch_ppocr_mobile_v2.0_cls_infer/',use_angle_cls=True,use_space_char=True, use_gpu=False)
@app.route('/xxxxxx/', methods=['get', 'POST'])
def xxxxxx():
img_path = request.args.get("fname")
result = ocr.ocr(img_path)
ret = ''
for line in result:
ret += str(line[1][0]) + "\n"
return str(ret)
if __name__ == '__main__':
app.run(host='0.0.0.0')
新版代码:
from paddleocr import PaddleOCR
from flask import Flask, request
import cv2
import numpy
from gevent import pywsgi
app = Flask(__name__)
ocr = PaddleOCR(det_model_dir='./inference/small/ch_PP-OCRv2_det_infer/', rec_model_dir='./inference/small/ch_PP-OCRv2_rec_infer/',cls_model_dir='./inference/small/ch_ppocr_mobile_v2.0_cls_infer/',use_angle_cls=True,use_space_char=True, use_gpu=False)
@app.route('/xxxxxx/', methods=['get', 'POST'])
def xxxxxx():
img_path = request.args.get("fname")
result = ocr.ocr(img_path)
ret = ''
for line in result:
ret += str(line[1][0]) + "\n"
return str(ret)
if __name__ == '__main__':
server = pywsgi.WSGIServer(('0.0.0.0', 5000), app)
server.serve_forever()
解决问题!
参考资料:
https://blog.csdn.net/weixin_43642491/article/details/114916769
https://www.cnblogs.com/mlp1234/p/13743254.html
版权属于:soarli
本文链接:https://blog.soarli.top/archives/650.html
转载时须注明出处及本声明。