【发布时间】:2020-05-24 22:45:54
【问题描述】:
我正在尝试使用 Python 和 Tkinter 创建图像分割应用程序。我无法获取通过filedialog.askopenfilename(用户从文件上传)获得的文件路径,并通过单击 Tkinter GUI 上的另一个按钮通过图像分割功能解析图像路径。
因为我想要一个按钮来收集文件路径,所以我创建了一个绑定到按钮的函数,但是图像分割函数无法在文件路径函数中获取路径变量。所以我创建了全局变量,但是,分段函数无法读取 string 或 NoneType 对象。另外,我尝试为所有这些创建一个class,但它不起作用。
这里是两个函数(分割函数需要decode_segmap):
# Define the helper function
def decode_segmap(image, nc=21):
label_colors = np.array([(0, 0, 0), # 0=background
# 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
(128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),
# 6=bus, 7=car, 8=cat, 9=chair, 10=cow
(0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0),
# 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person
(192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
# 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
(0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)])
r = np.zeros_like(image).astype(np.uint8)
g = np.zeros_like(image).astype(np.uint8)
b = np.zeros_like(image).astype(np.uint8)
for l in range(0, nc):
idx = image == l
r[idx] = label_colors[l, 0]
g[idx] = label_colors[l, 1]
b[idx] = label_colors[l, 2]
rgb = np.stack([r, g, b], axis=2)
return rgb
def segment(net, path):
img = Image.open(path)
plt.imshow(img); plt.axis('off'); plt.show()
# Comment the Resize and CenterCrop for better inference results
trf = T.Compose([T.Resize(256),
T.CenterCrop(224),
T.ToTensor(),
T.Normalize(mean = [0.485, 0.456, 0.406],
std = [0.229, 0.224, 0.225])])
inp = trf(img).unsqueeze(0)
out = net(inp)['out']
om = torch.argmax(out.squeeze(), dim=0).detach().cpu().numpy()
rgb = decode_segmap(om)
plt.imshow(rgb); plt.axis('off'); plt.show()
def open_img():
path = filedialog.askopenfilename(initialdir='/Downloads',title='Select Photo', filetypes=(('JPEG files', '*.jpg'),('PNG files', '*.png')))
img = Image.open(path)
plt.imshow(img); plt.axis('off'); plt.show()
Tkinter 代码(两帧,一帧用于按钮,另一帧用于预览图像):
如您所见,segment 函数有两个参数,fcn 是神经网络,是文件中的“全局”变量,但是无法获取路径参数,因为该变量位于绑定到按钮的另一个函数中。
window = Tk()
window.geometry("500x300")
btn_frame = Frame(window, width=500, height=100)
btn_frame.pack(side="top", expand=True, fill="both")
bottom_frame = Frame(window, width=500, height=200)
bottom_frame.pack(side="bottom", expand=True, fill="both")
btn1 = Button(btn_frame, text="Open", width = 10, height = 1, cursor = "hand2", command=open_img)
btn1.pack(side="left")
btn2 = Button(btn_frame, text="Segment", width = 10, height = 1, cursor = "hand2", command=segment(net=fcn, path=path))
btn2.pack(side="left")
btn3 = Button(btn_frame, text="Save", width = 10, height = 1, cursor = "hand2")
btn3.pack(side="left")
window.mainloop()
任何帮助将不胜感激。
【问题讨论】:
-
你还在使用
command=segment(net=fcn, path=path),这已经在你之前的同一个问题中得到了回答:python-tkinter-multiple-buttons-wont-display-in-frame -
这能回答你的问题吗? Tutorial - 9.2. Python Scopes and Namespaces
-
试试这个?
btn2 = Button(btn_frame, text="Segment", width = 10, height = 1, cursor = "hand2", command=lambda: segment(net=fcn, path=path)) -
@stovfl 谢谢,我最终根据以下答案将您的建议添加到新代码中。
-
@CoolCloud 原来这个错误与 MacOS 有关,我得到了解决。我终于让应用程序正常运行了。
标签: python python-3.x numpy tkinter