Skip to content

Commit 5752522

Browse files
Add files via upload
Added all the files.
1 parent 77cbb0e commit 5752522

File tree

9 files changed

+245
-0
lines changed

9 files changed

+245
-0
lines changed

__pycache__/app.cpython-39.pyc

3.9 KB
Binary file not shown.

__pycache__/camera.cpython-39.pyc

1.01 KB
Binary file not shown.

__pycache__/model.cpython-39.pyc

1.51 KB
Binary file not shown.

app.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+

brain.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from sklearn.svm import LinearSVC
2+
import numpy as np
3+
import cv2 as cv
4+
import PIL
5+
6+
class Model:
7+
8+
def __init__(self):
9+
self.model = LinearSVC()
10+
11+
def trainModel(self):
12+
img_list = np.array([])
13+
class_list = np.array([])
14+
15+
for i in range(1, counters[0]):
16+
img = cv.imread(f'class1/frame{i}.jpg')[:,:,0]
17+
img = img.reshape(16800) # product of your resolutions
18+
img_list = np.append(img_list, [img])
19+
class_list = np.append(class_list, 1)
20+
21+
for i in range(1, counters[0]):
22+
img = cv.imread(f'class2/frame{i}.jpg')[:,:,0]
23+
img = img.reshape(16800) # product of your resolutions
24+
img_list = np.append(img_list, [img])
25+
class_list = np.append(class_list, 2)
26+
27+
img_list = img_list.reshape(counters[0] - 1 + counters[1] - 1, 16800)
28+
self.model.fit(img_list, class_list)
29+
print("Model Successfully Trained")
30+
31+
def predict(self, frame):
32+
frame = frame[1]
33+
cv.imwrite('frame.jpg', cv.cvtColor(frame, cv.COLOR_RGB2GRAY))
34+
img = PIL.Image.open('frame.jpg')
35+
img.thumbnail((150, 150), PIL.Image.ANTIALIAS)
36+
img.save('frame.jpg')
37+
38+
img = cv.imread(frame.jpg)[:,:,0]
39+
img = img.reshape(16800)
40+
prediction = self.model.predict([img])
41+
42+
return prediction[0]

camera.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import cv2 as cv
2+
3+
class Camera:
4+
5+
def __init__(self):
6+
self.camera = cv.VideoCapture(0)
7+
if not self.camera.isOpened():
8+
raise ValueError("Unable to open the camera!")
9+
10+
self.width = self.camera.get(cv.CAP_PROP_FRAME_WIDTH)
11+
self.height = self.camera.get(cv.CAP_PROP_FRAME_HEIGHT)
12+
13+
14+
def __del__(self):
15+
if self.camera.isOpened():
16+
self.camera.release()
17+
18+
19+
def getFrame(self):
20+
if self.camera.isOpened():
21+
ret, frame = self.camera.read()
22+
23+
if ret:
24+
return (ret, cv.cvtColor(frame, cv.COLOR_BGR2RGB))
25+
else:
26+
return (ret, None)
27+
else:
28+
return None
29+

frame.jpg

3.05 KB
Loading

main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import app
2+
import sys
3+
4+
print(sys.getrecursionlimit())
5+
6+
def main():
7+
app.App(window_title = "Camera Classifier")
8+
9+
if __name__ == '__main__':
10+
main()

model.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from sklearn.svm import LinearSVC
2+
import numpy as np
3+
import cv2 as cv
4+
import PIL
5+
# from tensorflow.keras import layers, models
6+
7+
class Model:
8+
9+
def __init__(self):
10+
self.model = LinearSVC()
11+
12+
def trainModel(self, counters):
13+
img_list = np.array([])
14+
class_list = np.array([])
15+
16+
for i in range(1, counters[0]):
17+
img = cv.imread(f'class1/frame{i}.jpg')[:,:,0]
18+
img = img.reshape(150, 113) # product of your resolutions
19+
img_list = np.append(img_list, [img])
20+
class_list = np.append(class_list, 1)
21+
22+
for j in range(1, counters[0]):
23+
img = cv.imread(f'class2/frame{j}.jpg')[:,:,0]
24+
img = img.reshape(150, 113) # product of your resolutions
25+
img_list = np.append(img_list, [img])
26+
class_list = np.append(class_list, 2)
27+
28+
img_list = img_list.reshape(counters[0] - 1 + counters[1] - 1, 16950)
29+
self.model.fit(img_list, class_list)
30+
print("Model Successfully Trained")
31+
32+
def predict(self, frame):
33+
frame = frame[1]
34+
cv.imwrite('frame.jpg', cv.cvtColor(frame, cv.COLOR_RGB2GRAY))
35+
img = PIL.Image.open('frame.jpg')
36+
img.thumbnail((150, 150), PIL.Image.ANTIALIAS)
37+
img.save('frame.jpg')
38+
39+
img = cv.imread('frame.jpg')[:,:,0]
40+
img = img.reshape(16950)
41+
prediction = self.model.predict([img])
42+
43+
return prediction[0]

0 commit comments

Comments
 (0)