From 0b291735e9a8e16afb1521c228f5580e378e8849 Mon Sep 17 00:00:00 2001 From: FEIJINTI <83849113+FEIJINTI@users.noreply.github.com> Date: Tue, 28 Mar 2023 16:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E5=A4=8D=E6=9D=82?= =?UTF-8?q?=E9=80=9A=E8=AE=AF=E5=92=8C=E7=AE=80=E5=8D=95=E9=80=9A=E8=AE=AF?= =?UTF-8?q?=E7=9A=84=E4=BB=A3=E7=A0=81=EF=BC=88=E6=9C=AA=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E7=AD=89=E7=AB=AF=E5=A4=A7=E4=BD=AC=E8=A1=A5?= =?UTF-8?q?=E5=85=85qt=E7=9A=84=E6=B5=8B=E8=AF=95=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classifer.py | 21 ++++++++++++++++----- socket_detector.py | 9 +++++++-- utils.py | 24 +++++++++++++++++++----- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/classifer.py b/classifer.py index f6f98db..036681c 100644 --- a/classifer.py +++ b/classifer.py @@ -508,6 +508,7 @@ class WoodClass(object): :return: """ x_data, y_data, img_names = self.get_train_data(data_dir, plot_2d=plot_2d) + x_data = x_data[:, [0, 1, 2]] kmeans = KMeans(n_clusters=3, random_state=0).fit(x_data) # 获取聚类后的数据 @@ -541,11 +542,20 @@ class WoodClass(object): :return: 调整后的数据 ''' sorted_idx = sorted(range(len(img_names)), key=lambda x: int(img_names[x][3:-4])) - x_data = x_data[sorted_idx, [0, 1, 2]] + x_data = x_data[sorted_idx] y_data = y_data[sorted_idx] labels = labels[sorted_idx] - img_names = img_names[sorted_idx] - return x_data, y_data, labels, img_names + img_names = [img_names[i] for i in sorted_idx] + mapping = {0: 's', 1: 'z', 2: 'q'} + y_data = [mapping[i] for i in y_data] + labels = [mapping[i] for i in labels] + data = [] + for i in range(len(img_names)): + x_tmp = np.round(x_data[i, :]).astype(np.int) + x_tmp = np.char.zfill(x_tmp.astype(np.str), 3) + x_tmp = "".join(x_tmp) + data.append(x_tmp + y_data[i] + labels[i]) + return data if __name__ == '__main__': @@ -559,9 +569,10 @@ if __name__ == '__main__': # wood.correct() # wood.load() # fit 相应的文件夹 - settings.model_path = str(ROOT_DIR / 'models' / wood.fit_pictures(data_path=data_path)) + # settings.model_path = str(ROOT_DIR / 'models' / wood.fit_pictures(data_path=data_path)) - wood.get_kmeans_data(data_path, plot_2d=True) + x_data, y_data, labels, img_names = wood.get_kmeans_data(data_path, plot_2d=False) + send_data = wood.data_adjustments(x_data, y_data, labels, img_names) # 测试单张图片的预测,predict_mode=True表示导入本地的model, False为现场训练的 pic = cv2.imread(r"data/318/dark/rgb89.png") diff --git a/socket_detector.py b/socket_detector.py index 69c8e1a..deab92d 100644 --- a/socket_detector.py +++ b/socket_detector.py @@ -45,8 +45,13 @@ def process_cmd(cmd: str, data: any, connected_sock: socket.socket, detector: Wo settings.model_path = data detector.load(path=settings.model_path) response = simple_sock(connected_sock, cmd_type=cmd) - elif cmd == 'DT': - pass + elif cmd == 'KM': + x_data, y_data, labels, img_names = detector.get_kmeans_data(data, plot_2d=False) + result = detector.data_adjustments(x_data, y_data, labels, img_names) + result = ','.join([str(x) for x in result]) + response = simple_sock(connected_sock, cmd_type=cmd, result=result) + + else: logging.error(f'错误指令,指令为{cmd}') response = False diff --git a/utils.py b/utils.py index 456bb82..47f5378 100644 --- a/utils.py +++ b/utils.py @@ -278,7 +278,7 @@ def ack_sock(send_sock: socket.socket, cmd_type: str) -> bool: return True -def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool: +def done_sock(send_sock: socket.socket, cmd_type: str, result) -> bool: ''' 发送任务完成指令 :param cmd_type:指令类型 @@ -292,8 +292,17 @@ def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool logging.error('结果在这种指令里很没必要') result = b'\xff' elif cmd_type == 'IM': - result = result.to_bytes(1, "big") - msg = b'\xaa\x00\x00\x00\x05' + (' D' + cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb' + if result == 0: + result = b'S' + elif result == 1: + result = b'Z' + elif result == 2: + result = b'Q' + elif cmd_type == 'KM': + result.encode('ascii') + length = len(result) + 4 + length = length.to_bytes(4, byteorder='big') + msg = b'\xaa' +length + (' D' + cmd_type).upper().encode('ascii') + result + b'\xff\xff\xbb' try: send_sock.send(msg) except Exception as e: @@ -302,7 +311,7 @@ def done_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool return True -def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bool: +def simple_sock(send_sock: socket.socket, cmd_type: str, result) -> bool: ''' 发送任务完成指令 :param cmd_type:指令类型 @@ -322,6 +331,12 @@ def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bo msg = b'A' elif cmd_type == 'MD': msg = b'D' + elif cmd_type == 'KM': + msg = b'K' + result = result.encode('ascii') + result = b',' + result + length = len(result) + msg = msg + length.to_bytes(1, 'big') + result try: send_sock.send(msg) except Exception as e: @@ -331,7 +346,6 @@ def simple_sock(send_sock: socket.socket, cmd_type: str, result: int = '') -> bo - if __name__ == '__main__': log = Logger(is_to_file=True) log.log("nihao")