소스 검색

部署接口

fangjiasheng 2 년 전
부모
커밋
deb6a35bb5

+ 2 - 2
border_recognize/bdr_interface.py

@@ -96,5 +96,5 @@ def test_bdr_model(from_remote=True):
 
 
 if __name__ == "__main__":
-    # app.run(host='127.0.0.1', port=17000, debug=False)
-    test_bdr_model()
+    app.run(host='127.0.0.1', port=17055, debug=False)
+    # test_bdr_model()

+ 3 - 4
chinese_detect/chd_interface.py

@@ -74,7 +74,7 @@ class ChdModels:
 
 
 def test_chd_model(from_remote=True):
-    paths = glob("D:/Project/captcha/data/test/phrase_5.jpg")
+    paths = glob("D:/Project/captcha/data/test/phrase_2.jpg")
     for file_path in paths:
         img_np = cv2.imread(file_path)
         file_bytes = np2bytes(img_np)
@@ -82,8 +82,7 @@ def test_chd_model(from_remote=True):
 
         if from_remote:
             file_json = {"data": file_base64}
-            # _url = "http://192.168.2.102:17000/ocr"
-            _url = "http://127.0.0.1:17000/chd"
+            _url = "http://127.0.0.1:17054/chd"
             result = json.loads(request_post(_url, file_json))
             if result.get("success"):
                 out_boxes = result.get("data")
@@ -97,5 +96,5 @@ def test_chd_model(from_remote=True):
 
 
 if __name__ == "__main__":
-    app.run(host='127.0.0.1', port=17000, debug=False)
+    app.run(host='127.0.0.1', port=17056, debug=False)
     # test_chd_model()

+ 7 - 1
chinese_detect/inference_yolo_char.py

@@ -92,7 +92,13 @@ def detect(image_np, model=None, sess=None, draw=False, is_tips=0):
             right = min(image_pil.size[0], np.floor(right + 0.5).astype('int32'))
             temp_boxes.append([(left, top), (right, bottom)])
         out_boxes = temp_boxes
-    out_boxes = [[int(x[0][0]), int(x[0][1]), int(x[1][0]), int(x[1][1])] for x in out_boxes]
+
+    # 加大box
+    threshold = 2
+    out_boxes = [[max(int(x[0][0]-threshold), 0),
+                  max(int(x[0][1]-threshold), 0),
+                  min(int(x[1][0]+threshold), w),
+                  min(int(x[1][1]+threshold), h)] for x in out_boxes]
     out_boxes.sort(key=lambda x: (x[0], x[1], x[2], x[3]))
     return image_np, out_boxes, out_classes
 

+ 1 - 1
chinese_detect/model_260.py

@@ -186,7 +186,7 @@ def tiny_yolo_body(inputs, num_anchors, num_classes):
             DarknetConv2D_BN_Leaky(256, (3,3)),
             DarknetConv2D(num_anchors*(num_classes+5), (1,1)))([x2,x1])
     model = Model(inputs, [y1,y2])
-    model.summary(120)
+    # model.summary(120)
     return model
 
 

+ 2 - 2
chinese_order/cho_interface.py

@@ -86,5 +86,5 @@ def test_cho_model(from_remote=True):
 
 
 if __name__ == "__main__":
-    # app.run(host='127.0.0.1', port=17000, debug=False)
-    test_cho_model()
+    app.run(host='127.0.0.1', port=17058, debug=False)
+    # test_cho_model()

+ 7 - 5
chinese_recognize/chr_interface.py

@@ -77,10 +77,12 @@ class ChrModels:
 
 
 def test_chr_model(from_remote=True):
-    paths = glob("D:/Project/captcha/data/test/char_*.jpg")
+    paths = glob("D:/Project/captcha/data/test/phrase_2.jpg")
     str_list = []
     for file_path in paths:
-        img_np = cv2.imread(file_path)
+        img_np = cv2.imread(file_path)[67:117, 35:83, :]
+        cv2.imshow("img_np", img_np)
+        cv2.waitKey(0)
         h, w = img_np.shape[:2]
         file_bytes = np2bytes(img_np)
         file_base64 = base64.b64encode(file_bytes)
@@ -89,7 +91,7 @@ def test_chr_model(from_remote=True):
 
     if from_remote:
         file_json = {"data": json.dumps(str_list)}
