From f0fc28d82725cbe62ea9390b5691cafd1378309e Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 Oct 2025 15:37:48 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=87=E9=9B=86=E5=8D=A1bug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui_config.py | 15 +++++++++++++-- preview.py | 12 ++++++++++-- utils/get_image.py | 12 ++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/gui_config.py b/gui_config.py index bc69884..ba6b104 100644 --- a/gui_config.py +++ b/gui_config.py @@ -202,11 +202,22 @@ class ConfigGUI: import warnings found = [] - # 临时设置OpenCV日志级别 + # 临时设置OpenCV日志级别(兼容不同版本) import os old_level = os.environ.get('OPENCV_LOG_LEVEL', '') os.environ['OPENCV_LOG_LEVEL'] = 'ERROR' - cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) + + # 尝试设置日志级别(不同版本的OpenCV API不同) + try: + if hasattr(cv2, 'setLogLevel'): + # OpenCV 4.x + if hasattr(cv2, 'LOG_LEVEL_ERROR'): + cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) + elif hasattr(cv2, 'utils'): + # 某些版本的OpenCV + cv2.utils.setLogLevel(0) # 0=ERROR级别 + except Exception: + pass # 如果设置失败,继续执行 try: for idx in range(max_index + 1): diff --git a/preview.py b/preview.py index 251cf35..b5c128d 100644 --- a/preview.py +++ b/preview.py @@ -8,9 +8,17 @@ import warnings import os from config import config_manager -# 抑制OpenCV的警告信息 +# 抑制OpenCV的警告信息(兼容不同版本) os.environ['OPENCV_LOG_LEVEL'] = 'ERROR' -cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) +try: + if hasattr(cv2, 'setLogLevel'): + # OpenCV 4.x + if hasattr(cv2, 'LOG_LEVEL_ERROR'): + cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) + elif hasattr(cv2, 'utils'): + cv2.utils.setLogLevel(0) # 0=ERROR级别 +except Exception: + pass # 如果设置失败,继续执行 class PreviewWindow: """采集卡预览窗口""" diff --git a/utils/get_image.py b/utils/get_image.py index 727c343..2c6f64e 100644 --- a/utils/get_image.py +++ b/utils/get_image.py @@ -43,10 +43,18 @@ import cv2 import threading import warnings -# 抑制OpenCV的警告信息 +# 抑制OpenCV的警告信息(兼容不同版本) import os os.environ['OPENCV_LOG_LEVEL'] = 'ERROR' -cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) +try: + if hasattr(cv2, 'setLogLevel'): + # OpenCV 4.x + if hasattr(cv2, 'LOG_LEVEL_ERROR'): + cv2.setLogLevel(cv2.LOG_LEVEL_ERROR) + elif hasattr(cv2, 'utils'): + cv2.utils.setLogLevel(0) # 0=ERROR级别 +except Exception: + pass # 如果设置失败,继续执行 class GetImage: def __init__(self, cam_index=0, width=1920, height=1080):