forked from leenachennuru/objRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
51 lines (40 loc) · 1.65 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
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import cv2
from matplotlib import pyplot as plt
from scipy import misc as msc
import scipy.spatial.distance as computeMod
def kmeansCodeBook(features,K):
# Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1)
# Set flags (Just to avoid line break in the code)
flags = cv2.KMEANS_RANDOM_CENTERS
compactness, labels, centers = cv2.kmeans(features, K, criteria, 10, flags)
return compactness, labels, centers
def minDistance(descriptorSet, clusterCenters):
distMatrix = computeMod.cdist(descriptorSet,clusterCenters, 'euclidean')
minPoints= np.argmin(distMatrix, axis =1)
histPoints = np.histogram(minPoints, bins=np.arange(np.size(clusterCenters,0)+1), density=False)
return histPoints
class StatModel(object):
'''parent class - starting point to add abstraction'''
def load(self, fn):
self.model.load(fn)
def save(self, fn):
self.model.save(fn)
class SVM(StatModel):
'''wrapper for OpenCV SimpleVectorMachine algorithm'''
def __init__(self):
self.model = cv2.SVM()
def train(self, samples, responses):
#setting algorithm parameters
params = dict( kernel_type = cv2.SVM_RBF,
svm_type = cv2.SVM_C_SVC, C=2.67, gamma=5.383,
)
self.model.train(samples, responses, params = params)
def predict(self, samples):
return np.float32( [self.model.predict(s) for s in samples])
'''Uncomment when giving it training data. Remaining is self explanatory
clf = SVM()
clf.train(samples, y_train)
y_val = clf.predict(samples)
'''