Преглед на файлове

identity card recognize v1.1: with fail notice.

Tinger преди 2 години
родител
ревизия
5b7bbff97f
променени са 3 файла, в които са добавени 49 реда и са изтрити 16 реда
  1. 15 7
      Readme.md
  2. 14 8
      blues/idc.py
  3. 20 1
      utils/util.py

+ 15 - 7
Readme.md

@@ -7,24 +7,32 @@
 **接口说明:[内网](http://192.168.1.6:10393/shareDoc?issue=b0f1852e84df1504ccf22ea3d170a0b9) 、
 [外网](https://console-docs.apipost.cn/preview/4c3153799dc211ad/be9b128db3073b6e)**
 
+**参数中: * 为必传参数**
+
+**返回数据:{success: "bool:是否成功", message: "text:说明信息", data: "json:数据主体"}**
+
 ---
 
 + $host/  
-  + 请求方法:浏览器访问
+  + 请求方法:GET、浏览器直接访问
   + 说明:在线测试演示首页
 
-+ $host/ocr-raw-api/
++ $host/com/
+  + 通用OCR接口
   + 请求方法:POST
   + 参数列表:
-    + picture: 待识别的图片文件
-  + 返回数据:JSON
+    + *picture: 待识别的图片文件
+    + type: 操作类型(raw:原生OCR内容,filter:仅返回识别文字列表【默认】,html:结果以HTML展示)
+  + 返回数据:JSON(API)、HTML(演示)
   + 详见接口说明:[内网](http://192.168.1.6:10393/shareDoc?issue=b575bc0bb3eed8ad21ea192ac75f9df2) 、
     [外网](https://console-docs.apipost.cn/preview/29f73aa84047b12a/b294d4eccd3ec68e)
 
-+ $host/ocr-filter/
++ $host/idc/
+  + 大陆居民身份证识别接口
   + 请求方法:POST
   + 参数列表:
-    + picture: 待识别的图片文件
-  + 返回数据:JSON
+    + *picture: 待识别的图片文件
+    + *which: 身份证正反面(face:人像面,icon:国徽面)
+  + 返回数据:JSON(API)、HTML(演示)
   + 详见接口说明:[内网](http://192.168.1.6:10393/shareDoc?issue=67be17c547640a13c30db5a3fcff1dea) 、
     [外网](https://console-docs.apipost.cn/preview/91517399fb42f789/47a77024dd17b335)

+ 14 - 8
blues/idc.py

@@ -72,19 +72,24 @@ class IdcView(views.MethodView):
             fp.write(content)
             fp.close()
 
-        ocr_res, _ = recognize(content)
-        words = [it[1][0] for it in ocr_res]
-
         which = request.form.get("which")
         if which is not None:
             which = which.lower()
-        if which == "face":
-            return Response(data=get_face_info(words))
-        elif which == "icon":
-            return Response(data=get_icon_info(words))
-        else:
+        if which not in ["face", "icon"]:
             return Response(f"not recognized arg <which>: '{which}'")
 
+        ocr_res, _ = recognize(content)
+        words = [it[1][0] for it in ocr_res]
+        if which == "face":
+            info = get_face_info(words)
+            if json_all(info):
+                return Response(data=info)
+            return Response("识别失败,请重新选择", info)
+        info = get_icon_info(words)
+        if json_all(info):
+            return Response(data=info)
+        return Response("识别失败,请重新选择", info)
+
 
 class IdcHtmlView(views.MethodView):
     @staticmethod
@@ -118,6 +123,7 @@ class IdcHtmlView(views.MethodView):
             info = get_face_info(words)
         else:
             info = get_icon_info(words)
+        info["SUCCESS"] = str(json_all(info)).upper()
         return render_template("k-v_result.html", raw=raw_path, rec=rec_path, data=info)
 
 

+ 20 - 1
utils/util.py

@@ -7,7 +7,8 @@ from time import localtime, strftime
 from paddleocr.tools.infer.utility import draw_box_txt_fine
 
 __all__ = [
-    "Args", "Response", "rand_str", "current_time", "get_ext_name", "is_image_ext", "recognize", "draw_img"
+    "Args", "Response", "rand_str", "current_time", "get_ext_name", "is_image_ext", "recognize", "draw_img",
+    "json_all"
 ]
 
 __StrBase = "qwertyuioplkjhgfdsazxcvbnm1234567890ZXCVBNMLKJHGFDSAQWERTYUIOP"
@@ -113,3 +114,21 @@ def draw_img(shape: "tuple", data: "list[dict]", path: "str", drop: "float" = 0.
         img = cv2.bitwise_and(img, text)  # noqa
 
     cv2.imwrite(path, np.array(img))  # noqa
+
+
+def json_all(data: "dict or list") -> "bool":
+    if isinstance(data, list):
+        for item in data:
+            if isinstance(item, str) and not item:
+                return False
+            elif isinstance(item, (list, dict)) and not json_all(item):
+                return False
+        return True
+    elif isinstance(data, dict):
+        for value in data.values():
+            if isinstance(value, str) and not value:
+                return False
+            elif isinstance(value, (list, dict)) and not json_all(value):
+                return False
+        return True
+    raise TypeError(f"except node type are: [list, dict], but got a {type(data)} instead.")