From 0e16ec99c3a7bf8b0e7d7873c5a6b6fe827bac7f Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 29 Oct 2025 14:06:05 +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 --- main.py | 26 ++++++++++++++++++ test_camera.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++ utils/get_image.py | 37 ++++++++++++++++++++----- 3 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 test_camera.py diff --git a/main.py b/main.py index 5634de6..a70e9e3 100644 --- a/main.py +++ b/main.py @@ -39,6 +39,15 @@ get_image = GetImage( height=active_group['camera_height'] ) +# 检查采集卡是否初始化成功 +if get_image.cap is None: + print(f"❌ 采集卡 {active_group['camera_index']} 初始化失败") + print("请检查:") + print("1. 采集卡是否正确连接") + print("2. 采集卡索引是否正确") + print("3. 采集卡驱动是否安装") + exit(1) + print(f"✅ 初始化完成 - 串口:{active_group['serial_port']} 采集卡:{active_group['camera_index']}") # 全局变量 @@ -171,10 +180,18 @@ while True: 'zhaozi': None } im_opencv = get_image.get_frame() # [RGB,PIL] + if im_opencv is None: + print("⚠️ 无法获取图像帧,跳过本次循环") + time.sleep(0.1) + continue detections = yolo_shibie(im_opencv[1], detections, model) if shizi.tuwai(im_opencv[0]): # 进图算法 im_opencv = get_image.get_frame() # [RGB,PIL] + if im_opencv is None: + print("⚠️ 无法获取图像帧,跳过本次循环") + time.sleep(0.1) + continue detections = yolo_shibie(im_opencv[1], detections, model0) print('当前在城镇中') if detections['npc1'] is not None and sq(rw, detections['npc1']) > 80: @@ -203,6 +220,9 @@ while True: keyboard.release() time.sleep(1) im_opencv = get_image.get_frame() # [RGB,PIL] + if im_opencv is None: + print("⚠️ 无法获取图像帧") + continue if shizi.daoying(im_opencv[0]): mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1) time.sleep(1) @@ -212,6 +232,9 @@ while True: move_to(rw, detections['npc4']) time.sleep(1) im_opencv = get_image.get_frame() # [RGB,PIL] + if im_opencv is None: + print("⚠️ 无法获取图像帧") + continue if shizi.daoying(im_opencv[0]): mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1) time.sleep(1) @@ -255,6 +278,9 @@ while True: time.sleep(0.5) im_opencv = get_image.get_frame() # [RGB,PIL] + if im_opencv is None: + print("⚠️ 无法获取图像帧") + continue detections = { 'center': None, 'next': None, diff --git a/test_camera.py b/test_camera.py new file mode 100644 index 0000000..aa8e060 --- /dev/null +++ b/test_camera.py @@ -0,0 +1,67 @@ +""" +采集卡测试脚本 +用于测试采集卡是否能正常工作 +""" +import cv2 +from utils.get_image import GetImage +from config import config_manager + +def test_camera(): + """测试采集卡""" + print("🔧 采集卡测试工具") + print("=" * 50) + + # 获取活动配置 + active_group = config_manager.get_active_group() + if active_group is None: + print("❌ 没有活动的配置组") + print("请先运行 python gui_config.py 设置配置") + return + + print(f"📋 使用配置: {active_group['name']}") + print(f" 采集卡索引: {active_group['camera_index']}") + print(f" 分辨率: {active_group['camera_width']}x{active_group['camera_height']}") + print() + + # 初始化采集卡 + get_image = GetImage( + cam_index=active_group['camera_index'], + width=active_group['camera_width'], + height=active_group['camera_height'] + ) + + if get_image.cap is None: + print("❌ 采集卡初始化失败") + return + + print("✅ 采集卡初始化成功") + print("按 'q' 退出测试") + print() + + # 测试循环 + frame_count = 0 + while True: + frame_data = get_image.get_frame() + + if frame_data is None: + print("⚠️ 无法获取帧") + continue + + frame_count += 1 + if frame_count % 30 == 0: # 每30帧显示一次状态 + print(f"📊 已获取 {frame_count} 帧") + + # 显示图像 + cv2.imshow('采集卡测试', frame_data[0]) + + # 按 'q' 退出 + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + # 清理 + get_image.release() + cv2.destroyAllWindows() + print("🔚 测试结束") + +if __name__ == "__main__": + test_camera() diff --git a/utils/get_image.py b/utils/get_image.py index 36a12a2..31f66b4 100644 --- a/utils/get_image.py +++ b/utils/get_image.py @@ -44,33 +44,56 @@ import threading 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.frame = None self.running = True + self.cam_index = cam_index + + # 启动更新线程 threading.Thread(target=self.update, daemon=True).start() + + # 等待几帧确保采集卡正常工作 + import time + time.sleep(1.0) + print(f"✅ 采集卡 {cam_index} 初始化完成") def update(self): - while self.running: + 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} 读取失败") def get_frame(self): - if self.frame is None: + if self.cap is None or self.frame is None: + return None + try: + im_opencv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB) + im_opencv = im_opencv[30:30+720, 0:1280] + im_PIL = Image.fromarray(im_opencv) + return [im_opencv, im_PIL] + except Exception as e: + print(f"⚠️ 图像处理错误: {e}") return None - im_opencv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB) - im_opencv = im_opencv[30:30+720, 0:1280] - im_PIL = Image.fromarray(im_opencv) - return [im_opencv, im_PIL] def release(self): self.running = False time.sleep(0.2) - self.cap.release() + if self.cap is not None: + self.cap.release() cv2.destroyAllWindows() # get_image 将在main.py中初始化