diff --git a/.idea/.name b/.idea/.name
index 721f4b2..11a5d8e 100644
--- a/.idea/.name
+++ b/.idea/.name
@@ -1 +1 @@
-04average.ipynb
\ No newline at end of file
+main.py
\ No newline at end of file
diff --git a/.idea/supermachine--tomato-passion_fruit.iml b/.idea/supermachine--tomato-passion_fruit.iml
index 2368678..5ad5e2b 100644
--- a/.idea/supermachine--tomato-passion_fruit.iml
+++ b/.idea/supermachine--tomato-passion_fruit.iml
@@ -4,7 +4,7 @@
-
+
\ No newline at end of file
diff --git a/20240410RGBtest1/super-tomato/defect_big.bmp b/20240410RGBtest1/super-tomato/defect_big.bmp
deleted file mode 100644
index bd07045..0000000
Binary files a/20240410RGBtest1/super-tomato/defect_big.bmp and /dev/null differ
diff --git a/20240410RGBtest1/新建 Markdown.md b/20240410RGBtest1/新建 Markdown.md
deleted file mode 100644
index e69de29..0000000
diff --git a/20240419RGBtest2/root_dir.py b/20240419RGBtest2/root_dir.py
new file mode 100644
index 0000000..754b950
--- /dev/null
+++ b/20240419RGBtest2/root_dir.py
@@ -0,0 +1,5 @@
+
+import pathlib
+
+file_path = pathlib.Path(__file__)
+ROOT_DIR = file_path.parent
\ No newline at end of file
diff --git a/20240529RGBtest3/main.py b/20240529RGBtest3/main.py
index 2a254a9..0a922f0 100644
--- a/20240529RGBtest3/main.py
+++ b/20240529RGBtest3/main.py
@@ -18,7 +18,7 @@ import time
import os
from root_dir import ROOT_DIR
import logging
-from utils import create_pipes, receive_rgb_data, send_data, receive_spec_data, analyze_tomato, analyze_passion_fruit
+from utils import parse_protocol, create_pipes, receive_rgb_data, send_data, receive_spec_data, analyze_tomato, analyze_passion_fruit
from collections import deque
import time
import io
@@ -26,7 +26,7 @@ from PIL import Image
import threading
import queue
-def process_data(img: any) -> tuple:
+def process_data(cmd: str, img: any) -> tuple:
"""
处理指令
@@ -65,56 +65,55 @@ def main(is_debug=False):
while True:
long_axis_list = []
short_axis_list = []
- defect_num_sum = 0
- total_defect_area_sum = 0
+ max_defect_num = 0 # 初始化最大缺陷数量为0
+ max_total_defect_area = 0 # 初始化最大总像素数为0
rp = None
- start_time = time.time()
+ # start_time = time.time()
for i in range(5):
# start_time = time.time()
-
- img_data = receive_rgb_data(rgb_receive)
- image = Image.open(io.BytesIO(img_data))
- img = np.array(image)
- print(img.shape)
-
+ data = receive_rgb_data(rgb_receive)
+ cmd, img = parse_protocol(data)
+ # print(img.shape)
# end_time = time.time()
# elapsed_time = end_time - start_time
# print(f'接收时间:{elapsed_time}秒')
- long_axis, short_axis, number_defects, total_pixels, rp = process_data(img=img)
+ long_axis, short_axis, number_defects, total_pixels, rp = process_data(cmd=cmd, img=img)
# print(long_axis, short_axis, number_defects, type(total_pixels), rp.shape)
if i <= 2:
long_axis_list.append(long_axis)
short_axis_list.append(short_axis)
+ # 更新最大缺陷数量和最大总像素数
+ max_defect_num = max(max_defect_num, number_defects)
+ max_total_defect_area = max(max_total_defect_area, total_pixels)
if i == 1:
rp_result = rp
- defect_num_sum += number_defects
- total_defect_area_sum += total_pixels
-
long_axis = round(sum(long_axis_list) / 3)
short_axis = round(sum(short_axis_list) / 3)
# print(type(long_axis), type(short_axis), type(defect_num_sum), type(total_defect_area_sum), type(rp_result))
spec_data = receive_spec_data(spec_receive)
- print(f'光谱数据接收长度:', len(spec_data))
+ cmd, spec_data = parse_protocol(spec_data)
+
+ # print(f'光谱数据接收长度:', len(spec_data))
response = send_data(pipe_send=rgb_send, long_axis=long_axis, short_axis=short_axis,
- defect_num=defect_num_sum, total_defect_area=total_defect_area_sum, rp=rp_result)
+ defect_num=max_defect_num, total_defect_area=max_total_defect_area, rp=rp_result)
- end_time = time.time()
- elapsed_time = (end_time - start_time) * 1000
- print(f'总时间:{elapsed_time}毫秒')
-
- print(long_axis, short_axis, defect_num_sum, total_defect_area_sum, rp_result.shape)
+ # end_time = time.time()
+ # elapsed_time = (end_time - start_time) * 1000
+ # print(f'总时间:{elapsed_time}毫秒')
+ #
+ # print(long_axis, short_axis, defect_num_sum, total_defect_area_sum, rp_result.shape)
diff --git a/20240529RGBtest3/root_dir.py b/20240529RGBtest3/root_dir.py
new file mode 100644
index 0000000..754b950
--- /dev/null
+++ b/20240529RGBtest3/root_dir.py
@@ -0,0 +1,5 @@
+
+import pathlib
+
+file_path = pathlib.Path(__file__)
+ROOT_DIR = file_path.parent
\ No newline at end of file
diff --git a/20240529RGBtest3/utils.py b/20240529RGBtest3/utils.py
index 8eb80dd..8c82559 100644
--- a/20240529RGBtest3/utils.py
+++ b/20240529RGBtest3/utils.py
@@ -19,36 +19,85 @@ import select
import msvcrt
from classifer import Tomato, Passion_fruit
+
def receive_rgb_data(pipe):
try:
- # 读取图片数据
+ # 读取图片数据长度
len_img = win32file.ReadFile(pipe, 4, None)
data_size = int.from_bytes(len_img[1], byteorder='big')
- result, img_data = win32file.ReadFile(pipe, data_size, None)
- return img_data
+ # 读取实际图片数据
+ result, data = win32file.ReadFile(pipe, data_size, None)
+ # 检查读取操作是否成功
+ if result != 0:
+ print(f"读取失败,错误代码: {result}")
+ return None
+ # 返回成功读取的数据
+ return data
except Exception as e:
print(f"数据接收失败,错误原因: {e}")
return None
-
def receive_spec_data(pipe):
try:
- # 读取图片数据长度
+ # 读取光谱数据长度
len_spec = win32file.ReadFile(pipe, 4, None)
- if len_spec is None:
- # 未能读取到数据长度,返回"0"
- return "0"
data_size = int.from_bytes(len_spec[1], byteorder='big')
- if data_size == 0:
- # 接收到空数据,返回"0"
- return "0"
-
- # 读取图片数据
+ # 读取光谱数据
result, spec_data = win32file.ReadFile(pipe, data_size, None)
+ # 检查读取操作是否成功
+ if result != 0:
+ print(f"读取失败,错误代码: {result}")
+ return None
+ # 返回成功读取的数据
return spec_data
except Exception as e:
print(f"数据接收失败,错误原因: {e}")
- return '0'
+ return None
+
+def parse_protocol(data: bytes) -> (str, any):
+ """
+ 指令转换.
+
+ :param data:接收到的报文
+ :return: 指令类型和内容
+ """
+ try:
+ assert len(data) > 2
+ except AssertionError:
+ logging.error('指令转换失败,长度不足3')
+ return '', None
+ cmd, data = data[:2], data[2:]
+ cmd = cmd.decode('ascii').strip().upper()
+ if cmd == 'TO':
+ 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]]
+ except Exception as e:
+ logging.error(f'长宽转换失败, 错误代码{e}, 报文大小: n_rows:{n_rows}, n_cols: {n_cols}')
+ return '', None
+ try:
+ assert n_rows * n_cols * 3 == len(img)
+ # 因为是float32类型 所以长度要乘12 ,如果是uint8则乘3
+ except AssertionError:
+ logging.error('图像指令IM转换失败,数据长度错误')
+ return '', None
+ img = np.frombuffer(img, dtype=np.uint8).reshape((n_rows, n_cols, -1))
+ return cmd, img
+ elif cmd == 'PF':
+ n_rows, n_cols, n_bands, spec = data[:2], data[2:4], data[4:6], data[6:]
+ try:
+ n_rows, n_cols, n_bands = [int.from_bytes(x, byteorder='big') for x in [n_rows, n_cols, n_bands]]
+ except Exception as e:
+ logging.error(f'长宽转换失败, 错误代码{e}, 报文大小: n_rows:{n_rows}, n_cols: {n_cols}, n_bands: {n_bands}')
+ return '', None
+ try:
+ assert n_rows * n_cols * n_bands * 4 == len(spec)
+
+ except AssertionError:
+ logging.error('图像指令转换失败,数据长度错误')
+ return '', None
+ spec = np.frombuffer(spec, dtype=np.uint16).reshape(n_cols, n_rows, -1)
+ return cmd, spec
def create_pipes(rgb_receive_name, rgb_send_name, spec_receive_name):
while True: