idc.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from flask import Blueprint, views, render_template, request
  2. from utils.util import *
  3. import re
  4. from utils.conf import MAX_CONTENT_LENGTH
  5. idc = Blueprint("idc", __name__, url_prefix="/idc")
  6. __exclude = "中国CHINA *#★☆"
  7. __face_ptn = r"^姓名(?P<name>.+)性别(?P<gender>男|女)民族(?P<nation>.+)" \
  8. r"出生(?P<year>\d{4})年(?P<month>\d\d)月(?P<day>\d\d)日" \
  9. r"住址(?P<addr>.+)公民身份号码(?P<idn>\d{17}\d|x|X)$"
  10. __icon_ptn = r"^中华人民共和国居民身份证签发机关(?P<agent>.+)" \
  11. r"有效期限(?P<from_year>\d{4})\.(?P<from_month>\d{2})\.(?P<from_day>\d{2})" \
  12. r"[^\d]+(?P<to_year>\d{4})\.(?P<to_month>\d{2})\.(?P<to_day>\d{2})$"
  13. def get_face_info(data: "list[str]") -> "tuple[dict, str, bool]":
  14. res = {"name": "", "gender": "", "nation": "", "birth": {"year": "", "month": "", "day": ""}, "addr": "", "idn": ""}
  15. if len(data) < 5: # 最少 5 个识别结果
  16. return res, "请使用正确的身份证人像面照片", False
  17. str_all = "".join([item for item in data if not str_include(__exclude, item)])
  18. if match := re.match(__face_ptn, str_all):
  19. res["name"] = match.group("name")
  20. res["gender"] = match.group("gender")
  21. res["nation"] = match.group("nation")
  22. res["birth"] = {
  23. "year": match.group("year"),
  24. "month": match.group("month"),
  25. "day": match.group("day")
  26. }
  27. res["addr"] = match.group("addr")
  28. res["idn"] = match.group("idn")
  29. return res, str_all, True
  30. return res, "识别失败,请重新选择", False
  31. def get_icon_info(data: "list[str]"):
  32. res = {"agent": "", "from": {"year": "", "month": "", "day": ""}, "to": {"year": "", "month": "", "day": ""}}
  33. if len(data) < 4: # 最少 4 个识别结果
  34. return res, "请使用正确的身份证国徽面照片", False
  35. str_all = "".join([item for item in data if not str_include(__exclude, item)])
  36. if match := re.match(__icon_ptn, str_all):
  37. res["agent"] = match.group("agent")
  38. res["from"] = {
  39. "year": match.group("from_year"),
  40. "month": match.group("from_month"),
  41. "day": match.group("from_day"),
  42. }
  43. res["to"] = {
  44. "year": match.group("to_year"),
  45. "month": match.group("to_month"),
  46. "day": match.group("to_day"),
  47. }
  48. return res, str_all, True
  49. return res, "识别失败,请重新选择", False
  50. class IdcView(views.MethodView):
  51. @staticmethod
  52. def get():
  53. return render_template("idc_index.html")
  54. @staticmethod
  55. def post():
  56. which = request.form.get("which")
  57. if which is not None:
  58. which = which.lower()
  59. if which not in ["face", "icon"]:
  60. return Response(f"not recognized arg <which>: '{which}'")
  61. pic = request.files.get("picture")
  62. if pic is None:
  63. return Response("empty body")
  64. ext = get_ext_name(pic.filename)
  65. if not is_image_ext(ext):
  66. return Response("文件类型错误")
  67. content = pic.read()
  68. if len(content) > MAX_CONTENT_LENGTH:
  69. return Response("文件过大,请重新选择")
  70. images = rotate(content)
  71. rec = rec_multi(images)
  72. info, msg, sta, idx = {}, "识别失败,请重新选择", False, 0
  73. for idx, (ocr_res, _) in enumerate(rec):
  74. words = [it[1][0].replace(" ", "") for it in ocr_res]
  75. if which == "face":
  76. if not words[0].startswith("姓名"):
  77. continue
  78. info, msg, sta = get_face_info(words)
  79. else:
  80. if not words[0].startswith("中华"):
  81. continue
  82. info, msg, sta = get_icon_info(words)
  83. if sta:
  84. break
  85. if sta:
  86. raw_path = f"static/images/{current_time()}_{rand_str()}.{ext}"
  87. save_img(raw_path, images[idx])
  88. return Response(data=info)
  89. return Response(msg, info)
  90. class IdcHtmlView(views.MethodView):
  91. @staticmethod
  92. def post():
  93. which = request.form.get("which")
  94. if which is not None:
  95. which = which.lower()
  96. if which not in ["face", "icon"]:
  97. return Response(f"not recognized arg <which>: '{which}'")
  98. pic = request.files.get("picture")
  99. if pic is None:
  100. return Response("empty body")
  101. ext = get_ext_name(pic.filename)
  102. if not is_image_ext(ext):
  103. return Response("文件类型错误")
  104. content = pic.read()
  105. if len(content) > MAX_CONTENT_LENGTH:
  106. return Response("文件过大,请重新选择")
  107. images = rotate(content)
  108. rec = rec_multi(images)
  109. info, msg, sta, idx = {}, "识别失败,请重新选择", False, 0
  110. for idx, (ocr_res, _) in enumerate(rec):
  111. words = [it[1][0].replace(" ", "") for it in ocr_res]
  112. if which == "face":
  113. if not words[0].startswith("姓名"):
  114. continue
  115. info, msg, sta = get_face_info(words)
  116. else:
  117. if not words[0].startswith("中华"):
  118. continue
  119. info, msg, sta = get_icon_info(words)
  120. if sta:
  121. break
  122. cut, rnd = current_time(), rand_str()
  123. raw_path = f"static/images/{cut}_{rnd}.{ext}"
  124. rec_path = f"static/images/{cut}_{rnd}_rec.{ext}"
  125. save_img(raw_path, images[idx])
  126. draw_img(rec[idx][1], [{"pos": it[0], "word": it[1][0], "rate": it[1][1]} for it in rec[idx][0]], rec_path)
  127. info["SUCCESS"] = str(sta).upper()
  128. info["MESSAGE"] = msg
  129. return render_template("k-v_result.html", raw=raw_path, rec=rec_path, data=info)
  130. idc.add_url_rule("/", view_func=IdcView.as_view("idc"))
  131. idc.add_url_rule("/html/", view_func=IdcHtmlView.as_view("idc-html"))