File “/data/data/com.termux/files/home/1.py”, line 41 else: ^^^^ SyntaxError: invalid syntax

时间: 2025-06-04 22:41:28 浏览: 19
<think>嗯,用户遇到了一个Python语法错误,在文件1.py的第41行,else语句那里出现了SyntaxError。我需要先分析可能的原因。首先,Python中的else语句必须紧跟在if或elif之后,且不能有额外的缩进或者空格。常见的错误可能是else前面没有对应的if或elif,或者前面的if/elif块没有正确结束,比如漏掉了冒号或者代码块缩进不正确。 另外,用户可能在使用else的时候,前面有其他的代码打断了if-else结构,比如在if和else之间写了别的语句,导致语法错误。也有可能是在循环结构中的else使用不当,比如在for或while循环后面使用else时格式不对。 还需要检查括号是否匹配,有时候前面的if语句可能因为缺少括号导致结构错误,进而使else找不到对应的if。此外,检查是否有拼写错误,比如把else写成esle或者其他拼写错误,不过这种情况通常会提示不同的错误信息。 用户提供的错误信息显示else后面有四个^符号,可能是指else的缩进问题,或者前面的if语句没有正确闭合。例如,如果前面的if语句后面没有跟冒号,或者代码块没有正确缩进,会导致else无法正确关联。 可能的解决步骤包括:检查第41行附近的代码,确认else对应的if语句是否存在且结构正确;检查缩进是否一致,特别是在混合使用空格和制表符时容易出错;查看前面的if/elif语句是否有语法错误,比如缺少冒号或括号;确保在if和else之间没有其他未被缩进的代码打断结构。 如果用户有提供具体的代码片段,可以更准确地定位问题,但根据现有信息,这些是常见的可能原因和解决方法。建议用户检查第41行及其之前的if语句结构,确认语法正确,并注意缩进的一致性。</think>你遇到的 `SyntaxError: invalid syntax` 错误通常是由于 Python 代码中 `else` 语句的使用不符合语法规则导致的。以下是常见原因和解决方法: --- ### **常见原因** 1. **`else` 缺少对应的 `if` 或 `elif`** Python 要求 `else` 必须直接跟在 `if` 或 `elif` 代码块之后,中间不能有其他代码。如果前面的 `if` 被错误关闭(如缺少缩进、多写了一个分号),`else` 会找不到匹配的 `if`。 2. **缩进错误** Python 依赖缩进定义代码块。如果 `else` 的缩进与对应的 `if` 不一致,会触发语法错误。 3. **`if` 语句未正确结束** 如果 `if` 语句的末尾缺少冒号 `:`,或 `if` 后的条件表达式不完整,会导致后续的 `else` 无法正确关联。 --- ### **示例与修复** #### ❌ 错误代码示例: ```python if x > 0: print("正数") print("其他情况") # 这行代码打断了 if 和 else 的关联 else: print("非正数") ``` #### ✅ 正确写法: ```python if x > 0: print("正数") else: print("非正数") ``` --- ### **排查步骤** 1. **检查 `else` 对应的 `if`** 找到第 41 行(报错行)之前的 `if` 或 `elif` 语句,确认它们是否完整且缩进正确。 2. **检查冒号和缩进** 确保 `if` 语句以 `:` 结尾,且代码块内的语句统一缩进(通常 4 空格)。 3. **检查是否有代码打断 `if-else`** 在 `if` 和 `else` 之间不能插入其他独立的代码(除非它们属于嵌套结构)。 --- ### 其他可能性 - 如果代码中有 `try-except` 或循环中的 `else`,需确保其位置正确(例如 `for...else` 或 `while...else`)。 - 检查是否意外删除了某个括号或引号,导致语法错误波及到 `else`。 --- 如果提供具体的代码片段(如第 40-42 行内容),可以更精准地定位问题!
阅读全文

相关推荐

import os import pandas as pd import numpy as np # 设置主文件夹路径 main_folder = 'C:/Users\Lenovo\Desktop\crcw不同端12k在0负载下\风扇端' # 创建空列表,用于存储数据和标签 data_list = [] label_list = [] def processTarget(): # 遍历主文件夹中的每个子文件夹,并处理每个.csv文件 for folder_name in sorted(os.listdir(main_folder)): folder_path = os.path.join(main_folder, folder_name) if os.path.isdir(folder_path): csv_files = [f for f in os.listdir(folder_path) if f.endswith('.csv')] print(f"Processing folder: {folder_name}, found {len(csv_files)} CSV files.") # 打印 CSV 文件数量 # 遍历该类别文件夹中的.csv文件 for filename in sorted(csv_files): file_path = os.path.join(folder_path, filename) # 读取.csv文件 csv_data = pd.read_csv(file_path, header=None) # 检查数据形状,确保至少有4列 if csv_data.shape[1] >= 4: # 确保至少有4列 csv_data = csv_data.iloc[:, [0, 1, 2]].values # 只保留前3列,忽略第4列(RPM) else: print(f"Skipping file {filename}, unexpected shape: {csv_data.shape}") continue # 跳过不符合要求的文件 # 将当前文件的数据添加到 data_list 中 data_list.append(csv_data) # 添加为二维数组 # 添加相应的标签 if '内圈故障' in folder_name: class_label = 0 # 0: 内圈故障 elif '球故障' in folder_name: class_label = 1 # 1: 球故障 else: continue # 如果文件夹名称不符合预期,跳过 label_list.append(class_label) # 直接添加标签 # 确保 data_list 和 label_list 不为空 if data_list and label_list: # 将数据转换为三维 NumPy 数组 data = np.array(data_list) # shape: (文件数量, 1000, 3) label = np.array(label_list) # shape: (文件数量,) return data, label else: raise ValueError("没有有效的数据可供处理。") # 调用 processTarget 函数 try: data0, label0 = processTarget() print(data0.shape) # 输出数据形状 print(label0.shape) # 输出标签形状 except ValueError as e: print(e)File "C:/Users/Lenovo/AppData/Roaming/JetBrains/PyCharmCE2020.2/scratches/scratch_21.py", line 23 self.trans_matrix_ = np.dot(inv_K, Pt.reshape(nt, 1)) ^ SyntaxError: invalid syntax

