From ebf41ab0fe8fd16731dedaae1814bfdd37c03c40 Mon Sep 17 00:00:00 2001 From: TG <905865530@qq.com> Date: Wed, 19 Jun 2024 21:39:17 +0800 Subject: [PATCH] =?UTF-8?q?test:qt=5Ftest=E6=94=B9=E4=B8=BA=E5=BE=AA?= =?UTF-8?q?=E7=8E=AF=E5=8F=91=E9=80=8120=E7=BB=84=E6=95=B0=E6=8D=AE(5?= =?UTF-8?q?=E5=BC=A0RGB=E5=9B=BE+1=E4=BB=BDspec)=EF=BC=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=85=A8=E6=B5=81=E7=A8=8B=E7=94=A8=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20240529RGBtest3/main.py | 38 +++++++++------- 20240529RGBtest3/qt_test.py | 86 ++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/20240529RGBtest3/main.py b/20240529RGBtest3/main.py index 38e1e4b..1670ac3 100644 --- a/20240529RGBtest3/main.py +++ b/20240529RGBtest3/main.py @@ -86,7 +86,7 @@ def main(is_debug=False): detector = Spec_predict(ROOT_DIR/'models'/'passion_fruit_2.joblib') classifier = ImageClassifier(ROOT_DIR/'models'/'resnet18_0616.pth', ROOT_DIR/'models'/'class_indices.json') dp = Data_processing() - + print('系统初始化中...') _ = detector.predict(np.ones((30, 30, 224), dtype=np.uint16)) _ = classifier.predict(np.ones((224, 224, 3), dtype=np.uint8)) # _, _, _, _, _ =dp.analyze_tomato(cv2.imread(r'D:\project\supermachine--tomato-passion_fruit\20240529RGBtest3\data\tomato_img\bad\71.bmp')) @@ -99,29 +99,30 @@ def main(is_debug=False): pipe = Pipe(rgb_receive_name, rgb_send_name, spec_receive_name) rgb_receive, rgb_send, spec_receive = pipe.create_pipes(rgb_receive_name, rgb_send_name, spec_receive_name) # 预热循环,只处理cmd为'YR'的数据 - # while True: - # start_time00 = time.time() - # data = pipe.receive_rgb_data(rgb_receive) - # cmd, _ = pipe.parse_img(data) - # end_time00 = time.time() - # print(f'接收预热数据时间:{end_time00 - start_time00}秒') - # if cmd == 'YR': - # break # 当接收到的不是预热命令时,结束预热循环 + while True: + start_time00 = time.time() + data = pipe.receive_rgb_data(rgb_receive) + cmd, _ = pipe.parse_img(data) + end_time00 = time.time() + print(f'接收预热数据时间:{(end_time00 - start_time00) * 1000}毫秒') + if cmd == 'YR': + break # 当接收到的不是预热命令时,结束预热循环 + q = 1 while True: start_time = time.time() images = [] cmd = None - for _ in range(5): + for i in range(5): start_time1 = time.time() data = pipe.receive_rgb_data(rgb_receive) end_time10 = time.time() - # print(f'接收一份数据时间:{end_time10 - start_time1}秒') + # print(f'接收第{q}组第{i}份RGB数据时间:{(end_time10 - start_time1) * 1000}毫秒') start_time11 = time.time() cmd, img = pipe.parse_img(data) end_time1 = time.time() - # print(f'处理一份数据时间:{end_time1 - start_time11}秒') - # print(f'接收一张图时间:{end_time1 - start_time1}秒') + # print(f'解析第{q}组第{i}份RGB数据时间:{(end_time1 - start_time11) * 1000}毫秒') + print(f'接收第{q}组第{i}张RGB图时间:{(end_time1 - start_time1) * 1000}毫秒') # 使用分类器进行预测 # prediction = classifier.predict(img) @@ -144,15 +145,19 @@ def main(is_debug=False): if cmd == 'PF': start_time2 = time.time() spec_data = pipe.receive_spec_data(spec_receive) + print(f'接收第{q}组光谱数据长度:{len(spec_data)}') _, spec = pipe.parse_spec(spec_data) + print(f'处理第{q}组光谱数据长度:{len(spec)}') + print(spec.shape) + print(f'解析第{q}组光谱数据时间:{(time.time() - start_time2) * 1000}毫秒') end_time2 = time.time() - # print(f'接收光谱数据时间:{end_time2 - start_time2}秒') + print(f'接收第{q}组光谱数据时间:{(end_time2 - start_time2) * 1000}毫秒') start_time3 = time.time() if images: # 确保images不为空 response = process_data(cmd, images, spec, dp, pipe, detector) end_time3 = time.time() - # print(f'处理时间:{end_time3 - start_time3}秒') + print(f'第{q}组处理时间:{(end_time3 - start_time3) * 1000}毫秒') if response: logging.info(f'处理成功,响应为: {response}') else: @@ -161,7 +166,8 @@ def main(is_debug=False): logging.error("没有有效的图像进行处理") end_time = time.time() - print(f'全流程时间:{end_time - start_time}秒') + print(f'第{q}组全流程时间:{(end_time - start_time) * 1000}毫秒') + q += 1 if __name__ == '__main__': diff --git a/20240529RGBtest3/qt_test.py b/20240529RGBtest3/qt_test.py index 3ec7e8f..ce1ac79 100644 --- a/20240529RGBtest3/qt_test.py +++ b/20240529RGBtest3/qt_test.py @@ -74,54 +74,54 @@ class MainWindow(QMainWindow): spec_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.raw')][:1] self.send_YR() - - for image_path in rgb_files: - img = cv2.imread(image_path, cv2.IMREAD_COLOR) - img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - img = np.asarray(img, dtype=np.uint8) + for _ in range(20): + for image_path in rgb_files: + img = cv2.imread(image_path, cv2.IMREAD_COLOR) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + img = np.asarray(img, dtype=np.uint8) - try: - # win32file.WriteFile(self.rgb_send, len(img_data).to_bytes(4, byteorder='big')) - height = img.shape[0] - width = img.shape[1] - height = height.to_bytes(2, byteorder='big') - width = width.to_bytes(2, byteorder='big') - img_data = img.tobytes() - length = (len(img_data) + 6).to_bytes(4, byteorder='big') - # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 - cmd = 'TO' - data_send = length + cmd.upper().encode('ascii') + height + width + img_data - win32file.WriteFile(self.rgb_send, data_send) - print(f'发送的图像数据长度: {len(data_send)}') - except Exception as e: - print(f"数据发送失败. 错误原因: {e}") + try: + # win32file.WriteFile(self.rgb_send, len(img_data).to_bytes(4, byteorder='big')) + height = img.shape[0] + width = img.shape[1] + height = height.to_bytes(2, byteorder='big') + width = width.to_bytes(2, byteorder='big') + img_data = img.tobytes() + length = (len(img_data) + 6).to_bytes(4, byteorder='big') + # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 + cmd = 'PF' + data_send = length + cmd.upper().encode('ascii') + height + width + img_data + win32file.WriteFile(self.rgb_send, data_send) + print(f'发送的图像数据长度: {len(data_send)}') + except Exception as e: + print(f"数据发送失败. 错误原因: {e}") - if spec_files: - spec_file = spec_files[0] - with open(spec_file, 'rb') as f: - spec_data = f.read() + if spec_files: + spec_file = spec_files[0] + with open(spec_file, 'rb') as f: + spec_data = f.read() - try: - # win32file.WriteFile(self.spec_send, len(spec_data).to_bytes(4, byteorder='big')) - # print(f"发送的光谱数据长度: {len(spec_data)}") - heigth = 30 - weight = 30 - bands = 224 - heigth = heigth.to_bytes(2, byteorder='big') - weight = weight.to_bytes(2, byteorder='big') - bands = bands.to_bytes(2, byteorder='big') - length = (len(spec_data)+8).to_bytes(4, byteorder='big') - # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 - cmd = 'TO' - data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data - win32file.WriteFile(self.spec_send, data_send) - print(f'发送的光谱数据长度: {len(data_send)}') - print(f'spec长度: {len(spec_data)}') - except Exception as e: - print(f"数据发送失败. 错误原因: {e}") + try: + # win32file.WriteFile(self.spec_send, len(spec_data).to_bytes(4, byteorder='big')) + # print(f"发送的光谱数据长度: {len(spec_data)}") + heigth = 30 + weight = 30 + bands = 224 + heigth = heigth.to_bytes(2, byteorder='big') + weight = weight.to_bytes(2, byteorder='big') + bands = bands.to_bytes(2, byteorder='big') + length = (len(spec_data)+8).to_bytes(4, byteorder='big') + # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 + cmd = 'PF' + data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data + win32file.WriteFile(self.spec_send, data_send) + print(f'发送的光谱数据长度: {len(data_send)}') + print(f'spec长度: {len(spec_data)}') + except Exception as e: + print(f"数据发送失败. 错误原因: {e}") - self.receive_result() + self.receive_result() def send_YR(self): '''