diff --git a/openbr/core/opencvutils.cpp b/openbr/core/opencvutils.cpp index 25214b5..ccee66a 100644 --- a/openbr/core/opencvutils.cpp +++ b/openbr/core/opencvutils.cpp @@ -23,6 +23,8 @@ #include "opencvutils.h" #include "qtutils.h" +#include + using namespace cv; using namespace std; @@ -261,6 +263,39 @@ QStringList OpenCVUtils::matrixToStringList(const Mat &m) return results; } +void OpenCVUtils::storeModel(const CvStatModel &model, QDataStream &stream) +{ + // Create local file + QTemporaryFile tempFile; + tempFile.open(); + tempFile.close(); + + // Save MLP to local file + model.save(qPrintable(tempFile.fileName())); + + // Copy local file contents to stream + tempFile.open(); + QByteArray data = tempFile.readAll(); + tempFile.close(); + stream << data; +} + +void OpenCVUtils::loadModel(CvStatModel &model, QDataStream &stream) +{ + // Copy local file contents from stream + QByteArray data; + stream >> data; + + // Create local file + QTemporaryFile tempFile(QDir::tempPath()+"/model"); + tempFile.open(); + tempFile.write(data); + tempFile.close(); + + // Load MLP from local file + model.load(qPrintable(tempFile.fileName())); +} + Point2f OpenCVUtils::toPoint(const QPointF &qPoint) { return Point2f(qPoint.x(), qPoint.y()); diff --git a/openbr/core/opencvutils.h b/openbr/core/opencvutils.h index 8632d39..516fb1b 100644 --- a/openbr/core/opencvutils.h +++ b/openbr/core/opencvutils.h @@ -22,6 +22,7 @@ #include #include #include +#include #include namespace OpenCVUtils @@ -49,6 +50,10 @@ namespace OpenCVUtils QString matrixToString(const cv::Mat &m); QStringList matrixToStringList(const cv::Mat &m); + // Model storage + void storeModel(const CvStatModel &model, QDataStream &stream); + void loadModel(CvStatModel &model, QDataStream &stream); + template T getElement(const cv::Mat &m, int r, int c) { diff --git a/openbr/plugins/svm.cpp b/openbr/plugins/svm.cpp index b80901e..8acd3a7 100644 --- a/openbr/plugins/svm.cpp +++ b/openbr/plugins/svm.cpp @@ -26,39 +26,6 @@ using namespace cv; namespace br { -static void storeSVM(const SVM &svm, QDataStream &stream) -{ - // Create local file - QTemporaryFile tempFile; - tempFile.open(); - tempFile.close(); - - // Save SVM to local file - svm.save(qPrintable(tempFile.fileName())); - - // Copy local file contents to stream - tempFile.open(); - QByteArray data = tempFile.readAll(); - tempFile.close(); - stream << data; -} - -static void loadSVM(SVM &svm, QDataStream &stream) -{ - // Copy local file contents from stream - QByteArray data; - stream >> data; - - // Create local file - QTemporaryFile tempFile(QDir::tempPath()+"/SVM"); - tempFile.open(); - tempFile.write(data); - tempFile.close(); - - // Load SVM from local file - svm.load(qPrintable(tempFile.fileName())); -} - static void trainSVM(SVM &svm, Mat data, Mat lab, int kernel, int type, float C, float gamma, int folds, bool balanceFolds, int termCriteria) { if (data.type() != CV_32FC1) @@ -190,13 +157,13 @@ private: void store(QDataStream &stream) const { - storeSVM(svm, stream); + OpenCVUtils::storeModel(svm, stream); stream << labelMap << reverseLookup; } void load(QDataStream &stream) { - loadSVM(svm, stream); + OpenCVUtils::loadModel(svm, stream); stream >> labelMap >> reverseLookup; } @@ -281,12 +248,12 @@ private: void store(QDataStream &stream) const { - storeSVM(svm, stream); + OpenCVUtils::storeModel(svm, stream); } void load(QDataStream &stream) { - loadSVM(svm, stream); + OpenCVUtils::loadModel(svm, stream); } }; diff --git a/openbr/plugins/tree.cpp b/openbr/plugins/tree.cpp index 9cba347..d4b1996 100644 --- a/openbr/plugins/tree.cpp +++ b/openbr/plugins/tree.cpp @@ -1,9 +1,5 @@ -#include - #include "openbr_internal.h" #include "openbr/core/opencvutils.h" -#include -#include using namespace std; using namespace cv; @@ -11,39 +7,6 @@ using namespace cv; namespace br { -static void storeModel(const CvStatModel &model, QDataStream &stream) -{ - // Create local file - QTemporaryFile tempFile; - tempFile.open(); - tempFile.close(); - - // Save MLP to local file - model.save(qPrintable(tempFile.fileName())); - - // Copy local file contents to stream - tempFile.open(); - QByteArray data = tempFile.readAll(); - tempFile.close(); - stream << data; -} - -static void loadModel(CvStatModel &model, QDataStream &stream) -{ - // Copy local file contents from stream - QByteArray data; - stream >> data; - - // Create local file - QTemporaryFile tempFile(QDir::tempPath()+"/model"); - tempFile.open(); - tempFile.write(data); - tempFile.close(); - - // Load MLP from local file - model.load(qPrintable(tempFile.fileName())); -} - /*! * \ingroup transforms * \brief Wraps OpenCV's random trees framework @@ -127,12 +90,12 @@ class ForestTransform : public Transform void load(QDataStream &stream) { - loadModel(forest,stream); + OpenCVUtils::loadModel(forest,stream); } void store(QDataStream &stream) const { - storeModel(forest,stream); + OpenCVUtils::storeModel(forest,stream); } void init() @@ -233,12 +196,12 @@ private: void load(QDataStream &stream) { - loadModel(boost,stream); + OpenCVUtils::loadModel(boost,stream); } void store(QDataStream &stream) const { - storeModel(boost,stream); + OpenCVUtils::storeModel(boost,stream); }