多台控制

This commit is contained in:
ray
2025-10-30 23:39:27 +08:00
parent 069613fe09
commit 7623e22e5c
9 changed files with 350 additions and 190 deletions

View File

@@ -2,6 +2,8 @@ import time
from PIL import Image
import cv2
from utils.logger import logger, throttle
import logging
# class GetImage:
# def __init__(self, cam_index=0, width=1920, height=1080):
# self.cap = cv2.VideoCapture(cam_index,cv2.CAP_DSHOW)
@@ -63,7 +65,7 @@ except Exception:
class GetImage:
def __init__(self, cam_index=0, width=1920, height=1080):
print(f"🔧 正在初始化采集卡 {cam_index}...")
logger.info(f"🔧 正在初始化采集卡 {cam_index}...")
self.cap = None
self.frame = None
self.running = True
@@ -96,7 +98,7 @@ class GetImage:
# 测试读取一帧
ret, test_frame = self.cap.read()
if ret and test_frame is not None:
print(f"✅ 采集卡 {cam_index} 打开成功")
logger.info(f"✅ 采集卡 {cam_index} 打开成功")
break
else:
self.cap.release()
@@ -114,12 +116,8 @@ class GetImage:
sys.stderr = old_stderr
if self.cap is None or not self.cap.isOpened():
print(f"❌ 无法打开采集卡 {cam_index}")
print("请检查:")
print(" 1. 采集卡是否正确连接")
print(" 2. 采集卡索引是否正确(尝试扫描采集卡)")
print(" 3. 采集卡驱动是否安装")
print(" 4. 采集卡是否被其他程序占用")
logger.error(f"❌ 无法打开采集卡 {cam_index}")
logger.error("请检查:\n 1. 采集卡是否正确连接\n 2. 采集卡索引是否正确(尝试扫描采集卡)\n 3. 采集卡驱动是否安装\n 4. 采集卡是否被其他程序占用")
self.cap = None
return
@@ -130,9 +128,9 @@ class GetImage:
# 实际获取设置后的分辨率
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}")
logger.info(f" 分辨率设置: {width}x{height} -> 实际: {actual_width}x{actual_height}")
except Exception as e:
print(f"⚠️ 设置分辨率失败: {e}")
logger.warning(f"⚠️ 设置分辨率失败: {e}")
# 启动更新线程
threading.Thread(target=self.update, daemon=True).start()
@@ -140,7 +138,7 @@ class GetImage:
# 等待几帧确保采集卡正常工作
import time
time.sleep(1.0)
print(f"✅ 采集卡 {cam_index} 初始化完成")
logger.info(f"✅ 采集卡 {cam_index} 初始化完成")
def update(self):
while self.running and self.cap is not None:
@@ -148,12 +146,14 @@ class GetImage:
ret, frame = self.cap.read()
if ret and frame is not None:
self.frame = frame
# 限制读取频率避免占满CPU
time.sleep(0.008)
else:
# 读取失败时不打印,避免刷屏
pass
time.sleep(0.02)
except Exception as e:
# 只在异常时打印错误
print(f"⚠️ 采集卡 {self.cam_index} 读取异常: {e}")
throttle(f"cap_read_err_{self.cam_index}", 2.0, logging.WARNING, f"⚠️ 采集卡 {self.cam_index} 读取异常: {e}")
import time
time.sleep(0.1) # 出错时短暂延迟
@@ -166,7 +166,7 @@ class GetImage:
im_PIL = Image.fromarray(im_opencv)
return [im_opencv, im_PIL]
except Exception as e:
print(f"⚠️ 图像处理错误: {e}")
throttle(f"img_proc_err_{self.cam_index}", 2.0, logging.WARNING, f"⚠️ 图像处理错误: {e}")
return None
def release(self):