Initial commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,61 @@
|
||||
import tkinter as tk
|
||||
|
||||
import numpy as np
|
||||
from PIL import ImageGrab, ImageTk
|
||||
from PIL.Image import Resampling
|
||||
|
||||
class DigitDrawer(tk.Frame):
|
||||
def __init__(self, parent, canvas_width, canvas_height):
|
||||
super().__init__(parent)
|
||||
self.canvas_width = canvas_width
|
||||
self.canvas_height = canvas_height
|
||||
self.brush_size = 3
|
||||
self.update_ui()
|
||||
|
||||
def clear_ui(self):
|
||||
for widget in self.winfo_children():
|
||||
widget.destroy()
|
||||
|
||||
def update_ui(self):
|
||||
self.clear_ui()
|
||||
# Create a Canvas to draw on
|
||||
self.canvas = tk.Canvas(self, width=self.canvas_width, height=self.canvas_height, bg='white')
|
||||
self.canvas.pack(padx=10, pady=10)
|
||||
self.canvas_demo = tk.Canvas(self, width=28, height=28, bg='white')
|
||||
self.canvas_demo.pack(padx=10, pady=10)
|
||||
|
||||
# Clear Button
|
||||
self.clear_button = tk.Button(self, text="Clear", command=self.clear_canvas)
|
||||
self.clear_button.pack(expand=True, fill='both')
|
||||
|
||||
# Bind mouse events to draw on the canvas
|
||||
self.canvas.bind("<B1-Motion>", self.paint)
|
||||
|
||||
def paint(self, event):
|
||||
"""Draw on the canvas by creating ovals (circles) at mouse position."""
|
||||
x1, y1 = (event.x - self.brush_size), (event.y - self.brush_size)
|
||||
x2, y2 = (event.x + self.brush_size), (event.y + self.brush_size)
|
||||
self.canvas.create_oval(x1, y1, x2, y2, fill='black', outline='black')
|
||||
|
||||
def clear_canvas(self):
|
||||
"""Clear the canvas to allow the user to draw a new digit."""
|
||||
self.canvas.delete("all")
|
||||
|
||||
def convert_to_array(self):
|
||||
"""Convert the canvas drawing to a 28x28 grayscale array."""
|
||||
# Get the canvas's pixel data and save it temporarily
|
||||
x = self.winfo_rootx() + self.canvas.winfo_x()
|
||||
y = self.winfo_rooty() + self.canvas.winfo_y()
|
||||
x1 = x + self.canvas.winfo_width()
|
||||
y1 = y + self.canvas.winfo_height()
|
||||
|
||||
# Capture the canvas area and convert it into a grayscale image using PIL
|
||||
image = ImageGrab.grab((x, y, x1, y1)).convert("L").resize((28, 28), resample=Resampling.HAMMING)
|
||||
self.demo_image = ImageTk.PhotoImage(image)
|
||||
self.canvas_demo.create_image(0, 0, anchor=tk.NW, image=self.demo_image)
|
||||
|
||||
image_array = np.asarray(image) / 255.0
|
||||
print(np.array(image_array).reshape((28, 28)))
|
||||
|
||||
flat_array = image_array.flatten()
|
||||
return flat_array
|
||||
@@ -0,0 +1,21 @@
|
||||
import tkinter as tk
|
||||
|
||||
from ui.icons.icons import icons
|
||||
|
||||
class LabelWithRefresh(tk.Frame):
|
||||
def __init__(self, parent, initial_text, callback, initial_state=tk.DISABLED):
|
||||
super().__init__(parent)
|
||||
self.callback = callback
|
||||
self._create_ui(initial_text, initial_state)
|
||||
|
||||
def _create_ui(self, initial_text, initial_state):
|
||||
self.refresh_button = tk.Button(self, image=icons["refresh"], state=initial_state, command=self.callback)
|
||||
self.refresh_button.pack(side=tk.RIGHT, padx=5)
|
||||
self.label = tk.Label(self, text=initial_text)
|
||||
self.label.pack(side=tk.RIGHT, padx=5)
|
||||
|
||||
def set_state(self, state):
|
||||
self.refresh_button.config(state=state)
|
||||
|
||||
def set_text(self, text):
|
||||
self.label.config(text=text)
|
||||
@@ -0,0 +1,14 @@
|
||||
import tkinter as tk
|
||||
|
||||
class NumberSlider(tk.Frame):
|
||||
def __init__(self, parent, value, from_, to, resolution):
|
||||
super().__init__(parent)
|
||||
self.value = value
|
||||
self.update_ui(from_, to, resolution)
|
||||
|
||||
def update_ui(self, from_, to, resolution):
|
||||
self.entry = tk.Entry(self, textvariable=self.value)
|
||||
self.entry.pack(side=tk.RIGHT, padx=5)
|
||||
self.scaler = tk.Scale(self, from_=from_, to=to, length=200, resolution=resolution, showvalue=False, orient=tk.HORIZONTAL, sliderrelief="flat", relief="flat", borderwidth=0, variable=self.value)
|
||||
self.scaler.set(self.value.get())
|
||||
self.scaler.pack(side=tk.RIGHT, padx=5)
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
import tkinter as tk
|
||||
|
||||
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
|
||||
from matplotlib.figure import Figure
|
||||
|
||||
from ui.plotters.plotter import Plotter
|
||||
|
||||
class PlotFrame(tk.Frame):
|
||||
def __init__(self, parent, width=None, height=None):
|
||||
super().__init__(parent, width=width, height=height)
|
||||
if width is not None or height is not None:
|
||||
self.pack_propagate(False)
|
||||
self.figure = self.create_plot_figure()
|
||||
self.plotter: Plotter = None
|
||||
|
||||
def create_plot_figure(self):
|
||||
figure = Figure(layout="compressed", facecolor=(0,0,0))
|
||||
# Create a matplotlib canvas to display the plot
|
||||
canvas = FigureCanvasTkAgg(figure, self)
|
||||
canvas.draw()
|
||||
(canvas.get_tk_widget()
|
||||
.pack(fill=tk.BOTH, expand=False, padx=0, pady=0, ipadx=0, ipady=0))
|
||||
return figure
|
||||
|
||||
def update_data(self, data):
|
||||
self.plotter.update_plot(data)
|
||||
Reference in New Issue
Block a user