Commit 6ff74e33127b29271f88c112f7ba263327c2b813

Authored by Brendan K
2 parents 153fd33c 64a915e9

Merge pull request #384 from biometrics/serializeAlgorithm

Serialize algorithm
openbr/core/opencvutils.cpp
@@ -280,6 +280,25 @@ void OpenCVUtils::storeModel(const CvStatModel &model, QDataStream &stream) @@ -280,6 +280,25 @@ void OpenCVUtils::storeModel(const CvStatModel &model, QDataStream &stream)
280 stream << data; 280 stream << data;
281 } 281 }
282 282
  283 +void OpenCVUtils::storeModel(const cv::Algorithm &model, QDataStream &stream)
  284 +{
  285 + // Create local file
  286 + QTemporaryFile tempFile;
  287 + tempFile.open();
  288 + tempFile.close();
  289 +
  290 + // Save MLP to local file
  291 + cv::FileStorage fs(tempFile.fileName().toStdString(), cv::FileStorage::WRITE);
  292 + model.write(fs);
  293 + fs.release();
  294 +
  295 + // Copy local file contents to stream
  296 + tempFile.open();
  297 + QByteArray data = tempFile.readAll();
  298 + tempFile.close();
  299 + stream << data;
  300 +}
  301 +
283 void OpenCVUtils::loadModel(CvStatModel &model, QDataStream &stream) 302 void OpenCVUtils::loadModel(CvStatModel &model, QDataStream &stream)
284 { 303 {
285 // Copy local file contents from stream 304 // Copy local file contents from stream
@@ -296,6 +315,23 @@ void OpenCVUtils::loadModel(CvStatModel &amp;model, QDataStream &amp;stream) @@ -296,6 +315,23 @@ void OpenCVUtils::loadModel(CvStatModel &amp;model, QDataStream &amp;stream)
296 model.load(qPrintable(tempFile.fileName())); 315 model.load(qPrintable(tempFile.fileName()));
297 } 316 }
298 317
  318 +void OpenCVUtils::loadModel(cv::Algorithm &model, QDataStream &stream)
  319 +{
  320 + // Copy local file contents from stream
  321 + QByteArray data;
  322 + stream >> data;
  323 +
  324 + // Create local file
  325 + QTemporaryFile tempFile(QDir::tempPath()+"/model");
  326 + tempFile.open();
  327 + tempFile.write(data);
  328 + tempFile.close();
  329 +
  330 + // Load MLP from local file
  331 + cv::FileStorage fs(tempFile.fileName().toStdString(), cv::FileStorage::READ);
  332 + model.read(fs[""]);
  333 +}
  334 +
299 Point2f OpenCVUtils::toPoint(const QPointF &qPoint) 335 Point2f OpenCVUtils::toPoint(const QPointF &qPoint)
300 { 336 {
301 return Point2f(qPoint.x(), qPoint.y()); 337 return Point2f(qPoint.x(), qPoint.y());
openbr/core/opencvutils.h
@@ -52,7 +52,9 @@ namespace OpenCVUtils @@ -52,7 +52,9 @@ namespace OpenCVUtils
52 52
53 // Model storage 53 // Model storage
54 void storeModel(const CvStatModel &model, QDataStream &stream); 54 void storeModel(const CvStatModel &model, QDataStream &stream);
  55 + void storeModel(const cv::Algorithm &model, QDataStream &stream);
55 void loadModel(CvStatModel &model, QDataStream &stream); 56 void loadModel(CvStatModel &model, QDataStream &stream);
  57 + void loadModel(cv::Algorithm &model, QDataStream &stream);
56 58
57 template <typename T> 59 template <typename T>
58 T getElement(const cv::Mat &m, int r, int c) 60 T getElement(const cv::Mat &m, int r, int c)