采集卡bug修复

This commit is contained in:
Ray
2025-10-29 16:53:34 +08:00
parent d3d1299323
commit b0ff800826

View File

@@ -198,11 +198,26 @@ class PreviewWindow:
return return
# 计算每个预览窗口的位置和大小 # 计算每个预览窗口的位置和大小
window_width = root.winfo_width() if root.winfo_width() > 1 else 1000 # 确保画布已完全初始化
window_height = root.winfo_height() if root.winfo_height() > 1 else 700 root.update_idletasks()
canvas.update_idletasks()
cell_width = window_width // columns # 获取画布实际尺寸
cell_height = window_height // rows canvas_width = canvas.winfo_width()
canvas_height = canvas.winfo_height()
# 如果画布还没初始化,使用默认值
if canvas_width <= 1 or canvas_height <= 1:
canvas_width = 1000
canvas_height = 700
# 确保尺寸有效
if canvas_width < 100 or canvas_height < 100:
canvas_width = 1000
canvas_height = 700
cell_width = max(10, canvas_width // columns)
cell_height = max(10, canvas_height // rows)
# 先收集所有需要显示的图像 # 先收集所有需要显示的图像
images_to_draw = [] images_to_draw = []
@@ -218,31 +233,57 @@ class PreviewWindow:
center_y = y + cell_height // 2 center_y = y + cell_height // 2
name = self.caps[idx]['name'] name = self.caps[idx]['name']
if idx in self.frames and self.frames[idx] is not None: # 检查帧数据
has_frame = idx in self.frames and self.frames[idx] is not None
if has_frame:
# 调整图像大小 # 调整图像大小
try: try:
frame = self.frames[idx] frame = self.frames[idx]
h, w = frame.shape[:2] h, w = frame.shape[:2]
scale = min(cell_width / w, cell_height / h) * 0.9
new_w = int(w * scale) # 确保尺寸有效
new_h = int(h * scale) if w <= 0 or h <= 0:
texts_to_draw.append((center_x, center_y, f"{name}\n尺寸无效", 'red'))
frame_idx += 1
continue
# 计算缩放比例,留一些边距
scale = min(cell_width / w, cell_height / h, 1.0) * 0.85
new_w = max(1, int(w * scale))
new_h = max(1, int(h * scale))
# 确保缩放后的尺寸有效
if new_w <= 0 or new_h <= 0:
texts_to_draw.append((center_x, center_y, f"{name}\n缩放失败", 'red'))
frame_idx += 1
continue
# 调试输出(仅前几次)
if frame_idx == 0 and len(images_to_draw) == 0:
print(f"🔍 预览调试: 画布={canvas_width}x{canvas_height}, 单元格={cell_width}x{cell_height}, 原始帧={w}x{h}, 缩放后={new_w}x{new_h}")
resized_frame = cv2.resize(frame, (new_w, new_h)) resized_frame = cv2.resize(frame, (new_w, new_h))
# 转换为PIL图像 # 转换为PIL图像
# 确保颜色通道正确BGR -> RGB
if len(resized_frame.shape) == 3 and resized_frame.shape[2] == 3:
resized_frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(resized_frame) pil_image = Image.fromarray(resized_frame)
photo = ImageTk.PhotoImage(image=pil_image) photo = ImageTk.PhotoImage(image=pil_image)
# 保持引用避免被GC # 保持引用避免被GC
self.photo_objects[idx] = photo self.photo_objects[idx] = photo
images_to_draw.append((photo, center_x, center_y)) images_to_draw.append((photo, center_x, center_y))
texts_to_draw.append((center_x, y + 20, name, 'white')) texts_to_draw.append((center_x, y + 15, name, 'white'))
frame_idx += 1 frame_idx += 1
except Exception as e: except Exception as e:
# 忽略pyimage相关错误避免刷屏 # 忽略pyimage相关错误避免刷屏
if "pyimage" not in str(e).lower(): if "pyimage" not in str(e).lower():
print(f"处理帧 {idx} 错误: {e}") print(f"处理帧 {idx} 错误: {e}")
import traceback
traceback.print_exc()
# 如果处理失败,显示等待提示 # 如果处理失败,显示等待提示
texts_to_draw.append((center_x, center_y, f"{name}\n处理失败", 'red')) texts_to_draw.append((center_x, center_y, f"{name}\n处理失败", 'red'))
frame_idx += 1 frame_idx += 1
@@ -254,15 +295,27 @@ class PreviewWindow:
# 清空画布 # 清空画布
canvas.delete("all") canvas.delete("all")
# 绘制所有图像和文本 # 绘制所有图像(底层)
for photo, x, y in images_to_draw: if images_to_draw:
canvas.create_image(x, y, image=photo, anchor='center') if frame_idx == len(images_to_draw):
print(f"✅ 准备绘制 {len(images_to_draw)} 个图像")
for photo, x, y in images_to_draw:
try:
canvas.create_image(x, y, image=photo, anchor='center')
except Exception as e:
if "pyimage" not in str(e).lower():
print(f"绘制图像错误: {e}")
# 再绘制所有文本(上层)
for x, y, text, color in texts_to_draw: for x, y, text, color in texts_to_draw:
if color == 'white': try:
canvas.create_text(x, y, text=text, fill=color, font=('Arial', 12, 'bold')) if color == 'white':
else: canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10, 'bold'))
canvas.create_text(x, y, text=text, fill=color, font=('Arial', 12)) else:
canvas.create_text(x, y, text=text, fill=color, font=('Arial', 10))
except Exception as e:
print(f"绘制文本错误: {e}")
except Exception as e: except Exception as e:
print(f"更新帧错误: {e}") print(f"更新帧错误: {e}")
@@ -289,8 +342,15 @@ class PreviewWindow:
canvas.bind('<Button-1>', on_canvas_click) canvas.bind('<Button-1>', on_canvas_click)
# 使用after在主线程中循环刷 # 等待窗口完全初始化后再开始更
root.after(33, update_frames_once) def start_updates():
"""延迟启动更新,确保窗口已完全显示"""
root.update_idletasks()
root.update()
update_frames_once()
# 使用after在主线程中循环刷新延迟启动
root.after(100, start_updates)
def on_closing(): def on_closing():
"""关闭窗口""" """关闭窗口"""
@@ -335,6 +395,9 @@ class PreviewWindow:
new_h = int(h * scale) new_h = int(h * scale)
resized_frame = cv2.resize(frame, (new_w, new_h)) resized_frame = cv2.resize(frame, (new_w, new_h))
# 确保颜色通道正确BGR -> RGB
if len(resized_frame.shape) == 3 and resized_frame.shape[2] == 3:
resized_frame = cv2.cvtColor(resized_frame, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(resized_frame) pil_image = Image.fromarray(resized_frame)
photo = ImageTk.PhotoImage(image=pil_image) photo = ImageTk.PhotoImage(image=pil_image)
# 保存引用到字典确保不被GC # 保存引用到字典确保不被GC