# -*- coding: utf-8 -*- import sys import os import cv2 import numpy as np from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout, QHBoxLayout, QMessageBox, QLabel, QFileDialog, QToolBar, QComboBox, QStatusBar, QGroupBox, QSlider, QDockWidget, QProgressDialog, QLineEdit, QRadioButton, QButtonGroup, QCheckBox) from PyQt5.QtCore import QRect, Qt, QSettings, QThread, pyqtSignal from CamOperation_class import CameraOperation sys.path.append("D:\\海康\\MVS\\Development\\Samples\\Python\\BasicDemo") import ctypes from datetime import datetime from MvCameraControl_class import * from MvErrorDefine_const import * from CameraParams_header import * from PyUICBasicDemo import Ui_MainWindow import logging import platform import serial import socket import time from scipy import ndimage import skimage.measure from skimage.feature import ORB, match_descriptors import subprocess # 配置日志系统 logging.basicConfig( level=logging.DEBUG, # 设置为DEBUG级别获取更多信息 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("cloth_inspection_debug.log"), logging.StreamHandler() ] ) logging.info("布料印花检测系统启动") # 全局变量 current_sample_path = "" # 当前使用的样本路径 detection_history = [] # 检测历史记录 isGrabbing = False # 相机取流状态 isOpen = False # 相机打开状态 obj_cam_operation = None # 相机操作对象 frame_monitor_thread = None # 帧监控线程 sensor_monitor_thread = None # 传感器监控线程 sensor_controller = None # 传感器控制器 # ==================== 传感器通讯模块 ==================== class SensorController: def __init__(self): self.sensor_type = None # 'serial' 或 'ethernet' self.serial_conn = None self.socket_conn = None self.sensor_data = { 'tension': 0.0, # 布料张力 (N) 'speed': 0.0, # 布料速度 (m/s) 'temperature': 25.0, # 环境温度 (°C) 'humidity': 50.0 # 环境湿度 (%) } self.connected = False def connect(self, config): """连接传感器""" try: if config['type'] == 'serial': self.sensor_type = 'serial' self.serial_conn = serial.Serial( port=config['port'], baudrate=config['baudrate'], timeout=config['timeout'] ) logging.info(f"串口传感器已连接: {config['port']}") elif config['type'] == 'ethernet': self.sensor_type = 'ethernet' self.socket_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket_conn.connect((config['ip'], config['port'])) self.socket_conn.settimeout(config['timeout']) logging.info(f"以太网传感器已连接: {config['ip']}:{config['port']}") self.connected = True return True except Exception as e: logging.error(f"传感器连接失败: {str(e)}") self.connected = False return False def disconnect(self): """断开传感器连接""" if self.serial_conn and self.serial_conn.is_open: self.serial_conn.close() if self.socket_conn: try: self.socket_conn.shutdown(socket.SHUT_RDWR) self.socket_conn.close() except: pass self.connected = False logging.info("传感器已断开") def read_data(self): """从传感器读取数据(模拟实现)""" if not self.connected: return None # 实际应用中应替换为真实传感器协议 if self.sensor_type == 'serial' and self.serial_conn: try: # 模拟串口数据读取 self.sensor_data = { 'tension': np.random.uniform(5.0, 20.0), 'speed': np.random.uniform(0.5, 2.5), 'temperature': 25.0 + np.random.uniform(-2, 2), 'humidity': 50.0 + np.random.uniform(-10, 10) } return self.sensor_data except serial.SerialException as e: logging.error(f"串口读取失败: {str(e)}") self.disconnect() return None elif self.sensor_type == 'ethernet' and self.socket_conn: try: # 模拟以太网数据读取 self.sensor_data = { 'tension': np.random.uniform(5.0, 20.0), 'speed': np.random.uniform(0.5, 2.5), 'temperature': 25.0 + np.random.uniform(-2, 2), 'humidity': 50.0 + np.random.uniform(-10, 10) } return self.sensor_data except (socket.timeout, socket.error) as e: logging.error(f"网络传感器读取失败: {str(e)}") self.disconnect() return None return None def send_command(self, command): """向传感器发送控制命令""" if not self.connected: return False try: if self.sensor_type == 'serial' and self.serial_conn: # 实际应用中应根据传感器协议构造命令 self.serial_conn.write(command.encode()) return True elif self.sensor_type == 'ethernet' and self.socket_conn: self.socket_conn.send(command.encode()) return True return False except Exception as e: logging.error(f"发送传感器命令失败: {str(e)}") return False # 帧监控线程 class FrameMonitorThread(QThread): frame_status = pyqtSignal(str) def __init__(self, cam_operation): super().__init__() self.cam_operation = cam_operation self.running = True def run(self): while self.running: if self.cam_operation: status = self.cam_operation.get_frame_status() frame_text = "有帧" if status.get('current_frame', False) else "无帧" self.frame_status.emit(f"帧状态: {frame_text}") QThread.msleep(500) def stop(self): self.running = False # 传感器数据监控线程 class SensorMonitorThread(QThread): data_updated = pyqtSignal(dict) def __init__(self, sensor_controller): super().__init__() self.sensor_controller = sensor_controller self.running = True def run(self): while self.running: if self.sensor_controller and self.sensor_controller.connected: data = self.sensor_controller.read_data() if data: self.data_updated.emit(data) QThread.msleep(1000) # 每秒更新一次 def stop极(self): self.running = False # ==================== 优化后的检测算法 ==================== def enhanced_check_print_quality(sample_image_path, test_image, threshold=0.05, sensor_data=None): """ 优化版布料印花检测算法,增加图像配准和特征匹配 :param sample_image_path: 合格样本图像路径 :param test_image: 测试图像 (numpy数组) :param threshold: 差异阈值 :param sensor_data: 传感器数据字典 :return: 是否合格,差异值,标记图像 """ # 根据传感器数据动态调整阈值 if sensor_data: # 速度越高,允许的差异阈值越大 speed_factor = min(1.0 + sensor_data['speed'] * 0.1, 1.5) # 温度/湿度影响 env_factor = 1.0 + abs(sensor_data['temperature'] - 25) * 0.01 + abs(sensor_data['humidity'] - 50) * 0.005 adjusted_threshold = threshold * speed_factor * env_factor logging.info(f"根据传感器数据调整阈值: 原始={threshold:.4f}, 调整后={adjusted_threshold:.4f}") else: adjusted_threshold = threshold try: # 读取样本图像 sample_img_data = np.fromfile(sample_image_path, dtype=np.uint8) sample_image = cv2.imdecode(sample_img_data, cv2.IMREAD_GRAYSCALE) if sample_image is None: logging.error(f"无法解码样本图像: {sample_image_path}") return None, None, None except Exception as e: logging.exception(f"样本图像读取异常: {str(e)}") return None, None, None # 确保测试图像是灰度图 if len(test_image.shape) == 3: test_image_gray = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY) else: test_image_gray = test_image.copy() # 1. 图像预处理 sample_image = cv2.GaussianBlur(sample_image, (5, 5), 0) test_image_gray = cv2.GaussianBlur(test_image_gray, (5, 5), 0) # 2. 图像配准(解决位置偏移问题) try: # 使用ORB特征匹配进行图像配准 orb = cv2.ORB_create(nfeatures=200) keypoints1, descriptors1 = orb.detectAndCompute(sample_image, None) keypoints2, descriptors2 = orb.detectAndCompute(test_image_gray, None) if descriptors1 is None or descriptors2 is None: logging.warning("无法提取特征描述符,跳过配准") aligned_sample = sample_image else: # 使用BFMatcher进行特征匹配 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(descriptors1, descriptors2) matches = sorted(matches, key=lambda x: x.distance) if len(matches) > 10: # 提取匹配点的坐标 src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) # 计算单应性矩阵 H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) if H is not None: # 应用变换 aligned_sample = cv2.warpPerspective( sample_image, H, (test_image_gray.shape[1], test_image_gray.shape[0]) ) logging.info("图像配准成功,使用配准后样本") else: aligned_sample = sample_image logging.warning("无法计算单应性矩阵,使用原始样本") else: aligned_sample = sample_image logging.warning("特征点匹配不足,跳过图像配准") except Exception as e: logging.error(f"图像配准失败: {str(e)}") aligned_sample = sample_image # 3. 确保图像大小一致 try: if aligned_sample.shape != test_image_gray.shape: test_image_gray = cv2.resize(test_image_gray, (aligned_sample.shape[1], aligned_sample.shape[0])) except Exception as e: logging.error(f"图像调整大小失败: {str(e)}") return None, None, None # 4. 计算结构相似性(SSIM)和差异 ssim_score, ssim_diff = skimage.measure.compare_ssim( aligned_sample, test_image_gray, full=True, gaussian_weights=True ) ssim_diff = (1 - ssim_diff) * 255 # 转换为0-255范围 # 5. 计算绝对差异 abs_diff = cv2.absdiff(aligned_sample, test_image_gray) # 6. 组合差异(SSIM差异对结构变化敏感,绝对差异对亮度变化敏感) combined_diff = cv2.addWeighted(ssim_diff.astype(np.uint8), 0.7, abs_diff, 0.3, 0) # 7. 二值化差异 _, thresholded = cv2.threshold(combined_diff, 30, 255, cv2.THRESH_BINARY) # 8. 形态学操作去除噪声 kernel = np.ones((3, 3), np.uint8) thresholded = cv2.morphologyEx(thresholded, cv2.MORPH_OPEN, kernel) thresholded = cv2.morphologyEx(thresholded, cv2.MORPH_CLOSE, kernel) # 9. 计算差异比例 diff_pixels = np.count_nonzero(thresholded) total_pixels = aligned_sample.size diff_ratio = diff_pixels / total_pixels # 10. 判断是否合格 is_qualified = diff_ratio <= adjusted_threshold # 11. 创建标记图像 marked_image = cv2.cvtColor(test_image_gray, cv2.COLOR_GRAY2BGR) marked_image[thresholded == 255] = [0, 0, 255] # 红色标记缺陷 # 12. 标记大面积缺陷区域 labels = skimage.measure.label(thresholded) properties = skimage.measure.regionprops(labels) for prop in properties: if prop.area > 50: # 只标记大于50像素的区域 y, x = prop.centroid cv2.putText(marked_image, f"Defect", (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255), 1) return is_qualified, diff_ratio, marked_image # ==================== 传感器控制的质量检测流程 ==================== def sensor_controlled_check(): """传感器控制的质量检测主流程""" global isGrabbing, obj_cam_operation, current_sample_path, detection_history, sensor_controller logging.info("传感器控制的质量检测启动") # 1. 检查传感器连接 if not sensor_controller or not sensor_controller.connected: QMessageBox.warning(mainWindow, "传感器错误", "传感器未连接,请先连接传感器!", QMessageBox.Ok) return # 2. 读取传感器数据 sensor_data = sensor_controller.read_data() if not sensor_data: QMessageBox.warning(mainWindow, "传感器错误", "无法读取传感器数据!", QMessageBox.Ok) return # 3. 根据传感器数据调整生产参数 adjust_production_parameters(sensor_data) # 4. 执行图像捕获和检测 check_print_with_sensor(sensor_data) def adjust_production_parameters(sensor_data): """根据传感器数据调整生产参数""" global obj_cam_operation # 示例:根据张力调整相机参数 tension = sensor_data['tension'] # 张力过大时增加曝光时间 if tension > 15.0 and obj_cam_operation: logging.info(f"高张力({tension}N)环境,增加曝光时间") try: current_exposure = obj_cam_operation.exposure_time new_exposure = min(current_exposure * 1.2, 100000) # 增加20%,上限100ms obj_cam_operation.set_exposure(new_exposure) logging.info(f"曝光时间调整为: {new_exposure}us") except Exception as e: logging.error(f"调整曝光失败: {str(e)}") # 速度过快时降低图像分辨率 speed = sensor_data['speed'] if speed > 2.0 and obj_cam_operation: logging.info(f"高速度({speed}m/s)环境,降低分辨率") try: # 实际应用中应调用相机SDK的分辨率设置 # 这里仅为示例 pass except Exception as e: logging.error(f"调整分辨率失败: {str(e)}") # 发送控制命令到传感器 if tension < 5.0 and sensor_controller: sensor_controller.send_command("INCREASE_TENSION") logging.info("发送增加张力命令") elif tension > 18.0 and sensor_controller: sensor_controller.send_command("DECREASE_TENSION") logging.info("发送减少张力命令") # 布料印花检测函数(使用优化算法) def check_print_with_sensor(sensor_data=None): """ 使用优化算法检测布料印花是否合格 """ global isGrabbing, obj_cam_operation, current_sample_path, detection_history logging.info("检测印花质量按钮按下") # 1. 检查相机状态 if not isGrabbing: logging.warning("相机未取流") QMessageBox.warning(mainWindow, "错误", "请先开始取流并捕获图像!", QMessageBox.Ok) return # 2. 检查相机操作对象 if not obj_cam_operation: logging.error("相机操作对象未初始化") QMessageBox.warning(mainWindow, "错误", "相机未正确初始化!", QMessageBox.Ok) return # 3. 检查样本路径 if not current_sample_path or not os.path.exists(current_sample_path): logging.warning(f"无效样本路径: {current_sample_path}") QMessageBox.warning(mainWindow, "错误", "请先设置有效的标准样本图像!", QMessageBox.Ok) return # 使用进度对话框防止UI阻塞 progress = QProgressDialog("正在检测...", "取消", 0, 100, mainWindow) progress.setWindowModality(Qt.WindowModal) progress.setValue(10) try: # 4. 获取当前帧 logging.info("尝试获取当前帧") test_image = obj_cam_operation.get_current_frame() progress.setValue(30) if test_image is None: logging.warning("获取当前帧失败") QMessageBox.warning(mainWindow, "错误", "无法获取当前帧图像!", QMessageBox.Ok) return # 5. 获取差异度阈值 diff_threshold = mainWindow.ui.sliderDiffThreshold.value() / 100.0 logging.info(f"使用差异度阈值: {diff_threshold}") progress.setValue(50) # 6. 执行检测 is_qualified, diff_ratio, marked_image = enhanced_check_print_quality( current_sample_path, test_image, threshold=diff_threshold, sensor_data=sensor_data ) progress.setValue(70) # 检查返回结果是否有效 if is_qualified is None: logging.error("检测函数返回无效结果") QMessageBox.critical(mainWindow, "检测错误", "检测失败,请检查日志", QMessageBox.Ok) return logging.info(f"检测结果: 合格={is_qualified}, 差异={diff_ratio}") progress.setValue(90) # 7. 更新UI update_diff_display(diff_ratio, is_qualified) result_text = f"印花是否合格: {'合格' if is_qualified else '不合格'}\n差异占比: {diff_ratio*100:.2f}%\n阈值: {diff_threshold*100:.2f}%" QMessageBox.information(mainWindow, "检测结果", result_text, QMessageBox.Ok) if marked_image is not None: cv2.imshow("缺陷标记结果", marked_image) cv2.waitKey(0) cv2.destroyAllWindows() else: logging.warning("标记图像为空") # 8. 记录检测结果 detection_result = { 'timestamp': datetime.now(), 'qualified': is_qualified, 'diff_ratio': diff_ratio, 'threshold': diff_threshold, 'sensor_data': sensor_data if sensor_data else {} } detection_history.append(detection_result) update_history_display() progress.setValue(100) except Exception as e: logging.exception("印花检测失败") QMessageBox.critical(mainWindow, "检测错误", f"检测过程中发生错误: {str(e)}", QMessageBox.Ok) finally: progress.close() # 更新检测结果显示 def update_diff_display(diff_ratio, is_qualified): """ 更新差异度显示控件 """ # 更新当前差异度显示 mainWindow.ui.lblCurrentDiff.setText(f"当前差异度: {diff_ratio*100:.2f}%") # 根据合格状态设置颜色 if is_qualified: mainWindow.ui.lblDiffStatus.setText("状态: 合格") mainWindow.ui.lblDiffStatus.setStyleSheet("color: green; font-size: 12px;") else: mainWindow.ui.lblDiffStatus.setText("状态: 不合格") mainWindow.ui.lblDiffStatus.setStyleSheet("color: red; font-size: 12px;") # 更新差异度阈值显示 def update_diff_threshold(value): """ 当滑块值改变时更新阈值显示 """ mainWindow.ui.lblDiffValue.setText(f"{value}%") # 保存标准样本函数 def save_sample_image(): global isGrabbing, obj_cam_operation, current_sample_path if not isGrabbing: QMessageBox.warning(mainWindow, "错误", "请先开始取流并捕获图像!", QMessageBox.Ok) return # 检查是否有有效图像 if not obj_cam_operation.is_frame_available(): QMessageBox.warning(mainWindow, "无有效图像", "未捕获到有效图像,请检查相机状态!", QMessageBox.Ok) return # 读取上次使用的路径 settings = QSettings("ClothInspection", "CameraApp") last_dir = settings.value("last_save_dir", os.path.join(os.getcwd(), "captures")) # 创建默认文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") default_filename = f"sample_{timestamp}" # 弹出文件保存对话框 file_path, selected_filter = QFileDialog.getSaveFileName( mainWindow, "保存标准样本图像", os.path.join(last_dir, default_filename), "BMP Files (*.bmp);;PNG Files (*.png);;JPEG Files (*.jpg);;所有文件 (*)", options=QFileDialog.DontUseNativeDialog ) if not file_path: logging.info("用户取消了图像保存操作") return # 用户取消保存 # 处理文件扩展名 file_extension = os.path.splitext(file_path)[1].lower() if not file_extension: # 根据选择的过滤器添加扩展名 if "BMP" in selected_filter: file_path += ".bmp" elif "PNG" in selected_filter: file_path += ".png" elif "JPEG" in selected_filter or "JPG" in selected_filter: file_path += ".jpg" else: # 默认使用BMP格式 file_path += ".bmp" file_extension = os.path.splitext(file_path)[1].lower() # 根据扩展名设置保存格式 format_mapping = { ".bmp": "bmp", ".png": "png", ".jpg": "jpg", ".jpeg": "jpg" } save_format = format_mapping.get(file_extension) if not save_format: QMessageBox.warning(mainWindow, "错误", "不支持的文件格式!", QMessageBox.Ok) return # 确保目录存在 directory = os.path.dirname(file_path) if directory and not os.path.exists(directory): try: os.makedirs(directory, exist_ok=True) logging.info(f"创建目录: {directory}") except OSError as e: error_msg = f"无法创建目录 {directory}: {str(e)}" QMessageBox.critical(mainWindow, "目录创建错误", error_msg, QMessageBox.Ok) return # 保存当前帧作为标准样本 try: ret = obj_cam_operation.save_image(file_path, save_format) if ret != MV_OK: strError = f"保存样本图像失败: {hex(ret)}" QMessageBox.warning(mainWindow, "错误", strError, QMessageBox.Ok) else: success_msg = f"标准样本已保存至:\n{file_path}" QMessageBox.information(mainWindow, "成功", success_msg, QMessageBox.Ok) # 更新当前样本路径 current_sample_path = file_path update_sample_display() # 保存当前目录 settings.setValue("last_save_dir", os.path.dirname(file_path)) except Exception as e: error_msg = f"保存图像时发生错误: {str(e)}" QMessageBox.critical(mainWindow, "异常错误", error_msg, QMessageBox.Ok) logging.exception("保存样本图像时发生异常") # 预览当前样本 def preview_sample(): global current_sample_path if not current_sample_path or not os.path.exists(current_sample_path): QMessageBox.warning(mainWindow, "错误", "请先设置有效的标准样本图像!", QMessageBox.Ok) return try: # 使用安全方法读取图像 img_data = np.fromfile(current_sample_path, dtype=np.uint8) sample_img = cv2.imdecode(img_data, cv2.IMREAD_COLOR) if sample_img is None: raise Exception("无法加载图像") cv2.imshow("标准样本预览", sample_img) cv2.waitKey(0) cv2.destroyAllWindows() except Exception as e: QMessageBox.warning(mainWindow, "错误", f"预览样本失败: {str(e)}", QMessageBox.Ok) # 更新样本路径显示 def update_sample_display(): global current_sample_path if current_sample_path: mainWindow.ui.lblSamplePath.setText(f"当前样本: {os.path.basename(current_sample_path)}") mainWindow.ui.lblSamplePath.setToolTip(current_sample_path) mainWindow.ui.bnPreviewSample.setEnabled(True) else: mainWindow.ui.lblSamplePath.setText("当前样本: 未设置样本") mainWindow.ui.bnPreviewSample.setEnabled(False) # 更新历史记录显示 def update_history_display(): global detection_history mainWindow.ui.cbHistory.clear() for i, result in enumerate(detection_history[-10:]): # 显示最近10条记录 timestamp = result['timestamp'].strftime("%H:%M:%S") status = "合格" if result['qualified'] else "不合格" ratio = f"{result['diff_ratio']*100:.2f}%" mainWindow.ui.cbHistory.addItem(f"[{timestamp}] {status} - 差异: {ratio}") # 获取选取设备信息的索引,通过[]之间的字符去解析 def TxtWrapBy(start_str, end, all): start = all.find(start_str) if start >= 0: start += len(start_str) end = all.find(end, start) if end >= 0: return all[start:end].strip() # 将返回的错误码转换为十六进制显示 def ToHexStr(num): """将错误码转换为十六进制字符串""" # 处理非整数输入 if not isinstance(num, int): try: # 尝试转换为整数 num = int(num) except: # 无法转换时返回类型信息 return f"<非整数:{type(num)}>" chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'} hexStr = "" # 处理负数 if num < 0: num = num + 2 ** 32 # 转换为十六进制 while num >= 16: digit = num % 16 hexStr = chaDic.get(digit, str(digit)) + hexStr num //= 16 hexStr = chaDic.get(num, str(num)) + hexStr return "0x" + hexStr # 绑定下拉列表至设备信息索引 def xFunc(event): global nSelCamIndex nSelCamIndex = TxtWrapBy("[", "]", mainWindow.ui.ComboDevices.get()) # Decoding Characters def decoding_char(c_ubyte_value): c_char_p_value = ctypes.cast(c_ubyte_value, ctypes.c_char_p) try: decode_str = c_char_p_value.value.decode('gbk') # Chinese characters except UnicodeDecodeError: decode_str = str(c_char_p_value.value) return decode_str # ch:枚举相机 | en:enum devices def enum_devices(): global deviceList global obj_cam_operation deviceList = MV_CC_DEVICE_INFO_LIST() n_layer_type = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE) ret = MvCamera.MV_CC_EnumDevices(n_layer_type, deviceList) if ret != 0: strError = "Enum devices fail! ret = :" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) return ret if deviceList.nDeviceNum == 0: QMessageBox.warning(mainWindow, "Info", "Find no device", QMessageBox.Ok) return ret print("Find %d devices!" % deviceList.nDeviceNum) devList = [] for i in range(0, deviceList.nDeviceNum): mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE: print("\ngige device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24) nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16) nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8) nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff) print("current ip: %d.%d.%d.%d " % (nip1, nip2, nip3, nip4)) devList.append( "[" + str(i) + "]GigE: " + user_defined_name + " " + model_name + "(" + str(nip1) + "." + str( nip2) + "." + str(nip3) + "." + str(nip4) + ")") elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE: print("\nu3v device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]USB: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE: print("\nCML device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]CML: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE: print("\nCXP device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: "+strSerialNumber) devList.append("[" + str(i) + "]CXP: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE: print("\nXoF device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]XoF: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") mainWindow.ui.ComboDevices.clear() mainWindow.ui.ComboDevices.addItems(devList) mainWindow.ui.ComboDevices.setCurrentIndex(0) # ch:打开相机 | en:open device def open_device(): global deviceList global nSelCamIndex global obj_cam_operation global isOpen global frame_monitor_thread global mainWindow if isOpen: QMessageBox.warning(mainWindow, "Error", 'Camera is Running!', QMessageBox.Ok) return MV_E_CALLORDER nSelCamIndex = mainWindow.ui.ComboDevices.currentIndex() if nSelCamIndex < 0: QMessageBox.warning(mainWindow, "Error", 'Please select a camera!', QMessageBox.Ok) return MV_E_CALLORDER obj_cam_operation = CameraOperation(obj_cam_operation, deviceList, nSelCamIndex) ret = obj_cam_operation.open_device() if 0 != ret: strError = "Open device failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) isOpen = False else: set_continue_mode() get_param() isOpen = True enable_controls() # 启动帧监控线程 frame_monitor_thread = FrameMonitorThread(obj_cam_operation) frame_monitor_thread.frame_status.connect(mainWindow.ui.statusBar.showMessage) frame_monitor_thread.start() # ch:开始取流 | en:Start grab image def start_grabbing(): global obj_cam_operation global isGrabbing ret = obj_cam_operation.start_grabbing(mainWindow.ui.widgetDisplay.winId()) if ret != 0: strError = "Start grabbing failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: isGrabbing = True enable_controls() # ch:停止取流 | en:Stop grab image def stop_grabbing(): global obj_cam_operation global isGrabbing ret = obj_cam_operation.Stop_grabbing() if ret != 0: strError = "Stop grabbing failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: isGrabbing = False enable_controls() # ch:关闭设备 | Close device def close_device(): global isOpen global isGrabbing global obj_cam_operation global frame_monitor_thread # 停止帧监控线程 if frame_monitor_thread and frame_monitor_thread.isRunning(): frame_monitor_thread.stop() frame_monitor_thread.wait(2000) if isOpen: obj_cam_operation.close_device() isOpen = False isGrabbing = False enable_controls() # ch:设置触发模式 | en:set trigger mode def set_continue_mode(): ret = obj_cam_operation.set_trigger_mode(False) if ret != 0: strError = "Set continue mode failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: mainWindow.ui.radioContinueMode.setChecked(True) mainWindow.ui.radioTriggerMode.setChecked(False) mainWindow.ui.bnSoftwareTrigger.setEnabled(False) # ch:设置软触发模式 | en:set software trigger mode def set_software_trigger_mode(): ret = obj_cam_operation.set_trigger_mode(True) if ret != 0: strError = "Set trigger mode failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: mainWindow.ui.radioContinueMode.setChecked(False) mainWindow.ui.radioTriggerMode.setChecked(True) mainWindow.ui.bnSoftwareTrigger.setEnabled(isGrabbing) # ch:设置触发命令 | en:set trigger software def trigger_once(): ret = obj_cam_operation.trigger_once() if ret != 0: strError = "TriggerSoftware failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) # 保存图像对话框 def save_image_dialog(): """ 打开保存图像对话框并保存当前帧 """ global isGrabbing, obj_cam_operation # 检查相机状态 if not isGrabbing: QMessageBox.warning(mainWindow, "相机未就绪", "请先开始取流并捕获图像!", QMessageBox.Ok) return # 检查是否有有效图像 if not obj_cam_operation.is_frame_available(): QMessageBox.warning(mainWindow, "无有效图像", "未捕获到有效图像,请检查相机状态!", QMessageBox.Ok) return # 读取上次使用的路径 settings = QSettings("ClothInspection", "CameraApp") last_dir = settings.value("last_save_dir", os.path.join(os.getcwd(), "captures")) # 创建默认文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") default_filename = f"capture_{timestamp}" # 弹出文件保存对话框 file_path, selected_filter = QFileDialog.getSaveFileName( mainWindow, "保存图像", os.path.join(last_dir, default_filename), # 初始路径 "BMP 图像 (*.bmp);;JPEG 图像 (*.jpg);;PNG 图像 (*.png);;TIFF 图像 (*.tiff);;所有文件 (*)", options=QFileDialog.DontUseNativeDialog ) # 用户取消操作 if not file_path: logging.info("用户取消了图像保存操作") return # 处理文件扩展名 file_extension = os.path.splitext(file_path)[1].lower() if not file_extension: # 根据选择的过滤器添加扩展名 if "BMP" in selected_filter: file_path += ".bmp" elif "JPEG" in selected_filter or "JPG" in selected_filter: file_path += ".jpg" elif "PNG" in selected_filter: file_path += ".png" elif "TIFF" in selected_filter: file_path += ".tiff" else: # 默认使用BMP格式 file_path += ".bmp" # 确定保存格式 format_mapping = { ".bmp": "bmp", ".jpg": "jpg", ".jpeg": "jpg", ".png": "png", ".tiff": "tiff", ".tif": "tiff" } file_extension = os.path.splitext(file_path)[1].lower() save_format = format_mapping.get(file_extension, "bmp") # 确保目录存在 directory = os.path.dirname(file_path) if directory and not os.path.exists(directory): try: os.makedirs(directory, exist_ok=True) except OSError as e: QMessageBox.critical(mainWindow, "目录错误", f"无法创建目录:\n{str(e)}", QMessageBox.Ok) return # 保存图像 try: ret = obj_cam_operation.save_image(file_path, save_format) if ret == MV_OK: QMessageBox.information(mainWindow, "保存成功", f"图像已保存至:\n{file_path}", QMessageBox.Ok) logging.info(f"图像保存成功: {file_path}") # 保存当前目录 settings.setValue("last_save_dir", os.path.dirname(file_path)) else: error_msg = f"保存失败! 错误代码: {hex(ret)}" QMessageBox.warning(mainWindow, "保存失败", error_msg, QMessageBox.Ok) logging.error(f"图像保存失败: {file_path}, 错误代码: {hex(ret)}") except Exception as e: QMessageBox.critical(mainWindow, "保存错误", f"保存图像时发生错误:\n{str(e)}", QMessageBox.Ok) logging.exception(f"保存图像时发生异常: {file_path}") def is_float(str): try: float(str) return True except ValueError: return False # ch: 获取参数 | en:get param def get_param(): try: # 调用方法获取参数 ret = obj_cam_operation.get_parameters() # 记录调用结果(调试用) logging.debug(f"get_param() 返回: {ret} (类型: {type(ret)})") # 处理错误码 if ret != MV_OK: strError = "获取参数失败,错误码: " + ToHexStr(ret) QMessageBox.warning(mainWindow, "错误", strError, QMessageBox.Ok) else: # 成功获取参数后更新UI mainWindow.ui.edtExposureTime.setText("{0:.2f}".format(obj_cam_operation.exposure_time)) mainWindow.ui.edtGain.setText("{0:.2f}".format(obj_cam_operation.gain)) mainWindow.ui.edtFrameRate.setText("{0:.2f}".format(obj_cam_operation.frame_rate)) # 记录成功信息 logging.info("成功获取相机参数") except Exception as e: # 处理所有异常 error_msg = f"获取参数时发生错误: {str(e)}" logging.error(error_msg) QMessageBox.critical(mainWindow, "严重错误", error_msg, QMessageBox.Ok) # ch: 设置参数 | en:set param def set_param(): frame_rate = mainWindow.ui.edtFrameRate.text() exposure = mainWindow.ui.edtExposureTime.text() gain = mainWindow.ui.edtGain.text() if not (is_float(frame_rate) and is_float(exposure) and is_float(gain)): strError = "设置参数失败: 参数必须是有效的浮点数" QMessageBox.warning(mainWindow, "错误", strError, QMessageBox.Ok) return MV_E_PARAMETER try: # 使用正确的参数顺序和关键字 ret = obj_cam_operation.set_param( frame_rate=float(frame_rate), exposure_time=float(exposure), gain=float(gain) ) if ret != MV_OK: strError = "设置参数失败,错误码: " + ToHexStr(ret) QMessageBox.warning(mainWindow, "错误", strError, QMessageBox.Ok) else: logging.info("参数设置成功") return MV_OK except Exception as e: error_msg = f"设置参数时发生错误: {str(e)}" logging.error(error_msg) QMessageBox.critical(mainWindow, "严重错误", error_msg, QMessageBox.Ok) return MV_E_STATE # ch: 设置控件状态 | en:set enable status def enable_controls(): global isGrabbing global isOpen # 先设置group的状态,再单独设置各控件状态 mainWindow.ui.groupGrab.setEnabled(isOpen) mainWindow.ui.groupParam.setEnabled(isOpen) mainWindow.ui.bnOpen.setEnabled(not isOpen) mainWindow.ui.bnClose.setEnabled(isOpen) mainWindow.ui.bnStart.setEnabled(isOpen and (not isGrabbing)) mainWindow.ui.bnStop.setEnabled(isOpen and isGrabbing) mainWindow.ui.bnSoftwareTrigger.setEnabled(isGrabbing and mainWindow.ui.radioTriggerMode.isChecked()) mainWindow.ui.bnSaveImage.setEnabled(isOpen and isGrabbing) mainWindow.ui #添加检测按钮控制 mainWindow.ui.bnCheckPrint.setEnabled(isOpen and isGrabbing) mainWindow.ui.bnSaveSample.setEnabled(isOpen and isGrabbing) mainWindow.ui.bnPreviewSample.setEnabled(bool(current_sample_path)) class MainWindow(QMainWindow): def __init__(self): super().__init__() # 创建UI实例 self.ui = Ui_MainWindow() self.ui.setupUi(self) def closeEvent(self, event): """重写关闭事件,执行清理操作""" logging.info("主窗口关闭,执行清理...") # 关闭设备 close_device() # 断开传感器 disconnect_sensor() event.accept() if __name__ == "__main__": # 初始化UI app = QApplication(sys.argv) # 创建主窗口实例 mainWindow = MainWindow() # 扩大主窗口尺寸 mainWindow.resize(1200, 800) # 宽度1200,高度800 # 创建工具栏 toolbar = mainWindow.addToolBar("检测工具") # 添加检测按钮 mainWindow.ui.bnCheckPrint = QPushButton("检测印花质量") toolbar.addWidget(mainWindow.ui.bnCheckPrint) # 添加保存样本按钮 mainWindow.ui.bnSaveSample = QPushButton("保存标准样本") toolbar.addWidget(mainWindow.ui.bnSaveSample) # 添加预览样本按钮 mainWindow.ui.bnPreviewSample = QPushButton("预览样本") toolbar.addWidget(mainWindow.ui.bnPreviewSample) # 添加历史记录下拉框 mainWindow.ui.cbHistory = QComboBox() mainWindow.ui.cbHistory.setMinimumWidth(300) toolbar.addWidget(QLabel("历史记录:")) toolbar.addWidget(mainWindow.ui.cbHistory) # 添加当前样本显示标签 mainWindow.ui.lblSamplePath = QLabel("当前样本: 未设置样本") status_bar = mainWindow.statusBar() status_bar.addPermanentWidget(mainWindow.ui.lblSamplePath) # === 新增差异度调整控件 === # 创建右侧面板容器 right_panel = QWidget() right_layout = QVBoxLayout(right_panel) right_layout.setContentsMargins(10, 10, 10, 10) # 创建差异度调整组 diff_group = QGroupBox("差异度调整") diff_layout = QVBoxLayout(diff_group) # 差异度阈值控制 mainWindow.ui.lblDiffThreshold = QLabel("差异度阈值 (0-100%):") mainWindow.ui.sliderDiffThreshold = QSlider(Qt.Horizontal) mainWindow.ui.sliderDiffThreshold.setRange(0, 100) # 0-100% mainWindow.ui.sliderDiffThreshold.setValue(5) # 默认5% mainWindow.ui.lblDiffValue = QLabel("5%") # 当前差异度显示 mainWindow.ui.lblCurrentDiff = QLabel("当前差异度: -") mainWindow.ui.lblCurrentDiff.setStyleSheet("font-size: 14px; font-weight: bold;") # 差异度状态指示器 mainWindow.ui.lblDiffStatus = QLabel("状态: 未检测") mainWindow.ui.lblDiffStatus.setStyleSheet("font-size: 12px;") # 布局控件 diff_layout.addWidget(mainWindow.ui.lblDiffThreshold) diff_layout.addWidget(mainWindow.ui.sliderDiffThreshold) diff_layout.addWidget(mainWindow.ui.lblDiffValue) diff_layout.addWidget(mainWindow.ui.lblCurrentDiff) diff_layout.addWidget(mainWindow.ui.lblDiffStatus) # 添加差异度组到右侧布局 right_layout.addWidget(diff_group) # === 新增传感器控制面板 === sensor_panel = QGroupBox("传感器控制") sensor_layout = QVBoxLayout(sensor_panel) # 传感器类型选择 sensor_type_layout = QHBoxLayout() mainWindow.ui.lblSensorType = QLabel("传感器类型:") mainWindow.ui.cbSensorType = QComboBox() mainWindow.ui.cbSensorType.addItems(["串口", "以太网"]) sensor_type_layout.addWidget(mainWindow.ui.lblSensorType) sensor_type_layout.addWidget(mainWindow.ui.cbSensorType) # 串口参数 mainWindow.ui.serialGroup = QGroupBox("串口参数") serial_layout = QVBoxLayout(mainWindow.ui.serialGroup) mainWindow.ui.lblComPort = QLabel("端口:") mainWindow.ui.cbComPort = QComboBox() # 获取可用串口 (Windows) if platform.system() == 'Windows': ports = [f"COM{i}" for i in range(1, 21)] else: ports = [f"/dev/ttyS{i}" for i in range(0, 4)] + [f"/dev/ttyUSB{i}" for i in range(0, 4)] mainWindow.ui.cbComPort.addItems(ports) mainWindow.ui.lblBaudrate = QLabel("波特率:") mainWindow.ui.cbBaudrate = QComboBox() mainWindow.ui.cbBaudrate.addItems(["9600", "19200", "38400", "57600", "115200"]) mainWindow.ui.cbBaudrate.setCurrentText("115200") serial_layout.addWidget(mainWindow.ui.lblComPort) serial_layout.addWidget(mainWindow.ui.cbComPort) serial_layout.addWidget(mainWindow.ui.lblBaudrate) serial_layout.addWidget(mainWindow.ui.cbBaudrate) # 以太网参数 mainWindow.ui.ethernetGroup = QGroupBox("以太网参数") ethernet_layout = QVBoxLayout(mainWindow.ui.ethernetGroup) mainWindow.ui.lblIP = QLabel("IP地址:") mainWindow.ui.edtIP = QLineEdit("192.168.1.100") mainWindow.ui.lblPort = QLabel("端口:") mainWindow.ui.edtPort = QLineEdit("502") ethernet_layout.addWidget(mainWindow.ui.lblIP) ethernet_layout.addWidget(mainWindow.ui.edtIP) ethernet_layout.addWidget(mainWindow.ui.lblPort) ethernet_layout.addWidget(mainWindow.ui.edtPort) # 连接/断开按钮 mainWindow.ui.bnConnectSensor = QPushButton("连接传感器") mainWindow.ui.bnDisconnectSensor = QPushButton("断开传感器") mainWindow.ui.bnDisconnectSensor.setEnabled(False) # 传感器数据显示 mainWindow.ui.lblSensorData = QLabel("传感器数据: 未连接") mainWindow.ui.lblSensorData.setStyleSheet("font-size: 10pt;") # 添加到布局 sensor_layout.addLayout(sensor_type_layout) sensor_layout.addWidget(mainWindow.ui.serialGroup) sensor_layout.addWidget(mainWindow.ui.ethernetGroup) sensor_layout.addWidget(mainWindow.ui.bnConnectSensor) sensor_layout.addWidget(mainWindow.ui.bnDisconnectSensor) sensor_layout.addWidget(mainWindow.ui.lblSensorData) # 添加到右侧面板 right_layout.addWidget(sensor_panel) # 添加拉伸项使控件靠上 right_layout.addStretch(1) # 创建停靠窗口 dock = QDockWidget("检测控制面板", mainWindow) dock.setWidget(right_panel) dock.setFeatures(QDockWidget.DockWidgetMovable | QDockWidget.DockWidgetFloatable) mainWindow.addDockWidget(Qt.RightDockWidgetArea, dock) # === 差异度调整功能实现 === # 更新差异度阈值显示 def update_diff_threshold(value): mainWindow.ui.lblDiffValue.setText(f"{value}%") # 连接滑块信号 mainWindow.ui.sliderDiffThreshold.valueChanged.connect(update_diff_threshold) # 更新检测结果显示 def update_diff_display(diff_ratio, is_qualified): # 更新当前差异度显示 mainWindow.ui.lblCurrentDiff.setText(f"当前差异度: {diff_ratio*100:.2f}%") # 根据合格状态设置颜色 if is_qualified: mainWindow.ui.lblDiffStatus.setText("状态: 合格") mainWindow.ui.lblDiffStatus.setStyleSheet("color: green; font-size: 12px;") else: mainWindow.ui.lblDiffStatus.setText("状态: 不合格") mainWindow.ui.lblDiffStatus.setStyleSheet("color: red; font-size: 12px;") # 绑定按钮事件 mainWindow.ui.bnCheckPrint.clicked.connect(sensor_controlled_check) mainWindow.ui.bnSaveSample.clicked.connect(save_sample_image) mainWindow.ui.bnPreviewSample.clicked.connect(preview_sample) # 传感器类型切换 def update_sensor_ui(index): mainWindow.ui.serialGroup.setVisible(index == 0) mainWindow.ui.ethernetGroup.setVisible(index == 1) mainWindow.ui.cbSensorType.currentIndexChanged.connect(update_sensor_ui) update_sensor_ui(0) # 初始显示串口 # 传感器连接 def connect_sensor(): global sensor_monitor_thread, sensor_controller sensor_type = mainWindow.ui.cbSensorType.currentText() if sensor_controller is None: sensor_controller = SensorController() if sensor_type == "串口": config = { 'type': 'serial', 'port': mainWindow.ui.cbComPort.currentText(), 'baudrate': int(mainWindow.ui.cbBaudrate.currentText()), 'timeout': 1.0 } else: # 以太网 config = { 'type': 'ethernet', 'ip': mainWindow.ui.edtIP.text(), 'port': int(mainWindow.ui.edtPort.text()), 'timeout': 1.0 } if sensor_controller.connect(config): mainWindow.ui.bnConnectSensor.setEnabled(False) mainWindow.ui.bnDisconnectSensor.setEnabled(True) # 启动传感器数据监控线程 sensor_monitor_thread = SensorMonitorThread(sensor_controller) sensor_monitor_thread.data_updated.connect(update_sensor_display) sensor_monitor_thread.start() # 传感器断开 def disconnect_sensor(): global sensor_monitor_thread if sensor_controller: sensor_controller.disconnect() mainWindow.ui.bnConnectSensor.setEnabled(True) mainWindow.ui.bnDisconnectSensor.setEnabled(False) if sensor_monitor_thread and sensor_monitor_thread.isRunning(): sensor_monitor_thread.stop() sensor_monitor_thread.wait(2000) sensor_monitor_thread = None mainWindow.ui.lblSensorData.setText("传感器数据: 未连接") mainWindow.ui.bnConnectSensor.clicked.connect(connect_sensor) mainWindow.ui.bnDisconnectSensor.clicked.connect(disconnect_sensor) def update_sensor_display(data): text = (f"张力: {data['tension']:.2f}N | " f"速度: {data['speed']:.2f}m/s | " f"温度: {data['temperature']:.1f}°C | " f"湿度: {data['humidity']:.1f}%") mainWindow.ui.lblSensorData.setText(text) # 绑定其他按钮事件 mainWindow.ui.bnEnum.clicked.connect(enum_devices) mainWindow.ui.bnOpen.clicked.connect(open_device) mainWindow.ui.bnClose.clicked.connect(close_device) mainWindow.ui.bnStart.clicked.connect(start_grabbing) mainWindow.ui.bnStop.clicked.connect(stop_grabbing) mainWindow.ui.bnSoftwareTrigger.clicked.connect(trigger_once) mainWindow.ui.radioTriggerMode.clicked.connect(set_software_trigger_mode) mainWindow.ui.radioContinueMode.clicked.connect(set_continue_mode) mainWindow.ui.bnGetParam.clicked.connect(get_param) mainWindow.ui.bnSetParam.clicked.connect(set_param) # 修改保存图像按钮连接 mainWindow.ui.bnSaveImage.clicked.connect(save_image_dialog) # 显示主窗口 mainWindow.show() # 执行应用 app.exec_() # 关闭设备 close_device() # 断开传感器 disconnect_sensor() sys.exit() 这个程序还是出现了下面的问题 SyntaxError: invalid syntax >>> cd d:/海康/MVS/Development/Samples/Python/MvImport File "<stdin>", line 1 cd d:/海康/MVS/Development/Samples/Python/MvImport ^ SyntaxError: invalid syntax >>> & D:/python/python.exe d:/海康/MVS/Development/Samples/Python/MvImport/three.py File "<stdin>", line 1 & D:/python/python.exe d:/海康/MVS/Development/Samples/Python/MvImport/three.py ^ SyntaxError: invalid syntax

