fix:修复了一些小问题

This commit is contained in:
TG 2024-06-16 18:26:31 +08:00
parent d9d0152c5f
commit 3e620df4c6
4 changed files with 140 additions and 98 deletions

View File

@ -12,11 +12,12 @@ import logging
from utils import Pipe from utils import Pipe
import numpy as np import numpy as np
pipe = Pipe()
dp = Data_processing()
rgb_receive_name = r'\\.\pipe\rgb_receive' rgb_receive_name = r'\\.\pipe\rgb_receive'
rgb_send_name = r'\\.\pipe\rgb_send' rgb_send_name = r'\\.\pipe\rgb_send'
spec_receive_name = r'\\.\pipe\spec_receive' spec_receive_name = r'\\.\pipe\spec_receive'
pipe = Pipe(rgb_receive_name, rgb_send_name, spec_receive_name)
dp = Data_processing()
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)
def process_data(cmd: str, images: list, spec: any, detector: Spec_predict) -> bool: def process_data(cmd: str, images: list, spec: any, detector: Spec_predict) -> bool:
@ -72,7 +73,7 @@ def process_data(cmd: str, images: list, spec: any, detector: Spec_predict) -> b
return response return response
def main(is_debug=False): def main(is_debug=False):
file_handler = logging.FileHandler(os.path.join(ROOT_DIR, 'report.log')) file_handler = logging.FileHandler(os.path.join(ROOT_DIR, 'tomato.log'))
file_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING) file_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING)
console_handler = logging.StreamHandler(sys.stdout) console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING) console_handler.setLevel(logging.DEBUG if is_debug else logging.WARNING)

View File

@ -1,79 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"id": "initial_id",
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2024-06-03T08:44:12.688855Z",
"start_time": "2024-06-03T08:44:07.661963Z"
}
},
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"# 读取彩色图像,使用原始字符串处理文件路径\n",
"image = cv2.imread(r\"D:\\project\\supermachine--tomato-passion_fruit\\20240529RGBtest3\\data\\bad\\36.bmp\")\n",
"\n",
"# 将RGB图像转换到HSV颜色空间\n",
"hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)\n",
"\n",
"# 将RGB图像转换到Lab颜色空间\n",
"lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n",
"\n",
"# 提取S分量\n",
"s_channel = hsv_image[:,:,1]\n",
"\n",
"# 提取L分量\n",
"l_channel = lab_image[:,:,0]\n",
"\n",
"# 计算S+L图像\n",
"sl_image = cv2.addWeighted(s_channel, 0.5, l_channel, 0.5, 0)\n",
"\n",
"# 使用Otsu阈值分割\n",
"_, otsu_segmentation = cv2.threshold(sl_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)\n",
"\n",
"# 显示原始图像和分割结果\n",
"cv2.imshow(\"Original Image\", image)\n",
"cv2.imshow(\"Otsu Segmentation\", otsu_segmentation)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()\n",
"#存图\n",
"# cv2.imwrite(r\"D:\\project\\supermachine--tomato-passion_fruit\\20240529RGBtest3\\33_otsu.bmp\", otsu_segmentation)\n"
],
"outputs": [],
"execution_count": 5
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "29d27b11f43683db"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

129
20240529RGBtest3/qt_test.py Normal file
View File

@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
# @Time : 2024/6/16 17:13
# @Author : TG
# @File : qt_test.py
# @Software: PyCharm
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
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Tomato Image Sender")
self.setGeometry(100, 100, 800, 600)
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
self.image_label = QLabel()
layout.addWidget(self.image_label)
self.rgb_send_name = r'\\.\pipe\rgb_receive' # 发送数据管道名对应 main.py 的接收数据管道名
self.rgb_receive_name = r'\\.\pipe\rgb_send' # 接收数据管道名对应 main.py 的发送数据管道名
self.spec_send_name = r'\\.\pipe\spec_receive' # 发送数据管道名对应 main.py 的接收数据管道名
# 连接main.py创建的命名管道
self.rgb_send = win32file.CreateFile(
self.rgb_send_name,
win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
self.rgb_receive = win32file.CreateFile(
self.rgb_receive_name,
win32file.GENERIC_READ,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
self.spec_send = win32file.CreateFile(
self.spec_send_name,
win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
def send_image_group(self, image_dir):
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]
for image_path in rgb_files:
with open(image_path, 'rb') as f:
img_data = f.read()
try:
win32file.WriteFile(self.rgb_send, len(img_data).to_bytes(4, byteorder='big'))
win32file.WriteFile(self.rgb_send, img_data)
except Exception as e:
print(f"数据发送失败. 错误原因: {e}")
if spec_files:
spec_file = spec_files[0]
with open(spec_file, 'rb') as f:
spec_data = f.read()
try:
win32file.WriteFile(self.spec_send, len(spec_data).to_bytes(4, byteorder='big'))
print(f"发送的光谱数据长度: {len(spec_data)}")
win32file.WriteFile(self.spec_send, spec_data)
print(f'发送的光谱数据长度: {len(spec_data)}')
except Exception as e:
print(f"数据发送失败. 错误原因: {e}")
self.receive_result()
def receive_result(self):
try:
# 读取结果数据
long_axis = int.from_bytes(win32file.ReadFile(self.rgb_receive, 2)[1], byteorder='big')
short_axis = int.from_bytes(win32file.ReadFile(self.rgb_receive, 2)[1], byteorder='big')
defect_num = int.from_bytes(win32file.ReadFile(self.rgb_receive, 2)[1], byteorder='big')
total_defect_area = int.from_bytes(win32file.ReadFile(self.rgb_receive, 4)[1], byteorder='big')
len_img = int.from_bytes(win32file.ReadFile(self.rgb_receive, 4)[1], byteorder='big')
img_data = win32file.ReadFile(self.rgb_receive, len_img)[1]
print(f"长径: {long_axis}, 短径: {short_axis}, 缺陷个数: {defect_num}, 缺陷面积: {total_defect_area}")
# 显示结果图像
image = Image.open(io.BytesIO(img_data))
qimage = QImage(image.tobytes(), image.size[0], image.size[1], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
self.image_label.setPixmap(pixmap)
except Exception as e:
print(f"数据接收失败. 错误原因: {e}")
def open_file_dialog(self):
directory_dialog = QFileDialog()
directory_dialog.setFileMode(QFileDialog.Directory)
if directory_dialog.exec_():
selected_directory = directory_dialog.selectedFiles()[0]
self.send_image_group(selected_directory)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
main_window.open_file_dialog()
sys.exit(app.exec_())

View File

@ -4,20 +4,11 @@
# @File : utils.py # @File : utils.py
# @Software: PyCharm # @Software: PyCharm
import time
import logging
import numpy as np
import shutil import shutil
import cv2
import os import os
from scipy.ndimage.measurements import label, find_objects
import win32pipe
import win32file
import io
from PIL import Image
import select
import msvcrt
from classifer import Tomato, Passion_fruit
import win32file import win32file
@ -37,24 +28,24 @@ class Pipe:
self.rgb_send = None self.rgb_send = None
self.spec_receive = None self.spec_receive = None
def create_pipes(self): def create_pipes(self, rgb_receive_name, rgb_send_name, spec_receive_name):
while True: while True:
try: try:
# 打开或创建命名管道 # 打开或创建命名管道
self.rgb_receive = win32pipe.CreateNamedPipe( self.rgb_receive = win32pipe.CreateNamedPipe(
self.rgb_receive_name, rgb_receive_name,
win32pipe.PIPE_ACCESS_INBOUND, win32pipe.PIPE_ACCESS_INBOUND,
win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT,
1, 80000000, 80000000, 0, None 1, 80000000, 80000000, 0, None
) )
self.rgb_send = win32pipe.CreateNamedPipe( self.rgb_send = win32pipe.CreateNamedPipe(
self.rgb_send_name, rgb_send_name,
win32pipe.PIPE_ACCESS_OUTBOUND, # 修改为输出模式 win32pipe.PIPE_ACCESS_OUTBOUND, # 修改为输出模式
win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT,
1, 80000000, 80000000, 0, None 1, 80000000, 80000000, 0, None
) )
self.spec_receive = win32pipe.CreateNamedPipe( self.spec_receive = win32pipe.CreateNamedPipe(
self.spec_receive_name, spec_receive_name,
win32pipe.PIPE_ACCESS_INBOUND, win32pipe.PIPE_ACCESS_INBOUND,
win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT, win32pipe.PIPE_TYPE_BYTE | win32pipe.PIPE_WAIT,
1, 200000000, 200000000, 0, None 1, 200000000, 200000000, 0, None