Image segmentation model GUI application: implemented based on Tkinter and MMseg

Introduction

This blog introduces an image segmentation model GUI application created using Python's Tkinter library and MMseg image segmentation library. The application allows the user to load a folder of images, browse the loaded images, and perform segmentation inference on the selected image, displaying the segmentation results. This application demonstrates how to use a graphical interface combined with a deep learning model to perform image segmentation tasks in an interactive manner.

Background introduction

Introduces the concepts and application fields of image segmentation tasks, emphasizing the importance of image segmentation in computer vision.

GUI application overview

Explain the purpose and function of the image segmentation GUI application created using Python's Tkinter library.

Dependent libraries

List the key dependent libraries used by the application, including Tkinter, PIL, MMseg, etc., and explain the role of each library.

Application function explanation

Explain in detail the functions of each button and component of the application interface, such as the "Load Folder" button to load image folders, the "Next Image" button to switch to browsing images, and the "Perform Inference" button to perform image segmentation inference, etc.

Model initialization and inference

Introduces the initialization process of the image segmentation model used in the application, including configuration file path, model weight file path, and device selection.
Explain how to use the MMseg library for image segmentation inference, including loading images, performing inference, and obtaining segmentation results.

Image display and segmentation results

Explain how to display the loaded raw image in the application interface and display the segmentation results after performing segmentation inference.

Application display and operation process

Through step-by-step examples, the application's operation flow is demonstrated, from loading an image folder to performing image segmentation inference, and viewing a demonstration of the segmentation results.

code analysis

Analyze the main code snippets, explain the role of key functions and methods, and help readers understand the code implementation details.

Show results

Shows screenshots of the application running, including the interface for loading images, performing inference, and viewing segmentation results.

import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os
from mmseg.apis import inference_segmentor, init_segmentor
import mmcv

# Initialize the model
#配置文件路径
config_file = 'XXXX'
#pth模型路径
checkpoint_file = 'XXXXX'
#有gpu就device='cuda:0'、没gpu就device='cpu'
model = init_segmentor(config_file, checkpoint_file, device='cuda:0')

class ImageInferenceApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Image Inference GUI")
        self.root.geometry("800x600")

        self.image_paths = []
        self.current_image_idx = 0
        self.result_image = None

        self.load_button = tk.Button(root, text="Load Folder", command=self.load_folder)
        self.load_button.pack(side="top", padx=10, pady=10)

        self.next_button = tk.Button(root, text="Next Image", command=self.next_image)
        self.next_button.pack(side="top", padx=10, pady=10)

        self.perform_inference_button = tk.Button(root, text="Perform Inference", command=self.perform_inference)
        self.perform_inference_button.pack(side="top", padx=10, pady=10)


        self.center_frame = tk.Frame(root)
        self.center_frame.pack(side="top", pady=20)

        self.image_label = tk.Label(self.center_frame)
        self.image_label.pack(side="left")

        self.result_label = tk.Label(self.center_frame)
        self.result_label.pack(side="right")

    def load_folder(self):
        folder_path = filedialog.askdirectory()
        if folder_path:
            self.image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path)]
            self.current_image_idx = 0
            self.load_current_image()

    def next_image(self):
        if self.image_paths:
            self.current_image_idx = (self.current_image_idx + 1) % len(self.image_paths)
            self.load_current_image()

    def load_current_image(self):
        if self.image_paths:
            image_path = self.image_paths[self.current_image_idx]
            image = Image.open(image_path)
            #image = image.resize((512, 512))  # 调整图像尺寸为 512x512
            self.display_image(image)
            self.result_image = None
            self.result_label.configure(image=None)

    def display_image(self, image):
        tk_image = ImageTk.PhotoImage(image)
        self.image_label.configure(image=tk_image)
        self.image_label.image = tk_image

    def perform_inference(self):
        if self.image_paths:
            image_path = self.image_paths[self.current_image_idx]
            img = mmcv.imread(image_path)
            result = inference_segmentor(model, img)
            result_image = model.show_result(image_path, result, out_file=None, opacity=0.5)
            self.result_image = Image.fromarray(result_image)
            tk_result_image = ImageTk.PhotoImage(self.result_image)
            self.result_label.configure(image=tk_result_image)
            self.result_label.image = tk_result_image

if __name__ == "__main__":
    root = tk.Tk()
    app = ImageInferenceApp(root)
    root.mainloop()

Insert image description here
Show me a picture of the GUI, it’s so ugly.

Guess you like

Origin blog.csdn.net/hasque2019/article/details/132581367