import os import shutil import json import platform import subprocess import zipfile import argparse import re from pathlib import Path import sys # 工具配置 TOOL_VERSION = "2.0" SUPPORTED_YINGDAO_VERSIONS = ["5.0", "5.1", "5.2", "6.0"] IGNORE_DIRS = [".git", "__pycache__", "temp", "logs", "venv"] IGNORE_FILES = [".DS_Store", "thumbs.db", "desktop.ini"] # 主文件检测模式 MAIN_FILE_PATTERNS = [ r"main\.ya?ml", # main.yaml 或 main.yml r"main\.json", r"main\.py", r"main_\d+\.ya?ml", # main_001.yaml 等 r"main_\d+\.json", r"main_\d+\.py", r"project_main\.ya?ml", r"project_main\.json", r"project_main\.py" ] class YingDaoProjectValidator: def __init__(self, project_path): self.project_path = Path(project_path) self.valid = False self.main_file = None self.project_json = None self.validation_errors = [] def validate(self): """验证影刀项目结构""" self._check_project_json() self._find_main_file() self._check_required_dirs() self.valid = not bool(self.validation_errors) return self.valid def _check_project_json(self): """检查project.json文件""" project_json_path = self.project_path / "project.json" if project_json_path.exists(): try: with open(project_json_path, "r") as f: self.project_json = json.load(f) # 检查基本结构 if "projectName" not in self.project_json: self.validation_errors.append("project.json缺少projectName字段") except json.JSONDecodeError: self.validation_errors.append("project.json解析失败") else: # 尝试在子目录中查找 for root, _, files in os.walk(self.project_path): if "project.json" in files: self.project_json = Path(root) / "project.json" return self.validation_errors.append("缺失project.json文件") def _find_main_file(self): """查找主流程文件""" # 首先检查项目配置中是否指定了主文件 if self.project_json and "mainFile" in self.project_json: main_file_path = self.project_path / self.project_json["mainFile"] if main_file_path.exists(): self.main_file = main_file_path return # 使用模式匹配查找主文件 for pattern in MAIN_FILE_PATTERNS: regex = re.compile(pattern, re.IGNORECASE) for file in self.project_path.glob("**/*"): if file.is_file() and regex.match(file.name): self.main_file = file return # 如果仍未找到,尝试创建默认主文件 self._create_default_main_file() def _create_default_main_file(self): """创建默认主文件""" possible_names = [ "main.yaml", "main.yml", "main.json", "main.py" ] for name in possible_names: candidate = self.project_path / name if not candidate.exists(): try: # 创建简单的YAML主文件 if name.endswith(('.yaml', '.yml')): candidate.write_text("# 自动生成的影刀主流程\nsteps:\n - name: 开始\n action: print\n args: 'Hello YingDao!'") # 创建JSON主文件 elif name.endswith('.json'): candidate.write_text('{"steps": [{"name": "开始", "action": "print", "args": "Hello YingDao!"}]}') # 创建Python主文件 elif name.endswith('.py'): candidate.write_text('print("Hello YingDao!")') self.main_file = candidate self.validation_errors.append(f"已创建默认主文件: {name}") return except Exception as e: self.validation_errors.append(f"创建默认主文件失败: {str(e)}") self.validation_errors.append("无法找到或创建主流程文件") def _check_required_dirs(self): """检查必要的目录结构""" required_dirs = ["modules", "resources", "data"] for dir_name in required_dirs: dir_path = self.project_path / dir_name if not dir_path.exists(): try: dir_path.mkdir() self.validation_errors.append(f"已创建缺失目录: {dir_name}") except Exception as e: self.validation_errors.append(f"无法创建目录 {dir_name}: {str(e)}") class YingDaoMigrator: def __init__(self, project_path): self.project_path = Path(project_path) self.migration_package = Path.cwd() / f"{self.project_path.name}_migration_package" self.dependencies = set() self.validator = YingDaoProjectValidator(project_path) def validate_project(self): """验证并修复影刀项目""" print(f"[*] 验证项目: {self.project_path}") if not self.validator.validate(): print("[!] 项目验证存在问题:") for error in self.validator.validation_errors: print(f" - {error}") # 用户确认是否继续 response = input("项目存在问题,是否继续迁移? (y/n): ").lower() if response != 'y': print("迁移已取消") sys.exit(1) print(f"[+] 项目验证通过, 主文件: {self.validator.main_file.name}") def collect_project_files(self): """收集项目文件到迁移包""" print(f"[*] 收集项目文件: {self.project_path.name}") # 创建目录结构 project_dir = self.migration_package / "project" project_dir.mkdir(parents=True, exist_ok=True) # 复制项目文件(排除忽略项) for item in self.project_path.glob("**/*"): if any(ignore in item.parts for ignore in IGNORE_DIRS): continue if item.name in IGNORE_FILES: continue rel_path = item.relative_to(self.project_path) dest = project_dir / rel_path if item.is_dir(): dest.mkdir(exist_ok=True) else: shutil.copy2(item, dest) def detect_environment(self): """检测Python环境和依赖项""" print("[*] 检测系统环境...") # 获取系统信息 env_info = { "platform": platform.system(), "platform_version": platform.version(), "python_version": platform.python_version(), "yingdao_version": self.detect_yingdao_version(), "environment_vars": dict(os.environ) } # 保存环境信息 with open(self.migration_package / "environment.json", "w") as f: json.dump(env_info, f, indent=2) return env_info def detect_yingdao_version(self): """尝试检测影刀版本""" try: # 方法1: 检查注册表 (Windows) if platform.system() == "Windows": import winreg with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\YingDaoRPA") as key: return winreg.QueryValueEx(key, "Version")[0] # 方法2: 检查安装目录 common_paths = [ Path("/Applications/YingDaoRPA.app/Contents/Info.plist"), Path.home() / "AppData/Local/YingDaoRPA" ] for path in common_paths: if path.exists(): if path.suffix == ".plist": # macOS 解析 plist import plistlib with open(path, "rb") as f: return plistlib.load(f).get("CFBundleShortVersionString") else: # Windows 版本文件 version_file = path / "version.txt" if version_file.exists(): return version_file.read_text().strip() except Exception as e: print(f"警告: 无法检测影刀版本 - {str(e)}") return "unknown" def collect_dependencies(self): """收集Python依赖项""" print("[*] 收集依赖项...") # 查找所有Python文件 for py_file in self.project_path.glob("**/*.py"): with open(py_file, "r", encoding="utf-8") as f: try: content = f.read() # 简单解析导入语句 (实际项目应使用AST) for line in content.splitlines(): line = line.strip() if line.startswith("import ") or line.startswith("from "): parts = line.split() if len(parts) > 1: module = parts[1].split(".")[0] if module and module not in ["os", "sys", "json"]: self.dependencies.add(module) except UnicodeDecodeError: continue # 保存依赖列表 with open(self.migration_package / "requirements.txt", "w") as f: f.write("\n".join(sorted(self.dependencies))) print(f"找到 {len(self.dependencies)} 个依赖: {', '.join(sorted(self.dependencies))}") def create_installer_script(self): """创建安装脚本""" print("[*] 创建安装脚本...") # 根据平台生成不同的安装脚本 installer_path = self.migration_package / "install.bat" if platform.system() == "Windows" else self.migration_package / "install.sh" with open(installer_path, "w") as f: if platform.system() == "Windows": f.write("""@echo off echo 正在安装影刀迁移包... echo. echo [1/3] 创建项目目录... set /p PROJECT_DIR="请输入项目安装路径 (默认: %USERPROFILE%\YingDaoProjects): " if "%PROJECT_DIR%"=="" set PROJECT_DIR=%USERPROFILE%\YingDaoProjects mkdir "%PROJECT_DIR%" >nul 2>&1 echo [2/3] 复制项目文件... xcopy /E /I /Y "project" "%PROJECT_DIR%" echo [3/3] 安装Python依赖... pip install -r requirements.txt echo 迁移完成! 项目已安装到: %PROJECT_DIR% pause """) else: # macOS/Linux f.write("""#!/bin/bash echo "正在安装影刀迁移包..." echo echo "[1/3] 创建项目目录..." read -p "请输入项目安装路径 (默认: ~/YingDaoProjects): " PROJECT_DIR PROJECT_DIR=${PROJECT_DIR:-~/YingDaoProjects} mkdir -p "$ PROJECT_DIR" echo "[2/3] 复制项目文件..." cp -R project/* "$ PROJECT_DIR/" echo "[3/3] 安装Python依赖..." pip3 install -r requirements.txt echo "迁移完成! 项目已安装到: $ PROJECT_DIR" """) # 设置执行权限 (Unix) if platform.system() != "Windows": os.chmod(installer_path, 0o755) def create_migration_package(self): """创建完整的迁移包""" print(f"[*] 创建迁移包: {self.migration_package}") # 清理旧包 if self.migration_package.exists(): shutil.rmtree(self.migration_package) # 创建目录 self.migration_package.mkdir() # 执行迁移步骤 self.validate_project() # 修改后的验证方法 self.collect_project_files() self.detect_environment() self.collect_dependencies() self.create_installer_script() # 添加说明文件 with open(self.migration_package / "README.txt", "w") as f: f.write(f"""影刀应用程序迁移包 版本: {TOOL_VERSION} 生成时间: {subprocess.getoutput('date /t' if platform.system()=='Windows' else 'date')} 项目状态报告: {'✓ 项目验证通过' if self.validator.valid else '⚠ 项目存在问题'} 主文件: {self.validator.main_file.name if self.validator.main_file else '未找到'} 修复操作: {chr(10).join(self.validator.validation_errors) if self.validator.validation_errors else '无'} 使用说明: 1. 将此文件夹复制到目标电脑 2. 运行{'install.bat' if platform.system()=='Windows' else 'install.sh'} 3. 按照提示完成安装 注意: - 目标电脑需要安装相同版本的影刀RPA ({self.detect_yingdao_version()}) - 确保目标电脑已安装Python {platform.python_version()} """) # 压缩为ZIP文件 zip_path = self.migration_package.with_suffix(".zip") with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: for root, _, files in os.walk(self.migration_package): for file in files: file_path = Path(root) / file zipf.write(file_path, arcname=file_path.relative_to(self.migration_package.parent)) print(f"[+] 迁移包创建完成: {zip_path}") return zip_path def main(): parser = argparse.ArgumentParser(description="影刀RPA应用程序迁移工具") parser.add_argument("project_path", help="影刀项目目录路径") args = parser.parse_args() try: migrator = YingDaoMigrator(args.project_path) migrator.create_migration_package() except Exception as e: print(f"[!] 迁移失败: {str(e)}") if isinstance(e, FileNotFoundError): print("请确保指定的路径是有效的影刀项目目录") if __name__ == "__main__": main()

