comparison python/ml.py @ 978:184f1dd307f9

corrected print and exception statements for Python 3
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 08 Feb 2018 05:53:50 -0500
parents ec1682ed999f
children 23f98ebb113f
comparison
equal deleted inserted replaced
977:9c27a0315c4d 978:184f1dd307f9
9 from matplotlib.pylab import text 9 from matplotlib.pylab import text
10 import matplotlib as mpl 10 import matplotlib as mpl
11 import matplotlib.pyplot as plt 11 import matplotlib.pyplot as plt
12 from scipy.cluster.vq import kmeans, whiten, vq 12 from scipy.cluster.vq import kmeans, whiten, vq
13 from sklearn import mixture 13 from sklearn import mixture
14 import cv2 14 try:
15 import cv2
16 opencvAvailable = True
17 except ImportError:
18 print('OpenCV library could not be loaded (video replay functions will not be available)') # TODO change to logging module
19 opencvAvailable = False
15 20
16 import utils 21 import utils
17 22
18 ##################### 23 #####################
19 # OpenCV ML models 24 # OpenCV ML models
36 print('Provided filename {} does not exist: model not loaded!'.format(filename)) 41 print('Provided filename {} does not exist: model not loaded!'.format(filename))
37 42
38 def save(self, filename): 43 def save(self, filename):
39 self.model.save(filename) 44 self.model.save(filename)
40 45
41 class SVM(StatModel): 46 if opencvAvailable:
42 '''wrapper for OpenCV SimpleVectorMachine algorithm''' 47 class SVM(StatModel):
43 def __init__(self, svmType = cv2.SVM_C_SVC, kernelType = cv2.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0): 48 '''wrapper for OpenCV SimpleVectorMachine algorithm'''
44 self.model = cv2.SVM() 49 def __init__(self, svmType = cv2.SVM_C_SVC, kernelType = cv2.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0):
45 self.params = dict(svm_type = svmType, kernel_type = kernelType, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p) 50 self.model = cv2.SVM()
46 # OpenCV3 51 self.params = dict(svm_type = svmType, kernel_type = kernelType, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p)
47 # self.model = cv2.SVM() 52 # OpenCV3
48 # self.model.setType(svmType) 53 # self.model = cv2.SVM()
49 # self.model.setKernel(kernelType) 54 # self.model.setType(svmType)
50 # self.model.setDegree(degree) 55 # self.model.setKernel(kernelType)
51 # self.model.setGamma(gamma) 56 # self.model.setDegree(degree)
52 # self.model.setCoef0(coef0) 57 # self.model.setGamma(gamma)
53 # self.model.setC(Cvalue) 58 # self.model.setCoef0(coef0)
54 # self.model.setNu(nu) 59 # self.model.setC(Cvalue)
55 # self.model.setP(p) 60 # self.model.setNu(nu)
56 61 # self.model.setP(p)
57 def train(self, samples, responses, computePerformance = False): 62
58 self.model.train(samples, responses, params = self.params) 63 def train(self, samples, responses, computePerformance = False):
59 if computePerformance: 64 self.model.train(samples, responses, params = self.params)
60 return computeConfusionMatrix(self, samples, responses) 65 if computePerformance:
61 66 return computeConfusionMatrix(self, samples, responses)
62 def predict(self, hog): 67
63 return self.model.predict(hog) 68 def predict(self, hog):
69 return self.model.predict(hog)
64 70
65 71
66 ##################### 72 #####################
67 # Clustering 73 # Clustering
68 ##################### 74 #####################