采集卡bug修复
This commit is contained in:
26
main.py
26
main.py
@@ -39,6 +39,15 @@ get_image = GetImage(
|
|||||||
height=active_group['camera_height']
|
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']}")
|
print(f"✅ 初始化完成 - 串口:{active_group['serial_port']} 采集卡:{active_group['camera_index']}")
|
||||||
|
|
||||||
# 全局变量
|
# 全局变量
|
||||||
@@ -171,10 +180,18 @@ while True:
|
|||||||
'zhaozi': None
|
'zhaozi': None
|
||||||
}
|
}
|
||||||
im_opencv = get_image.get_frame() # [RGB,PIL]
|
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)
|
detections = yolo_shibie(im_opencv[1], detections, model)
|
||||||
|
|
||||||
if shizi.tuwai(im_opencv[0]): # 进图算法
|
if shizi.tuwai(im_opencv[0]): # 进图算法
|
||||||
im_opencv = get_image.get_frame() # [RGB,PIL]
|
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)
|
detections = yolo_shibie(im_opencv[1], detections, model0)
|
||||||
print('当前在城镇中')
|
print('当前在城镇中')
|
||||||
if detections['npc1'] is not None and sq(rw, detections['npc1']) > 80:
|
if detections['npc1'] is not None and sq(rw, detections['npc1']) > 80:
|
||||||
@@ -203,6 +220,9 @@ while True:
|
|||||||
keyboard.release()
|
keyboard.release()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
im_opencv = get_image.get_frame() # [RGB,PIL]
|
im_opencv = get_image.get_frame() # [RGB,PIL]
|
||||||
|
if im_opencv is None:
|
||||||
|
print("⚠️ 无法获取图像帧")
|
||||||
|
continue
|
||||||
if shizi.daoying(im_opencv[0]):
|
if shizi.daoying(im_opencv[0]):
|
||||||
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
|
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -212,6 +232,9 @@ while True:
|
|||||||
move_to(rw, detections['npc4'])
|
move_to(rw, detections['npc4'])
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
im_opencv = get_image.get_frame() # [RGB,PIL]
|
im_opencv = get_image.get_frame() # [RGB,PIL]
|
||||||
|
if im_opencv is None:
|
||||||
|
print("⚠️ 无法获取图像帧")
|
||||||
|
continue
|
||||||
if shizi.daoying(im_opencv[0]):
|
if shizi.daoying(im_opencv[0]):
|
||||||
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
|
mouse_gui.send_data_absolute(rw[0], rw[1] - 110, may=1)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
@@ -255,6 +278,9 @@ while True:
|
|||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
|
|
||||||
im_opencv = get_image.get_frame() # [RGB,PIL]
|
im_opencv = get_image.get_frame() # [RGB,PIL]
|
||||||
|
if im_opencv is None:
|
||||||
|
print("⚠️ 无法获取图像帧")
|
||||||
|
continue
|
||||||
detections = {
|
detections = {
|
||||||
'center': None,
|
'center': None,
|
||||||
'next': None,
|
'next': None,
|
||||||
|
|||||||
67
test_camera.py
Normal file
67
test_camera.py
Normal file
@@ -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()
|
||||||
@@ -44,33 +44,56 @@ import threading
|
|||||||
|
|
||||||
class GetImage:
|
class GetImage:
|
||||||
def __init__(self, cam_index=0, width=1920, height=1080):
|
def __init__(self, cam_index=0, width=1920, height=1080):
|
||||||
|
print(f"🔧 正在初始化采集卡 {cam_index}...")
|
||||||
self.cap = cv2.VideoCapture(cam_index, cv2.CAP_DSHOW)
|
self.cap = cv2.VideoCapture(cam_index, cv2.CAP_DSHOW)
|
||||||
if not self.cap.isOpened():
|
if not self.cap.isOpened():
|
||||||
|
print(f"⚠️ DSHOW模式失败,尝试默认模式...")
|
||||||
self.cap = cv2.VideoCapture(cam_index)
|
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_WIDTH, width)
|
||||||
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
|
||||||
self.frame = None
|
self.frame = None
|
||||||
self.running = True
|
self.running = True
|
||||||
|
self.cam_index = cam_index
|
||||||
|
|
||||||
|
# 启动更新线程
|
||||||
threading.Thread(target=self.update, daemon=True).start()
|
threading.Thread(target=self.update, daemon=True).start()
|
||||||
|
|
||||||
|
# 等待几帧确保采集卡正常工作
|
||||||
|
import time
|
||||||
|
time.sleep(1.0)
|
||||||
|
print(f"✅ 采集卡 {cam_index} 初始化完成")
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
while self.running:
|
while self.running and self.cap is not None:
|
||||||
ret, frame = self.cap.read()
|
ret, frame = self.cap.read()
|
||||||
if ret:
|
if ret:
|
||||||
self.frame = frame
|
self.frame = frame
|
||||||
|
else:
|
||||||
|
print(f"⚠️ 采集卡 {self.cam_index} 读取失败")
|
||||||
|
|
||||||
def get_frame(self):
|
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
|
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):
|
def release(self):
|
||||||
self.running = False
|
self.running = False
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
self.cap.release()
|
if self.cap is not None:
|
||||||
|
self.cap.release()
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
|
|
||||||
# get_image 将在main.py中初始化
|
# get_image 将在main.py中初始化
|
||||||
|
|||||||
Reference in New Issue
Block a user