#!/usr/bin/env python # -*- coding: utf-8 -*- import os import re import sys import argparse import xlwt from collections import defaultdict # 分区名称映射表(前缀 → 友好名称) PARTITION_NAME_MAP = { '02_': 'system', '03_': 'vendor', '04_': 'product', '05_': 'odm', '06_': 'my_product', '07_': 'my_engineering', '08_': 'my_stock', '09_': 'my_heytap', '10_': 'my_company', '11_': 'my_carrier', '12_': 'my_region', '13_': 'my_preload', '14_': 'data', '15_': 'my_bigball', '16_': 'my_manifest', '17_system_dlkm': 'system_dlkm', # 新增独立分区 '17_vendor_dlkm': 'vendor_dlkm', # 新增独立分区 '17_cache': 'cache' # 新增独立分区 } def parse_du_file(file_path): """解析du命令输出文件并转换为MB""" data = {} try: with open(file_path, 'r') as f: for line in f: if 'Permission denied' in line or 'No such file' in line or not line.strip(): continue match = re.match(r'(\d+\.?\d*)\s*([KMG]?)[Bb]?\s+(.*)', line.strip()) if match: size, unit, path = match.groups() size = float(size) # 单位转换到MB if unit == 'K': size = size / 1024.0 elif unit == '': size = size / (1024*1024.0) # 默认字节转MB elif unit == 'M': pass # 已经是MB elif unit == 'G': size = size * 1024.0 data[path] = round(size, 4) # 保留两位小数 except IOError as e: print("警告: 无法读取文件 {}: {}".format(file_path, str(e))) return data def extract_file_prefix(filename): """提取文件前缀""" if filename.startswith('17_'): # 移除.txt扩展名后返回完整标识 return filename.replace('.txt', '') match = re.match(r'^(\d+_)', filename) return match.group(1) if match else "other_" def is_main_partition_file(filename, prefix): """检查是否为主分区文件""" # 17_开头的文件都视为主分区文件 if prefix.startswith('17_'): return True # 使用兼容的字符串拼接方式替代f-string expected_name = prefix + PARTITION_NAME_MAP[prefix] + ".txt" return filename == expected_name def generate_dual_report(folder1, folder2, output_xlsx): """生成双机对比报告""" # 获取文件夹名称用于显示 folder1_name = os.path.basename(os.path.normpath(folder1)) folder2_name = os.path.basename(os.path.normpath(folder2)) # 验证文件夹是否存在 for folder in [folder1, folder2]: if not os.path.exists(folder): print("错误: 目录不存在 - {}".format(folder)) return "目录 {} 不存在,请检查路径".format(folder) if not os.path.isdir(folder): print("错误: 路径不是目录 - {}".format(folder)) return "{} 不是有效目录".format(folder) # 初始化数据结构 # 主文件数据(用于总览页) machine1_main_data = {} # {prefix: {path: size}} machine2_main_data = {} # {prefix: {path: size}} # 所有文件数据(用于分区详细页) machine1_all_files = defaultdict(dict) # {filename: {path: size}} machine2_all_files = defaultdict(dict) # {filename: {path: size}} # 收集数据 for folder_path, main_dict, all_dict in [ (folder1, machine1_main_data, machine1_all_files), (folder2, machine2_main_data, machine2_all_files) ]: print("处理目录: {}".format(folder_path)) try: for filename in os.listdir(folder_path): if not filename.endswith('.txt'): continue # 提取文件前缀 prefix = extract_file_prefix(filename) # 跳过01_mount.txt和无效前缀 if prefix == '01_' or prefix not in PARTITION_NAME_MAP: continue file_path = os.path.join(folder_path, filename) partition_name = PARTITION_NAME_MAP[prefix] # 解析文件数据 file_data = parse_du_file(file_path) # 添加到所有文件数据 all_dict[filename] = file_data # 如果是主文件,添加到主文件数据(用于总览页) if is_main_partition_file(filename, prefix): print("解析主分区文件: {}".format(file_path)) main_dict[prefix] = file_data except OSError as e: print("目录访问错误: {}".format(str(e))) return "无法访问目录 {}: {}".format(folder_path, str(e)) # 创建Excel工作簿 try: wb = xlwt.Workbook(encoding='utf-8') header_style = xlwt.easyxf('font: bold on') title_style = xlwt.easyxf('font: bold on, height 280; align: wrap on, vert centre') normal_style = xlwt.easyxf() added_style = xlwt.easyxf('pattern: pattern solid, fore_colour light_green;') removed_style = xlwt.easyxf('pattern: pattern solid, fore_colour rose;') summary_style = xlwt.easyxf('font: bold on, color blue;') # ====== 创建总览Sheet页(添加备注列) ====== ws_overview = wb.add_sheet('总览') print("创建总览Sheet页(仅主文件数据)") current_row = 0 # 写入总览标题(扩展列数) ws_overview.write_merge( current_row, current_row, 0, 5, # 扩展到6列 "存储使用总览(仅主分区文件)", title_style ) current_row += 1 # 写入文件夹名称 ws_overview.write(current_row, 1, folder1_name, header_style) ws_overview.write(current_row, 2, folder2_name, header_style) current_row += 1 # 写入表头(增加备注列) headers = ['分区', '总大小(MB)', '总大小(MB)', '差值(MB)', '标记', '增大TOP路径'] for col, header in enumerate(headers): ws_overview.write(current_row, col, header, header_style) current_row += 1 # 存储各分区汇总数据 overview_data = [] total_machine1 = 0.0 total_machine2 = 0.0 # 按分区顺序处理数据 for prefix in sorted(PARTITION_NAME_MAP.keys()): partition_name = PARTITION_NAME_MAP[prefix] # 跳过data分区 if partition_name == 'data': continue # 获取主文件数据 data1 = machine1_main_data.get(prefix, {}) data2 = machine2_main_data.get(prefix, {}) # 计算主文件总大小 partition_total1 = round(sum(data1.values()), 2) partition_total2 = round(sum(data2.values()), 2) diff = partition_total1 - partition_total2 # 更新总计 total_machine1 += partition_total1 total_machine2 += partition_total2 # 确定标记样式 if diff > 0: mark = "增加" style = added_style elif diff < 0: mark = "减少" style = removed_style else: mark = "无变化" style = normal_style # 计算分区中增大的TOP3路径(仅当分区增大时) top_notes = [] if diff > 0: path_diffs = [] all_paths = set(data1.keys()) | set(data2.keys()) for path in all_paths: size1 = data1.get(path, 0.0) size2 = data2.get(path, 0.0) path_diff = size1 - size2 if path_diff > 0: # 只记录增大的路径 path_diffs.append((path, path_diff)) # 按增大值降序排序,取TOP3 path_diffs.sort(key=lambda x: x[1], reverse=True) for i, (path, diff_val) in enumerate(path_diffs[:3]): # 简化长路径显示 if len(path) > 50: path = "..." + path[-47:] top_notes.append(f"{i+1}. {path}: +{diff_val:.2f}MB") # 保存分区数据 overview_data.append({ 'name': partition_name, 'machine1': partition_total1, 'machine2': partition_total2, 'diff': diff, 'style': style, 'mark': mark, 'notes': "\n".join(top_notes) if top_notes else "无显著增大路径" }) # 写入行数据到总览页(新增备注列) ws_overview.write(current_row, 0, partition_name, style) ws_overview.write(current_row, 1, partition_total1, style) ws_overview.write(current_row, 2, partition_total2, style) ws_overview.write(current_row, 3, diff, style) ws_overview.write(current_row, 4, mark, style) ws_overview.write(current_row, 5, overview_data[-1]['notes'], wrap_style) current_row += 1 # 设置备注列宽度(100字符) ws_overview.col(5).width = 256 * 100 # 添加空行 current_row += 1 # 写入总计行(使用新的差值计算方式) total_diff = total_machine1 - total_machine2 if total_diff > 0: total_mark = "总增加" # folder1 > folder2 total_style = added_style elif total_diff < 0: total_mark = "总减少" # folder1 < folder2 total_style = removed_style else: total_mark = "无变化" total_style = normal_style ws_overview.write(current_row, 0, "总计", header_style) ws_overview.write(current_row, 1, total_machine1, header_style) ws_overview.write(current_row, 2, total_machine2, header_style) ws_overview.write(current_row, 3, total_diff, header_style) ws_overview.write(current_row, 4, total_mark, header_style) # ====== 为每个文件创建单独的Sheet页 ====== # 获取所有唯一的文件名(两个文件夹的并集) all_filenames = sorted(set(machine1_all_files.keys()) | set(machine2_all_files.keys())) for filename in all_filenames: # 提取文件前缀 prefix = extract_file_prefix(filename) # 跳过无效前缀 if prefix not in PARTITION_NAME_MAP: continue # 获取分区名称 partition_name = PARTITION_NAME_MAP[prefix] # 创建Sheet页名称(文件名不带扩展名) sheet_name = filename.replace('.txt', '') if len(sheet_name) > 31: # Excel sheet名称长度限制 sheet_name = sheet_name[:31] # 创建Sheet页 ws = wb.add_sheet(sheet_name) print("创建文件Sheet页: {}".format(sheet_name)) # 当前行指针 current_row = 0 # 写入分区标题 title = "分区: {} - 文件: {}".format(partition_name, filename) ws.write_merge( current_row, current_row, 0, 5, title, title_style ) current_row += 1 # 写入文件夹名称(新格式) ws.write_merge(current_row, current_row, 0, 1, folder1_name, header_style) ws.write_merge(current_row, current_row, 2, 3, folder2_name, header_style) ws.write(current_row, 4, "差异(M)", header_style) ws.write(current_row, 5, "标记", header_style) current_row += 1 # 写入表头(新格式) headers = ['路径', '大小(M)', '路径', '大小(M)', '差异(M)', '标记'] for col, header in enumerate(headers): ws.write(current_row, col, header, header_style) current_row += 1 # 获取文件数据 data1 = machine1_all_files.get(filename, {}) data2 = machine2_all_files.get(filename, {}) # 获取所有路径(合并两个文件夹的路径) all_paths = sorted(set(data1.keys()) | set(data2.keys())) # 初始化变化统计数据 total_increase = 0.0 # 增大总和 total_decrease = 0.0 # 减小总和 total_added = 0.0 # 新增文件总和 total_removed = 0.0 # 去除文件总和 # 写入数据行(新格式) for path in all_paths: size1 = data1.get(path, 0.0) size2 = data2.get(path, 0.0) # 修改为 folder1 - folder2 的差值计算方式 diff = size1 - size2 # 确定标记和样式(根据新的差值计算方式调整) if size1 == 0 and size2 > 0: mark = "除去" cell_style = removed_style total_removed += size2 elif size1 > 0 and size2 == 0: mark = "新增" cell_style = added_style total_added += size1 else: if diff > 0: mark = "增大" cell_style = added_style total_increase += diff elif diff < 0: mark = "减小" cell_style = removed_style total_decrease += abs(diff) else: mark = "相同" cell_style = normal_style # 写入行数据(新格式) # folder1列 if size1 > 0: ws.write(current_row, 0, path, cell_style) ws.write(current_row, 1, size1, cell_style) else: ws.write(current_row, 0, "", cell_style) ws.write(current_row, 1, "", cell_style) # folder2列 if size2 > 0: ws.write(current_row, 2, path, cell_style) ws.write(current_row, 3, size2, cell_style) else: ws.write(current_row, 2, "", cell_style) ws.write(current_row, 3, "", cell_style) # 差异和标记列 ws.write(current_row, 4, diff, cell_style) ws.write(current_row, 5, mark, cell_style) current_row += 1 # 添加文件汇总行(新格式) file_total1 = sum(data1.values()) file_total2 = sum(data2.values()) file_diff = file_total1 - file_total2 # 写入汇总行(新格式) ws.write(current_row, 0, "文件汇总", header_style) ws.write(current_row, 1, file_total1, header_style) ws.write(current_row, 2, "", header_style) ws.write(current_row, 3, file_total2, header_style) ws.write(current_row, 4, file_diff, header_style) ws.write(current_row, 5, "", header_style) current_row += 1 # 添加变化分类统计行 - 修改为Python 2.7兼容的格式 message = ( u"{partition_name}路径下: " u"减小{total_decrease:.2f}M " u"增大{total_increase:.2f}M " u"新增文件{total_added:.2f}M " u"减少文件{total_removed:.2f}M" ).format( partition_name=partition_name, total_decrease=total_decrease, total_increase=total_increase, total_added=total_added, total_removed=total_removed ) ws.write_merge( current_row, current_row, 0, 5, message, summary_style ) # 保存文件 wb.save(output_xlsx) return "对比报告已成功生成: {}".format(output_xlsx) except Exception as e: import traceback traceback.print_exc() return "生成Excel文件时出错: {}".format(str(e)) def generate_single_report(folder, output_xlsx): """生成单机拆解报告""" # 获取文件夹名称用于显示 folder_name = os.path.basename(os.path.normpath(folder)) # 验证文件夹是否存在 if not os.path.exists(folder): print("错误: 目录不存在 - {}".format(folder)) return "目录 {} 不存在,请检查路径".format(folder) if not os.path.isdir(folder): print("错误: 路径不是目录 - {}".format(folder)) return "{} 不是有效目录".format(folder) # 初始化数据结构 main_data = {} # {prefix: {path: size}} 主文件数据 all_files = defaultdict(dict) # {filename: {path: size}} 所有文件数据 print("处理目录: {}".format(folder)) try: for filename in os.listdir(folder): if not filename.endswith('.txt'): continue # 提取文件前缀 prefix = extract_file_prefix(filename) # 跳过01_mount.txt和无效前缀 if prefix == '01_' or prefix not in PARTITION_NAME_MAP: continue file_path = os.path.join(folder, filename) partition_name = PARTITION_NAME_MAP[prefix] # 解析文件数据 file_data = parse_du_file(file_path) # 添加到所有文件数据 all_files[filename] = file_data # 如果是主文件,添加到主文件数据(用于总览页) if is_main_partition_file(filename, prefix): print("解析主分区文件: {}".format(file_path)) main_data[prefix] = file_data except OSError as e: print("目录访问错误: {}".format(str(e))) return "无法访问目录 {}: {}".format(folder, str(e)) # 创建Excel工作簿 try: wb = xlwt.Workbook(encoding='utf-8') header_style = xlwt.easyxf('font: bold on') title_style = xlwt.easyxf('font: bold on, height 280; align: wrap on, vert centre') normal_style = xlwt.easyxf() # ====== 创建总览Sheet页(只包含主文件数据) ====== ws_overview = wb.add_sheet('总览') print("创建总览Sheet页(仅主文件数据)") # 当前行指针 current_row = 0 # 写入总览标题 ws_overview.write_merge( current_row, current_row, 0, 1, "存储使用总览(仅主分区文件) - 单机报告", title_style ) current_row += 1 # 写入文件夹名称 ws_overview.write(current_row, 0, "分区", header_style) ws_overview.write(current_row, 1, "总大小(MB)", header_style) current_row += 1 # 存储各分区汇总数据 total_size = 0.0 # 按分区顺序处理数据 for prefix in sorted(PARTITION_NAME_MAP.keys()): partition_name = PARTITION_NAME_MAP[prefix] # 跳过data分区 if partition_name == 'data': continue # 获取主文件数据 data = main_data.get(prefix, {}) partition_total = round(sum(data.values()), 2) total_size += partition_total # 写入行数据到总览页 ws_overview.write(current_row, 0, partition_name, normal_style) ws_overview.write(current_row, 1, partition_total, normal_style) current_row += 1 # 添加空行 current_row += 1 # 写入总计行 ws_overview.write(current_row, 0, "总计", header_style) ws_overview.write(current_row, 1, total_size, header_style) # ====== 为每个文件创建单独的Sheet页 ====== for filename in sorted(all_files.keys()): # 提取文件前缀 prefix = extract_file_prefix(filename) # 跳过无效前缀 if prefix not in PARTITION_NAME_MAP: continue # 获取分区名称 partition_name = PARTITION_NAME_MAP[prefix] # 创建Sheet页名称(文件名不带扩展名) sheet_name = filename.replace('.txt', '') if len(sheet_name) > 31: # Excel sheet名称长度限制 sheet_name = sheet_name[:31] # 创建Sheet页 ws = wb.add_sheet(sheet_name) print("创建文件Sheet页: {}".format(sheet_name)) # 当前行指针 current_row = 0 # 写入分区标题 title = "分区: {} - 文件: {}".format(partition_name, filename) ws.write_merge( current_row, current_row, 0, 1, title, title_style ) current_row += 1 # 写入表头 headers = ['路径', '大小(M)'] for col, header in enumerate(headers): ws.write(current_row, col, header, header_style) current_row += 1 # 获取文件数据 data = all_files[filename] # 按路径排序 sorted_paths = sorted(data.keys()) # 写入数据行 for path in sorted_paths: size = data[path] ws.write(current_row, 0, path, normal_style) ws.write(current_row, 1, size, normal_style) current_row += 1 # 写入汇总行 file_total = sum(data.values()) ws.write(current_row, 0, "文件汇总", header_style) ws.write(current_row, 1, file_total, header_style) # 保存文件 wb.save(output_xlsx) return "单机报告已成功生成: {}".format(output_xlsx) except Exception as e: import traceback traceback.print_exc() return "生成Excel文件时出错: {}".format(str(e)) if __name__ == "__main__": # 创建参数解析器 parser = argparse.ArgumentParser(description='存储空间分析工具') subparsers = parser.add_subparsers(dest='mode', help='运行模式') # 双机对比模式 dual_parser = subparsers.add_parser('dual', help='双机对比模式') dual_parser.add_argument('folder1', help='第一个文件夹路径') dual_parser.add_argument('folder2', help='第二个文件夹路径') dual_parser.add_argument('output', help='输出Excel文件路径') # 单机拆解模式 single_parser = subparsers.add_parser('single', help='单机拆解模式') single_parser.add_argument('folder', help='待分析文件夹路径') single_parser.add_argument('output', help='输出Excel文件路径') # 解析参数 args = parser.parse_args() if args.mode == 'dual': print("运行双机对比模式...") result = generate_dual_report(args.folder1, args.folder2, args.output) elif args.mode == 'single': print("运行单机拆解模式...") result = generate_single_report(args.folder, args.output) else: result = "错误:请选择 'dual' 或 'single' 模式" print(result)V27960021@dg03podv27960021kj4p:~/存储/hawaii$ python storage.py dual ./22610/存储/测试机 ./22610/存储/对比机 22610_report_dual.xlsx File "storage.py", line 222 top_notes.append(f"{i+1}. {path}: +{diff_val:.2f}MB") ^ SyntaxError: invalid syntax

