diff 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
line wrap: on
line diff
--- a/python/ml.py	Tue Feb 06 11:50:43 2018 -0500
+++ b/python/ml.py	Thu Feb 08 05:53:50 2018 -0500
@@ -11,7 +11,12 @@
 import matplotlib.pyplot as plt
 from scipy.cluster.vq import kmeans, whiten, vq
 from sklearn import mixture
-import cv2
+try:
+    import cv2
+    opencvAvailable = True
+except ImportError:
+    print('OpenCV library could not be loaded (video replay functions will not be available)') # TODO change to logging module
+    opencvAvailable = False
 
 import utils
 
@@ -38,29 +43,30 @@
     def save(self, filename):
         self.model.save(filename)
 
-class SVM(StatModel):
-    '''wrapper for OpenCV SimpleVectorMachine algorithm'''
-    def __init__(self, svmType = cv2.SVM_C_SVC, kernelType = cv2.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0):
-        self.model = cv2.SVM()
-        self.params = dict(svm_type = svmType, kernel_type = kernelType, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p)
-        # OpenCV3
-        # self.model = cv2.SVM()
-        # self.model.setType(svmType)
-        # self.model.setKernel(kernelType)
-        # self.model.setDegree(degree)
-        # self.model.setGamma(gamma)
-        # self.model.setCoef0(coef0)
-        # self.model.setC(Cvalue)
-        # self.model.setNu(nu)
-        # self.model.setP(p)
+if opencvAvailable:
+    class SVM(StatModel):
+        '''wrapper for OpenCV SimpleVectorMachine algorithm'''
+        def __init__(self, svmType = cv2.SVM_C_SVC, kernelType = cv2.SVM_RBF, degree = 0, gamma = 1, coef0 = 0, Cvalue = 1, nu = 0, p = 0):
+            self.model = cv2.SVM()
+            self.params = dict(svm_type = svmType, kernel_type = kernelType, degree = degree, gamma = gamma, coef0 = coef0, Cvalue = Cvalue, nu = nu, p = p)
+            # OpenCV3
+            # self.model = cv2.SVM()
+            # self.model.setType(svmType)
+            # self.model.setKernel(kernelType)
+            # self.model.setDegree(degree)
+            # self.model.setGamma(gamma)
+            # self.model.setCoef0(coef0)
+            # self.model.setC(Cvalue)
+            # self.model.setNu(nu)
+            # self.model.setP(p)
 
-    def train(self, samples, responses, computePerformance = False):
-        self.model.train(samples, responses, params = self.params)
-        if computePerformance:
-            return computeConfusionMatrix(self, samples, responses)
+        def train(self, samples, responses, computePerformance = False):
+            self.model.train(samples, responses, params = self.params)
+            if computePerformance:
+                return computeConfusionMatrix(self, samples, responses)
 
-    def predict(self, hog):
-        return self.model.predict(hog)
+        def predict(self, hog):
+            return self.model.predict(hog)
 
 
 #####################