123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- from flask import Blueprint, views, render_template, request
- from utils.util import *
- from utils.conf import MAX_CONTENT_LENGTH
- 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_CONTENT_LENGTH:
- return Response("文件过大,请重新选择")
- cur, rnd = current_time(), rand_str()
- raw_path = f"static/images/{cur}_{rnd}.{ext}"
- rec_path = f"static/images/{cur}_{rnd}-rec.{ext}"
- with open(raw_path, "wb") as fp:
- fp.write(content)
- fp.close()
- ocr_res, img_shape = recognize(content)
- kind = request.form.get("type")
- if kind is not None:
- kind = kind.lower()
- if kind == "raw":
- return Response(data=[{"pos": it[0], "word": it[1][0], "rate": it[1][1]} for it in ocr_res])
- elif kind == "html":
- data = [{"pos": it[0], "word": it[1][0], "rate": it[1][1]} for it in ocr_res]
- draw_img(img_shape, data, rec_path)
- return render_template("com_result.html", raw=raw_path, rec=rec_path, data=data)
- else:
- return Response(data=[it[1][0] for it in ocr_res])
- com.add_url_rule("/", view_func=ComView.as_view("com"))
|