fangjiasheng 2 tahun lalu
induk
melakukan
b766f9d727

+ 2 - 2
border_recognize/bdr_interface.py

@@ -14,7 +14,7 @@ import tensorflow as tf
 from flask import Flask, request
 from border_recognize.model import u_net_drag
 from border_recognize.inference_drag import recognize
-from utils import pil_resize, np2bytes, request_post, bytes2np
+from utils import pil_resize, np2bytes, request_post, bytes2np, base64_decode
 
 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 tf.compat.v1.disable_eager_execution()
@@ -48,7 +48,7 @@ def bdr():
             globals().update({"global_bdr_model": bdr_model})
 
         # 数据转换
-        data = base64.b64decode(data)
+        data = base64_decode(data)
         image_np = bytes2np(data)
         # 预测
         w = recognize(image_np, bdr_model, sess)

+ 2 - 2
chinese_detect/chd_interface.py

@@ -13,7 +13,7 @@ sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
 import tensorflow as tf
 from flask import Flask, request
 from chinese_detect.inference_yolo_char import get_tiny_inference_model, detect
-from utils import pil_resize, np2bytes, request_post, bytes2np, get_anchors, get_classes, get_colors
+from utils import pil_resize, np2bytes, request_post, bytes2np, get_anchors, get_classes, get_colors, base64_decode
 
 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 tf.compat.v1.disable_eager_execution()
@@ -50,7 +50,7 @@ def chd():
             globals().update({"global_chd_model": chd_model})
 
         # 数据转换
-        data = base64.b64decode(data)
+        data = base64_decode(data)
         image_np = bytes2np(data)
         # 预测
         _, out_boxes, out_classes = detect(image_np, chd_model, sess, is_tips=is_tips)

+ 2 - 2
chinese_recognize/chr_interface.py

@@ -14,7 +14,7 @@ import tensorflow as tf
 from flask import Flask, request
 from chinese_recognize.model import cnn_net
 from chinese_recognize.inference_char import recognize
-from utils import pil_resize, np2bytes, request_post, bytes2np
+from utils import pil_resize, np2bytes, request_post, bytes2np, base64_decode
 
 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 tf.compat.v1.disable_eager_execution()
@@ -52,7 +52,7 @@ def _chr():
         image_np_list = []
         for _str in str_list:
             b64 = _str.encode("utf-8")
-            image_np = bytes2np(base64.b64decode(b64))
+            image_np = bytes2np(base64_decode(b64))
             image_np_list.append(image_np)
 
         # 预测

+ 2 - 2
interface/captcha_interface.py

@@ -190,12 +190,12 @@ def prepare_data(data, _type):
 
 def test_interface(from_remote=True):
     captcha_url = "http://192.168.2.103:17054/captcha"
-    paths = glob("../dev/click_captcha/data/test/yolo_1.jpg")
+    paths = glob("../dev/click_captcha/data/test/yolo_19.jpg")
     for file_path in paths:
         img_np = cv2.imread(file_path)
         file_bytes = np2bytes(img_np)
         file_base64 = base64.b64encode(file_bytes)
-        code = 3
+        code = 1
 
         if from_remote:
             if code in [1, 4]:

+ 36 - 0
interface/test.py

@@ -0,0 +1,36 @@
+import base64
+import io
+
+import numpy as np
+import cv2
+from PIL import Image
+
+from utils import base64_decode
+
+with open(r"C:\Users\Administrator\Desktop\base64_2.txt", "r") as f:
+    _str = f.read()
+
+_str, _str_old = base64_decode(_str, is_test=True)
+print(len(_str_old)%4)
+
+with open(r"C:\Users\Administrator\Desktop\base64_wrong.txt", "w") as f:
+    f.write(_str_old)
+
+image_np = cv2.imdecode(np.frombuffer(_str, np.uint8), cv2.IMREAD_COLOR)
+cv2.imshow("i", image_np)
+cv2.waitKey(0)
+
+
+
+# with open(r"C:\Users\Administrator\Desktop\2.png", "rb") as f:
+#     _str = f.read()
+# _str = base64.b64encode(_str)
+# with open(r"C:\Users\Administrator\Desktop\base64_right.txt", "wb") as f:
+#     f.write(_str)
+#
+# _str = base64.b64decode(_str)
+# image_np = cv2.imdecode(np.frombuffer(_str, np.uint8), cv2.IMREAD_COLOR)
+# cv2.imshow("i", image_np)
+# cv2.waitKey(0)
+# with open(r"C:\Users\Administrator\Desktop\2.jpg", "wb") as f:
+#     f.write(_str)

+ 3 - 2
puzzle_detect/pzd_interface.py

@@ -11,7 +11,7 @@ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
 sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
 import tensorflow as tf
 from flask import Flask, request
-from utils import np2bytes, request_post, bytes2np, get_anchors, get_classes, get_colors
+from utils import np2bytes, request_post, bytes2np, get_anchors, get_classes, get_colors, base64_decode
 from puzzle_detect.inference_yolo_puzzle import get_tiny_inference_model, detect
 
 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
@@ -48,7 +48,8 @@ def pzd():
             globals().update({"global_pzd_model": pzd_model})
 
         # 数据转换
-        data = base64.b64decode(data)
+        # print("base64_data", data)
+        data = base64_decode(data)
         image_np = bytes2np(data)
         # 预测
         _, out_boxes, out_classes = detect(image_np, pzd_model, sess)

+ 13 - 1
utils.py

@@ -1,4 +1,6 @@
+import base64
 import colorsys
+import re
 import socket
 import traceback
 from enum import Enum
@@ -273,4 +275,14 @@ def bytes2np(_b):
         return None
     except:
         traceback.print_exc()
-        return None
+        return None
+
+
+def base64_decode(b64, is_test=False):
+    # b64 = re.sub(r"\r\n", "\n", b64)
+    missing_padding = 4 - len(b64) % 4
+    if missing_padding:
+        b64 += '=' * missing_padding
+    if is_test:
+        return base64.b64decode(b64), b64
+    return base64.b64decode(b64)