Commit db0796b03db070b67719eb3760d624fa70091f84

Authored by Josh Klontz
1 parent b5b624a4

introduced experimental helper functions for converting to/from Template/br_utemplate

openbr/openbr_plugin.cpp
... ... @@ -17,6 +17,8 @@
17 17 #include <QCoreApplication>
18 18 #include <QCryptographicHash>
19 19 #include <QFutureSynchronizer>
  20 +#include <QJsonDocument>
  21 +#include <QJsonObject>
20 22 #include <QLocalSocket>
21 23 #include <QMetaProperty>
22 24 #include <qnumeric.h>
... ... @@ -399,6 +401,44 @@ int FileList::failures() const
399 401 }
400 402  
401 403 /* Template - global methods */
  404 +template <typename T>
  405 +static T findAndRemove(QVariantMap &map, const QString &key, const T &defaultValue)
  406 +{
  407 + T result = defaultValue;
  408 + if (map.contains(key)) {
  409 + result = map.value(key).value<T>();
  410 + map.remove(key);
  411 + }
  412 + return result;
  413 +}
  414 +
  415 +br_utemplate Template::toUniversalTemplate(const Template &t)
  416 +{
  417 + QVariantMap map = t.file.localMetadata();
  418 + const int32_t algorithmID = findAndRemove<int32_t> (map, "AlgorithmID", 0);
  419 + const int32_t x = findAndRemove<int32_t> (map, "X" , 0);
  420 + const int32_t y = findAndRemove<int32_t> (map, "Y" , 0);
  421 + const uint32_t width = findAndRemove<uint32_t>(map, "Width" , 0);
  422 + const uint32_t height = findAndRemove<uint32_t>(map, "Height" , 0);
  423 + const float confidence = findAndRemove<float> (map, "Confidence" , 0);
  424 + const QByteArray metadata = QJsonDocument(QJsonObject::fromVariantMap(map)).toJson();
  425 + const Mat &m = t;
  426 + return br_new_utemplate(algorithmID, x, y, width, height, confidence, metadata.data(), (const char*) m.data, m.rows * m.cols * m.elemSize());
  427 +}
  428 +
  429 +Template Template::fromUniversalTemplate(const br_utemplate &ut)
  430 +{
  431 + QVariantMap map = QJsonDocument::fromJson(QByteArray((const char*) ut->data)).object().toVariantMap();
  432 + map.insert("AlgorithmID", ut->algorithmID);
  433 + map.insert("X" , ut->x );
  434 + map.insert("Y" , ut->y );
  435 + map.insert("Width" , ut->width );
  436 + map.insert("Height" , ut->height );
  437 + map.insert("Confidence" , ut->confidence );
  438 + const Mat m = Mat(1, ut->fvSize, CV_8UC1, ut->data + ut->mdSize).clone();
  439 + return Template(File(map), m);
  440 +}
  441 +
402 442 QDataStream &br::operator<<(QDataStream &stream, const Template &t)
403 443 {
404 444 return stream << static_cast<const QList<cv::Mat>&>(t) << t.file;
... ...
openbr/openbr_plugin.h
... ... @@ -41,6 +41,7 @@
41 41 #include <QVector>
42 42 #include <opencv2/core/core.hpp>
43 43 #include <openbr/openbr.h>
  44 +#include <openbr/universal_template.h>
44 45 #include <assert.h>
45 46  
46 47 namespace br
... ... @@ -292,6 +293,9 @@ struct Template : public QList&lt;cv::Mat&gt;
292 293 foreach (const cv::Mat &m, *this) other += m.clone();
293 294 return other;
294 295 }
  296 +
  297 + static br_utemplate toUniversalTemplate(const Template &t);
  298 + static Template fromUniversalTemplate(const br_utemplate &ut);
295 299 };
296 300  
297 301 BR_EXPORT QDataStream &operator<<(QDataStream &stream, const Template &t);
... ...