1

我正在尝试使用 Python 和 Tkinter 创建图像分割应用程序。我无法获取通过filedialog.askopenfilename(用户从文件上传)获得的文件路径,并通过单击 Tkinter GUI 上的另一个按钮通过图像分割功能解析图像路径。

因为我想要一个按钮来收集文件路径,所以我创建了一个绑定到按钮的函数,但是图像分割函数无法在文件路径函数中获取路径变量。所以我创建了全局变量,但是,分割函数无法读取stringNoneType对象。另外,我尝试为所有这些创建一个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()

任何帮助将不胜感激。

4

1 回答 1

1

有3种方法可以解决这个问题,但首先:

command=function()例如,将函数的返回值设置为“命令”,因为函数将直接执行。在您的示例中,它应该引发错误,因为路径似乎尚未定义。

无论如何,这里有2个“解决方法”:

  1. 您可以创建一个名为 path 的变量,将语句global path放在 -function 的开头,open_img以便能够全局设置路径。然后你就可以从你的segment-function 中删除“path”参数,同时仍然读取 path 变量的内容。
  2. 创建一个类并将路径保存为类字段self.path或类似的东西,以便您也可以使用它segment

这是我的做法:

from tkinter import *
from PIL.ImageTk import Image, PhotoImage
import numpy as np
import matplotlib.pyplot as plt
#from ? import T

class App(Tk):
    def __init__(self)
        Tk.__init__(self)
        self.geometry("500x300")

        self.path = ''

        btn_frame = Frame(self, width=500, height=100)
        btn_frame.pack(side=TOP, expand=True, fill=BOTH)
        btn_frame.pack_propagate(False) #otherwise your width and height options would be useless
        bottom_frame = Frame(self, 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=self.open_img)
        btn1.pack(side=LEFT)
        btn2 = Button(btn_frame, text="Segment", width = 10, height = 1, cursor = "hand2", command=self.segment)
        #command=segment(net=fcn, path=path) would instantly execute the command but there is no path variable
        btn2.pack(side=LEFT)
        btn3 = Button(btn_frame, text="Save", width = 10, height = 1, cursor = "hand2")
        btn3.pack(side=LEFT)

        self.image_label = Label(bottom_frame)
        self.image_label.pack(fill=BOTH, expand=True)

        self.mainloop()
    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(self, net):
        #if an image is chosen path will be any other than ''
        if path: img = Image.open(self.path)
        else: return
        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(self):
        path = filedialog.askopenfilename(initialdir='/Downloads',title='Select Photo', filetypes=(('JPEG files', '*.jpg'),('PNG files', '*.png')))
        if path:
            self.path = path
            img = PhotoImage(Image.open(path))
            #img = tkinter.PhotoImage(file=path) should work too but then you'll need to remove the import of PILs PhotoImage
            self.image_label.img = img #keep a reference to the image object
            self.image_label.config(image=img)
            #plt.imshow(img); plt.axis('off'); plt.show()
于 2020-05-24T14:45:54.230 回答