Commit db0796b03db070b67719eb3760d624fa70091f84
1 parent
b5b624a4
introduced experimental helper functions for converting to/from Template/br_utemplate
Showing
2 changed files
with
44 additions
and
0 deletions
openbr/openbr_plugin.cpp
| @@ -17,6 +17,8 @@ | @@ -17,6 +17,8 @@ | ||
| 17 | #include <QCoreApplication> | 17 | #include <QCoreApplication> |
| 18 | #include <QCryptographicHash> | 18 | #include <QCryptographicHash> |
| 19 | #include <QFutureSynchronizer> | 19 | #include <QFutureSynchronizer> |
| 20 | +#include <QJsonDocument> | ||
| 21 | +#include <QJsonObject> | ||
| 20 | #include <QLocalSocket> | 22 | #include <QLocalSocket> |
| 21 | #include <QMetaProperty> | 23 | #include <QMetaProperty> |
| 22 | #include <qnumeric.h> | 24 | #include <qnumeric.h> |
| @@ -399,6 +401,44 @@ int FileList::failures() const | @@ -399,6 +401,44 @@ int FileList::failures() const | ||
| 399 | } | 401 | } |
| 400 | 402 | ||
| 401 | /* Template - global methods */ | 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 | QDataStream &br::operator<<(QDataStream &stream, const Template &t) | 442 | QDataStream &br::operator<<(QDataStream &stream, const Template &t) |
| 403 | { | 443 | { |
| 404 | return stream << static_cast<const QList<cv::Mat>&>(t) << t.file; | 444 | return stream << static_cast<const QList<cv::Mat>&>(t) << t.file; |
openbr/openbr_plugin.h
| @@ -41,6 +41,7 @@ | @@ -41,6 +41,7 @@ | ||
| 41 | #include <QVector> | 41 | #include <QVector> |
| 42 | #include <opencv2/core/core.hpp> | 42 | #include <opencv2/core/core.hpp> |
| 43 | #include <openbr/openbr.h> | 43 | #include <openbr/openbr.h> |
| 44 | +#include <openbr/universal_template.h> | ||
| 44 | #include <assert.h> | 45 | #include <assert.h> |
| 45 | 46 | ||
| 46 | namespace br | 47 | namespace br |
| @@ -292,6 +293,9 @@ struct Template : public QList<cv::Mat> | @@ -292,6 +293,9 @@ struct Template : public QList<cv::Mat> | ||
| 292 | foreach (const cv::Mat &m, *this) other += m.clone(); | 293 | foreach (const cv::Mat &m, *this) other += m.clone(); |
| 293 | return other; | 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 | BR_EXPORT QDataStream &operator<<(QDataStream &stream, const Template &t); | 301 | BR_EXPORT QDataStream &operator<<(QDataStream &stream, const Template &t); |