Skip to content

Commit aa2be20

Browse files
committed
Add prediction confidence window with progress bars
Introduces a new window displaying the predicted digit and confidence levels for each class using progress bars. Improves drawing smoothness by tracking the last mouse position, and updates the UI to reflect prediction results and clear state.
1 parent 962c6a9 commit aa2be20

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

main.py

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import sys
3+
from tkinter import ttk
34

45
import numpy as np
56
import pandas as pd
@@ -226,6 +227,8 @@ def __init__(self, neuralNetwork: NeuralNetwork):
226227
self.canvas_size = 280
227228
self.image_size = 28
228229

230+
self.last_x, self.last_y = None, None
231+
229232
self.canvas = tk.Canvas(self.root, width=self.canvas_size, height=self.canvas_size, bg="white")
230233
self.canvas.pack()
231234

@@ -241,21 +244,79 @@ def __init__(self, neuralNetwork: NeuralNetwork):
241244
self.draw = ImageDraw.Draw(self.image)
242245

243246
self.canvas.bind("<B1-Motion>", self.paint)
247+
self.canvas.bind("<ButtonPress-1>", self.start_pos)
248+
self.canvas.bind("<ButtonRelease-1>", self.reset_last_pos)
249+
250+
self.bar_labels = None
251+
self.bars = None
252+
self.bar_frames = None
253+
self.label_pred = None
254+
self.conf_window = None
255+
256+
self.create_confidence_window()
244257

245258
self.root.mainloop()
246259

260+
def create_confidence_window(self):
261+
self.conf_window = tk.Toplevel(self.root)
262+
self.conf_window.title("Prediction Confidence")
263+
264+
self.label_pred = tk.Label(self.conf_window, text="Prediction: None", font=("Arial", 24))
265+
self.label_pred.pack(pady=10)
266+
267+
self.bar_frames = []
268+
self.bars = []
269+
self.bar_labels = []
270+
271+
for i in range(10):
272+
frame = tk.Frame(self.conf_window)
273+
frame.pack(fill="x", padx=10, pady=2)
274+
275+
tk.Label(frame, text=str(i), width=2).pack(side="left")
276+
bar = ttk.Progressbar(frame, length=200, maximum=1.0)
277+
bar.pack(side="left", padx=5)
278+
label = tk.Label(frame, text="0.00%")
279+
label.pack(side="left")
280+
281+
self.bar_frames.append(frame)
282+
self.bars.append(bar)
283+
self.bar_labels.append(label)
284+
285+
def start_pos(self, event):
286+
self.last_x, self.last_y = event.x, event.y
287+
288+
def reset_last_pos(self, event):
289+
self.last_x, self.last_y = None, None
290+
247291
def paint(self, event):
248292
x, y = event.x, event.y
249293
r = 10
250-
self.canvas.create_oval(x-r, y-r, x+r, y+r, fill="black", outline="black")
251-
self.draw.ellipse([x-r, y-r, x+r, y+r], fill=0)
294+
295+
if self.last_x is not None and self.last_y is not None:
296+
self.canvas.create_line(self.last_x, self.last_y, x, y, fill="black", width=r * 2, capstyle=tk.ROUND,
297+
smooth=True)
298+
299+
self.draw.line([self.last_x, self.last_y, x, y], fill=0, width=r * 2)
300+
else:
301+
self.canvas.create_oval(x - r, y - r, x + r, y + r, fill="black", outline="black")
302+
self.draw.ellipse([x - r, y - r, x + r, y + r], fill=0)
303+
304+
self.last_x, self.last_y = x, y
305+
252306
self.predict_digit()
253307

254308
def clear_canvas(self):
255309
self.canvas.delete("all")
256310
self.image = Image.new("L", (self.canvas_size, self.canvas_size), 255)
257311
self.draw = ImageDraw.Draw(self.image)
258312

313+
self.label_pred.config(text=f"Prediction: None")
314+
315+
for i in range(10):
316+
prob = 0
317+
self.bars[i]['value'] = prob
318+
self.bar_labels[i].config(text=f"{prob * 100:.2f}%")
319+
259320
def preprocess_image(self):
260321
img = self.image.convert("L")
261322
img = ImageOps.invert(img)
@@ -276,8 +337,14 @@ def preprocess_image(self):
276337

277338
def predict_digit(self):
278339
img_array = self.preprocess_image()
279-
prediction, _ = self.neural_network.forward(img_array)
280-
print("Prediction:", prediction)
340+
prediction, probabilities = self.neural_network.forward(img_array)
341+
342+
self.label_pred.config(text=f"Prediction: {prediction}")
343+
344+
for i in range(10):
345+
prob = probabilities[i, 0]
346+
self.bars[i]['value'] = prob
347+
self.bar_labels[i].config(text=f"{prob * 100:.2f}%")
281348

282349
def show_input(self):
283350
img_array = self.preprocess_image()

0 commit comments

Comments
 (0)