-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_v2.py
More file actions
67 lines (58 loc) · 2.13 KB
/
predict_v2.py
File metadata and controls
67 lines (58 loc) · 2.13 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
from tkinter import font
import torch
from model import Net
from PIL import Image, ImageTk
import torchvision.transforms as transforms
import tkinter as tk
from tkinter import filedialog
# Initialize the model
model = Net()
model.load_state_dict(torch.load('mnist_model.pth'))
model.eval()
# Preprocessing function
def preprocess(image_path):
image = Image.open(image_path).convert('L')
transform = transforms.Compose([
transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
return transform(image).unsqueeze(0)
# Prediction function
def predict(image_path):
image_tensor = preprocess(image_path)
with torch.no_grad():
output = model(image_tensor)
pred = output.argmax(dim=1, keepdim=True)
return pred.item()
# create the UI interface
class App:
def __init__(self, root):
label_font = font.Font(family="Helvetica", size=18)
button_font = font.Font(family="Helvetica", size=16)
self.root = root
self.root.title("NN Network MNIST(SINGLE) Prediction")
self.canvas = tk.Canvas(root, width=400, height=400)
self.canvas.pack()
# self.canvas.font = label_font
self.label = tk.Label(root, text="Click 'Choose Image' to predict", width=50, height=4, font=label_font)
self.label.pack()
self.button = tk.Button(root, text="Choose Image", command=self.choose_image, font=button_font)
self.button.pack()
def choose_image(self):
file_path = filedialog.askopenfilename()
if file_path:
prediction = predict(file_path)
self.show_image(file_path)
self.label.config(text=f'Predicted digit: {prediction}')
def show_image(self, file_path):
image = Image.open(file_path)
image.thumbnail((400, 400))
photo = ImageTk.PhotoImage(image)
self.canvas.create_image(200, 200, image=photo)
self.canvas.image = photo
# run the UI interface
if __name__ == '__main__':
root = tk.Tk()
app = App(root)
root.mainloop()