From f1cf707f6e857b0a4dfe98a8342743577e83da43 Mon Sep 17 00:00:00 2001 From: FEIJINTI <83849113+FEIJINTI@users.noreply.github.com> Date: Wed, 12 Oct 2022 17:04:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86float32=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E5=9B=BE=E7=89=87=E4=B9=B1=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classifer.py | 3 ++- socket_detector.py | 3 ++- utils.py | 14 +++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/classifer.py b/classifer.py index c8c9c18..c637591 100644 --- a/classifer.py +++ b/classifer.py @@ -13,6 +13,7 @@ from sklearn.cluster import KMeans from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score +from sklearn.tree import DecisionTreeClassifier from scipy.stats import binom import matplotlib.pyplot as plt import time @@ -22,7 +23,7 @@ sys.path.append(os.getcwd()) from root_dir import ROOT_DIR import utils -FEATURE_INDEX = [0, 1, 2, 3, 4, 5] +FEATURE_INDEX = [1, 2] class WoodClass(object): diff --git a/socket_detector.py b/socket_detector.py index cab92f7..2c8ba27 100644 --- a/socket_detector.py +++ b/socket_detector.py @@ -48,7 +48,7 @@ def main(is_debug=False): console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING) logging.basicConfig(format='%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s - %(message)s', handlers=[file_handler, console_handler], level=logging.DEBUG) - dual_sock = DualSock(connect_ip='192.168.2.221') + dual_sock = DualSock(connect_ip='127.0.0.1') while not dual_sock.status: dual_sock.reconnect() @@ -56,6 +56,7 @@ def main(is_debug=False): # model_path = os.path.join(ROOT_DIR, r"models\model_2022-09-28_13-15.p") detector = WoodClass(w=4096, h=1200, n=3000, debug_mode=False) detector.load(path=model_path) + _ = detector.predict(np.random.randint(1, 254, (1200, 4096, 3), dtype=np.uint8)) while True: pack, next_pack = receive_sock(dual_sock) if pack == b"": diff --git a/utils.py b/utils.py index 1976e00..456bb82 100644 --- a/utils.py +++ b/utils.py @@ -201,7 +201,7 @@ bytes, bytes): try: temp += recv_sock.receive(data_len) except Exception as e: - logging.error(f'接收报文内容失败, 错误代码: \n{e},\n报文内容\n{temp}') + logging.error(f'接收报文内容失败, 错误代码: \n{e}') return b'', b'' data, next_pack = temp[:data_len], temp[data_len:] recv_sock.set_prepack(next_pack) @@ -216,7 +216,7 @@ bytes, bytes): try: temp += recv_sock.receive(1) except Exception as e: - logging.error(f'接收报文校验失败, 错误代码: \n{e}, 报文如下: \n{temp}') + logging.error(f'接收报文校验失败, 错误代码: \n{e}') return b'', b'' if temp == b'\xff\xff\xbb': return data, next_pack @@ -244,15 +244,15 @@ def parse_protocol(data: bytes) -> (str, any): try: n_rows, n_cols = [int.from_bytes(x, byteorder='big') for x in [n_rows, n_cols]] except Exception as e: - logging.error(f'长宽转换失败, 错误代码{e}, 报文内容: n_rows:{n_rows}, n_cols: {n_cols}') + logging.error(f'长宽转换失败, 错误代码{e}, 报文大小: n_rows:{n_rows}, n_cols: {n_cols}') return '', None try: - assert n_rows * n_cols * 12 == len(img) + assert n_rows * n_cols * 3 == len(img) # 因为是float32类型 所以长度要乘12 ,如果是uint8则乘3 except AssertionError: logging.error('图像指令IM转换失败,数据长度错误') return '', None - img = np.frombuffer(img, dtype=np.float32).reshape((n_rows, n_cols, -1)) + img = np.frombuffer(img, dtype=np.uint8).reshape((n_rows, n_cols, -1)) return cmd, img elif cmd == 'TR': data = data.decode('ascii') @@ -313,11 +313,11 @@ def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bo cmd_type = cmd_type.strip().upper() if cmd_type == 'IM': if result == 0: - msg = b'Q' + msg = b'S' elif result == 1: msg = b'Z' elif result == 2: - msg = b'S' + msg = b'Q' elif cmd_type == 'TR': msg = b'A' elif cmd_type == 'MD':