From a7aa198b4caa07895b41593fdd9adf480a40c5c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Mon, 4 Jan 2016 15:47:26 +0100 Subject: [PATCH 1/4] fixing issue #4969 of Itseez/opencv. Someone forgot to wrap the load function for SVMs in the corresponding ml python module. Fixed that. --- modules/ml/include/opencv2/ml.hpp | 4 ++++ modules/ml/src/svm.cpp | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index 862f3f950c..013d9187fe 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -719,6 +719,10 @@ public: Use StatModel::train to train the model. Since %SVM has several parameters, you may want to find the best parameters for your problem, it can be done with SVM::trainAuto. */ CV_WRAP static Ptr create(); + + CV_WRAP virtual void read( const FileNode& fn ) = 0; + + CV_WRAP static Ptr load(const String& fs); }; /****************************************************************************************\ diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 757bb7a171..639f6e2168 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -2261,6 +2261,17 @@ Ptr SVM::create() return makePtr(); } +Ptr SVM::load(const String& filename) +{ + FileStorage fs; + fs.open(filename, FileStorage::READ); + + Ptr svm = makePtr(); + + svm->read(fs.getFirstTopLevelNode()); + return svm; +} + Mat SVM::getUncompressedSupportVectors() const { const SVMImpl* this_ = dynamic_cast(this); From 6c8bc6a25b1fb433a198520455a5a5c27a03cc5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Thu, 7 Jan 2016 08:00:01 +0100 Subject: [PATCH 2/4] fixed ABI incompatibilities as proposed by alalek related to issue 4969 fixes issue 5891 fixes issue 5922 --- modules/ml/include/opencv2/ml.hpp | 11 ++++++++--- modules/ml/src/svm.cpp | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index 013d9187fe..cfd95a5240 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -720,9 +720,14 @@ public: find the best parameters for your problem, it can be done with SVM::trainAuto. */ CV_WRAP static Ptr create(); - CV_WRAP virtual void read( const FileNode& fn ) = 0; - - CV_WRAP static Ptr load(const String& fs); + /** @brief Loads and creates a serialized svm from a file + * + * Use SVM::save to serialize and store an SVM to disk. + * Load the SVM from this file again, by calling this function with the path to the file. + * + * @param fs Filename + */ + CV_WRAP static Ptr load(const String& filepath); }; /****************************************************************************************\ diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp index 639f6e2168..34acebb991 100644 --- a/modules/ml/src/svm.cpp +++ b/modules/ml/src/svm.cpp @@ -2261,14 +2261,14 @@ Ptr SVM::create() return makePtr(); } -Ptr SVM::load(const String& filename) +Ptr SVM::load(const String& filepath) { FileStorage fs; - fs.open(filename, FileStorage::READ); + fs.open(filepath, FileStorage::READ); Ptr svm = makePtr(); - svm->read(fs.getFirstTopLevelNode()); + ((SVMImpl*)svm.get())->read(fs.getFirstTopLevelNode()); return svm; } From 66eda72f668e9ab0740a0bd4432af4f4566915c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Thu, 7 Jan 2016 08:19:06 +0100 Subject: [PATCH 3/4] fixed a doxygen issue --- modules/ml/include/opencv2/ml.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index cfd95a5240..4f99bf4b36 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -725,7 +725,7 @@ public: * Use SVM::save to serialize and store an SVM to disk. * Load the SVM from this file again, by calling this function with the path to the file. * - * @param fs Filename + * @param filepath path to serialized svm */ CV_WRAP static Ptr load(const String& filepath); }; From 3f172731b20e4e646b89985154b34a0532cd7b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sp=C3=B6rk?= Date: Mon, 11 Jan 2016 10:59:15 +0100 Subject: [PATCH 4/4] added wrapped load function for python as suggested by gat3way --- modules/ml/include/opencv2/ml.hpp | 10 ++++++++++ modules/ml/src/ann_mlp.cpp | 14 +++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/ml/include/opencv2/ml.hpp b/modules/ml/include/opencv2/ml.hpp index 4f99bf4b36..7acce7f33c 100644 --- a/modules/ml/include/opencv2/ml.hpp +++ b/modules/ml/include/opencv2/ml.hpp @@ -1398,6 +1398,16 @@ public: Note that the train method has optional flags: ANN_MLP::TrainFlags. */ CV_WRAP static Ptr create(); + + /** @brief Loads and creates a serialized ANN from a file + * + * Use ANN::save to serialize and store an ANN to disk. + * Load the ANN from this file again, by calling this function with the path to the file. + * + * @param filepath path to serialized ANN + */ + CV_WRAP static Ptr load(const String& filepath); + }; /****************************************************************************************\ diff --git a/modules/ml/src/ann_mlp.cpp b/modules/ml/src/ann_mlp.cpp index ff6512dedd..19ee913320 100644 --- a/modules/ml/src/ann_mlp.cpp +++ b/modules/ml/src/ann_mlp.cpp @@ -1317,6 +1317,18 @@ Ptr ANN_MLP::create() return makePtr(); } -}} +Ptr ANN_MLP::load(const String& filepath) +{ + FileStorage fs; + fs.open(filepath, FileStorage::READ); + + Ptr ann = makePtr(); + + ((ANN_MLPImpl*)ann.get())->read(fs.getFirstTopLevelNode()); + return ann; +} + + + }} /* End of file. */