-        _url = "http://127.0.0.1:17000/chr"
+        _url = "http://127.0.0.1:17055/chr"
         result = json.loads(request_post(_url, file_json))
         if result.get("success"):
             char_list = result.get("data")
@@ -100,5 +102,5 @@ def test_chr_model(from_remote=True):
 
 
 if __name__ == "__main__":
-    # app.run(host='127.0.0.1', port=17000, debug=False)
-    test_chr_model()
+    app.run(host='127.0.0.1', port=17057, debug=False)
+    # test_chr_model()

+ 25 - 1
chinese_recognize/inference_char.py

@@ -45,6 +45,30 @@ def recognize(image_np_list, model=None, sess=None):
     return char_list
 
 
+def recognize_no_sess(image_np_list, model=None):
+    if model is None:
+        model = cnn_net(input_shape=image_shape)
+        model.load_weights(model_path)
+
+    if len(image_np_list) > 30:
+        raise
+
+    # 准备批次数据
+    X = np.zeros((len(image_np_list), image_shape[0], image_shape[1], image_shape[2]))
+    for i in range(len(image_np_list)):
+        img = pil_resize(image_np_list[i], image_shape[0], image_shape[1])
+        img = img / 255.
+        X[i] = img
+
+    pred = model.predict(X)
+    index_list = np.argmax(pred, axis=1).tolist()
+
+    char_list = []
+    for index in index_list:
+        char_list.append(char_str[index])
+    return char_list
+
+
 if __name__ == "__main__":
-    _path = "D:/Project/captcha/data/test/char_2.jpg"
+    _path = "D:/Project/captcha/data/test/char_6.jpg"
     print(recognize([cv2.imread(_path)]))

+ 75 - 28
interface/captcha_interface.py

@@ -6,7 +6,7 @@ import sys
 import time
 import traceback
 from glob import glob
-
+import numpy as np
 import cv2
 
 sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
@@ -16,12 +16,12 @@ from utils import request_post, bytes2np, np2bytes
 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 
 
-bdr_url = "http://127.0.0.1:17053/bdr"
-chd_url = "http://127.0.0.1:17054/chd"
-chr_url = "http://127.0.0.1:17055/chr"
-cho_url = "http://127.0.0.1:17056/cho"
-pzd_url = "http://127.0.0.1:17057/pzd"
-captcha_url = "http://127.0.0.1:17058/captcha"
+bdr_url = "http://127.0.0.1:17055/bdr"
+chd_url = "http://127.0.0.1:17056/chd"
+chr_url = "http://127.0.0.1:17057/chr"
+cho_url = "http://127.0.0.1:17058/cho"
+pzd_url = "http://127.0.0.1:17059/pzd"
+captcha_url = "http://127.0.0.1:17054/captcha"
 
 
 # 接口配置
@@ -36,10 +36,11 @@ def captcha():
         # 接收网络数据
         if not request.form:
             logging.info("captcha no data!")
-            return json.dumps({"success": False})
+            return json.dumps({"success": False, "cost": time.time()-start_time})
         base64_data = request.form.get("base64pic")
         base64_data2 = request.form.get("base64pic2")
-        code = request.form.get("code")
+        code = int(request.form.get("code"))
+        logging.info("code " + str(code))
         logging.info("captcha_interface get data time" + str(time.time()-start_time))
 
         if base64_data2 is None:
@@ -47,10 +48,12 @@ def captcha():
         else:
             result = get_captcha_result([base64_data, base64_data2], code)
 
-        return json.dumps({"predict": result, "success": True})
+        if result is None:
+            return json.dumps({"success": False, "cost": time.time()-start_time})
+        return json.dumps({"predict": result, "success": True, "cost": time.time()-start_time})
     except:
         traceback.print_exc()
-        return json.dumps({"success": False})
+        return json.dumps({"success": False, "cost": time.time()-start_time})
     finally:
         logging.info("captcha interface finish time " + str(time.time()-start_time))
 
@@ -72,42 +75,48 @@ def get_captcha_result(base64_list, code):
         result = json.loads(request_post(pzd_url, prepare_data(base64_list[0], "pzd")))
         if result.get("success"):
             predict = result.get("data")
+            logging.info("code " + str(code) + "  pzd  " + "predict " + str(predict))
             return predict
 
     elif code == 2:
         result = json.loads(request_post(bdr_url, prepare_data(base64_list[0], "bdr")))
         if result.get("success"):
             predict = result.get("data")
