采集卡bug修复
This commit is contained in:
75
preview.py
75
preview.py
@@ -4,8 +4,14 @@ from tkinter import Canvas
|
||||
from PIL import Image, ImageTk
|
||||
import threading
|
||||
import numpy as np
|
||||
import warnings
|
||||
import os
|
||||
from config import config_manager
|
||||
|
||||
# 抑制OpenCV的警告信息
|
||||
os.environ['OPENCV_LOG_LEVEL'] = 'ERROR'
|
||||
cv2.setLogLevel(cv2.LOG_LEVEL_ERROR)
|
||||
|
||||
class PreviewWindow:
|
||||
"""采集卡预览窗口"""
|
||||
def __init__(self):
|
||||
@@ -31,31 +37,60 @@ class PreviewWindow:
|
||||
cam_idx = group['camera_index']
|
||||
print(f" 尝试打开采集卡 {cam_idx} ({group['name']})...")
|
||||
|
||||
cap = cv2.VideoCapture(int(cam_idx), cv2.CAP_DSHOW)
|
||||
if not cap.isOpened():
|
||||
print(f" DSHOW模式失败,尝试默认模式...")
|
||||
cap = cv2.VideoCapture(int(cam_idx))
|
||||
cap = None
|
||||
# 尝试多种后端打开
|
||||
backends_to_try = [
|
||||
(int(cam_idx), cv2.CAP_DSHOW),
|
||||
(int(cam_idx), cv2.CAP_ANY),
|
||||
(int(cam_idx), None),
|
||||
]
|
||||
|
||||
if cap.isOpened():
|
||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, group['camera_width'])
|
||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, group['camera_height'])
|
||||
# 测试读取一帧
|
||||
ret, test_frame = cap.read()
|
||||
if ret:
|
||||
self.caps[i] = {
|
||||
'cap': cap,
|
||||
'group': group,
|
||||
'name': group['name']
|
||||
}
|
||||
loaded_count += 1
|
||||
print(f" ✅ 采集卡 {cam_idx} 初始化成功")
|
||||
else:
|
||||
cap.release()
|
||||
print(f" ❌ 采集卡 {cam_idx} 无法读取帧")
|
||||
for idx, backend in backends_to_try:
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings('ignore')
|
||||
if backend is not None:
|
||||
cap = cv2.VideoCapture(idx, backend)
|
||||
else:
|
||||
cap = cv2.VideoCapture(idx)
|
||||
|
||||
if cap.isOpened():
|
||||
# 测试读取一帧
|
||||
ret, test_frame = cap.read()
|
||||
if ret and test_frame is not None:
|
||||
break
|
||||
else:
|
||||
cap.release()
|
||||
cap = None
|
||||
except Exception:
|
||||
if cap:
|
||||
try:
|
||||
cap.release()
|
||||
except:
|
||||
pass
|
||||
cap = None
|
||||
continue
|
||||
|
||||
if cap and cap.isOpened():
|
||||
try:
|
||||
cap.set(cv2.CAP_PROP_FRAME_WIDTH, group['camera_width'])
|
||||
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, group['camera_height'])
|
||||
except Exception as e:
|
||||
print(f" ⚠️ 设置分辨率失败: {e}")
|
||||
|
||||
self.caps[i] = {
|
||||
'cap': cap,
|
||||
'group': group,
|
||||
'name': group['name']
|
||||
}
|
||||
loaded_count += 1
|
||||
print(f" ✅ 采集卡 {cam_idx} 初始化成功")
|
||||
else:
|
||||
print(f" ❌ 采集卡 {cam_idx} 无法打开")
|
||||
except Exception as e:
|
||||
print(f" ❌ 采集卡 {group.get('camera_index', '?')} 初始化失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
if loaded_count == 0:
|
||||
print("⚠️ 警告:没有成功加载任何采集卡!")
|
||||
|
||||
Reference in New Issue
Block a user