diff --git a/20240529RGBtest3/config.py b/20240529RGBtest3/config.py new file mode 100644 index 0000000..9575cd0 --- /dev/null +++ b/20240529RGBtest3/config.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +# @Time : 2024/6/17 下午3:36 +# @Author : TG +# @File : config.py +# @Software: PyCharm + + + +class Config: + #文件相关参数 + #预热参数 + n_rows, n_cols, n_channels = 256, 256, 3 \ No newline at end of file diff --git a/20240529RGBtest3/main.py b/20240529RGBtest3/main.py index 543fdcc..58e4f01 100644 --- a/20240529RGBtest3/main.py +++ b/20240529RGBtest3/main.py @@ -89,50 +89,56 @@ def main(is_debug=False): _ = detector.predict(np.ones((30, 30, 224), dtype=np.uint16)) _, _, _, _, _ =dp.analyze_tomato(cv2.imread(r'D:\project\supermachine--tomato-passion_fruit\20240529RGBtest3\data\tomato_img\bad\71.bmp')) _, _, _, _, _ = dp.analyze_passion_fruit(cv2.imread(r'D:\project\supermachine--tomato-passion_fruit\20240529RGBtest3\data\passion_fruit_img\38.bmp')) - print('初始化完成') + # print('初始化完成') rgb_receive_name = r'\\.\pipe\rgb_receive' rgb_send_name = r'\\.\pipe\rgb_send' spec_receive_name = r'\\.\pipe\spec_receive' 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: + data = pipe.receive_rgb_data(rgb_receive) + cmd, _ = pipe.parse_img(data) + if cmd != 'YR': + break # 当接收到的不是预热命令时,结束预热循环 while True: start_time = time.time() images = [] cmd = None for _ in range(5): - start_time1 = time.time() + # start_time1 = time.time() data = pipe.receive_rgb_data(rgb_receive) - end_time10 = time.time() - print(f'接收一份数据时间:{end_time10 - start_time1}秒') - start_time11 = time.time() + # end_time10 = time.time() + # 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'接收1张图时间:{end_time1 - start_time1}秒') + # end_time1 = time.time() + # print(f'处理一份数据时间:{end_time1 - start_time11}秒') + # print(f'接收1张图时间:{end_time1 - start_time1}秒') # print(cmd, img.shape) # #打印img的数据类型 # print(img.dtype) images.append(img) # print(len(images)) - if cmd not in ['TO', 'PF']: + if cmd not in ['TO', 'PF', 'YR']: logging.error(f'错误指令,指令为{cmd}') continue spec = None if cmd == 'PF': - start_time2 = time.time() + # start_time2 = time.time() spec_data = pipe.receive_spec_data(spec_receive) _, spec = pipe.parse_spec(spec_data) - end_time2 = time.time() - print(f'接收光谱数据时间:{end_time2 - start_time2}秒') + # end_time2 = time.time() + # print(f'接收光谱数据时间:{end_time2 - start_time2}秒') # print(spec.shape) - start_time3 = time.time() + # start_time3 = time.time() response = process_data(cmd, images, spec, dp, pipe, detector) - end_time3 = time.time() - print(f'处理时间:{end_time3 - start_time3}秒') + # end_time3 = time.time() + # print(f'处理时间:{end_time3 - start_time3}秒') end_time = time.time() - print(f'全流程时间:{end_time - start_time}秒') + # print(f'全流程时间:{end_time - start_time}秒') if response: logging.info(f'处理成功,响应为: {response}') else: diff --git a/20240529RGBtest3/utils.py b/20240529RGBtest3/utils.py index 3ea3ee1..97f63ae 100644 --- a/20240529RGBtest3/utils.py +++ b/20240529RGBtest3/utils.py @@ -109,13 +109,15 @@ class Pipe: :return: 指令类型和内容 """ try: - assert len(data) > 2 + assert len(data) > 1 except AssertionError: - logging.error('指令转换失败,长度不足3') + logging.error('指令转换失败,长度不足2') return '', None cmd, data = data[:2], data[2:] cmd = cmd.decode('ascii').strip().upper() - + # 如果收到的是预热指令'YR',直接返回命令和None,不处理图像数据 + if cmd == 'YR': + return cmd, None n_rows, n_cols, img = data[:2], data[2:4], data[4:] try: n_rows, n_cols = [int.from_bytes(x, byteorder='big') for x in [n_rows, n_cols]] @@ -126,7 +128,7 @@ class Pipe: assert n_rows * n_cols * 3 == len(img) # 因为是float32类型 所以长度要乘12 ,如果是uint8则乘3 except AssertionError: - logging.error('图像指令IM转换失败,数据长度错误') + logging.error('图像指令转换失败,数据长度错误') return '', None img = np.frombuffer(img, dtype=np.uint8).reshape(n_rows, n_cols, -1) return cmd, img diff --git a/20240529RGBtest3/test.py b/20240529RGBtest3/xs/image_preprocessing20240615.py similarity index 91% rename from 20240529RGBtest3/test.py rename to 20240529RGBtest3/xs/image_preprocessing20240615.py index fbd8f6c..449dd14 100644 --- a/20240529RGBtest3/test.py +++ b/20240529RGBtest3/xs/image_preprocessing20240615.py @@ -211,7 +211,7 @@ def extract_max_connected_area(image_path, lower_hsv, upper_hsv): def main(): parser = argparse.ArgumentParser(description='Process some integers.') - parser.add_argument('--dir_path', type=str, default=r'D:\project\supermachine--tomato-passion_fruit\20240529RGBtest3\data\broken', + parser.add_argument('--dir_path', type=str, default=r'D:\project\supermachine--tomato-passion_fruit\20240419RGBtest2\data', help='the directory path of images') parser.add_argument('--threshold_s_l', type=int, default=180, help='the threshold for s_l') @@ -227,8 +227,9 @@ def main(): otsu_thresholded = otsu_threshold(s_l) img_fore = bitwise_and_rgb_with_binary(cv2.imread(img_path), otsu_thresholded) img_fore_defect = extract_g_r(img_fore) + cv2.imshow('img_fore_defect1', img_fore_defect) img_fore_defect = threshold_segmentation(img_fore_defect, args.threshold_r_b) - # cv2.imshow('img_fore_defect', img_fore_defect) + cv2.imshow('img_fore_defect2', img_fore_defect) thresholded_s_l = threshold_segmentation(s_l, args.threshold_s_l) new_bin_img = largest_connected_component(thresholded_s_l) # zhongggggg = cv2.bitwise_or(new_bin_img, cv2.imread('defect_mask.bmp', cv2.IMREAD_GRAYSCALE)) @@ -239,11 +240,23 @@ def main(): cv2.imshow('defect', defect) edge, mask = draw_tomato_edge(cv2.imread(img_path), new_bin_img) org_defect = bitwise_and_rgb_with_binary(edge, new_bin_img) + + fore = bitwise_and_rgb_with_binary(cv2.imread(img_path), mask) + + fore_g_r_t = threshold_segmentation(extract_g_r(fore), 20) + #统计白色像素点个数 + print(np.sum(fore_g_r_t == 255)) + print(np.sum(mask == 255)) + print(np.sum(fore_g_r_t == 255)/np.sum(mask == 255)) + fore_g_r_t_ture = bitwise_and_rgb_with_binary(cv2.imread(img_path), fore_g_r_t) - cv2.imwrite('defect_big.bmp', fore_g_r_t_ture) + # cv2.imwrite('defect_big.bmp', fore_g_r_t_ture) + org_defect_new = bitwise_and_rgb_with_binary(edge, new_bin_img) res = cv2.bitwise_or(new_bin_img, fore_g_r_t) + nogreen = bitwise_and_rgb_with_binary(edge, res) + cv2.imshow('nogreen', nogreen) white = find_reflection(img_path) # SVM预测 @@ -260,13 +273,12 @@ def main(): cv2.imshow('fore', fore) cv2.imshow('fore_g_r_t', fore_g_r_t) cv2.imshow('mask', mask) - print('mask', mask.shape) - print('filled', filled_img.shape) - print('largest', new_bin_img.shape) - print('rp', org_defect.shape) cv2.imshow('res', res) - + # lower_hsv = np.array([19, 108, 15]) + # upper_hsv = np.array([118, 198, 134]) + # max_connected_area = extract_max_connected_area(img_path, lower_hsv, upper_hsv) + # cv2.imshow('Max Connected Area', max_connected_area) # 显示原始图像 original_img = cv2.imread(img_path) diff --git a/20240529RGBtest3/通信协议(20240612).md b/20240529RGBtest3/通信协议(20240612).md index a16334f..8b57df0 100644 --- a/20240529RGBtest3/通信协议(20240612).md +++ b/20240529RGBtest3/通信协议(20240612).md @@ -18,6 +18,7 @@ | 指令1 | 指令2 | 指令含义 | | :---: | :---: | :----------: | +| Y | R | 预热数据 | | T | O | 番茄数据 | | P | F | 百香果数据 | | R | E | 返回结果数据 | @@ -25,6 +26,10 @@ ## **数据** +**预热数据包:‘Y’‘R’**,不包含数据字段,仅有**长度字段+指令字段** + + + **RGB图像数据包:'指令1''指令2 '**,`数据1`~`数据i`包含了图像的行数rows(高度)、列数cols(宽度)、以及图像的RGB数据,组合方式为**高度+宽度+RGB数据** $$ i-4=rows \times cols \times 3 @@ -49,8 +54,6 @@ $$ - - **返回结果数据包:'R''E'**,`数据1`~`数据i`包含了糖度值Brix、颜色占比color、直径long、预估重量weight、缺陷个数num、缺陷面积area、结果图像的行数rows(高度)、列数cols(宽度)、以及结果图像的RGB数据,组合方式为**糖度值+颜色占比+直径+预估重量+缺陷个数+缺陷面积+高度+宽度+RGB数据** $$ i-16=rows \times cols \times 3 @@ -61,4 +64,6 @@ $$ | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | --- | --- | --- | --- | | Brix[15:8] | Brix[7:0] | color[7:0] | long[15:8] | long[7:0] | weight[7:0] | num[15:8] | num[7:0] | area[31:24] | area[23:16] | area[15:8] | area[7:0] | rows[15:8] | rows[7:0] | cols[15:8] | cols[7:0] | | ... | | + + **返回空果数据包:‘K’‘O’**,不包含数据字段,仅有**长度字段+指令字段**