+            logging.info("code " + str(code) + "  bdr  " + "predict " + str(predict))
             return predict
 
     elif code == 3:
         # detect tips
-        result = json.loads(request_post(chd_url, prepare_data(base64_list[0], "chd2")))
+        result = json.loads(request_post(chd_url, prepare_data(base64_list[1], "chd2")))
         if result.get("success"):
             box_list_tips = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chd2  " + "predict " + str(box_list_tips))
         # recognize tips
-        result = json.loads(request_post(chr_url, prepare_data([base64_list[0], box_list_tips], "chr")))
+        result = json.loads(request_post(chr_url, prepare_data([base64_list[1], box_list_tips], "chr")))
         if result.get("success"):
             char_list_tips = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chr  " + "predict " + str(char_list_tips))
         if len(char_list_tips) != len(box_list_tips):
             return None
 
         # detect
-        result = json.loads(request_post(chd_url, prepare_data(base64_list[1], "chd")))
+        result = json.loads(request_post(chd_url, prepare_data(base64_list[0], "chd")))
         if result.get("success"):
             box_list = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chd  " + "predict " + str(box_list))
         # recognize
-        result = json.loads(request_post(chr_url, prepare_data([base64_list[1], box_list], "chr")))
+        result = json.loads(request_post(chr_url, prepare_data([base64_list[0], box_list], "chr")))
         if result.get("success"):
             char_list = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chr  " + "predict " + str(char_list))
         if len(char_list) != len(box_list):
             return None
         for c in char_list_tips:
@@ -125,6 +134,7 @@ def get_captcha_result(base64_list, code):
             box_list = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chd  " + "predict " + str(box_list))
 
         # recognize
         result = json.loads(request_post(chr_url, prepare_data([base64_list[0], box_list], "chr")))
@@ -132,6 +142,7 @@ def get_captcha_result(base64_list, code):
             char_list = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  chr  " + "predict " + str(char_list))
         if len(char_list) != len(box_list):
             return None
 
@@ -141,6 +152,7 @@ def get_captcha_result(base64_list, code):
             ordered_char_list = result.get("data")
         else:
             return None
+        logging.info("code " + str(code) + "  cho  " + "predict " + str(ordered_char_list))
 
         _dict = {char_list[i]: box_list[i] for i in range(len(box_list))}
         predict = [_dict.get(x) for x in ordered_char_list]
@@ -177,26 +189,61 @@ def prepare_data(data, _type):
 
 
 def test_interface(from_remote=True):
