from utils.util import * from utils.conf import MAX_FILE_SIZE from flask import Blueprint, views, render_template, request com = Blueprint("com", __name__, url_prefix="/com") class ComView(views.MethodView): @staticmethod def get(): return render_template("com_index.html") @staticmethod def post(): pic = request.files.get("picture") if pic is None: return Response("empty body") ext = get_ext_name(pic.filename) if not is_image_ext(ext): return Response("文件类型错误") content = pic.read() if len(content) > MAX_FILE_SIZE: return Response("文件过大,请重新选择") file_path = f"static/images/{current_time()}_{rand_str()}.{ext}" save_img(file_path, content) img = read_img(content) ocr_res = Engine.rec_one(img) kind = request.form.get("type") if kind is not None: kind = kind.lower() if kind == "raw": return Response(data=ocr_res) elif kind == "html": data = [{"word": it[0], "rate": it[1], "index": i + 1} for i, it in enumerate(ocr_res)] return render_template("com_result.html", raw=file_path, data=data) else: return Response(data=[it[0] for it in ocr_res]) com.add_url_rule("/", view_func=ComView.as_view("com"))