From 86cf933d243840f6f78eaf5b59915567509158bb Mon Sep 17 00:00:00 2001 From: TG <905865530@qq.com> Date: Wed, 19 Jun 2024 08:54:01 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E5=B0=8F=E9=97=AE=E9=A2=98=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=95=AA=E8=8C=84=E8=83=8C=E6=99=AF=E5=88=A4?= =?UTF-8?q?=E6=96=AD=EF=BC=8C=E5=A6=82=E6=9E=9C=E7=9B=B4=E5=BE=84=E5=B0=8F?= =?UTF-8?q?=E4=BA=8E200=EF=BC=8C=E9=82=A3=E4=B9=88=E5=85=A8=E8=B5=8B0.?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=EF=BC=88100,100,3=EF=BC=89=E7=A9=BA=E5=9B=BE?= =?UTF-8?q?=EF=BC=9B=E7=94=B1=E4=BA=8E=E8=80=80=E5=93=A5&=E6=97=BB?= =?UTF-8?q?=E5=93=A5=E6=9A=82=E6=97=B6=E6=B2=A1=E5=86=99=E7=AE=A1=E9=81=93?= =?UTF-8?q?=E9=A2=84=E7=83=AD=EF=BC=8C=E9=A2=84=E7=83=AD=E9=83=A8=E5=88=86?= =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=8E=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20240529RGBtest3/classifer.py | 11 +++++++++-- 20240529RGBtest3/main.py | 30 +++++++++++++++--------------- 20240529RGBtest3/qt_test.py | 4 ++-- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/20240529RGBtest3/classifer.py b/20240529RGBtest3/classifer.py index cf9a9ee..10e5cab 100644 --- a/20240529RGBtest3/classifer.py +++ b/20240529RGBtest3/classifer.py @@ -359,7 +359,7 @@ class Data_processing: def contour_process(self, image_array): # 检查图像是否为空或全黑 if image_array is None or image_array.size == 0 or np.all(image_array == 0): - # print("输入的图像为空或全黑,返回一个全黑图像。") + logging.error("输入的图像为空或全黑,返回一个全黑图像。") return np.zeros_like(image_array) if image_array is not None else np.zeros((100, 100), dtype=np.uint8) # 应用中值滤波 image_filtered = cv2.medianBlur(image_array, 5) @@ -488,11 +488,18 @@ class Data_processing: # 将处理后的图像转换为 RGB 格式 rp = cv2.cvtColor(org_defect, cv2.COLOR_BGR2RGB) diameter = (long_axis + short_axis) / 2 + if diameter < 200: + diameter = 0 + green_percentage = 0 + number_defects = 0 + total_pixels = 0 + rp = cv2.cvtColor(np.ones((100, 100, 3), dtype=np.uint8), cv2.COLOR_BGR2RGB) + return diameter, green_percentage, number_defects, total_pixels, rp return diameter, green_percentage, number_defects, total_pixels, rp def analyze_passion_fruit(self, img, hue_value=37, hue_delta=10, value_target=25, value_delta=10): if img is None: - print("Error: 无图像数据.") + logging.error("Error: 无图像数据.") return None # 创建PassionFruit类的实例 diff --git a/20240529RGBtest3/main.py b/20240529RGBtest3/main.py index abd9599..38e1e4b 100644 --- a/20240529RGBtest3/main.py +++ b/20240529RGBtest3/main.py @@ -99,14 +99,14 @@ 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}秒') + # if cmd == 'YR': + # break # 当接收到的不是预热命令时,结束预热循环 while True: start_time = time.time() images = [] @@ -115,13 +115,13 @@ def main(is_debug=False): start_time1 = time.time() data = pipe.receive_rgb_data(rgb_receive) end_time10 = time.time() - print(f'接收一份数据时间:{end_time10 - start_time1}秒') + # print(f'接收一份数据时间:{end_time10 - start_time1}秒') 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'处理一份数据时间:{end_time1 - start_time11}秒') + # print(f'接收一张图时间:{end_time1 - start_time1}秒') # 使用分类器进行预测 # prediction = classifier.predict(img) @@ -133,7 +133,7 @@ def main(is_debug=False): else: response = pipe.send_data(cmd='KO', brix=0, diameter=0, green_percentage=0, weigth=0, defect_num=0, total_defect_area=0, rp=np.zeros((100, 100, 3), dtype=np.uint8)) - print("图像中无果,跳过此图像") + logging.info("图像中无果,跳过此图像") continue if cmd not in ['TO', 'PF', 'YR', 'KO']: @@ -146,19 +146,19 @@ def main(is_debug=False): spec_data = pipe.receive_spec_data(spec_receive) _, spec = pipe.parse_spec(spec_data) end_time2 = time.time() - print(f'接收光谱数据时间:{end_time2 - start_time2}秒') + # print(f'接收光谱数据时间:{end_time2 - start_time2}秒') 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'处理时间:{end_time3 - start_time3}秒') if response: logging.info(f'处理成功,响应为: {response}') else: logging.error('处理失败') else: - print("没有有效的图像进行处理") + logging.error("没有有效的图像进行处理") end_time = time.time() print(f'全流程时间:{end_time - start_time}秒') diff --git a/20240529RGBtest3/qt_test.py b/20240529RGBtest3/qt_test.py index b2207f3..3ec7e8f 100644 --- a/20240529RGBtest3/qt_test.py +++ b/20240529RGBtest3/qt_test.py @@ -90,7 +90,7 @@ class MainWindow(QMainWindow): img_data = img.tobytes() length = (len(img_data) + 6).to_bytes(4, byteorder='big') # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 - 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)}') @@ -113,7 +113,7 @@ class MainWindow(QMainWindow): bands = bands.to_bytes(2, byteorder='big') length = (len(spec_data)+8).to_bytes(4, byteorder='big') # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 - 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)}')