From 5d6d53c95dd5086dd73f4ebf25838b4f3590848e Mon Sep 17 00:00:00 2001 From: TG <905865530@qq.com> Date: Tue, 18 Jun 2024 10:43:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E6=94=B9qt=5Ftest=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E7=A1=AE=E4=BF=9D=E5=8F=91=E9=80=81=E7=9A=84?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=BARGB=E6=A0=BC=E5=BC=8F=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A=E5=8F=8A=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 20240529RGBtest3/qt_test.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/20240529RGBtest3/qt_test.py b/20240529RGBtest3/qt_test.py index e9755e5..b2207f3 100644 --- a/20240529RGBtest3/qt_test.py +++ b/20240529RGBtest3/qt_test.py @@ -8,11 +8,8 @@ import sys import os from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QLabel, QVBoxLayout, QWidget from PyQt5.QtGui import QPixmap, QImage -import win32pipe import win32file -import struct from PIL import Image -import io import numpy as np import cv2 @@ -68,6 +65,11 @@ class MainWindow(QMainWindow): ) def send_image_group(self, image_dir): + ''' + 发送图像数据 + :param image_dir: bmp和raw文件所在文件夹 + :return: + ''' rgb_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith(('.bmp'))][:5] spec_files = [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.raw')][:1] @@ -75,6 +77,7 @@ class MainWindow(QMainWindow): for image_path in rgb_files: img = cv2.imread(image_path, cv2.IMREAD_COLOR) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = np.asarray(img, dtype=np.uint8) @@ -86,6 +89,7 @@ class MainWindow(QMainWindow): width = width.to_bytes(2, byteorder='big') img_data = img.tobytes() length = (len(img_data) + 6).to_bytes(4, byteorder='big') + # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 cmd = 'PF' data_send = length + cmd.upper().encode('ascii') + height + width + img_data win32file.WriteFile(self.rgb_send, data_send) @@ -108,6 +112,7 @@ class MainWindow(QMainWindow): weight = weight.to_bytes(2, byteorder='big') bands = bands.to_bytes(2, byteorder='big') length = (len(spec_data)+8).to_bytes(4, byteorder='big') + # cmd = 'TO':测试番茄数据;cmd = 'PF':测试百香果数据 cmd = 'PF' data_send = length + cmd.upper().encode('ascii') + heigth + weight + bands + spec_data win32file.WriteFile(self.spec_send, data_send) @@ -119,7 +124,10 @@ class MainWindow(QMainWindow): self.receive_result() def send_YR(self): - + ''' + 发送预热指令 + :return: + ''' length = 2 length = length.to_bytes(4, byteorder='big') cmd = 'YR' @@ -133,15 +141,15 @@ class MainWindow(QMainWindow): def receive_result(self): try: # 读取结果数据 - # 读取4字节的数据长度信息,并将其转换为整数 + # 读取4个字节的数据长度信息,并将其转换为整数 data_length = int.from_bytes(win32file.ReadFile(self.rgb_receive, 4)[1], byteorder='big') print(f"应该接收到的数据长度: {data_length}") # 根据读取到的数据长度,读取对应长度的数据 data = win32file.ReadFile(self.rgb_receive, data_length)[1] - print(f"接收到的数据长度: {len(data)}") + print(f"实际接收到的数据长度: {len(data)}") # 解析数据 cmd_result = data[:2].decode('ascii').strip().upper() - brix = int.from_bytes(data[2:4], byteorder='big') + brix = (int.from_bytes(data[2:4], byteorder='big')) / 1000 green_percentage = int.from_bytes(data[4:5], byteorder='big') diameter = int.from_bytes(data[5:7], byteorder='big') weight = int.from_bytes(data[7:8], byteorder='big') @@ -150,9 +158,9 @@ class MainWindow(QMainWindow): heigth = int.from_bytes(data[14:16], byteorder='big') width = int.from_bytes(data[16:18], byteorder='big') rp = data[18:] - print(heigth, width) img = np.frombuffer(rp, dtype=np.uint8).reshape(heigth, width, -1) - print(f"接收到的结果数据: {cmd_result}, {brix}, {green_percentage}, {diameter}, {weight}, {defect_num}, {total_defect_area}, {img.shape}") + print(f"指令:{cmd_result}, 糖度值:{brix}, 绿色占比:{green_percentage}, 直径:{diameter}, " + f"预估重量:{weight}, 缺陷个数:{defect_num}, 缺陷面积:{total_defect_area}, 结果图的尺寸:{img.shape}") # 显示结果图像 @@ -179,6 +187,10 @@ if __name__ == "__main__": 4. 打开文件对话框 5. 进入Qt事件循环 ''' + #运行main.py后,运行qt_test.py + #运行qt_test.py后,选择文件夹,自动读取文件夹下的bmp和raw文件,发送到main.py + #main.py接收到数据后,返回结果数据,qt_test.py接收到结果数据,显示图片 + #为确保测试正确,测试文件夹下的文件数量应该为5个bmp文件和1个raw文件 app = QApplication(sys.argv) main_window = MainWindow() main_window.show()