Traceback (most recent call last): File “d:\海康\MVS\Development\Samples\Python\MvImport\two.py”, line 15, in from CamOperation_class import CameraOperation File “d:\海康\MVS\Development\Samples\Python\MvImport\CamOperation_class.py”, line 40 if not file_path.startswith(r"\?"): ^ SyntaxError: EOL while scanning string literal这是我运行下面这个代码出现的错误 # -*- coding: utf-8 -*- import sys import os import cv2 import numpy as np from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QWidget, QVBoxLayout, QHBoxLayout, QMessageBox, QLabel, QFileDialog, QToolBar, QComboBox, QStatusBar) from PyQt5.QtCore import QRect, Qt from CamOperation_class import CameraOperation sys.path.append("D:\\海康\\MVS\\Development\\Samples\\Python\\BasicDemo") from MvCameraControl_class import * from MvErrorDefine_const import * from CameraParams_header import * from PyUICBasicDemo import Ui_MainWindow import ctypes from datetime import datetime # 全局变量 current_sample_path = "" # 当前使用的样本路径 detection_history = [] # 检测历史记录 # 布料印花检测函数 def check_print_quality(sample_image_path, test_image_path, threshold=0.05): """ 检测布料印花是否合格,并在合格样本上标出错误位置 :param sample_image_path: 合格样本图像路径 :param test_image_path: 待检测图像路径 :param threshold: 差异阈值,超过该值则认为印花不合格 :return: 是否合格,差异值,带有错误标记的合格样本图像 """ # 读取图像 sample_image = cv2.imread(sample_image_path, cv2.IMREAD_GRAYSCALE) test_image = cv2.imread(test_image_path, cv2.IMREAD_GRAYSCALE) if sample_image is None or test_image is None: print("无法加载图像,请检查路径是否正确!") return None, None, None # 确保两个图像大小一致 test_image = cv2.resize(test_image, (sample_image.shape[1], sample_image.shape[0])) # 计算两个图像之间的差异 diff = cv2.absdiff(sample_image, test_image) # 将差异图像二值化 ret, diff_binary = cv2.threshold(diff, 50, 255, cv2.THRESH_BINARY) # 计算差异的占比 diff_ratio = np.sum(diff_binary) / (diff_binary.shape[0] * diff_binary.shape[1] * 255) # 修正计算方式 # 判断是否合格 is_qualified = diff_ratio < threshold # 在合格样本上标出错误位置 if is_qualified: marked_image = cv2.cvtColor(sample_image, cv2.COLOR_GRAY2BGR) else: marked_image = cv2.cvtColor(sample_image, cv2.COLOR_GRAY2BGR) contours, _ = cv2.findContours(diff_binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(marked_image, contours, -1, (0, 0, 255), 2) return is_qualified, diff_ratio, marked_image # 布料印花检测功能 def check_print(): global isGrabbing, obj_cam_operation, current_sample_path, detection_history if not isGrabbing: QMessageBox.warning(mainWindow, "错误", "请先开始取流并捕获图像!", QMessageBox.Ok) return # 检查样本路径是否有效 if not current_sample_path or not os.path.exists(current_sample_path): QMessageBox.warning(mainWindow, "错误", "请先设置有效的标准样本图像!", QMessageBox.Ok) return # 保存当前帧作为测试图像 test_path = "temp_test_image.bmp" ret = obj_cam_operation.Save_Bmp() if ret != MV_OK: QMessageBox.warning(mainWindow, "错误", "保存测试图像失败!", QMessageBox.Ok) return # 执行印花检测 is_qualified, diff_ratio, marked_image = check_print_quality(current_sample_path, test_path) if marked_image is not None: # 显示结果 result_text = f"印花是否合格: {'合格' if is_qualified else '不合格'}\n差异占比: {diff_ratio:.4f}" QMessageBox.information(mainWindow, "检测结果", result_text, QMessageBox.Ok) # 显示标记图像 cv2.imshow("缺陷标记结果", marked_image) cv2.waitKey(0) cv2.destroyAllWindows() # 记录检测结果 detection_result = { 'timestamp': datetime.now(), 'qualified': is_qualified, 'diff_ratio': diff_ratio, 'sample_path': current_sample_path, 'test_path': test_path } detection_history.append(detection_result) update_history_display() # 保存标准样本函数 def save_sample_image(): global isGrabbing, obj_cam_operation, current_sample_path if not isGrabbing: QMessageBox.warning(mainWindow, "错误", "请先开始取流并捕获图像!", QMessageBox.Ok) return # 弹出文件保存对话框 file_path, _ = QFileDialog.getSaveFileName( mainWindow, "保存标准样本图像", "", "BMP Files (*.bmp);;PNG Files (*.png);;JPEG Files (*.jpg)" ) if not file_path: return # 用户取消保存 # 获取文件扩展名 file_extension = os.path.splitext(file_path)[1].lower() # 根据扩展名设置保存格式 if file_extension == ".bmp": save_format = "bmp" elif file_extension == ".png": save_format = "png" elif file_extension == ".jpg" or file_extension == ".jpeg": save_format = "jpeg" else: QMessageBox.warning(mainWindow, "错误", "不支持的文件格式!", QMessageBox.Ok) return # 保存当前帧作为标准样本 ret = obj_cam_operation.Save_Image(file_path, save_format) if ret != MV_OK: strError = "保存样本图像失败: " + ToHexStr(ret) QMessageBox.warning(mainWindow, "错误", strError, QMessageBox.Ok) else: QMessageBox.information(mainWindow, "成功", f"标准样本已保存至:\n{file_path}", QMessageBox.Ok) # 更新当前样本路径 current_sample_path = file_path update_sample_display() # 预览当前样本 def preview_sample(): global current_sample_path if not current_sample_path or not os.path.exists(current_sample_path): QMessageBox.warning(mainWindow, "错误", "请先设置有效的标准样本图像!", QMessageBox.Ok) return try: sample_img = cv2.imread(current_sample_path) if sample_img is None: raise Exception("无法加载图像") cv2.imshow("标准样本预览", sample_img) except Exception as e: QMessageBox.warning(mainWindow, "错误", f"预览样本失败: {str(e)}", QMessageBox.Ok) # 更新样本路径显示 def update_sample_display(): global current_sample_path if current_sample_path: ui.lblSamplePath.setText(f"当前样本: {os.path.basename(current_sample_path)}") ui.lblSamplePath.setToolTip(current_sample_path) else: ui.lblSamplePath.setText("当前样本: 未设置样本") # 更新历史记录显示 def update_history_display(): global detection_history ui.cbHistory.clear() for i, result in enumerate(detection_history[-10:]): # 显示最近10条记录 timestamp = result['timestamp'].strftime("%H:%M:%S") status = "合格" if result['qualified'] else "不合格" ratio = f"{result['diff_ratio']:.4f}" ui.cbHistory.addItem(f"[{timestamp}] {status} - 差异: {ratio}") # 获取选取设备信息的索引,通过[]之间的字符去解析 def TxtWrapBy(start_str, end, all): start = all.find(start_str) if start >= 0: start += len(start_str) end = all.find(end, start) if end >= 0: return all[start:end].strip() # 将返回的错误码转换为十六进制显示 def ToHexStr(num): chaDic = {10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f'} hexStr = "" if num < 0: num = num + 2 ** 32 while num >= 16: digit = num % 16 hexStr = chaDic.get(digit, str(digit)) + hexStr num //= 16 hexStr = chaDic.get(num, str(num)) + hexStr return hexStr # ch:初始化SDK | en: initialize SDK MvCamera.MV_CC_Initialize() global deviceList deviceList = MV_CC_DEVICE_INFO_LIST() global cam cam = MvCamera() global nSelCamIndex nSelCamIndex = 0 global obj_cam_operation obj_cam_operation = 0 global isOpen isOpen = False global isGrabbing isGrabbing = False global isCalibMode # 是否是标定模式(获取原始图像) isCalibMode = True # 绑定下拉列表至设备信息索引 def xFunc(event): global nSelCamIndex nSelCamIndex = TxtWrapBy("[", "]", ui.ComboDevices.get()) # Decoding Characters def decoding_char(c_ubyte_value): c_char_p_value = ctypes.cast(c_ubyte_value, ctypes.c_char_p) try: decode_str = c_char_p_value.value.decode('gbk') # Chinese characters except UnicodeDecodeError: decode_str = str(c_char_p_value.value) return decode_str # ch:枚举相机 | en:enum devices def enum_devices(): global deviceList global obj_cam_operation deviceList = MV_CC_DEVICE_INFO_LIST() n_layer_type = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE) ret = MvCamera.MV_CC_EnumDevices(n_layer_type, deviceList) if ret != 0: strError = "Enum devices fail! ret = :" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) return ret if deviceList.nDeviceNum == 0: QMessageBox.warning(mainWindow, "Info", "Find no device", QMessageBox.Ok) return ret print("Find %d devices!" % deviceList.nDeviceNum) devList = [] for i in range(0, deviceList.nDeviceNum): mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE: print("\ngige device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24) nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16) nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8) nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff) print("current ip: %d.%d.%d.%d " % (nip1, nip2, nip3, nip4)) devList.append( "[" + str(i) + "]GigE: " + user_defined_name + " " + model_name + "(" + str(nip1) + "." + str( nip2) + "." + str(nip3) + "." + str(nip4) + ")") elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE: print("\nu3v device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]USB: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE: print("\nCML device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]CML: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE: print("\nCXP device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]CXP: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE: print("\nXoF device: [%d]" % i) user_defined_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chUserDefinedName) model_name = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName) print("device user define name: " + user_defined_name) print("device model name: " + model_name) strSerialNumber = "" for per in mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber: if per == 0: break strSerialNumber = strSerialNumber + chr(per) print("user serial number: " + strSerialNumber) devList.append("[" + str(i) + "]XoF: " + user_defined_name + " " + model_name + "(" + str(strSerialNumber) + ")") ui.ComboDevices.clear() ui.ComboDevices.addItems(devList) ui.ComboDevices.setCurrentIndex(0) # ch:打开相机 | en:open device def open_device(): global deviceList global nSelCamIndex global obj_cam_operation global isOpen if isOpen: QMessageBox.warning(mainWindow, "Error", 'Camera is Running!', QMessageBox.Ok) return MV_E_CALLORDER nSelCamIndex = ui.ComboDevices.currentIndex() if nSelCamIndex < 0: QMessageBox.warning(mainWindow, "Error", 'Please select a camera!', QMessageBox.Ok) return MV_E_CALLORDER obj_cam_operation = CameraOperation(cam, deviceList, nSelCamIndex) ret = obj_cam_operation.Open_device() if 0 != ret: strError = "Open device failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) isOpen = False else: set_continue_mode() get_param() isOpen = True enable_controls() # ch:开始取流 | en:Start grab image def start_grabbing(): global obj_cam_operation global isGrabbing ret = obj_cam_operation.Start_grabbing(ui.widgetDisplay.winId()) if ret != 0: strError = "Start grabbing failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: isGrabbing = True enable_controls() # ch:停止取流 | en:Stop grab image def stop_grabbing(): global obj_cam_operation global isGrabbing ret = obj_cam_operation.Stop_grabbing() if ret != 0: strError = "Stop grabbing failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: isGrabbing = False enable_controls() # ch:关闭设备 | Close device def close_device(): global isOpen global isGrabbing global obj_cam_operation if isOpen: obj_cam_operation.Close_device() isOpen = False isGrabbing = False enable_controls() # ch:设置触发模式 | en:set trigger mode def set_continue_mode(): ret = obj_cam_operation.Set_trigger_mode(False) if ret != 0: strError = "Set continue mode failed ret:" + ToHexStr(ret) + " mode is " + str(is_trigger_mode) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: ui.radioContinueMode.setChecked(True) ui.radioTriggerMode.setChecked(False) ui.bnSoftwareTrigger.setEnabled(False) # ch:设置软触发模式 | en:set software trigger mode def set_software_trigger_mode(): ret = obj_cam_operation.Set_trigger_mode(True) if ret != 0: strError = "Set trigger mode failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: ui.radioContinueMode.setChecked(False) ui.radioTriggerMode.setChecked(True) ui.bnSoftwareTrigger.setEnabled(isGrabbing) # ch:设置触发命令 | en:set trigger software def trigger_once(): ret = obj_cam_operation.Trigger_once() if ret != 0: strError = "TriggerSoftware failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) # ch:存图 | en:save image def save_bmp(): ret = obj_cam_operation.Save_Bmp() if ret != MV_OK: strError = "Save BMP failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: print("Save image success") def is_float(str): try: float(str) return True except ValueError: return False # ch: 获取参数 | en:get param def get_param(): ret = obj_cam_operation.Get_parameter() if ret != MV_OK: strError = "Get param failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) else: ui.edtExposureTime.setText("{0:.2f}".format(obj_cam_operation.exposure_time)) ui.edtGain.setText("{0:.2f}".format(obj_cam_operation.gain)) ui.edtFrameRate.setText("{0:.2f}".format(obj_cam_operation.frame_rate)) # ch: 设置参数 | en:set param def set_param(): frame_rate = ui.edtFrameRate.text() exposure = ui.edtExposureTime.text() gain = ui.edtGain.text() if is_float(frame_rate)!=True or is_float(exposure)!=True or is_float(gain)!=True: strError = "Set param failed ret:" + ToHexStr(MV_E_PARAMETER) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) return MV_E_PARAMETER ret = obj_cam_operation.Set_parameter(frame_rate, exposure, gain) if ret != MV_OK: strError = "Set param failed ret:" + ToHexStr(ret) QMessageBox.warning(mainWindow, "Error", strError, QMessageBox.Ok) return MV_OK # ch: 设置控件状态 | en:set enable status def enable_controls(): global isGrabbing global isOpen # 先设置group的状态,再单独设置各控件状态 ui.groupGrab.setEnabled(isOpen) ui.groupParam.setEnabled(isOpen) ui.bnOpen.setEnabled(not isOpen) ui.bnClose.setEnabled(isOpen) ui.bnStart.setEnabled(isOpen and (not isGrabbing)) ui.bnStop.setEnabled(isOpen and isGrabbing) ui.bnSoftwareTrigger.setEnabled(isGrabbing and ui.radioTriggerMode.isChecked()) ui.bnSaveImage.setEnabled(isOpen and isGrabbing) # 添加检测按钮控制 ui.bnCheckPrint.setEnabled(isOpen and isGrabbing) ui.bnSaveSample.setEnabled(isOpen and isGrabbing) ui.bnPreviewSample.setEnabled(bool(current_sample_path)) if __name__ == "__main__": # ch:初始化SDK | en: initialize SDK MvCamera.MV_CC_Initialize() deviceList = MV_CC_DEVICE_INFO_LIST() cam = MvCamera() nSelCamIndex = 0 obj_cam_operation = 0 isOpen = False isGrabbing = False isCalibMode = True # 是否是标定模式(获取原始图像) # 初始化UI app = QApplication(sys.argv) mainWindow = QMainWindow() ui = Ui_MainWindow() ui.setupUi(mainWindow) # 扩大主窗口尺寸 mainWindow.resize(1200, 800) # 宽度1200,高度800 # 创建工具栏 toolbar = mainWindow.addToolBar("检测工具") # 添加检测按钮 ui.bnCheckPrint = QPushButton("检测印花质量") toolbar.addWidget(ui.bnCheckPrint) # 添加保存样本按钮 ui.bnSaveSample = QPushButton("保存标准样本") toolbar.addWidget(ui.bnSaveSample) # 添加预览样本按钮 ui.bnPreviewSample = QPushButton("预览样本") toolbar.addWidget(ui.bnPreviewSample) # 添加历史记录下拉框 ui.cbHistory = QComboBox() ui.cbHistory.setMinimumWidth(300) toolbar.addWidget(QLabel("历史记录:")) toolbar.addWidget(ui.cbHistory) # 添加当前样本显示标签 ui.lblSamplePath = QLabel("当前样本: 未设置样本") status_bar = mainWindow.statusBar() status_bar.addPermanentWidget(ui.lblSamplePath) # 绑定按钮事件 ui.bnCheckPrint.clicked.connect(check_print) ui.bnSaveSample.clicked.connect(save_sample_image) ui.bnPreviewSample.clicked.connect(preview_sample) # 绑定其他按钮事件 ui.bnEnum.clicked.connect(enum_devices) ui.bnOpen.clicked.connect(open_device) ui.bnClose.clicked.connect(close_device) ui.bnStart.clicked.connect(start_grabbing) ui.bnStop.clicked.connect(stop_grabbing) ui.bnSoftwareTrigger.clicked.connect(trigger_once) ui.radioTriggerMode.clicked.connect(set_software_trigger_mode) ui.radioContinueMode.clicked.connect(set_continue_mode) ui.bnGetParam.clicked.connect(get_param) ui.bnSetParam.clicked.connect(set_param) ui.bnSaveImage.clicked.connect(save_bmp) # 显示主窗口 mainWindow.show() # 执行应用 app.exec_() # 关闭设备 close_device() # ch:反初始化SDK | en: finalize SDK MvCamera.MV_CC_Finalize() sys.exit()

