Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions draw_smiley_varied.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import torch
import torch.nn as nn

# --- Настройки ---
CELL_SIZE = 5
GRID_SIZE = 100
WINDOW_SIZE = CELL_SIZE * GRID_SIZE

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using device:", device)

# --- Модель ---
class SmileyCNN(nn.Module):
def __init__(self):
super().__init__()
Expand All @@ -29,45 +26,45 @@ def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x

# --- Загружаем модель ---
model = SmileyCNN().to(device)
model.load_state_dict(torch.load("smiley_model_varied.pth", map_location=device))
model.eval()

# --- Предсказание ---
def predict(grid):
img_tensor = torch.tensor(grid, dtype=torch.float32).unsqueeze(0).unsqueeze(0).to(device)
with torch.no_grad():
output = model(img_tensor)
pred = torch.argmax(output, dim=1).item()
return "Smiley" if pred==1 else "Not Smiley"
return "Smiley 😊" if pred==1 else "Not Smiley 😢"

# --- GUI ---
class DrawGrid:
def __init__(self, master):
self.master = master
self.canvas = tk.Canvas(master, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white")
self.canvas.pack()
self.master.configure(bg="#f0f0f0")
self.canvas = tk.Canvas(master, width=WINDOW_SIZE, height=WINDOW_SIZE, bg="white", highlightthickness=2, highlightbackground="black")
self.canvas.pack(padx=10, pady=10)
self.grid = np.zeros((GRID_SIZE, GRID_SIZE), dtype=np.float32)

# События
self.canvas.bind("<B1-Motion>", self.paint_and_predict)
self.canvas.bind("<Button-3>", self.clear) # правая кнопка мыши
self.canvas.bind("<Button-1>", self.paint_and_predict)
self.canvas.bind("<Button-3>", self.clear)
self.label = tk.Label(master, text="Prediction: ", font=("Arial", 16), bg="#f0f0f0")
self.label.pack(pady=5)
self.button_frame = tk.Frame(master, bg="#f0f0f0")
self.button_frame.pack(pady=5)

self.label = tk.Label(master, text="Prediction: ", font=("Arial", 16))
self.label.pack()
self.clear_button = tk.Button(self.button_frame, text="Clear", command=self.clear, bg="#ffcccc", fg="black", font=("Arial", 12), width=10)
self.clear_button.pack(side=tk.LEFT, padx=5)

self.clear_button = tk.Button(master, text="Clear", command=self.clear)
self.clear_button.pack()
self.exit_button = tk.Button(self.button_frame, text="Exit", command=master.quit, bg="#ccccff", fg="black", font=("Arial", 12), width=10)
self.exit_button.pack(side=tk.LEFT, padx=5)

def paint(self, event):
x, y = event.x // CELL_SIZE, event.y // CELL_SIZE
if 0 <= x < GRID_SIZE and 0 <= y < GRID_SIZE:
self.grid[y, x] = 1.0
self.canvas.create_rectangle(x*CELL_SIZE, y*CELL_SIZE,
(x+1)*CELL_SIZE, (y+1)*CELL_SIZE,
fill="black")
fill="black", outline="#888888")

def paint_and_predict(self, event):
self.paint(event)
Expand All @@ -78,10 +75,8 @@ def clear(self, event=None):
self.canvas.delete("all")
self.grid = np.zeros((GRID_SIZE, GRID_SIZE), dtype=np.float32)
self.label.config(text="Prediction: ")

# --- Запуск GUI ---
root = tk.Tk()
root.title("Draw a Smiley or Sad Face (Varied Model)")
root.title("Draw a Smiley or Sad Face 😀")
root.resizable(False, False)
app = DrawGrid(root)
root.mainloop()