| import os
|
| import torch
|
| import cv2
|
| import numpy as np
|
| from tkinter import filedialog, Tk, Label, Button
|
| from PIL import Image, ImageTk
|
| import matplotlib.pyplot as plt
|
|
|
| from models.dlinknet3 import DLinkNet34
|
| from postprocess import postprocess_mask
|
| from visualize import visualize_sample
|
|
|
|
|
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| model_path = "checkpoints/model_best.pth"
|
| threshold = 0.35
|
|
|
|
|
| model = DLinkNet34(num_classes=1)
|
| checkpoint = torch.load(model_path, map_location=device)
|
| model.load_state_dict(checkpoint['model_state_dict'])
|
| model.to(device)
|
| model.eval()
|
|
|
|
|
| def preprocess_image(image_path):
|
| image = cv2.imread(image_path)
|
| image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
|
|
| input_tensor = image_rgb.astype(np.float32) / 255.0
|
| input_tensor = input_tensor * 3.2 - 1.6
|
| input_tensor = np.transpose(input_tensor, (2, 0, 1))
|
| input_tensor = torch.tensor(input_tensor).unsqueeze(0).to(device)
|
|
|
| return image_rgb, input_tensor
|
|
|
|
|
| def predict(image_path):
|
| image_rgb, input_tensor = preprocess_image(image_path)
|
| with torch.no_grad():
|
| output = model(input_tensor)
|
| pred = (output.squeeze().cpu().numpy() > threshold).astype(np.uint8)
|
| pred = postprocess_mask(pred, min_size=10, kernel_size=1, dilate_iter=1)
|
| return image_rgb, pred
|
|
|
|
|
| class RoadSegApp:
|
| def __init__(self, master):
|
| self.master = master
|
| master.title("Road Segmentation App")
|
|
|
| self.label = Label(master, text="Select an image to predict road mask")
|
| self.label.pack(pady=10)
|
|
|
| self.select_button = Button(master, text="Select Image", command=self.load_image)
|
| self.select_button.pack(pady=5)
|
|
|
| self.quit_button = Button(master, text="Quit", command=master.quit)
|
| self.quit_button.pack(pady=5)
|
|
|
| def load_image(self):
|
| file_path = filedialog.askopenfilename(
|
| filetypes=[("Image Files", "*.jpg *.png *.tif *.tiff *.jpeg")]
|
| )
|
| if file_path:
|
| image_rgb, pred_mask = predict(file_path)
|
| visualize_sample(image_rgb, pred=pred_mask)
|
|
|
|
|
| if __name__ == "__main__":
|
| root = Tk()
|
| root.geometry("300x150")
|
| app = RoadSegApp(root)
|
| root.mainloop() |