大家在看

recommend-type

金蝶EAS通过套打模板实现后台生成PDF文件.docx

在EAS开发中,如果需要合同调用套打模板自动生成PDF进行档案归档备份,可通过后台服务器代码进行开发实现;
recommend-type

复盛压缩机选型软件.rar )

此款为官方专用,简单的压缩机可以选择。SRL型的没有,暂时不能使用请谨慎选择
recommend-type

基于边折叠的网格快速简化

Fast mesh simplification via edge collapsing This project contains an implementation of a "multiple choice" mesh simplfication algorithm. Over a number of iterations a random fraction of the total edges in the supplied mesh are processed with a subset of these processed edges collapsed (the lowest scoring collapses win when a collision occurs). The only non-standard dependency is the qef_simd.h single file header which you can find in my "qef" project, a version is also included here.
recommend-type

20201107-为rvv-llvm添加一个intrinsic-廖春玉1

3. multiclass signed_binary_v_vv_vx { 4. multiclass Binary<string name,/ string
recommend-type

一种低噪声便携式的心电监测仪设计

便携式监护仪小型方便,结构简单,性能稳定,可以随身携带,可由电池供电,一般用于非监护室及外出抢救病人的监护。心血管疾病是人类生命的最主要威胁之一,而心电(Electrocardiogram,ECG信号是诊断心血管疾病的主要依据,因此实时监测病人心电活动,设计自动采集病人心电信号的便携式系统具有重要意义。本文为人体日常生活方便,设计了导联电极脱落检测电路,防止运动输入电极脱落。

最新推荐

recommend-type

2014年直流电压电流采样仪生产方案:电路板、BOM单、STM单片机程序及应用 核心版

2014年设计的一款直流电压电流采样仪的整套产品生产方案。该产品已量产1000余套,适用于电力、电子、通信等领域。文中涵盖了硬件和软件两大部分的内容。硬件方面,包括电路板设计、BOM单、外围器件清单以及外壳设计;软件方面,则涉及STM单片机程序和配套的上位机电脑软件。该采样仪的最大测量范围为1000V/100A,具备高精度、高稳定性的特点,能记录并存储8组电压电流数据,并带有触发模式用于实时监测和故障诊断。 适合人群:从事电力、电子、通信领域的工程师和技术人员,尤其是对直流电压电流采样仪有需求的研发人员。 使用场景及目标:①帮助工程师和技术人员了解直流电压电流采样仪的整体设计方案;②提供详细的硬件和软件资料,便于实际生产和应用;③适用于需要高精度、高稳定性的电压电流测量场合。 其他说明:该产品已经成功量产并获得市场好评,文中提供的方案对于相关领域的项目开发具有重要参考价值。
recommend-type

springboot基于安卓的旅游景点导览APP的设计与实现论文

springboot旅游景点导览APP的设计与实现
recommend-type

SaaS 被 AI 工具替代风险与安全治理对比.doc

SaaS 被 AI 工具替代风险与安全治理对比.doc
recommend-type

Python程序TXLWizard生成TXL文件及转换工具介绍

### 知识点详细说明: #### 1. 图形旋转与TXL向导 图形旋转是图形学领域的一个基本操作,用于改变图形的方向。在本上下文中,TXL向导(TXLWizard)是由Esteban Marin编写的Python程序,它实现了特定的图形旋转功能,主要用于电子束光刻掩模的生成。光刻掩模是半导体制造过程中非常关键的一个环节,它确定了在硅片上沉积材料的精确位置。TXL向导通过生成特定格式的TXL文件来辅助这一过程。 #### 2. TXL文件格式与用途 TXL文件格式是一种基于文本的文件格式,它设计得易于使用,并且可以通过各种脚本语言如Python和Matlab生成。这种格式通常用于电子束光刻中,因为它的文本形式使得它可以通过编程快速创建复杂的掩模设计。TXL文件格式支持引用对象和复制对象数组(如SREF和AREF),这些特性可以用于优化电子束光刻设备的性能。 #### 3. TXLWizard的特性与优势 - **结构化的Python脚本:** TXLWizard 使用结构良好的脚本来创建遮罩,这有助于开发者创建清晰、易于维护的代码。 - **灵活的Python脚本:** 作为Python程序,TXLWizard 可以利用Python语言的灵活性和强大的库集合来编写复杂的掩模生成逻辑。 - **可读性和可重用性:** 生成的掩码代码易于阅读,开发者可以轻松地重用和修改以适应不同的需求。 - **自动标签生成:** TXLWizard 还包括自动为图形对象生成标签的功能,这在管理复杂图形时非常有用。 #### 4. TXL转换器的功能 - **查看.TXL文件:** TXL转换器(TXLConverter)允许用户将TXL文件转换成HTML或SVG格式,这样用户就可以使用任何现代浏览器或矢量图形应用程序来查看文件。 - **缩放和平移:** 转换后的文件支持缩放和平移功能,这使得用户在图形界面中更容易查看细节和整体结构。 - **快速转换:** TXL转换器还提供快速的文件转换功能,以实现有效的蒙版开发工作流程。 #### 5. 应用场景与技术参考 TXLWizard的应用场景主要集中在电子束光刻技术中,特别是用于设计和制作半导体器件时所需的掩模。TXLWizard作为一个向导,不仅提供了生成TXL文件的基础框架,还提供了一种方式来优化掩模设计,提高光刻过程的效率和精度。对于需要进行光刻掩模设计的工程师和研究人员来说,TXLWizard提供了一种有效的方法来实现他们的设计目标。 #### 6. 系统开源特性 标签“系统开源”表明TXLWizard遵循开放源代码的原则,这意味着源代码对所有人开放,允许用户自由地查看、修改和分发软件。开源项目通常拥有活跃的社区,社区成员可以合作改进软件,添加新功能,或帮助解决遇到的问题。这种开放性促进了技术创新,并允许用户根据自己的需求定制软件。 #### 7. 压缩包子文件的文件名称列表 文件名称列表中的“txlwizard-master”可能指的是TXLWizard项目的主版本库或主分支。这个名称表明了这是项目源代码的中心点,其他开发者会从这个主分支拉取代码进行合作开发或部署。以“-master”结尾通常是版本控制系统中表示主要开发线路的常见约定,例如Git中的master(现在更常被称为main)分支。 通过这些知识点的详细解释,我们可以看到TXLWizard不仅是一个用于生成TXL文件的工具,它还整合了一系列的功能,使得电子束光刻掩模的设计工作更为高效和直观。同时,作为一个开源项目,它能够借助社区的力量不断进步,为用户带来更多的便利和创新。
recommend-type

【创新图生成:扣子平台的技术前沿与创新思维】:引领图像生成技术的新潮流

# 1. 扣子平台的图像生成技术概述 在当今数字化时代,图像生成技术正成为推动技术创新和社会变革的关键力量。扣子平台正是这一技术革新的代表。本章将对扣子平台的图像生成技术进行一个全面概述,探究它如何将理论技术转化为实际应用,并带来显著的社会影响。 扣子平台运用了先进的图像生成技术,将现实世界和数字空间无缝融合,打造了沉浸式的视觉体验。其技术的核心是通过高度复杂的算法模型,快速生成高质
recommend-type

海康威视机器视觉工程师考核

<think>好的,我现在需要帮助用户了解海康威视机器视觉工程师的考核内容和要求。根据用户提供的引用资料,特别是引用[1]和[2],里面提到了考核素材包分为初级和中级,涵盖理论、算法、应用案例等。首先,我要整理这些信息,确保结构清晰,符合用户要求的格式。 接下来,我需要确认素材包的具体内容,比如初级和中级的不同点。引用[2]提到初级包含基础理论、算法实现和实际案例,中级则增加复杂算法和项目分析。这部分需要分点说明,方便用户理解层次。 另外,用户可能想知道如何准备考核,比如下载素材、学习顺序、模拟考核等,引用[2]中有使用说明和注意事项,这部分也要涵盖进去。同时要注意提醒用户考核窗口已关闭,
recommend-type

Linux环境下Docker Hub公共容器映像检测工具集

在给出的知识点中,我们需要详细解释有关Docker Hub、公共容器映像、容器编排器以及如何与这些工具交互的详细信息。同时,我们会涵盖Linux系统下的相关操作和工具使用,以及如何在ECS和Kubernetes等容器编排工具中运用这些检测工具。 ### Docker Hub 和公共容器映像 Docker Hub是Docker公司提供的一项服务,它允许用户存储、管理以及分享Docker镜像。Docker镜像可以视为应用程序或服务的“快照”,包含了运行特定软件所需的所有必要文件和配置。公共容器映像指的是那些被标记为公开可见的Docker镜像,任何用户都可以拉取并使用这些镜像。 ### 静态和动态标识工具 静态和动态标识工具在Docker Hub上用于识别和分析公共容器映像。静态标识通常指的是在不运行镜像的情况下分析镜像的元数据和内容,例如检查Dockerfile中的指令、环境变量、端口映射等。动态标识则需要在容器运行时对容器的行为和性能进行监控和分析,如资源使用率、网络通信等。 ### 容器编排器与Docker映像 容器编排器是用于自动化容器部署、管理和扩展的工具。在Docker环境中,容器编排器能够自动化地启动、停止以及管理容器的生命周期。常见的容器编排器包括ECS和Kubernetes。 - **ECS (Elastic Container Service)**:是由亚马逊提供的容器编排服务,支持Docker容器,并提供了一种简单的方式来运行、停止以及管理容器化应用程序。 - **Kubernetes**:是一个开源平台,用于自动化容器化应用程序的部署、扩展和操作。它已经成为容器编排领域的事实标准。 ### 如何使用静态和动态标识工具 要使用这些静态和动态标识工具,首先需要获取并安装它们。从给定信息中了解到,可以通过克隆仓库或下载压缩包并解压到本地系统中。之后,根据需要针对不同的容器编排环境(如Dockerfile、ECS、Kubernetes)编写配置,以集成和使用这些检测工具。 ### Dockerfile中的工具使用 在Dockerfile中使用工具意味着将检测工具的指令嵌入到构建过程中。这可能包括安装检测工具的命令、运行容器扫描的步骤,以及将扫描结果集成到镜像构建流程中,确保只有通过安全和合规检查的容器镜像才能被构建和部署。 ### ECS与Kubernetes中的工具集成 在ECS或Kubernetes环境中,工具的集成可能涉及到创建特定的配置文件、定义服务和部署策略,以及编写脚本或控制器来自动执行检测任务。这样可以在容器编排的过程中实现实时监控,确保容器编排器只使用符合预期的、安全的容器镜像。 ### Linux系统下的操作 在Linux系统下操作这些工具,用户可能需要具备一定的系统管理和配置能力。这包括使用Linux命令行工具、管理文件系统权限、配置网络以及安装和配置软件包等。 ### 总结 综上所述,Docker Hub上的静态和动态标识工具提供了一种方法来检测和分析公共容器映像,确保这些镜像的安全性和可靠性。这些工具在Linux开发环境中尤为重要,因为它们帮助开发人员和运维人员确保他们的容器映像满足安全要求。通过在Dockerfile、ECS和Kubernetes中正确使用这些工具,可以提高应用程序的安全性,减少由于使用不安全的容器镜像带来的风险。此外,掌握Linux系统下的操作技能,可以更好地管理和维护这些工具,确保它们能够有效地发挥作用。
recommend-type

【扣子平台图像艺术探究:理论与实践的完美结合】:深入学习图像生成的艺术

# 1. 图像艺术的理论基础 艺术领域的每一个流派和技巧都有其理论基础。在图像艺术中,理论基础不仅是对艺术表现形式的认知,也是掌握艺术创作内在逻辑的关键。深入理解图像艺术的理论基础,能够帮助艺术家们在创作过程中更加明确地表达自己的艺术意图,以及更好地与观众沟通。 图像艺术的理论
recommend-type

增广路定理的证明

<think>嗯,用户想查找增广路定理的证明,关于图论和网络流算法的。我得先理解增广路定理是什么,然后找到相关的证明方法。根据之前的引用,尤其是引用5提到最大流最小割定理,里面有三个等价条件,其中第二个是残余网络中没有增广路径时,流就是最大流。这可能和增广路定理相关,也就是当残余网络中没有增广路时,当前流就是最大流,这可能就是增广路定理的内容。 首先,我需要明确增广路定理的陈述。根据引用5,增广路定理可能指的是:一个流是最大流当且仅当残余网络中不存在增广路径。这个定理的证明需要用到最大流最小割定理,也就是第三个条件,即最大流的流量等于最小割的容量。 证明的步骤可能需要分为两个方向:必要性(
recommend-type

Pulse:基于SwiftUI的Apple平台高效日志记录与网络监控

从给定文件信息中,我们可以提取出以下IT知识点进行详细阐述: **Pulse概览:** Pulse是一个专门针对Apple平台(如iOS、iPadOS、macOS等)的功能强大的日志记录系统。其设计目的是为了简化开发者在这些平台上调试网络请求和应用日志的过程。Pulse的核心特色是它使用SwiftUI来构建,这有助于开发者利用现代Swift语言的声明式UI优势来快速开发和维护。 **SwiftUI框架:** SwiftUI是一种声明式框架,由苹果公司推出,用于构建用户界面。与传统的UIKit相比,SwiftUI使用更加简洁的代码来描述界面和界面元素,它允许开发者以声明的方式定义视图和界面布局。SwiftUI支持跨平台,这意味着同一套代码可以在不同的Apple设备上运行,大大提高了开发效率和复用性。Pulse选择使用SwiftUI构建,显示了其对现代化、高效率开发的支持。 **Network Inspector功能:** Pulse具备Network Inspector功能,这个功能使得开发者能够在开发iOS应用时,直接从应用内记录和检查网络请求和日志。这种内嵌式的网络诊断能力非常有助于快速定位网络请求中的问题,如不正确的URL、不返回预期响应等。与传统的需要外部工具来抓包和分析的方式相比,这样的内嵌式工具大大减少了调试的复杂性。 **日志记录和隐私保护:** Pulse强调日志是本地记录的,并保证不会离开设备。这种做法对隐私保护至关重要,尤其是考虑到当前数据保护法规如GDPR等的严格要求。因此,Pulse的设计在帮助开发者进行问题诊断的同时,也确保了用户数据的安全性。 **集成和框架支持:** Pulse不仅仅是一个工具,它更是一个框架。它能够记录来自URLSession的事件,这意味着它可以与任何使用URLSession进行网络通信的应用或框架配合使用,包括但不限于Apple官方的网络库。此外,Pulse与使用它的框架(例如Alamofire)也能够良好配合,Alamofire是一个流行的网络请求库,广泛应用于Swift开发中。Pulse提供了一个PulseUI视图组件,开发者可以将其集成到自己的应用中,从而展示网络请求和其他事件。 **跨平台体验:** 开发者不仅可以在iOS应用中使用Pulse Console记录日志,还可以在macOS上通过Pulse应用程序查看和共享这些日志。这种跨平台的能力意味着开发者可以在不同的设备上进行日志分析,增强了开发和调试的灵活性。 **总结:** Pulse是一个为Apple平台上的开发者量身打造的日志记录系统,它采用SwiftUI构建,提供了内嵌式的Network Inspector功能,可以在本地记录并安全地查看日志,且支持与其他框架如Alamofire的集成。它不仅提升了调试的便捷性和效率,同时也顾及到了用户的隐私保护。Pulse的跨平台查看能力也是其一大亮点,使得开发者能够在一个统一的环境中处理iOS和macOS上的日志数据。对于使用Swift开发Apple应用的开发者而言,Pulse无疑是一个强大的调试辅助工具。