├── README.md ├── __init__.py ├── app.py ├── camera.py ├── main.py └── model.py /README.md: -------------------------------------------------------------------------------- 1 | # camera-classifier 2 | A Python application that uses camera input to train a SVM to respond to specific actions. 3 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/amilmshaji/3D-plot-in-Matplotlib/bb20f8fa15423b8f4c7731b82d112a9484ed0e04/__init__.py -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Camera Classifier v0.1 Alpha 3 | Copyright (c) NeuralNine 4 | 5 | Instagram: @neuralnine 6 | YouTube: NeuralNine 7 | Website: www.neuralnine.com 8 | ''' 9 | 10 | import tkinter as tk 11 | from tkinter import simpledialog 12 | import cv2 as cv 13 | import os 14 | import PIL.Image, PIL.ImageTk 15 | import model 16 | import camera 17 | 18 | class App: 19 | 20 | def __init__(self, window=tk.Tk(), window_title="Camera Classifier"): 21 | 22 | self.window = window 23 | self.window_title = window_title 24 | 25 | self.counters = [1, 1] 26 | 27 | self.model = model.Model() 28 | 29 | self.auto_predict = False 30 | 31 | self.camera = camera.Camera() 32 | 33 | self.init_gui() 34 | 35 | self.delay = 15 36 | self.update() 37 | 38 | self.window.attributes("-topmost", True) 39 | self.window.mainloop() 40 | 41 | def init_gui(self): 42 | 43 | self.canvas = tk.Canvas(self.window, width=self.camera.width, height=self.camera.height) 44 | self.canvas.pack() 45 | 46 | self.btn_toggleauto = tk.Button(self.window, text="Auto Prediction", width=50, command=self.auto_predict_toggle) 47 | self.btn_toggleauto.pack(anchor=tk.CENTER, expand=True) 48 | 49 | self.classname_one = simpledialog.askstring("Classname One", "Enter the name of the first class:", parent=self.window) 50 | self.classname_two = simpledialog.askstring("Classname Two", "Enter the name of the second class:", parent=self.window) 51 | 52 | self.btn_class_one = tk.Button(self.window, text=self.classname_one, width=50, command=lambda: self.save_for_class(1)) 53 | self.btn_class_one.pack(anchor=tk.CENTER, expand=True) 54 | 55 | self.btn_class_two = tk.Button(self.window, text=self.classname_two, width=50, command=lambda: self.save_for_class(2)) 56 | self.btn_class_two.pack(anchor=tk.CENTER, expand=True) 57 | 58 | self.btn_train = tk.Button(self.window, text="Train Model", width=50, command=lambda: self.model.train_model(self.counters)) 59 | self.btn_train.pack(anchor=tk.CENTER, expand=True) 60 | 61 | self.btn_predict = tk.Button(self.window, text="Predcit", width=50, command=self.predict) 62 | self.btn_predict.pack(anchor=tk.CENTER, expand=True) 63 | 64 | self.btn_reset = tk.Button(self.window, text="Reset", width=50, command=self.reset) 65 | self.btn_reset.pack(anchor=tk.CENTER, expand=True) 66 | 67 | self.class_label = tk.Label(self.window, text="CLASS") 68 | self.class_label.config(font=("Arial", 20)) 69 | self.class_label.pack(anchor=tk.CENTER, expand=True) 70 | 71 | 72 | def auto_predict_toggle(self): 73 | self.auto_predict = not self.auto_predict 74 | 75 | def save_for_class(self, class_num): 76 | ret, frame = self.camera.get_frame() 77 | if not os.path.exists("1"): 78 | os.mkdir("1") 79 | if not os.path.exists("2"): 80 | os.mkdir("2") 81 | 82 | cv.imwrite(f'{class_num}/frame{self.counters[class_num-1]}.jpg', cv.cvtColor(frame, cv.COLOR_RGB2GRAY)) 83 | img = PIL.Image.open(f'{class_num}/frame{self.counters[class_num - 1]}.jpg') 84 | img.thumbnail((150, 150), PIL.Image.ANTIALIAS) 85 | img.save(f'{class_num}/frame{self.counters[class_num - 1]}.jpg') 86 | 87 | self.counters[class_num - 1] += 1 88 | 89 | def reset(self): 90 | for folder in ['1', '2']: 91 | for file in os.listdir(folder): 92 | file_path = os.path.join(folder, file) 93 | if os.path.isfile(file_path): 94 | os.unlink(file_path) 95 | 96 | self.counters = [1, 1] 97 | self.model = model.Model() 98 | self.class_label.config(text="CLASS") 99 | 100 | def update(self): 101 | if self.auto_predict: 102 | print(self.predict()) 103 | 104 | ret, frame = self.camera.get_frame() 105 | 106 | if ret: 107 | self.photo = PIL.ImageTk.PhotoImage(image=PIL.Image.fromarray(frame)) 108 | self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW) 109 | 110 | self.window.after(self.delay, self.update) 111 | 112 | def predict(self): 113 | frame = self.camera.get_frame() 114 | prediction = self.model.predict(frame) 115 | 116 | if prediction == 1: 117 | self.class_label.config(text=self.classname_one) 118 | return self.classname_one 119 | if prediction == 2: 120 | self.class_label.config(text=self.classname_two) 121 | return self.classname_two -------------------------------------------------------------------------------- /camera.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Camera Classifier v0.1 Alpha 3 | Copyright (c) NeuralNine 4 | 5 | Instagram: @neuralnine 6 | YouTube: NeuralNine 7 | Website: www.neuralnine.com 8 | ''' 9 | 10 | import cv2 as cv 11 | 12 | class Camera: 13 | 14 | def __init__(self): 15 | self.camera = cv.VideoCapture(0) 16 | if not self.camera.isOpened(): 17 | raise ValueError("Unable to open camera!") 18 | 19 | self.width = self.camera.get(cv.CAP_PROP_FRAME_WIDTH) 20 | self.height = self.camera.get(cv.CAP_PROP_FRAME_HEIGHT) 21 | 22 | def __del__(self): 23 | if self.camera.isOpened(): 24 | self.camera.release() 25 | 26 | def get_frame(self): 27 | if self.camera.isOpened(): 28 | ret, frame = self.camera.read() 29 | 30 | if ret: 31 | return (ret, cv.cvtColor(frame, cv.COLOR_BGR2RGB)) 32 | else: 33 | return (ret, None) 34 | else: 35 | return None -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Camera Classifier v0.1 Alpha 3 | Copyright (c) NeuralNine 4 | 5 | Instagram: @neuralnine 6 | YouTube: NeuralNine 7 | Website: www.neuralnine.com 8 | ''' 9 | 10 | import app 11 | 12 | def main(): 13 | app.App(window_title="Camera Classifier v0.1 Alpha") 14 | 15 | if __name__ == "__main__": 16 | main() -------------------------------------------------------------------------------- /model.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Camera Classifier v0.1 Alpha 3 | Copyright (c) NeuralNine 4 | 5 | Instagram: @neuralnine 6 | YouTube: NeuralNine 7 | Website: www.neuralnine.com 8 | ''' 9 | 10 | from sklearn.svm import LinearSVC 11 | import numpy as np 12 | import cv2 as cv 13 | import PIL 14 | 15 | class Model: 16 | 17 | def __init__(self): 18 | self.model = LinearSVC() 19 | 20 | def train_model(self, counters): 21 | img_list = np.array([]) 22 | class_list = np.array([]) 23 | 24 | for i in range(1, counters[0]): 25 | img = cv.imread(f'1/frame{i}.jpg')[:, :, 0] 26 | img = img.reshape(16800) 27 | img_list = np.append(img_list, [img]) 28 | class_list = np.append(class_list, 1) 29 | 30 | for i in range(1, counters[1]): 31 | img = cv.imread(f'2/frame{i}.jpg')[:, :, 0] 32 | img = img.reshape(16800) 33 | img_list = np.append(img_list, [img]) 34 | class_list = np.append(class_list, 2) 35 | 36 | img_list = img_list.reshape(counters[0] - 1 + counters[1] - 1, 16800) 37 | self.model.fit(img_list, class_list) 38 | print("Model successfully trained!") 39 | 40 | def predict(self, frame): 41 | frame = frame[1] 42 | cv.imwrite("frame.jpg", cv.cvtColor(frame, cv.COLOR_RGB2GRAY)) 43 | img = PIL.Image.open("frame.jpg") 44 | img.thumbnail((150, 150), PIL.Image.ANTIALIAS) 45 | img.save("frame.jpg") 46 | 47 | img = cv.imread('frame.jpg')[:, :, 0] 48 | img = img.reshape(16800) 49 | prediction = self.model.predict([img]) 50 | 51 | return prediction[0] --------------------------------------------------------------------------------