Tkinter软件——显示txt标签的目标水平边框图像
代码:
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import cv2
from PIL import Image, ImageTk
import osclass ImageBoxApp:def __init__(self, master):self.master = masterself.master.title("Image Box Drawer")# 第一行:加载TXT文件夹和TXT路径显示self.txt_frame = tk.Frame(master)self.txt_frame.pack()self.load_txt_button = tk.Button(self.txt_frame, text="加载TXT文件夹", command=self.load_txt_folder)self.load_txt_button.pack(side=tk.LEFT)self.txt_path_label = tk.Label(self.txt_frame, text="TXT 文件夹路径: ")self.txt_path_label.pack(side=tk.LEFT)# 第二行:加载图像文件夹和图像路径显示self.img_frame = tk.Frame(master)self.img_frame.pack()self.load_img_button = tk.Button(self.img_frame, text="加载图像文件夹", command=self.load_img_folder)self.load_img_button.pack(side=tk.LEFT)self.img_path_label = tk.Label(self.img_frame, text="图像文件夹路径: ")self.img_path_label.pack(side=tk.LEFT)# 图像显示self.image_label = tk.Label(master)self.image_label.pack()# 最后一行:上一张、下一张按钮和当前图像路径显示self.button_frame = tk.Frame(master)self.button_frame.pack()self.prev_button = tk.Button(self.button_frame, text="上一张", command=self.prev_image)self.prev_button.pack(side=tk.LEFT)self.next_button = tk.Button(self.button_frame, text="下一张", command=self.next_image)self.next_button.pack(side=tk.LEFT)self.image_path_label = tk.Label(self.button_frame, text="当前图像路径: ")self.image_path_label.pack(side=tk.LEFT)self.txt_folder = Noneself.img_folder = Noneself.txt_files = []self.img_files = []self.current_index = 0def load_txt_folder(self):self.txt_folder = filedialog.askdirectory()if not self.txt_folder:returnself.txt_files = [f for f in os.listdir(self.txt_folder) if f.endswith('.txt')]if not self.txt_files:messagebox.showerror("错误", "该文件夹中没有TXT文件")else:self.txt_path_label.config(text=f"TXT 文件夹路径: {self.txt_folder}")def load_img_folder(self):self.img_folder = filedialog.askdirectory()if not self.img_folder:returnself.img_files = [f for f in os.listdir(self.img_folder) if f.endswith(('.png', '.jpg', '.jpeg'))]if not self.img_files:messagebox.showerror("错误", "该文件夹中没有图像文件")else:self.img_path_label.config(text=f"图像文件夹路径: {self.img_folder}")self.current_index = 0self.show_image()def show_image(self):if self.img_files and self.txt_files:img_path = os.path.join(self.img_folder, self.img_files[self.current_index])txt_path = os.path.join(self.txt_folder, self.txt_files[self.current_index])self.image_path_label.config(text=f"当前图像路径: {img_path}")self.draw_boxes(img_path, txt_path)def draw_boxes(self, image_path, txt_path):image = cv2.imread(image_path)height, width, _ = image.shapewith open(txt_path, 'r') as f:lines = f.readlines()for line in lines:parts = line.strip().split()class_id = int(parts[0])x_center = float(parts[1]) * widthy_center = float(parts[2]) * heightbox_width = float(parts[3]) * widthbox_height = float(parts[4]) * heightx1 = int(x_center - box_width / 2)y1 = int(y_center - box_height / 2)x2 = int(x_center + box_width / 2)y2 = int(y_center + box_height / 2)cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)image = Image.fromarray(image)image = ImageTk.PhotoImage(image)self.image_label.config(image=image)self.image_label.image = imagedef prev_image(self):if self.img_files and self.txt_files:if self.current_index == 0:messagebox.showinfo("提示", "已经是第一张图像")else:self.current_index -= 1self.show_image()def next_image(self):if self.img_files and self.txt_files:if self.current_index == len(self.img_files) - 1:messagebox.showinfo("提示", "已经是最后一张图像")else:self.current_index += 1self.show_image()if __name__ == "__main__":root = tk.Tk()app = ImageBoxApp(root)root.mainloop()
测试: