forked from Nicolik/SimpleCNNClassifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_predict.py
More file actions
124 lines (97 loc) · 3.83 KB
/
Copy pathgui_predict.py
File metadata and controls
124 lines (97 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
############################################
# Simple GUI for Samplot classification
############################################
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk
from predict import load_model, get_classes, predict_image
def load_model_safe():
model_path = os.path.join('logs', 'dog_vs_cat.pth')
if not os.path.isfile(model_path):
messagebox.showerror("Model not found", f"Model file not found:\n{model_path}")
return None
try:
net = load_model(model_path)
classes = get_classes()
except Exception as err:
messagebox.showerror("Load error", f"Failed to load model:\n{err}")
return None
return net, classes
def choose_image():
filename = filedialog.askopenfilename(
title="Select a samplot image",
filetypes=[
("Image files", "*.jpg *.jpeg *.png *.bmp *.tif *.tiff"),
("All files", "*.*"),
],
)
if not filename:
return
selected_path.set(filename)
result_var.set("")
display_image(filename)
predict_button.config(state=tk.NORMAL)
def display_image(image_path):
try:
image = Image.open(image_path).convert('RGB')
except Exception as err:
messagebox.showerror("Image error", f"Unable to open image:\n{err}")
return
resample = getattr(Image, 'Resampling', None)
if resample is not None:
image.thumbnail((320, 320), resample.LANCZOS)
else:
image.thumbnail((320, 320), Image.LANCZOS)
photo = ImageTk.PhotoImage(image)
image_label.config(image=photo)
image_label.image = photo
def do_predict():
image_path = selected_path.get()
if not image_path or not os.path.isfile(image_path):
messagebox.showwarning("No image", "Please select an image first.")
return
try:
label = predict_image(net, image_path, classes)
result_var.set(label.capitalize())
except Exception as err:
messagebox.showerror("Prediction error", f"Failed to classify image:\n{err}")
def build_gui():
root = tk.Tk()
root.title("Samplot Classifier")
root.geometry("520x600")
root.resizable(False, False)
global selected_path, result_var
selected_path = tk.StringVar(root)
result_var = tk.StringVar(root)
frame = tk.Frame(root, padx=10, pady=10)
frame.pack(fill=tk.BOTH, expand=True)
title_label = tk.Label(frame, text="Samplot Classifier", font=("Arial", 18, "bold"))
title_label.pack(pady=(0, 10))
info_label = tk.Label(frame, text="Select a samplot image and click Predict.", font=("Arial", 11))
info_label.pack(pady=(0, 10))
choose_button = tk.Button(frame, text="Choose Image", command=choose_image, width=20)
choose_button.pack(pady=(0, 10))
path_label = tk.Label(frame, textvariable=selected_path, wraplength=480, justify=tk.LEFT)
path_label.pack(pady=(0, 10))
image_frame = tk.LabelFrame(frame, text="Preview", width=480, height=340)
image_frame.pack(fill=tk.BOTH, expand=False, pady=(0, 10))
image_frame.pack_propagate(False)
global image_label
image_label = tk.Label(image_frame)
image_label.pack(expand=True)
predict_btn = tk.Button(frame, text="Predict", command=do_predict, state=tk.DISABLED, width=20)
predict_btn.pack(pady=(0, 10))
global predict_button
predict_button = predict_btn
result_container = tk.Frame(frame)
result_container.pack(fill=tk.X, pady=(10, 0))
tk.Label(result_container, text="Result:", font=("Arial", 12)).pack(side=tk.LEFT)
tk.Label(result_container, textvariable=result_var, font=("Arial", 12, "bold"), fg="#007700").pack(side=tk.LEFT, padx=(10, 0))
root.mainloop()
if __name__ == '__main__':
loaded = load_model_safe()
if loaded is None:
raise SystemExit(1)
net, classes = loaded
build_gui()