From a4a2659dff7066abeca4e702ffb65c7ab1777977 Mon Sep 17 00:00:00 2001 From: Joe Minichino Date: Wed, 27 May 2015 15:43:52 +0100 Subject: [PATCH] fixed digits.py sample to work with opencv 3 --- samples/python2/digits.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/samples/python2/digits.py b/samples/python2/digits.py index 63dbb1e9d..18cdd4ce7 100755 --- a/samples/python2/digits.py +++ b/samples/python2/digits.py @@ -74,30 +74,35 @@ class StatModel(object): class KNearest(StatModel): def __init__(self, k = 3): self.k = k - self.model = cv2.KNearest() + self.model = cv2.ml.KNearest_create() def train(self, samples, responses): - self.model = cv2.KNearest() - self.model.train(samples, responses) + self.model = cv2.ml.KNearest_create() + self.model.train(samples, cv2.ml.ROW_SAMPLE, responses) def predict(self, samples): - retval, results, neigh_resp, dists = self.model.find_nearest(samples, self.k) + retval, results, neigh_resp, dists = self.model.findNearest(samples, self.k) return results.ravel() class SVM(StatModel): def __init__(self, C = 1, gamma = 0.5): - self.params = dict( kernel_type = cv2.SVM_RBF, - svm_type = cv2.SVM_C_SVC, + self.params = dict( kernel_type = cv2.ml.SVM_RBF, + svm_type = cv2.ml.SVM_C_SVC, C = C, gamma = gamma ) - self.model = cv2.SVM() + self.model = cv2.ml.SVM_create() def train(self, samples, responses): - self.model = cv2.SVM() - self.model.train(samples, responses, params = self.params) + self.model = cv2.ml.SVM_create() + """ original code """ + #self.model.train(samples, responses, params = self.params) + """ but it's either this """ + self.model.train(samples, cv2.ml.ROW_SAMPLE, responses) + """ or this """ + #self.model.train(samples, params = self.params) def predict(self, samples): - return self.model.predict_all(samples).ravel() + return self.model.predict(samples)[1][0].ravel() def evaluate_model(model, digits, samples, labels):