test:qt_test改为循环发送20组数据(5张RGB图+1份spec),测试全流程用时

This commit is contained in:
TG 2024-06-19 21:39:17 +08:00
parent ba583ba08d
commit ebf41ab0fe
2 changed files with 65 additions and 59 deletions

View File

@ -86,7 +86,7 @@ def main(is_debug=False):
detector = Spec_predict(ROOT_DIR/'models'/'passion_fruit_2.joblib') detector = Spec_predict(ROOT_DIR/'models'/'passion_fruit_2.joblib')
classifier = ImageClassifier(ROOT_DIR/'models'/'resnet18_0616.pth', ROOT_DIR/'models'/'class_indices.json') classifier = ImageClassifier(ROOT_DIR/'models'/'resnet18_0616.pth', ROOT_DIR/'models'/'class_indices.json')
dp = Data_processing() dp = Data_processing()
print('系统初始化中...')
_ = detector.predict(np.ones((30, 30, 224), dtype=np.uint16)) _ = detector.predict(np.ones((30, 30, 224), dtype=np.uint16))
_ = classifier.predict(np.ones((224, 224, 3), dtype=np.uint8)) _ = 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')) # _, _, _, _, _ =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) 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) rgb_receive, rgb_send, spec_receive = pipe.create_pipes(rgb_receive_name, rgb_send_name, spec_receive_name)
# 预热循环只处理cmd为'YR'的数据 # 预热循环只处理cmd为'YR'的数据
# while True: while True:
# start_time00 = time.time() start_time00 = time.time()
# data = pipe.receive_rgb_data(rgb_receive) data = pipe.receive_rgb_data(rgb_receive)
# cmd, _ = pipe.parse_img(data) cmd, _ = pipe.parse_img(data)
# end_time00 = time.time() end_time00 = time.time()
# print(f'接收预热数据时间:{end_time00 - start_time00}秒') print(f'接收预热数据时间:{(end_time00 - start_time00) * 1000}毫秒')
# if cmd == 'YR': if cmd == 'YR':
# break # 当接收到的不是预热命令时,结束预热循环 break # 当接收到的不是预热命令时,结束预热循环
q = 1
while True: while True:
start_time = time.time() start_time = time.time()
images = [] images = []
cmd = None cmd = None
for _ in range(5): for i in range(5):
start_time1 = time.time() start_time1 = time.time()
data = pipe.receive_rgb_data(rgb_receive) data = pipe.receive_rgb_data(rgb_receive)
end_time10 = time.time() end_time10 = time.time()
# print(f'接收一份数据时间:{end_time10 - start_time1}秒') # print(f'接收第{q}组第{i}份RGB数据时间{(end_time10 - start_time1) * 1000}毫秒')
start_time11 = time.time() start_time11 = time.time()
cmd, img = pipe.parse_img(data) cmd, img = pipe.parse_img(data)
end_time1 = time.time() end_time1 = time.time()
# print(f'处理一份数据时间:{end_time1 - start_time11}秒') # print(f'解析第{q}组第{i}份RGB数据时间{(end_time1 - start_time11) * 1000}毫秒')
# print(f'接收一张图时间:{end_time1 - start_time1}秒') print(f'接收第{q}组第{i}张RGB图时间{(end_time1 - start_time1) * 1000}毫秒')
# 使用分类器进行预测 # 使用分类器进行预测
# prediction = classifier.predict(img) # prediction = classifier.predict(img)
@ -144,15 +145,19 @@ def main(is_debug=False):
if cmd == 'PF': if cmd == 'PF':
start_time2 = time.time() start_time2 = time.time()
spec_data = pipe.receive_spec_data(spec_receive) spec_data = pipe.receive_spec_data(spec_receive)
print(f'接收第{q}组光谱数据长度:{len(spec_data)}')
_, spec = pipe.parse_spec(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() end_time2 = time.time()
# print(f'接收光谱数据时间:{end_time2 - start_time2}秒') print(f'接收第{q}组光谱数据时间:{(end_time2 - start_time2) * 1000}毫秒')
start_time3 = time.time() start_time3 = time.time()
if images: # 确保images不为空 if images: # 确保images不为空
response = process_data(cmd, images, spec, dp, pipe, detector) response = process_data(cmd, images, spec, dp, pipe, detector)
end_time3 = time.time() end_time3 = time.time()
# print(f'处理时间:{end_time3 - start_time3}秒') print(f'{q}组处理时间:{(end_time3 - start_time3) * 1000}毫秒')
if response: if response:
logging.info(f'处理成功,响应为: {response}') logging.info(f'处理成功,响应为: {response}')
else: else:
@ -161,7 +166,8 @@ def main(is_debug=False):
logging.error("没有有效的图像进行处理") logging.error("没有有效的图像进行处理")
end_time = time.time() end_time = time.time()
print(f'全流程时间:{end_time - start_time}') print(f'{q}组全流程时间:{(end_time - start_time) * 1000}毫秒')
q += 1
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -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] spec_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.raw')][:1]
self.send_YR() self.send_YR()
for _ in range(20):
for image_path in rgb_files: for image_path in rgb_files:
img = cv2.imread(image_path, cv2.IMREAD_COLOR) img = cv2.imread(image_path, cv2.IMREAD_COLOR)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.asarray(img, dtype=np.uint8) img = np.asarray(img, dtype=np.uint8)
try: try:
# win32file.WriteFile(self.rgb_send, len(img_data).to_bytes(4, byteorder='big')) # win32file.WriteFile(self.rgb_send, len(img_data).to_bytes(4, byteorder='big'))
height = img.shape[0] height = img.shape[0]
width = img.shape[1] width = img.shape[1]
height = height.to_bytes(2, byteorder='big') height = height.to_bytes(2, byteorder='big')
width = width.to_bytes(2, byteorder='big') width = width.to_bytes(2, byteorder='big')
img_data = img.tobytes() img_data = img.tobytes()
length = (len(img_data) + 6).to_bytes(4, byteorder='big') length = (len(img_data) + 6).to_bytes(4, byteorder='big')
# cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据 # cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据
cmd = 'TO' cmd = 'PF'
data_send = length + cmd.upper().encode('ascii') + height + width + img_data data_send = length + cmd.upper().encode('ascii') + height + width + img_data
win32file.WriteFile(self.rgb_send, data_send) win32file.WriteFile(self.rgb_send, data_send)
print(f'发送的图像数据长度: {len(data_send)}') print(f'发送的图像数据长度: {len(data_send)}')
except Exception as e: except Exception as e:
print(f"数据发送失败. 错误原因: {e}") print(f"数据发送失败. 错误原因: {e}")
if spec_files: if spec_files:
spec_file = spec_files[0] spec_file = spec_files[0]
with open(spec_file, 'rb') as f: with open(spec_file, 'rb') as f:
spec_data = f.read() spec_data = f.read()
try: try:
# win32file.WriteFile(self.spec_send, len(spec_data).to_bytes(4, byteorder='big')) # win32file.WriteFile(self.spec_send, len(spec_data).to_bytes(4, byteorder='big'))
# print(f"发送的光谱数据长度: {len(spec_data)}") # print(f"发送的光谱数据长度: {len(spec_data)}")
heigth = 30 heigth = 30
weight = 30 weight = 30
bands = 224 bands = 224
heigth = heigth.to_bytes(2, byteorder='big') heigth = heigth.to_bytes(2, byteorder='big')
weight = weight.to_bytes(2, byteorder='big') weight = weight.to_bytes(2, byteorder='big')
bands = bands.to_bytes(2, byteorder='big') bands = bands.to_bytes(2, byteorder='big')
length = (len(spec_data)+8).to_bytes(4, byteorder='big') length = (len(spec_data)+8).to_bytes(4, byteorder='big')
# cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据 # cmd = 'TO'测试番茄数据cmd = 'PF':测试百香果数据
cmd = 'TO' cmd = 'PF'
data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data
win32file.WriteFile(self.spec_send, data_send) win32file.WriteFile(self.spec_send, data_send)
print(f'发送的光谱数据长度: {len(data_send)}') print(f'发送的光谱数据长度: {len(data_send)}')
print(f'spec长度: {len(spec_data)}') print(f'spec长度: {len(spec_data)}')
except Exception as e: except Exception as e:
print(f"数据发送失败. 错误原因: {e}") print(f"数据发送失败. 错误原因: {e}")
self.receive_result() self.receive_result()
def send_YR(self): def send_YR(self):
''' '''