Commit a3cca74e3f32fc9a9f48cdecc0faa8cc039ac2f0
1 parent
919ddb61
Refactored opencv model storage
Showing
4 changed files
with
48 additions
and
78 deletions
openbr/core/opencvutils.cpp
| @@ -23,6 +23,8 @@ | @@ -23,6 +23,8 @@ | ||
| 23 | #include "opencvutils.h" | 23 | #include "opencvutils.h" |
| 24 | #include "qtutils.h" | 24 | #include "qtutils.h" |
| 25 | 25 | ||
| 26 | +#include <QTemporaryFile> | ||
| 27 | + | ||
| 26 | using namespace cv; | 28 | using namespace cv; |
| 27 | using namespace std; | 29 | using namespace std; |
| 28 | 30 | ||
| @@ -261,6 +263,39 @@ QStringList OpenCVUtils::matrixToStringList(const Mat &m) | @@ -261,6 +263,39 @@ QStringList OpenCVUtils::matrixToStringList(const Mat &m) | ||
| 261 | return results; | 263 | return results; |
| 262 | } | 264 | } |
| 263 | 265 | ||
| 266 | +void OpenCVUtils::storeModel(const CvStatModel &model, QDataStream &stream) | ||
| 267 | +{ | ||
| 268 | + // Create local file | ||
| 269 | + QTemporaryFile tempFile; | ||
| 270 | + tempFile.open(); | ||
| 271 | + tempFile.close(); | ||
| 272 | + | ||
| 273 | + // Save MLP to local file | ||
| 274 | + model.save(qPrintable(tempFile.fileName())); | ||
| 275 | + | ||
| 276 | + // Copy local file contents to stream | ||
| 277 | + tempFile.open(); | ||
| 278 | + QByteArray data = tempFile.readAll(); | ||
| 279 | + tempFile.close(); | ||
| 280 | + stream << data; | ||
| 281 | +} | ||
| 282 | + | ||
| 283 | +void OpenCVUtils::loadModel(CvStatModel &model, QDataStream &stream) | ||
| 284 | +{ | ||
| 285 | + // Copy local file contents from stream | ||
| 286 | + QByteArray data; | ||
| 287 | + stream >> data; | ||
| 288 | + | ||
| 289 | + // Create local file | ||
| 290 | + QTemporaryFile tempFile(QDir::tempPath()+"/model"); | ||
| 291 | + tempFile.open(); | ||
| 292 | + tempFile.write(data); | ||
| 293 | + tempFile.close(); | ||
| 294 | + | ||
| 295 | + // Load MLP from local file | ||
| 296 | + model.load(qPrintable(tempFile.fileName())); | ||
| 297 | +} | ||
| 298 | + | ||
| 264 | Point2f OpenCVUtils::toPoint(const QPointF &qPoint) | 299 | Point2f OpenCVUtils::toPoint(const QPointF &qPoint) |
| 265 | { | 300 | { |
| 266 | return Point2f(qPoint.x(), qPoint.y()); | 301 | return Point2f(qPoint.x(), qPoint.y()); |
openbr/core/opencvutils.h
| @@ -22,6 +22,7 @@ | @@ -22,6 +22,7 @@ | ||
| 22 | #include <QString> | 22 | #include <QString> |
| 23 | #include <QStringList> | 23 | #include <QStringList> |
| 24 | #include <opencv2/core/core.hpp> | 24 | #include <opencv2/core/core.hpp> |
| 25 | +#include <opencv2/ml/ml.hpp> | ||
| 25 | #include <assert.h> | 26 | #include <assert.h> |
| 26 | 27 | ||
| 27 | namespace OpenCVUtils | 28 | namespace OpenCVUtils |
| @@ -49,6 +50,10 @@ namespace OpenCVUtils | @@ -49,6 +50,10 @@ namespace OpenCVUtils | ||
| 49 | QString matrixToString(const cv::Mat &m); | 50 | QString matrixToString(const cv::Mat &m); |
| 50 | QStringList matrixToStringList(const cv::Mat &m); | 51 | QStringList matrixToStringList(const cv::Mat &m); |
| 51 | 52 | ||
| 53 | + // Model storage | ||
| 54 | + void storeModel(const CvStatModel &model, QDataStream &stream); | ||
| 55 | + void loadModel(CvStatModel &model, QDataStream &stream); | ||
| 56 | + | ||
| 52 | template <typename T> | 57 | template <typename T> |
| 53 | T getElement(const cv::Mat &m, int r, int c) | 58 | T getElement(const cv::Mat &m, int r, int c) |
| 54 | { | 59 | { |
openbr/plugins/svm.cpp
| @@ -26,39 +26,6 @@ using namespace cv; | @@ -26,39 +26,6 @@ using namespace cv; | ||
| 26 | namespace br | 26 | namespace br |
| 27 | { | 27 | { |
| 28 | 28 | ||
| 29 | -static void storeSVM(const SVM &svm, QDataStream &stream) | ||
| 30 | -{ | ||
| 31 | - // Create local file | ||
| 32 | - QTemporaryFile tempFile; | ||
| 33 | - tempFile.open(); | ||
| 34 | - tempFile.close(); | ||
| 35 | - | ||
| 36 | - // Save SVM to local file | ||
| 37 | - svm.save(qPrintable(tempFile.fileName())); | ||
| 38 | - | ||
| 39 | - // Copy local file contents to stream | ||
| 40 | - tempFile.open(); | ||
| 41 | - QByteArray data = tempFile.readAll(); | ||
| 42 | - tempFile.close(); | ||
| 43 | - stream << data; | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -static void loadSVM(SVM &svm, QDataStream &stream) | ||
| 47 | -{ | ||
| 48 | - // Copy local file contents from stream | ||
| 49 | - QByteArray data; | ||
| 50 | - stream >> data; | ||
| 51 | - | ||
| 52 | - // Create local file | ||
| 53 | - QTemporaryFile tempFile(QDir::tempPath()+"/SVM"); | ||
| 54 | - tempFile.open(); | ||
| 55 | - tempFile.write(data); | ||
| 56 | - tempFile.close(); | ||
| 57 | - | ||
| 58 | - // Load SVM from local file | ||
| 59 | - svm.load(qPrintable(tempFile.fileName())); | ||
| 60 | -} | ||
| 61 | - | ||
| 62 | static void trainSVM(SVM &svm, Mat data, Mat lab, int kernel, int type, float C, float gamma, int folds, bool balanceFolds, int termCriteria) | 29 | static void trainSVM(SVM &svm, Mat data, Mat lab, int kernel, int type, float C, float gamma, int folds, bool balanceFolds, int termCriteria) |
| 63 | { | 30 | { |
| 64 | if (data.type() != CV_32FC1) | 31 | if (data.type() != CV_32FC1) |
| @@ -190,13 +157,13 @@ private: | @@ -190,13 +157,13 @@ private: | ||
| 190 | 157 | ||
| 191 | void store(QDataStream &stream) const | 158 | void store(QDataStream &stream) const |
| 192 | { | 159 | { |
| 193 | - storeSVM(svm, stream); | 160 | + OpenCVUtils::storeModel(svm, stream); |
| 194 | stream << labelMap << reverseLookup; | 161 | stream << labelMap << reverseLookup; |
| 195 | } | 162 | } |
| 196 | 163 | ||
| 197 | void load(QDataStream &stream) | 164 | void load(QDataStream &stream) |
| 198 | { | 165 | { |
| 199 | - loadSVM(svm, stream); | 166 | + OpenCVUtils::loadModel(svm, stream); |
| 200 | stream >> labelMap >> reverseLookup; | 167 | stream >> labelMap >> reverseLookup; |
| 201 | } | 168 | } |
| 202 | 169 | ||
| @@ -281,12 +248,12 @@ private: | @@ -281,12 +248,12 @@ private: | ||
| 281 | 248 | ||
| 282 | void store(QDataStream &stream) const | 249 | void store(QDataStream &stream) const |
| 283 | { | 250 | { |
| 284 | - storeSVM(svm, stream); | 251 | + OpenCVUtils::storeModel(svm, stream); |
| 285 | } | 252 | } |
| 286 | 253 | ||
| 287 | void load(QDataStream &stream) | 254 | void load(QDataStream &stream) |
| 288 | { | 255 | { |
| 289 | - loadSVM(svm, stream); | 256 | + OpenCVUtils::loadModel(svm, stream); |
| 290 | } | 257 | } |
| 291 | }; | 258 | }; |
| 292 | 259 |
openbr/plugins/tree.cpp
| 1 | -#include <opencv2/ml/ml.hpp> | ||
| 2 | - | ||
| 3 | #include "openbr_internal.h" | 1 | #include "openbr_internal.h" |
| 4 | #include "openbr/core/opencvutils.h" | 2 | #include "openbr/core/opencvutils.h" |
| 5 | -#include <QString> | ||
| 6 | -#include <QTemporaryFile> | ||
| 7 | 3 | ||
| 8 | using namespace std; | 4 | using namespace std; |
| 9 | using namespace cv; | 5 | using namespace cv; |
| @@ -11,39 +7,6 @@ using namespace cv; | @@ -11,39 +7,6 @@ using namespace cv; | ||
| 11 | namespace br | 7 | namespace br |
| 12 | { | 8 | { |
| 13 | 9 | ||
| 14 | -static void storeModel(const CvStatModel &model, QDataStream &stream) | ||
| 15 | -{ | ||
| 16 | - // Create local file | ||
| 17 | - QTemporaryFile tempFile; | ||
| 18 | - tempFile.open(); | ||
| 19 | - tempFile.close(); | ||
| 20 | - | ||
| 21 | - // Save MLP to local file | ||
| 22 | - model.save(qPrintable(tempFile.fileName())); | ||
| 23 | - | ||
| 24 | - // Copy local file contents to stream | ||
| 25 | - tempFile.open(); | ||
| 26 | - QByteArray data = tempFile.readAll(); | ||
| 27 | - tempFile.close(); | ||
| 28 | - stream << data; | ||
| 29 | -} | ||
| 30 | - | ||
| 31 | -static void loadModel(CvStatModel &model, QDataStream &stream) | ||
| 32 | -{ | ||
| 33 | - // Copy local file contents from stream | ||
| 34 | - QByteArray data; | ||
| 35 | - stream >> data; | ||
| 36 | - | ||
| 37 | - // Create local file | ||
| 38 | - QTemporaryFile tempFile(QDir::tempPath()+"/model"); | ||
| 39 | - tempFile.open(); | ||
| 40 | - tempFile.write(data); | ||
| 41 | - tempFile.close(); | ||
| 42 | - | ||
| 43 | - // Load MLP from local file | ||
| 44 | - model.load(qPrintable(tempFile.fileName())); | ||
| 45 | -} | ||
| 46 | - | ||
| 47 | /*! | 10 | /*! |
| 48 | * \ingroup transforms | 11 | * \ingroup transforms |
| 49 | * \brief Wraps OpenCV's random trees framework | 12 | * \brief Wraps OpenCV's random trees framework |
| @@ -127,12 +90,12 @@ class ForestTransform : public Transform | @@ -127,12 +90,12 @@ class ForestTransform : public Transform | ||
| 127 | 90 | ||
| 128 | void load(QDataStream &stream) | 91 | void load(QDataStream &stream) |
| 129 | { | 92 | { |
| 130 | - loadModel(forest,stream); | 93 | + OpenCVUtils::loadModel(forest,stream); |
| 131 | } | 94 | } |
| 132 | 95 | ||
| 133 | void store(QDataStream &stream) const | 96 | void store(QDataStream &stream) const |
| 134 | { | 97 | { |
| 135 | - storeModel(forest,stream); | 98 | + OpenCVUtils::storeModel(forest,stream); |
| 136 | } | 99 | } |
| 137 | 100 | ||
| 138 | void init() | 101 | void init() |
| @@ -233,12 +196,12 @@ private: | @@ -233,12 +196,12 @@ private: | ||
| 233 | 196 | ||
| 234 | void load(QDataStream &stream) | 197 | void load(QDataStream &stream) |
| 235 | { | 198 | { |
| 236 | - loadModel(boost,stream); | 199 | + OpenCVUtils::loadModel(boost,stream); |
| 237 | } | 200 | } |
| 238 | 201 | ||
| 239 | void store(QDataStream &stream) const | 202 | void store(QDataStream &stream) const |
| 240 | { | 203 | { |
| 241 | - storeModel(boost,stream); | 204 | + OpenCVUtils::storeModel(boost,stream); |
| 242 | } | 205 | } |
| 243 | 206 | ||
| 244 | 207 |