|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import simpledialog |
| 3 | +import cv2 as cv |
| 4 | +import os |
| 5 | +import PIL.Image, PIL.ImageTk |
| 6 | +import camera |
| 7 | +import model |
| 8 | +import sys |
| 9 | + |
| 10 | +sys.setrecursionlimit(3000) |
| 11 | + |
| 12 | +class App: |
| 13 | + |
| 14 | + def __init__(self, window = tk.Tk(), window_title = "Recognizer"): |
| 15 | + |
| 16 | + self.window = window |
| 17 | + self.window_title = window_title |
| 18 | + |
| 19 | + self.counters = [1, 1] |
| 20 | + |
| 21 | + self.model = model.Model() |
| 22 | + |
| 23 | + self.autoPredict = False |
| 24 | + |
| 25 | + self.camera = camera.Camera() |
| 26 | + |
| 27 | + self.startGUI() |
| 28 | + |
| 29 | + self.delay = 15 |
| 30 | + self.update() |
| 31 | + |
| 32 | + self.window.attributes('-topmost', True) |
| 33 | + self.window.mainloop() |
| 34 | + |
| 35 | + def startGUI(self): |
| 36 | + self.canvas = tk.Canvas(self.window, width=self.camera.width, height=self.camera.height) |
| 37 | + self.canvas.pack(anchor=tk.CENTER, expand=True) |
| 38 | + |
| 39 | + self.btn_toggleauto = tk.Button(self.window, text="Auto Prediction", width=50, command=self.autoPredictToggle) |
| 40 | + self.btn_toggleauto.pack(anchor=tk.CENTER, expand=True) |
| 41 | + |
| 42 | + self.classname_one = simpledialog.askstring("Classname One", "Enter the name of the first class: ", parent=self.window) |
| 43 | + self.classname_two = simpledialog.askstring("Classname Two", "Enter the name of the second class: ", parent=self.window) |
| 44 | + |
| 45 | + self.btn_class_one = tk.Button(self.window, text=self.classname_one, width=50, command=lambda: self.saveForClass(1)) |
| 46 | + self.btn_class_one.pack(anchor=tk.CENTER, expand=True) |
| 47 | + |
| 48 | + self.btn_class_two = tk.Button(self.window, text=self.classname_two, width=50, command=lambda: self.saveForClass(2)) |
| 49 | + self.btn_class_two.pack(anchor=tk.CENTER, expand=True) |
| 50 | + |
| 51 | + self.btn_train = tk.Button(self.window, text="Train Model", width=50, command=lambda: self.model.trainModel(self.counters)) |
| 52 | + self.btn_train.pack(anchor=tk.CENTER, expand=True) |
| 53 | + |
| 54 | + self.btn_predict = tk.Button(self.window, text="Predict", width=50, command=self.predict) |
| 55 | + self.btn_predict.pack(anchor=tk.CENTER, expand=True) |
| 56 | + |
| 57 | + self.btn_reset = tk.Button(self.window, text="Reset", width=50, command=self.reset) |
| 58 | + self.btn_reset.pack(anchor=tk.CENTER, expand=True) |
| 59 | + |
| 60 | + self.class_label = tk.Label(self.window, text="CLASS") |
| 61 | + self.class_label.config(font=("Calibri", 20)) |
| 62 | + self.class_label.pack(anchor=tk.CENTER, expand=True) |
| 63 | + |
| 64 | + |
| 65 | + def autoPredictToggle(self): |
| 66 | + self.autoPredict = not self.autoPredict |
| 67 | + |
| 68 | + def saveForClass(self, class_num): |
| 69 | + ret, frame = self.camera.getFrame() |
| 70 | + if not os.path.exists('class1'): |
| 71 | + os.mkdir('class1') |
| 72 | + if not os.path.exists('class2'): |
| 73 | + os.mkdir('class2') |
| 74 | + |
| 75 | + cv.imwrite(f'class{class_num}/frame{self.counters[class_num - 1]}.jpg', cv.cvtColor(frame, cv.COLOR_RGB2GRAY)) |
| 76 | + img = PIL.Image.open(f'class{class_num}/frame{self.counters[class_num - 1]}.jpg') |
| 77 | + img.thumbnail((150, 150), PIL.Image.ANTIALIAS) |
| 78 | + img.save(f'class{class_num}/frame{self.counters[class_num - 1]}.jpg') |
| 79 | + |
| 80 | + self.counters[class_num - 1] += 1 |
| 81 | + |
| 82 | + |
| 83 | + def reset(self): |
| 84 | + for directory in ['class1', 'class2']: |
| 85 | + for image in os.listdir(directory): |
| 86 | + file_path = os.path.join(directory, image) |
| 87 | + if os.path.isfile(file_path): |
| 88 | + os.unlink(file_path) |
| 89 | + |
| 90 | + self.counters = [1, 1] |
| 91 | + self.model = model.Model() |
| 92 | + self.class_label.config(text="CLASS") |
| 93 | + |
| 94 | + |
| 95 | + def update(self): |
| 96 | + if self.autoPredict: |
| 97 | + self.predict() |
| 98 | + pass |
| 99 | + |
| 100 | + ret, frame = self.camera.getFrame() |
| 101 | + |
| 102 | + if ret: |
| 103 | + self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) |
| 104 | + self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) |
| 105 | + |
| 106 | + print("Updating...") |
| 107 | + self.window.after(self.delay, self.update) |
| 108 | + |
| 109 | + |
| 110 | + def predict(self): |
| 111 | + frame = self.camera.getFrame() |
| 112 | + prediction = self.model.predict(frame) |
| 113 | + |
| 114 | + if prediction == 1: |
| 115 | + self.class_label.config(text=self.classname_one) |
| 116 | + return self.classname_one |
| 117 | + |
| 118 | + if prediction == 2: |
| 119 | + self.class_label.config(text=self.classname_two) |
| 120 | + return self.classname_two |
| 121 | + |
0 commit comments