采集卡bug修复

This commit is contained in:
Ray
2025-10-29 15:34:28 +08:00
parent bcc971d528
commit 3a8873acc2
6 changed files with 738 additions and 56 deletions

View File

@@ -10,7 +10,8 @@ class ConfigGUI:
def __init__(self):
self.root = tk.Tk()
self.root.title("配置管理 - 火炬之光自动化")
self.root.geometry("800x600")
self.root.geometry("1000x720")
self.root.minsize(900, 600) # 设置最小尺寸
self.selected_index = 0
self.setup_ui()
@@ -102,7 +103,13 @@ class ConfigGUI:
ttk.Button(save_frame, text="保存配置", command=self.save_config).pack(side=tk.LEFT, padx=5)
ttk.Button(save_frame, text="启动预览", command=self.start_preview).pack(side=tk.LEFT, padx=5)
ttk.Button(save_frame, text="启动程序", command=self.start_program).pack(side=tk.LEFT, padx=5)
# 启动程序按钮组
start_frame = ttk.Frame(right_frame)
start_frame.pack(fill=tk.X, pady=5)
ttk.Button(start_frame, text="启动单个配置组", command=self.start_program).pack(side=tk.LEFT, padx=5)
ttk.Button(start_frame, text="启动多个配置组", command=self.start_multi_program).pack(side=tk.LEFT, padx=5)
def create_entry(self, parent, key, label, prefix=None):
"""创建输入框"""
@@ -192,22 +199,57 @@ class ConfigGUI:
def scan_cameras(self, max_index: int = 10):
"""扫描系统可用的采集卡索引,并填充下拉框"""
import warnings
found = []
for idx in range(max_index + 1):
cap = cv2.VideoCapture(idx, cv2.CAP_DSHOW)
if not cap.isOpened():
# 回退默认后端再试
cap = cv2.VideoCapture(idx)
if cap.isOpened():
found.append(str(idx))
cap.release()
# 临时设置OpenCV日志级别
import os
old_level = os.environ.get('OPENCV_LOG_LEVEL', '')
os.environ['OPENCV_LOG_LEVEL'] = 'ERROR'
cv2.setLogLevel(cv2.LOG_LEVEL_ERROR)
try:
for idx in range(max_index + 1):
cap = None
try:
with warnings.catch_warnings():
warnings.filterwarnings('ignore')
# 尝试DSHOW后端
cap = cv2.VideoCapture(idx, cv2.CAP_DSHOW)
if not cap.isOpened():
# 回退默认后端再试
cap = cv2.VideoCapture(idx)
if cap.isOpened():
# 测试读取一帧,确保真正可用
ret, test_frame = cap.read()
if ret and test_frame is not None:
found.append(str(idx))
cap.release()
except Exception:
if cap:
try:
cap.release()
except:
pass
continue
finally:
# 恢复原来的日志级别
if old_level:
os.environ['OPENCV_LOG_LEVEL'] = old_level
else:
os.environ.pop('OPENCV_LOG_LEVEL', None)
if not found:
found = ["0"] # 至少给一个默认项,避免为空
messagebox.showwarning("扫描完成", "未发现可用采集卡,已添加默认选项 0")
else:
messagebox.showinfo("扫描完成", f"发现可用采集卡索引: {', '.join(found)}")
self.camera_index_cb['values'] = found
# 若当前无选择,则选择第一项
if not self.camera_index_var.get() and found:
self.camera_index_cb.set(found[0])
messagebox.showinfo("扫描完成", f"发现可用采集卡索引: {', '.join(found)}")
def scan_ports(self):
"""扫描系统可用的串口,并填充下拉框"""
@@ -375,12 +417,55 @@ class ConfigGUI:
return result
def start_program(self):
"""启动主程序"""
# 保存配置
if not self.save_config():
"""启动单个配置组的主程序"""
# 保存配置(静默)
if not self.save_config_silent():
messagebox.showerror("错误", "配置保存失败")
return
messagebox.showinfo("提示", "配置已保存,请运行主程序")
# 检查是否有活动配置组
active_groups = [g for g in config_manager.config['groups'] if g.get('active', False)]
if not active_groups:
messagebox.showwarning("警告", "没有活动的配置组\n\n请先选择一个配置组并设置为活动")
return
# 启动单个配置组
import subprocess
import sys
try:
# 找到活动配置组的索引
active_group = active_groups[0]
group_index = config_manager.config['groups'].index(active_group)
subprocess.Popen([
sys.executable,
"main_single.py",
str(group_index)
], creationflags=subprocess.CREATE_NEW_CONSOLE if sys.platform == 'win32' else 0)
messagebox.showinfo("成功", f"已启动配置组: {active_group['name']}\n\n请在控制台查看运行状态")
except Exception as e:
messagebox.showerror("错误", f"启动失败: {e}")
def start_multi_program(self):
"""启动多个配置组的主程序"""
# 保存配置(静默)
if not self.save_config_silent():
messagebox.showerror("错误", "配置保存失败")
return
# 启动多配置组管理器
import subprocess
import sys
try:
subprocess.Popen([
sys.executable,
"main_multi.py"
], creationflags=subprocess.CREATE_NEW_CONSOLE if sys.platform == 'win32' else 0)
messagebox.showinfo("提示", "多配置组启动器已打开\n\n请在控制台中选择要启动的配置组")
except Exception as e:
messagebox.showerror("错误", f"启动失败: {e}")
def run(self):
"""运行GUI"""