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)}')