采集卡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

@@ -41,26 +41,76 @@ import cv2
# cv2.destroyAllWindows()
import threading
import warnings
# 抑制OpenCV的警告信息
import os
os.environ['OPENCV_LOG_LEVEL'] = 'ERROR'
cv2.setLogLevel(cv2.LOG_LEVEL_ERROR)
class GetImage:
def __init__(self, cam_index=0, width=1920, height=1080):
print(f"🔧 正在初始化采集卡 {cam_index}...")
self.cap = cv2.VideoCapture(cam_index, cv2.CAP_DSHOW)
if not self.cap.isOpened():
print(f"⚠️ DSHOW模式失败尝试默认模式...")
self.cap = cv2.VideoCapture(cam_index)
if not self.cap.isOpened():
print(f"❌ 无法打开采集卡 {cam_index}")
self.cap = None
return
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
self.cap = None
self.frame = None
self.running = True
self.cam_index = cam_index
# 尝试多种方式打开采集卡
backends_to_try = [
(cam_index, cv2.CAP_DSHOW),
(cam_index, cv2.CAP_ANY),
(cam_index, None), # 默认后端
]
for idx, backend in backends_to_try:
try:
with warnings.catch_warnings():
warnings.filterwarnings('ignore', category=UserWarning)
if backend is not None:
self.cap = cv2.VideoCapture(idx, backend)
else:
self.cap = cv2.VideoCapture(idx)
if self.cap.isOpened():
# 测试读取一帧
ret, test_frame = self.cap.read()
if ret and test_frame is not None:
print(f"✅ 采集卡 {cam_index} 打开成功")
break
else:
self.cap.release()
self.cap = None
except Exception as e:
if self.cap:
try:
self.cap.release()
except:
pass
self.cap = None
continue
if self.cap is None or not self.cap.isOpened():
print(f"❌ 无法打开采集卡 {cam_index}")
print("请检查:")
print(" 1. 采集卡是否正确连接")
print(" 2. 采集卡索引是否正确(尝试扫描采集卡)")
print(" 3. 采集卡驱动是否安装")
print(" 4. 采集卡是否被其他程序占用")
self.cap = None
return
# 设置分辨率
try:
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
# 实际获取设置后的分辨率
actual_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(f" 分辨率设置: {width}x{height} -> 实际: {actual_width}x{actual_height}")
except Exception as e:
print(f"⚠️ 设置分辨率失败: {e}")
# 启动更新线程
threading.Thread(target=self.update, daemon=True).start()
@@ -71,11 +121,18 @@ class GetImage:
def update(self):
while self.running and self.cap is not None:
ret, frame = self.cap.read()
if ret:
self.frame = frame
else:
print(f"⚠️ 采集卡 {self.cam_index} 读取失败")
try:
ret, frame = self.cap.read()
if ret and frame is not None:
self.frame = frame
else:
# 读取失败时不打印,避免刷屏
pass
except Exception as e:
# 只在异常时打印错误
print(f"⚠️ 采集卡 {self.cam_index} 读取异常: {e}")
import time
time.sleep(0.1) # 出错时短暂延迟
def get_frame(self):
if self.cap is None or self.frame is None: