diff --git a/openbr/plugins/nn.cpp b/openbr/plugins/nn.cpp new file mode 100644 index 0000000..430e3ad --- /dev/null +++ b/openbr/plugins/nn.cpp @@ -0,0 +1,117 @@ +#include + +#include "openbr_internal.h" +#include "openbr/core/qtutils.h" +#include "openbr/core/opencvutils.h" +#include "openbr/core/eigenutils.h" +#include +#include + +using namespace std; +using namespace cv; + +namespace br +{ + +static void storeMLP(const CvANN_MLP &mlp, QDataStream &stream) +{ + // Create local file + QTemporaryFile tempFile; + tempFile.open(); + tempFile.close(); + + // Save SVM to local file + mlp.save(qPrintable(tempFile.fileName())); + + // Copy local file contents to stream + tempFile.open(); + QByteArray data = tempFile.readAll(); + tempFile.close(); + stream << data; +} + +static void loadMLP(CvANN_MLP &mlp, QDataStream &stream) +{ + // Copy local file contents from stream + QByteArray data; + stream >> data; + + // Create local file + QTemporaryFile tempFile(QDir::tempPath()+"/MLP"); + tempFile.open(); + tempFile.write(data); + tempFile.close(); + + // Load SVM from local file + mlp.load(qPrintable(tempFile.fileName())); +} + +/*! + * \ingroup transforms + * \brief Wraps OpenCV's multi-layer perceptron framework + * \author Scott Klum \cite sklum + */ +class MLPTransform : public MetaTransform +{ + Q_OBJECT + + Q_PROPERTY(QStringList inputVariables READ get_inputVariables WRITE set_inputVariables RESET reset_inputVariables STORED false) + BR_PROPERTY(QStringList, inputVariables, QStringList()) + Q_PROPERTY(QStringList outputVariables READ get_outputVariables WRITE set_outputVariables RESET reset_outputVariables STORED false) + BR_PROPERTY(QStringList, outputVariables, QStringList()) + Q_PROPERTY(int hiddenLayers READ get_hiddenLayers WRITE set_hiddenLayers RESET reset_hiddenLayers STORED false) + BR_PROPERTY(int, hiddenLayers, 1) + Q_PROPERTY(QList neuronsPerLayer READ get_neuronsPerLayer WRITE set_neuronsPerLayer RESET reset_neuronsPerLayer STORED false) + BR_PROPERTY(QList, neuronsPerLayer, QList() << 6) + + CvANN_MLP mlp; + + void init() + { + Mat layers = Mat(hiddenLayers, 1, CV_32SC1); + for (int i=0; i(data, inputVariables.at(i))); + + mlp.train(_data,labels,Mat()); + } + + void project(const Template &src, Template &dst) const + { + // See above for response dimensionality + Mat response(outputVariables.size(), 1, CV_32FC1); + mlp.predict(src.m().reshape(1,1),response); + + for (int i=0; i(i,0)); + } + + void load(QDataStream &stream) + { + loadMLP(mlp,stream); + } + + void store(QDataStream &stream) const + { + storeMLP(mlp,stream); + } +}; + +BR_REGISTER(Transform, MLPTransform) + +} // namespace br + +#include "nn.moc"