采集卡bug修复

This commit is contained in:
Ray
2025-10-29 16:47:30 +08:00
parent 7af3e2353a
commit d3d1299323

View File

@@ -183,10 +183,9 @@ class PreviewWindow:
if not self.running: if not self.running:
return return
try: try:
canvas.delete("all")
# 如果没有加载任何采集卡,显示提示 # 如果没有加载任何采集卡,显示提示
if not self.caps: if not self.caps:
canvas.delete("all")
canvas.create_text( canvas.create_text(
root.winfo_width() // 2, root.winfo_width() // 2,
root.winfo_height() // 2, root.winfo_height() // 2,
@@ -205,15 +204,21 @@ class PreviewWindow:
cell_width = window_width // columns cell_width = window_width // columns
cell_height = window_height // rows cell_height = window_height // rows
# 先收集所有需要显示的图像
images_to_draw = []
texts_to_draw = []
frame_idx = 0 frame_idx = 0
for idx in self.caps.keys(): for idx in self.caps.keys():
if idx in self.frames and self.frames[idx] is not None:
row = frame_idx // columns row = frame_idx // columns
col = frame_idx % columns col = frame_idx % columns
x = col * cell_width x = col * cell_width
y = row * cell_height y = row * cell_height
center_x = x + cell_width // 2
center_y = y + cell_height // 2
name = self.caps[idx]['name']
if idx in self.frames and self.frames[idx] is not None:
# 调整图像大小 # 调整图像大小
try: try:
frame = self.frames[idx] frame = self.frames[idx]
@@ -230,29 +235,35 @@ class PreviewWindow:
# 保持引用避免被GC # 保持引用避免被GC
self.photo_objects[idx] = photo self.photo_objects[idx] = photo
# 绘制图像 images_to_draw.append((photo, center_x, center_y))
center_x = x + cell_width // 2 texts_to_draw.append((center_x, y + 20, name, 'white'))
center_y = y + cell_height // 2
canvas.create_image(center_x, center_y, image=photo, anchor='center')
# 绘制标签
name = self.caps[idx]['name']
canvas.create_text(center_x, y + 20, text=name, fill='white', font=('Arial', 12, 'bold'))
frame_idx += 1 frame_idx += 1
except Exception as e: except Exception as e:
# 忽略pyimage相关错误避免刷屏
if "pyimage" not in str(e).lower():
print(f"处理帧 {idx} 错误: {e}") print(f"处理帧 {idx} 错误: {e}")
# 如果处理失败,显示等待提示
texts_to_draw.append((center_x, center_y, f"{name}\n处理失败", 'red'))
frame_idx += 1
else: else:
# 显示等待提示 # 显示等待提示
row = frame_idx // columns texts_to_draw.append((center_x, center_y, f"{name}\n等待画面...", 'gray'))
col = frame_idx % columns
x = col * cell_width
y = row * cell_height
center_x = x + cell_width // 2
center_y = y + cell_height // 2
name = self.caps[idx]['name']
canvas.create_text(center_x, center_y, text=f"{name}\n等待画面...", fill='gray', font=('Arial', 12))
frame_idx += 1 frame_idx += 1
# 清空画布
canvas.delete("all")
# 绘制所有图像和文本
for photo, x, y in images_to_draw:
canvas.create_image(x, y, image=photo, anchor='center')
for x, y, text, color in texts_to_draw:
if color == 'white':
canvas.create_text(x, y, text=text, fill=color, font=('Arial', 12, 'bold'))
else:
canvas.create_text(x, y, text=text, fill=color, font=('Arial', 12))
except Exception as e: except Exception as e:
print(f"更新帧错误: {e}") print(f"更新帧错误: {e}")
import traceback import traceback
@@ -314,8 +325,7 @@ class PreviewWindow:
window_height = self.large_window.winfo_height() if self.large_window.winfo_height() > 1 else 720 window_height = self.large_window.winfo_height() if self.large_window.winfo_height() > 1 else 720
if idx in self.frames and self.frames[idx] is not None: if idx in self.frames and self.frames[idx] is not None:
canvas.delete("all") # 先处理图像,再清空画布
frame = self.frames[idx] frame = self.frames[idx]
h, w = frame.shape[:2] h, w = frame.shape[:2]
@@ -327,8 +337,11 @@ class PreviewWindow:
resized_frame = cv2.resize(frame, (new_w, new_h)) resized_frame = cv2.resize(frame, (new_w, new_h))
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
photo_obj['img'] = photo photo_obj['img'] = photo
# 清空并绘制新图像
canvas.delete("all")
canvas.create_image(window_width // 2, window_height // 2, image=photo, anchor='center') canvas.create_image(window_width // 2, window_height // 2, image=photo, anchor='center')
else: else:
# 显示等待提示 # 显示等待提示
@@ -341,9 +354,8 @@ class PreviewWindow:
font=('Arial', 16) font=('Arial', 16)
) )
except Exception as e: except Exception as e:
if "pyimage" not in str(e).lower(): # 忽略pyimage错误避免刷屏
print(f"更新大窗口错误: {e}") print(f"更新大窗口错误: {e}")
import traceback
traceback.print_exc()
self.large_window.after(33, update_large_once) self.large_window.after(33, update_large_once)
self.large_window.after(33, update_large_once) self.large_window.after(33, update_large_once)