回退import结构

This commit is contained in:
xinzhu.yin
2026-04-17 11:17:29 +08:00
parent 19e12d4aeb
commit 90d7d8e3f0

View File

@@ -6,10 +6,14 @@ import threading
import time import time
import os import os
import datetime import datetime
import colour
import json import json
import traceback import traceback
import numpy as np import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import algorithm.pq_algorithm as pq_algorithm import algorithm.pq_algorithm as pq_algorithm
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from app_version import APP_NAME, APP_VERSION, get_app_title from app_version import APP_NAME, APP_VERSION, get_app_title
from utils.caSerail import CASerail from utils.caSerail import CASerail
from utils.tvSerail import tvSerial from utils.tvSerail import tvSerial
@@ -23,11 +27,13 @@ from views.collapsing_frame import CollapsingFrame
# from views.pq_history_gui import PQHistoryGUI # from views.pq_history_gui import PQHistoryGUI
from views.pq_log_gui import PQLogGUI from views.pq_log_gui import PQLogGUI
from colormath.color_objects import xyYColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
from views.pq_debug_panel import PQDebugPanel
plt = None plt.rcParams["font.family"] = ["sans-serif"]
mpimg = None plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
FigureCanvasTkAgg = None
colour = None
def get_resource_path(relative_path): def get_resource_path(relative_path):
@@ -184,59 +190,6 @@ class PQAutomationApp:
) )
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X) self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def _ensure_matplotlib_loaded(self):
"""按需加载 matplotlib避免阻塞主界面启动。"""
global plt, mpimg, FigureCanvasTkAgg
if plt is None or mpimg is None or FigureCanvasTkAgg is None:
import matplotlib.pyplot as _plt
import matplotlib.image as _mpimg
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as _FigureCanvasTkAgg
_plt.rcParams["font.family"] = ["sans-serif"]
_plt.rcParams["font.sans-serif"] = ["Microsoft YaHei"]
plt = _plt
mpimg = _mpimg
FigureCanvasTkAgg = _FigureCanvasTkAgg
def _ensure_colour_loaded(self):
"""按需加载 colour-science减少冷启动导入压力。"""
global colour
if colour is None:
import colour as _colour
colour = _colour
def _initialize_chart(self, chart_name):
"""按需初始化图表。"""
if chart_name == "gamut":
self.init_gamut_chart()
elif chart_name == "gamma":
self.init_gamma_chart()
elif chart_name == "eotf":
self.init_eotf_chart()
elif chart_name == "cct":
self.init_cct_chart()
elif chart_name == "contrast":
self.init_contrast_chart()
elif chart_name == "accuracy":
self.init_accuracy_chart()
def _ensure_chart_initialized(self, chart_name):
"""确保目标图表已完成初始化。"""
if not self.chart_init_state.get(chart_name, False):
self._initialize_chart(chart_name)
self.chart_init_state[chart_name] = True
def _warmup_remaining_charts(self):
"""在界面显示后分帧预热其它图表,降低首屏阻塞。"""
for chart_name in ("gamma", "eotf", "cct", "contrast", "accuracy"):
if self.chart_init_state.get(chart_name, False):
continue
self._ensure_chart_initialized(chart_name)
self.root.after(1, self._warmup_remaining_charts)
return
def get_config_path(self): def get_config_path(self):
"""获取配置文件的完整路径(兼容打包后的程序)""" """获取配置文件的完整路径(兼容打包后的程序)"""
import os import os
@@ -274,7 +227,6 @@ class PQAutomationApp:
def init_gamut_chart(self): def init_gamut_chart(self):
"""初始化色域图表 - 手动设置subplot位置完全避免重叠""" """初始化色域图表 - 手动设置subplot位置完全避免重叠"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.gamut_chart_frame) container = ttk.Frame(self.gamut_chart_frame)
container.pack(expand=True, fill=tk.BOTH) container.pack(expand=True, fill=tk.BOTH)
@@ -311,7 +263,6 @@ class PQAutomationApp:
def init_gamma_chart(self): def init_gamma_chart(self):
"""初始化Gamma曲线图表 - 左侧曲线 + 右侧表格(✅ 4列 + 通用说明)""" """初始化Gamma曲线图表 - 左侧曲线 + 右侧表格(✅ 4列 + 通用说明)"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.gamma_chart_frame) container = ttk.Frame(self.gamma_chart_frame)
container.pack(expand=True, fill=tk.BOTH) container.pack(expand=True, fill=tk.BOTH)
@@ -421,7 +372,6 @@ class PQAutomationApp:
def init_eotf_chart(self): def init_eotf_chart(self):
"""初始化 EOTF 曲线图表HDR 专用)- 左侧曲线 + 右侧表格(✅ 4列""" """初始化 EOTF 曲线图表HDR 专用)- 左侧曲线 + 右侧表格(✅ 4列"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.eotf_chart_frame) container = ttk.Frame(self.eotf_chart_frame)
container.pack(expand=True, fill=tk.BOTH) container.pack(expand=True, fill=tk.BOTH)
@@ -527,7 +477,6 @@ class PQAutomationApp:
def init_cct_chart(self): def init_cct_chart(self):
"""初始化色度坐标图表 - 正向横坐标,标题居中最上方""" """初始化色度坐标图表 - 正向横坐标,标题居中最上方"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.cct_chart_frame) container = ttk.Frame(self.cct_chart_frame)
container.pack(expand=True) container.pack(expand=True)
@@ -573,7 +522,6 @@ class PQAutomationApp:
def init_contrast_chart(self): def init_contrast_chart(self):
"""初始化对比度图表 - 固定大小,居中显示""" """初始化对比度图表 - 固定大小,居中显示"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.contrast_chart_frame) container = ttk.Frame(self.contrast_chart_frame)
container.pack(expand=True) container.pack(expand=True)
@@ -609,7 +557,6 @@ class PQAutomationApp:
def init_accuracy_chart(self): def init_accuracy_chart(self):
"""初始化色准图表 - 固定大小,居中显示""" """初始化色准图表 - 固定大小,居中显示"""
self._ensure_matplotlib_loaded()
container = ttk.Frame(self.accuracy_chart_frame) container = ttk.Frame(self.accuracy_chart_frame)
container.pack(expand=True) container.pack(expand=True)
@@ -2844,7 +2791,6 @@ class PQAutomationApp:
def _run_custom_row_single_step(self, item_id, row_no): def _run_custom_row_single_step(self, item_id, row_no):
"""后台执行客户模板单步测试""" """后台执行客户模板单步测试"""
try: try:
self._ensure_colour_loaded()
self.root.after(0, lambda: self.status_var.set(f"单步测试第 {row_no} 行...")) self.root.after(0, lambda: self.status_var.set(f"单步测试第 {row_no} 行..."))
self.log_gui.log(f"开始单步测试第 {row_no}") self.log_gui.log(f"开始单步测试第 {row_no}")
@@ -3441,43 +3387,35 @@ class PQAutomationApp:
self.chart_notebook.add(self.contrast_chart_frame, text="对比度") self.chart_notebook.add(self.contrast_chart_frame, text="对比度")
self.chart_notebook.add(self.accuracy_chart_frame, text="色准") self.chart_notebook.add(self.accuracy_chart_frame, text="色准")
# 图表初始化状态:首屏仅初始化当前可见图表,其余延后。 # 初始化六个独立的图表
self.chart_init_state = { self.init_gamut_chart()
"gamut": False, self.init_gamma_chart()
"gamma": False, self.init_eotf_chart()
"eotf": False, self.init_cct_chart()
"cct": False, self.init_contrast_chart()
"contrast": False, self.init_accuracy_chart()
"accuracy": False,
}
# 首屏只初始化色域图,其他图表在切换 Tab 或空闲时初始化。
self._ensure_chart_initialized("gamut")
self.root.after(120, self._warmup_remaining_charts)
# 绑定Tab切换事件 # 绑定Tab切换事件
self.chart_notebook.bind("<<NotebookTabChanged>>", self.on_chart_tab_changed) self.chart_notebook.bind("<<NotebookTabChanged>>", self.on_chart_tab_changed)
# 旧版内嵌调试面板改为按需窗口,不在启动阶段创建复杂控件。 # ==================== ✅ 在图表下方创建单步调试面板 ====================
self.debug_container = ttk.LabelFrame(
self.result_frame, # ← 放在 result_frame 内,图表正下方
text="🔧 单步调试",
padding=10,
)
# 默认不显示
# 创建单步调试面板实例
self.debug_panel = PQDebugPanel(self.debug_container, self)
self.log_gui.log("✓ 单步调试面板已创建(放在测试结果图表下方)")
def on_chart_tab_changed(self, event): def on_chart_tab_changed(self, event):
"""Tab切换时的事件处理""" """Tab切换时的事件处理"""
try: try:
selected_tab = self.chart_notebook.select()
tab_to_chart = {
str(self.gamut_chart_frame): "gamut",
str(self.gamma_chart_frame): "gamma",
str(self.eotf_chart_frame): "eotf",
str(self.cct_chart_frame): "cct",
str(self.contrast_chart_frame): "contrast",
str(self.accuracy_chart_frame): "accuracy",
}
chart_name = tab_to_chart.get(selected_tab)
if chart_name:
self._ensure_chart_initialized(chart_name)
self._last_tab_index = self.chart_notebook.index( self._last_tab_index = self.chart_notebook.index(
selected_tab self.chart_notebook.select()
) )
except Exception as e: except Exception as e:
self.log_gui.log(f"Tab切换事件处理失败: {str(e)}") self.log_gui.log(f"Tab切换事件处理失败: {str(e)}")
@@ -6296,7 +6234,6 @@ class PQAutomationApp:
# 每完成一个 pattern实时写入客户模板结果表。 # 每完成一个 pattern实时写入客户模板结果表。
try: try:
self._ensure_colour_loaded()
xy = colour.XYZ_to_xy(np.array([X, Y, Z])) xy = colour.XYZ_to_xy(np.array([X, Y, Z]))
u_prime, v_prime, _ = colour.XYZ_to_CIE1976UCS( u_prime, v_prime, _ = colour.XYZ_to_CIE1976UCS(
np.array([X, Y, Z]) np.array([X, Y, Z])
@@ -7208,7 +7145,6 @@ class PQAutomationApp:
def plot_gamut(self, results, coverage, test_type): def plot_gamut(self, results, coverage, test_type):
"""绘制色域图 - 根据用户选择的参考标准动态计算覆盖率""" """绘制色域图 - 根据用户选择的参考标准动态计算覆盖率"""
self._ensure_chart_initialized("gamut")
self.gamut_ax_xy.clear() self.gamut_ax_xy.clear()
self.gamut_ax_uv.clear() self.gamut_ax_uv.clear()
@@ -7734,8 +7670,6 @@ class PQAutomationApp:
def plot_gamma(self, L_bar, results_with_gamma_list, target_gamma, test_type): def plot_gamma(self, L_bar, results_with_gamma_list, target_gamma, test_type):
"""绘制Gamma曲线 + 数据表格(包含实测亮度)""" """绘制Gamma曲线 + 数据表格(包含实测亮度)"""
self._ensure_chart_initialized("gamma")
# ========== 1. 清空并重置左侧曲线 ========== # ========== 1. 清空并重置左侧曲线 ==========
self.gamma_ax.clear() self.gamma_ax.clear()
self.gamma_ax.set_xlim(0, 105) self.gamma_ax.set_xlim(0, 105)
@@ -7870,8 +7804,6 @@ class PQAutomationApp:
def plot_eotf(self, L_bar, results_with_eotf_list, test_type): def plot_eotf(self, L_bar, results_with_eotf_list, test_type):
"""绘制 EOTF 曲线 + 数据表格HDR 专用,包含实测亮度)""" """绘制 EOTF 曲线 + 数据表格HDR 专用,包含实测亮度)"""
self._ensure_chart_initialized("eotf")
# ========== 1. 清空并重置左侧曲线 ========== # ========== 1. 清空并重置左侧曲线 ==========
self.eotf_ax.clear() self.eotf_ax.clear()
self.eotf_ax.set_xlim(0, 105) self.eotf_ax.set_xlim(0, 105)
@@ -8053,8 +7985,6 @@ class PQAutomationApp:
def plot_cct(self, test_type): def plot_cct(self, test_type):
"""绘制 x 和 y 坐标分离图 - 每个点标注纵坐标值""" """绘制 x 和 y 坐标分离图 - 每个点标注纵坐标值"""
self._ensure_chart_initialized("cct")
self.cct_fig.clear() self.cct_fig.clear()
gray_data = self.results.get_intermediate_data("shared", "gray") gray_data = self.results.get_intermediate_data("shared", "gray")
@@ -8371,8 +8301,6 @@ class PQAutomationApp:
def plot_contrast(self, contrast_data, test_type): def plot_contrast(self, contrast_data, test_type):
"""绘制对比度测试结果 - 固定布局版本""" """绘制对比度测试结果 - 固定布局版本"""
self._ensure_chart_initialized("contrast")
# 清空并重置 # 清空并重置
self.contrast_ax.clear() self.contrast_ax.clear()
self.contrast_ax.set_xlim(0, 1) self.contrast_ax.set_xlim(0, 1)
@@ -8534,8 +8462,6 @@ class PQAutomationApp:
def plot_accuracy(self, accuracy_data, test_type): def plot_accuracy(self, accuracy_data, test_type):
"""绘制色准测试结果 - 29色显示 - 简洁版布局(显示 Gamma""" """绘制色准测试结果 - 29色显示 - 简洁版布局(显示 Gamma"""
self._ensure_chart_initialized("accuracy")
self.accuracy_ax.clear() self.accuracy_ax.clear()
self.accuracy_ax.set_xlim(0, 1) self.accuracy_ax.set_xlim(0, 1)
self.accuracy_ax.set_ylim(0, 1) self.accuracy_ax.set_ylim(0, 1)