我使用数据集,训练了一个模型并想要获取更多数据,因此我在未标记的数据上使用我的模型,然后使用模型制作的标签绘制数据,看看它是否是一个好的标签。我想实现一个滑块来选择标签的不透明度,看看它是否是相关标签。 问题是滑块确实显示,但我无法与其交互。
这是我的代码,我有最新版本的 matplotlib,
def mise_a_jour_graphique(val):
global fig
alpha = slider_alpha.val
a_dessiner(alpha)
fig.canvas.draw_idle()
def a_dessiner(alpha):
global nb_label
global coor
global image_rgb
global ax
global fig
annotations = []
ax.clear()
ax.imshow(image_rgb) # Afficher l'image de fond
for i, el in coor:
annotation = dessin(i, el, image_rgb, alpha)
fig.canvas.draw_idle()
annotations.append(annotation)
nb_label = len(annotations)
adjust_text(annotations, arrowprops=dict(arrowstyle="->", color='black'))
ax.axis("off")
#fig.canvas.draw_idle()
def dessin(j, coor, img, alpha):
global ax
global fig
w, h, _ = img.shape
x_coor, y_coor = [], []
for i in range(0, len(coor)-1, 2):
x, y = float(coor[i]), float(coor[i+1])
x_coor.append(x * h)
y_coor.append(y * w)
ax.scatter(x_coor, y_coor, s=1, alpha=alpha)
ax.plot(x_coor, y_coor, color='black', zorder=1, alpha=alpha)
ax.fill(x_coor, y_coor, color="black", alpha=alpha)
annotation = ax.text(x_coor[0], y_coor[0], f'Label {j}', fontsize=9, ha='center', bbox=dict(boxstyle="round,pad=0.3", edgecolor="black", facecolor="white", alpha=alpha))
fig.canvas.draw_idle()
return annotation
def tracer_figure(path_files):
global nb_label
global coor
global image_rgb
global fig
global ax
global slider_alpha
if path_files.endswith('.txt'):
lbl_pt = path_files
img_pt = lbl_pt.replace('/labels', '/images').replace('txt', 'jpg')
elif path_files.endswith('.jpg'):
img_pt = path_files
lbl_pt = img_pt.replace('/images', '/labels').replace('jpg', 'txt')
else:
raise ValueError
coor = label_to_liste(lbl_pt)
image = cv2.imread(img_pt)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.25)
alpha_init = 0.7
a_dessiner(alpha_init)
ax.imshow(image_rgb)
axfreq = fig.add_axes([0.25, 0.1, 0.65, 0.03])
slider_alpha = Slider(ax=axfreq,label='Oppacité', valmin=0.1, valmax=1.0, valinit=alpha_init, valstep=0.1)
slider_alpha.on_changed(mise_a_jour_graphique)
plt.show()
你的代码问题在于你多次调用了
fig.canvas.draw_idle()
,而且在
a_dessiner
函数中的循环内部也调用了它。 这可能会干扰 matplotlib 的事件循环并阻止滑块正常工作。
以下是修改后的代码,删除了多余的
fig.canvas.draw_idle()
调用:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import cv2
# ... 其他函数定义 ...
def mise_a_jour_graphique(val):
global fig
alpha = slider_alpha.val
a_dessiner(alpha)
# 移除 fig.canvas.draw_idle()
def a_dessiner(alpha):
global nb_label
global coor
global image_rgb
global ax
global fig
annotations = []
ax.clear()
ax.imshow(image_rgb) # Afficher l'image de fond
for i, el in coor:
annotation = dessin(i, el, image_rgb, alpha)
# 移除 fig.canvas.draw_idle()
annotations.append(annotation)
nb_label = len(annotations)
adjust_text(annotations, arrowprops=dict(arrowstyle="->", color='black'))
ax.axis("off")
# 移除 fig.canvas.draw_idle()
def dessin(j, coor, img, alpha):
global ax
global fig
w, h, _ = img.shape
x_coor, y_coor = [], []
for i in range(0, len(coor)-1, 2):
x, y = float(coor[i]), float(coor[i+1])
x_coor.append(x * h)
y_coor.append(y * w)
ax.scatter(x_coor, y_coor, s=1, alpha=alpha)
ax.plot(x_coor, y_coor, color='black', zorder=1, alpha=alpha)
ax.fill(x_coor, y_coor, color="black", alpha=alpha)
annotation = ax.text(x_coor[0], y_coor[0], f'Label {j}', fontsize=9, ha='center', bbox=dict(boxstyle="round,pad=0.3", edgecolor="black", facecolor="white", alpha=alpha))
# 移除 fig.canvas.draw_idle()
return annotation
def tracer_figure(path_files):
global nb_label
global coor
global image_rgb
global fig
global ax
global slider_alpha
# ... 其他代码 ...
slider_alpha = Slider(ax=axfreq,label='Oppacité', valmin=0.1, valmax=1.0, valinit=alpha_init, valstep=0.1)
slider_alpha.on_changed(mise_a_jour_graphique)
plt.show()
通过只在
mise_a_jour_graphique
函数的末尾调用一次
fig.canvas.draw_idle()
,你就可以确保图形在滑块值改变后只更新一次,从而使滑块能够正常工作。