-    paths = glob("D:/Project/captcha/data/test/phrase_5.jpg")
+    captcha_url = "http://192.168.2.103:17054/captcha"
+    paths = glob("../dev/click_captcha/data/test/yolo_1.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
 
         if from_remote:
-            file_json = {"base64pic": file_base64}
-            result = json.loads(request_post(captcha_url, file_json))
-            if result.get("success"):
-                out_boxes = result.get("data")
-                print("out_boxes", out_boxes)
-                for box in out_boxes:
-                    cv2.rectangle(img_np, (box[0], box[1]), (box[2], box[3]), (0, 0, 255))
-                cv2.imshow("img_np", img_np)
+            if code in [1, 4]:
+                file_json = {"base64pic": file_base64, "code": code}
+                result = json.loads(request_post(captcha_url, file_json))
+                print("result", result)
+                if result.get("success"):
+                    out_boxes = result.get("predict")
+                    print("out_boxes", out_boxes)
+                    for box in out_boxes:
+                        cv2.rectangle(img_np, (box[0], box[1]), (box[2], box[3]), (0, 0, 255))
+                    cv2.imshow("img_np", img_np)
+                    cv2.waitKey(0)
+                else:
+                    print("failed!")
+            elif code in [2]:
+                file_json = {"base64pic": file_base64, "code": code}
+                result = json.loads(request_post(captcha_url, file_json))
+                print("result", result)
+                if result.get("success"):
+                    w = int(result.get("predict"))
+                    print("w", w)
+                    img_new = np.concatenate([img_np[:, w:, :], img_np[:, :w, :]], axis=1)
+                    cv2.imshow("img_np", img_np)
+                    cv2.imshow("img_new", img_new)
+                    cv2.waitKey(0)
+                else:
+                    print("failed!")
+            elif code in [3]:
+                file_base64_2 = cv2.imread("../dev/click_captcha/data/test/yolo_3.jpg")
+                cv2.imshow("file_base64_2", file_base64_2)
                 cv2.waitKey(0)
-            else:
-                print("failed!")
+                file_base64_2 = np2bytes(file_base64_2)
+                file_base64_2 = base64.b64encode(file_base64_2)
+                file_json = {"base64pic": file_base64, "base64pic2": file_base64_2, "code": code}
+                result = json.loads(request_post(captcha_url, file_json))
+                print("result", result)
+                if result.get("success"):
+                    out_boxes = result.get("predict")
+                    print("out_boxes", out_boxes)
+                    for box in out_boxes:
+                        cv2.rectangle(img_np, (box[0], box[1]), (box[2], box[3]), (0, 0, 255))
+                    cv2.imshow("img_np", img_np)
+                    cv2.waitKey(0)
+                else:
+                    print("failed!")
 
 
 if __name__ == "__main__":
-    app.run(host='127.0.0.1', port=17058, debug=False)
-    # test_interface()
+    # app.run(host='127.0.0.1', port=17054, debug=False)
+    test_interface()

+ 5 - 0
interface/start.sh

@@ -0,0 +1,5 @@
+nohup /home/user/anaconda3/envs/tf2/bin/gunicorn -w 2 -t 300 -b 0.0.0.0:17054 --chdir /data/python/fangjiasheng/VerificationCode/interface/ captcha_interface:app &
+nohup /home/user/anaconda3/envs/tf2/bin/gunicorn -w 2 -t 300 -b 0.0.0.0:17056 --chdir /data/python/fangjiasheng/VerificationCode/chinese_detect/ chd_interface:app &
+nohup /home/user/anaconda3/envs/tf2/bin/gunicorn -w 2 -t 300 -b 0.0.0.0:17057 --chdir /data/python/fangjiasheng/VerificationCode/chinese_recognize/ chr_interface:app &
+nohup /home/user/anaconda3/envs/tf2/bin/gunicorn -w 2 -t 300 -b 0.0.0.0:17058 --chdir /data/python/fangjiasheng/VerificationCode/chinese_order/ cho_interface:app &
+nohup /home/user/anaconda3/envs/tf2/bin/gunicorn -w 2 -t 300 -b 0.0.0.0:17059 --chdir /data/python/fangjiasheng/VerificationCode/puzzle_detect/ pzd_interface:app &

+ 5 - 0
interface/stop.sh

@@ -0,0 +1,5 @@
+kill -9 $(lsof -i:17054|sed -n '2,$p'|awk '{print $2}'|tr '\n' ' ')
+kill -9 $(lsof -i:17056|sed -n '2,$p'|awk '{print $2}'|tr '\n' ' ')
+kill -9 $(lsof -i:17057|sed -n '2,$p'|awk '{print $2}'|tr '\n' ' ')
+kill -9 $(lsof -i:17058|sed -n '2,$p'|awk '{print $2}'|tr '\n' ' ')
+kill -9 $(lsof -i:17059|sed -n '2,$p'|awk '{print $2}'|tr '\n' ' ')

+ 1 - 1
puzzle_detect/inference_yolo_puzzle.py

@@ -101,7 +101,7 @@ def get_tiny_inference_model(anchors, num_classes, weights_path='models/tiny_yol
                                                'num_classes': num_classes}
                                     )([model_body.output, need_shape])
     model = Model([model_body.input, need_shape], [boxes, scores, classes])
-    model.summary(120)
+    # model.summary(120)
     return model
 
 

+ 1 - 1
puzzle_detect/model_260.py

@@ -186,7 +186,7 @@ def tiny_yolo_body(inputs, num_anchors, num_classes):
             DarknetConv2D_BN_Leaky(256, (3,3)),
             DarknetConv2D(num_anchors*(num_classes+5), (1,1)))([x2,x1])
     model = Model(inputs, [y1,y2])
-    model.summary(120)
+    # model.summary(120)
     return model
 
 

+ 2 - 2
puzzle_detect/pzd_interface.py

@@ -95,5 +95,5 @@ def test_pzd_model(from_remote=True):
 
 
 if __name__ == "__main__":
-    # app.run(host='127.0.0.1', port=17000, debug=False)
-    test_pzd_model()
+    app.run(host='127.0.0.1', port=17059, debug=False)
+    # test_pzd_model()