-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassifier.py
39 lines (29 loc) · 1.25 KB
/
Classifier.py
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
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from skimage.feature import hog
import cv2
from sklearn.preprocessing import StandardScaler
class Classifier:
def __init__(self):
self.svc = LinearSVC()
def classify(self, img, features):
prediction = self.svc.predict(features)
return prediction
def train(self, car_features, non_car_features):
X = np.vstack((car_features,
not_car_features)).astype(np.float64)
# Fit a per-column scaler
X_scaler = StandardScaler().fit(X)
# Apply the scaler to X
scaled_X = X_scaler.transform(X)
# Define the labels vector
y = np.hstack((np.ones(len(car_features)),
np.zeros(len(not_car_features))))
# Split up data into randomized training and test sets
rand_state = np.random.randint(0, 100)
X_train, X_test, y_train, y_test = train_test_split(
scaled_X, y, test_size=0.2, random_state=rand_state)
# Check the training time for the SVC
self.svc.fit(X_train, y_train)
print('Test Accuracy of SVC = ', round(svc.score(X_test, y_test), 4))