-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
47 lines (34 loc) · 1.24 KB
/
Copy pathmodel.py
File metadata and controls
47 lines (34 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from sklearn.svm import LinearSVC
import numpy as np
import cv2 as cv
import PIL
class Model:
def __init__(self):
self.model = LinearSVC()
def train_model(self, counters):
img_list = []
class_list = []
for i in range(1, counters[0]):
img = cv.imread(f'1/frame{i}.jpg')[:, :, 0]
img = cv.resize(img, (140, 120)).reshape(16800)
img_list.append(img)
class_list.append(1)
for i in range(1, counters[1]):
img = cv.imread(f'2/frame{i}.jpg')[:, :, 0]
img = img = cv.resize(img, (140, 120)).reshape(16800)
img_list.append(img)
class_list.append(2)
img_list = np.array(img_list)
class_list = np.array(class_list)
self.model.fit(img_list, class_list)
print("Model successfully trained!")
def predict(self, frame):
# convert frame to grayscale
gray = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
# resize frame to match training data
resized = cv.resize(gray, (120, 140))
# flatten frame to 1-dimensional array
flattened = resized.flatten()
# make prediction
prediction = self.model.predict([flattened])
return prediction[0]