调用canvas.delete()时Tkinter Rectangle不会消失

时间:2018-06-11 06:52:54

标签: python user-interface tkinter

我正在尝试制作一个用户界面,允许用户在图像上绘制一些边界框,用于我希望的机器学习任务。

代码如下(为简洁省略了很多方法定义):

class BBoxTool():
    def __init__(self):
        self.box = []
        self.boxes = []
        self.box_rects = []
        self.current_file_idx = 0

        self.root_win = tk.Tk()
        self.root_win.title("Bounding Box Annotator")
        cfg.parse_config()
        self.read_annotations() # Read in existing annotations

        self.img_dir = os.getcwd() + "/" + cfg.config["IMG_DIR"]

        with open(cfg.config["DATA_DIR"] + cfg.config["ANNOTATIONS"]) as f:
            self.image_files = [img for img in os.listdir(self.img_dir) if os.path.isfile(os.path.join(self.img_dir, img))]
            self.change_image(self.current_file_idx)

            self.root_win.mainloop()

    def draw_existing_boxes(self):
        try:
            self.boxes = self.annotations[self.current_file]

            if len(self.boxes) > 0:
                for i in range(len(self.boxes)):
                    self.box = self.boxes[i]
                    self.draw_rect(i)

        except KeyError:
            print("Unannotated file: {0}".format(self.current_file))

    def change_image(self, index):
        self.current_file_idx = index
        self.current_file = self.image_files[self.current_file_idx]

        self.setup_display()

        self.draw_existing_boxes()

    def draw_rect(self, idx):
        try:
            self.box_rects[idx] = self.canvas.create_rectangle(self.box[0], self.box[1], self.box[2], self.box[3], outline = "#3ace27")

        except IndexError:
            self.box_rects.append(self.canvas.create_rectangle(self.box[0], self.box[1], self.box[2], self.box[3], outline = "#3ace27"))

    def setup_display(self):
        self.img = ImageTk.PhotoImage(Image.open(self.img_dir + self.current_file))
        width = self.img.width()
        height = self.img.height()

        try:
            self.canvas.destroy()
            self.next_button.destroy()
            self.prev_button.destroy()
            self.set_button.destroy()
            self.save_button.destroy()
            self.reset_button.destroy()
            self.del_last_button.destroy()
            self.label.destroy()

        except AttributeError:
            print("Starting")

        # Blah...
        self.del_last_button = tk.Button(self.frame, text = "DELETE LAST")
        self.del_last_button.grid(row = 0, column = 3)
        self.del_last_button.bind("<ButtonRelease-1>", self.delete_last)

        self.canvas = tk.Canvas(self.root_win, width = width, height = height)
        self.canvas.pack()
        self.canvas.create_image(width / 2, height / 2, image = self.img)

        self.canvas.bind("<Button-1>", self.click)
        self.canvas.bind("<B1-Motion>", self.drag)
        self.canvas.bind("<ButtonRelease-1>", self.release)

    def delete_last(self, event):
        idx = len(self.boxes) - 1

        if idx >= 0:
            rect = self.box_rects[idx]
            self.canvas.delete(rect)
            del self.box_rects[idx]
            del self.boxes[idx]

问题是:当我运行它并从文件中加载现有的边界框时,它工作正常,我可以看到图像上正确位置的框。但是,当我按下&#34;删除最后一个&#34;按钮,该框不会被删除。

这个特别奇怪的是,当我在图像上绘制一个新框然后尝试删除它时,它工作正常;该问题仅出现在使用从注释文件加载的坐标绘制的边界框中。通过调试输出,我知道框#34存在&#34;,因为我可以从self.canvas.bbox(rect)方法中调用delete_last()

0 个答案:

没有答案