diff --git a/openbr/openbr_plugin.cpp b/openbr/openbr_plugin.cpp index 633301e..08633bb 100644 --- a/openbr/openbr_plugin.cpp +++ b/openbr/openbr_plugin.cpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -399,6 +401,44 @@ int FileList::failures() const } /* Template - global methods */ +template +static T findAndRemove(QVariantMap &map, const QString &key, const T &defaultValue) +{ + T result = defaultValue; + if (map.contains(key)) { + result = map.value(key).value(); + map.remove(key); + } + return result; +} + +br_utemplate Template::toUniversalTemplate(const Template &t) +{ + QVariantMap map = t.file.localMetadata(); + const int32_t algorithmID = findAndRemove (map, "AlgorithmID", 0); + const int32_t x = findAndRemove (map, "X" , 0); + const int32_t y = findAndRemove (map, "Y" , 0); + const uint32_t width = findAndRemove(map, "Width" , 0); + const uint32_t height = findAndRemove(map, "Height" , 0); + const float confidence = findAndRemove (map, "Confidence" , 0); + const QByteArray metadata = QJsonDocument(QJsonObject::fromVariantMap(map)).toJson(); + const Mat &m = t; + return br_new_utemplate(algorithmID, x, y, width, height, confidence, metadata.data(), (const char*) m.data, m.rows * m.cols * m.elemSize()); +} + +Template Template::fromUniversalTemplate(const br_utemplate &ut) +{ + QVariantMap map = QJsonDocument::fromJson(QByteArray((const char*) ut->data)).object().toVariantMap(); + map.insert("AlgorithmID", ut->algorithmID); + map.insert("X" , ut->x ); + map.insert("Y" , ut->y ); + map.insert("Width" , ut->width ); + map.insert("Height" , ut->height ); + map.insert("Confidence" , ut->confidence ); + const Mat m = Mat(1, ut->fvSize, CV_8UC1, ut->data + ut->mdSize).clone(); + return Template(File(map), m); +} + QDataStream &br::operator<<(QDataStream &stream, const Template &t) { return stream << static_cast&>(t) << t.file; diff --git a/openbr/openbr_plugin.h b/openbr/openbr_plugin.h index eb7344d..ac7ef5e 100644 --- a/openbr/openbr_plugin.h +++ b/openbr/openbr_plugin.h @@ -41,6 +41,7 @@ #include #include #include +#include #include namespace br @@ -292,6 +293,9 @@ struct Template : public QList foreach (const cv::Mat &m, *this) other += m.clone(); return other; } + + static br_utemplate toUniversalTemplate(const Template &t); + static Template fromUniversalTemplate(const br_utemplate &ut); }; BR_EXPORT QDataStream &operator<<(QDataStream &stream, const Template &t);