Commit 94f235509a2991bcdfa5189b8f0b35cef75b5f37
Merge pull request #75 from biometrics/file_get
introduced static File::get that can operate on both FileList and TemplateList
Showing
8 changed files
with
51 additions
and
58 deletions
openbr/core/bee.cpp
| @@ -268,8 +268,8 @@ cv::Mat BEE::makeMask(const br::FileList &targets, const br::FileList &queries, | @@ -268,8 +268,8 @@ cv::Mat BEE::makeMask(const br::FileList &targets, const br::FileList &queries, | ||
| 268 | { | 268 | { |
| 269 | // Would like to use indexProperty for this, but didn't make a version of that for Filelist yet | 269 | // Would like to use indexProperty for this, but didn't make a version of that for Filelist yet |
| 270 | // -cao | 270 | // -cao |
| 271 | - QList<QString> targetLabels = targets.get<QString>("Subject", "-1"); | ||
| 272 | - QList<QString> queryLabels = queries.get<QString>("Subject", "-1"); | 271 | + QList<QString> targetLabels = File::get<QString>(targets, "Subject", "-1"); |
| 272 | + QList<QString> queryLabels = File::get<QString>(queries, "Subject", "-1"); | ||
| 273 | QList<int> targetPartitions = targets.crossValidationPartitions(); | 273 | QList<int> targetPartitions = targets.crossValidationPartitions(); |
| 274 | QList<int> queryPartitions = queries.crossValidationPartitions(); | 274 | QList<int> queryPartitions = queries.crossValidationPartitions(); |
| 275 | 275 |
openbr/core/cluster.cpp
| @@ -280,7 +280,7 @@ void br::EvalClustering(const QString &csv, const QString &input) | @@ -280,7 +280,7 @@ void br::EvalClustering(const QString &csv, const QString &input) | ||
| 280 | 280 | ||
| 281 | // We assume clustering algorithms store assigned cluster labels as integers (since the clusters are | 281 | // We assume clustering algorithms store assigned cluster labels as integers (since the clusters are |
| 282 | // not named). | 282 | // not named). |
| 283 | - QList<int> labels = TemplateList::fromGallery(input).files().get<int>("Subject"); | 283 | + QList<int> labels = File::get<int>(TemplateList::fromGallery(input), "Subject"); |
| 284 | 284 | ||
| 285 | QHash<int, int> labelToIndex; | 285 | QHash<int, int> labelToIndex; |
| 286 | int nClusters = 0; | 286 | int nClusters = 0; |
openbr/openbr_plugin.cpp
| @@ -436,7 +436,7 @@ TemplateList TemplateList::fromGallery(const br::File &gallery) | @@ -436,7 +436,7 @@ TemplateList TemplateList::fromGallery(const br::File &gallery) | ||
| 436 | // stores the index values in "Label" of the output template list | 436 | // stores the index values in "Label" of the output template list |
| 437 | TemplateList TemplateList::relabel(const TemplateList &tl, const QString & propName) | 437 | TemplateList TemplateList::relabel(const TemplateList &tl, const QString & propName) |
| 438 | { | 438 | { |
| 439 | - const QList<QString> originalLabels = tl.get<QString>(propName); | 439 | + const QList<QString> originalLabels = File::get<QString>(tl, propName); |
| 440 | QHash<QString,int> labelTable; | 440 | QHash<QString,int> labelTable; |
| 441 | foreach (const QString & label, originalLabels) | 441 | foreach (const QString & label, originalLabels) |
| 442 | if (!labelTable.contains(label)) | 442 | if (!labelTable.contains(label)) |
| @@ -464,7 +464,7 @@ QList<int> TemplateList::indexProperty(const QString & propName, QHash<QString, | @@ -464,7 +464,7 @@ QList<int> TemplateList::indexProperty(const QString & propName, QHash<QString, | ||
| 464 | valueMap.clear(); | 464 | valueMap.clear(); |
| 465 | reverseLookup.clear(); | 465 | reverseLookup.clear(); |
| 466 | 466 | ||
| 467 | - const QList<QVariant> originalLabels = values(propName); | 467 | + const QList<QVariant> originalLabels = File::values(*this, propName); |
| 468 | foreach (const QVariant & label, originalLabels) { | 468 | foreach (const QVariant & label, originalLabels) { |
| 469 | QString labelString = label.toString(); | 469 | QString labelString = label.toString(); |
| 470 | if (!valueMap.contains(labelString)) { | 470 | if (!valueMap.contains(labelString)) { |
| @@ -481,9 +481,9 @@ QList<int> TemplateList::indexProperty(const QString & propName, QHash<QString, | @@ -481,9 +481,9 @@ QList<int> TemplateList::indexProperty(const QString & propName, QHash<QString, | ||
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | // uses -1 for missing values | 483 | // uses -1 for missing values |
| 484 | -QList<int> TemplateList::applyIndex(const QString & propName, const QHash<QString, int> & valueMap) const | 484 | +QList<int> TemplateList::applyIndex(const QString &propName, const QHash<QString, int> &valueMap) const |
| 485 | { | 485 | { |
| 486 | - const QList<QString> originalLabels = get<QString>(propName); | 486 | + const QList<QString> originalLabels = File::get<QString>(*this, propName); |
| 487 | 487 | ||
| 488 | QList<int> result; | 488 | QList<int> result; |
| 489 | for (int i=0; i<originalLabels.size(); i++) { | 489 | for (int i=0; i<originalLabels.size(); i++) { |
openbr/openbr_plugin.h
| @@ -228,7 +228,20 @@ struct BR_EXPORT File | @@ -228,7 +228,20 @@ struct BR_EXPORT File | ||
| 228 | return variant.value<T>(); | 228 | return variant.value<T>(); |
| 229 | } | 229 | } |
| 230 | 230 | ||
| 231 | - /*!< \brief Returns a list of type T for the key, throwing an error if the key does not exist or if the value cannot be converted to the specified type. */ | 231 | + /*!< \brief Returns a value for the key, returning \em defaultValue if the key does not exist or can't be converted. */ |
| 232 | + template <typename T> | ||
| 233 | + T get(const QString &key, const T &defaultValue) const | ||
| 234 | + { | ||
| 235 | + if (!contains(key)) return defaultValue; | ||
| 236 | + QVariant variant = value(key); | ||
| 237 | + if (!variant.canConvert<T>()) return defaultValue; | ||
| 238 | + return variant.value<T>(); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + /*!< \brief Specialization for boolean type. */ | ||
| 242 | + bool getBool(const QString &key, bool defaultValue = false) const; | ||
| 243 | + | ||
| 244 | + /*!< \brief Specialization for list type. Returns a list of type T for the key, throwing an error if the key does not exist or if the value cannot be converted to the specified type. */ | ||
| 232 | template <typename T> | 245 | template <typename T> |
| 233 | QList<T> getList(const QString &key) const | 246 | QList<T> getList(const QString &key) const |
| 234 | { | 247 | { |
| @@ -241,17 +254,31 @@ struct BR_EXPORT File | @@ -241,17 +254,31 @@ struct BR_EXPORT File | ||
| 241 | return list; | 254 | return list; |
| 242 | } | 255 | } |
| 243 | 256 | ||
| 244 | - /*!< \brief Specialization for boolean type. */ | ||
| 245 | - bool getBool(const QString &key, bool defaultValue = false) const; | 257 | + /*!< \brief Returns the value for the specified key for every file in the list. */ |
| 258 | + template<class U> | ||
| 259 | + static QList<QVariant> values(const QList<U> &fileList, const QString &key) | ||
| 260 | + { | ||
| 261 | + QList<QVariant> values; values.reserve(fileList.size()); | ||
| 262 | + foreach (const U &f, fileList) values.append(((const File&)f).value(key)); | ||
| 263 | + return values; | ||
| 264 | + } | ||
| 246 | 265 | ||
| 247 | - /*!< \brief Returns a value for the key, returning \em defaultValue if the key does not exist or can't be converted. */ | ||
| 248 | - template <typename T> | ||
| 249 | - T get(const QString &key, const T &defaultValue) const | 266 | + /*!< \brief Returns a value for the key for every file in the list, throwing an error if the key does not exist. */ |
| 267 | + template<class T, class U> | ||
| 268 | + static QList<T> get(const QList<U> &fileList, const QString &key) | ||
| 250 | { | 269 | { |
| 251 | - if (!contains(key)) return defaultValue; | ||
| 252 | - QVariant variant = value(key); | ||
| 253 | - if (!variant.canConvert<T>()) return defaultValue; | ||
| 254 | - return variant.value<T>(); | 270 | + QList<T> result; result.reserve(fileList.size()); |
| 271 | + foreach (const U &f, fileList) result.append(((const File&)f).get<T>(key)); | ||
| 272 | + return result; | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + /*!< \brief Returns a value for the key for every file in the list, returning \em defaultValue if the key does not exist or can't be converted. */ | ||
| 276 | + template<class T, class U> | ||
| 277 | + static QList<T> get(const QList<U> &fileList, const QString &key, const T &defaultValue) | ||
| 278 | + { | ||
| 279 | + QList<T> result; result.reserve(fileList.size()); | ||
| 280 | + foreach (const U &f, fileList) result.append(static_cast<const File&>(f).get<T>(key, defaultValue)); | ||
| 281 | + return result; | ||
| 255 | } | 282 | } |
| 256 | 283 | ||
| 257 | inline bool failed() const { return getBool("FTE") || getBool("FTO"); } /*!< \brief Returns \c true if the file failed to open or enroll, \c false otherwise. */ | 284 | inline bool failed() const { return getBool("FTE") || getBool("FTO"); } /*!< \brief Returns \c true if the file failed to open or enroll, \c false otherwise. */ |
| @@ -297,23 +324,6 @@ struct BR_EXPORT FileList : public QList<File> | @@ -297,23 +324,6 @@ struct BR_EXPORT FileList : public QList<File> | ||
| 297 | QStringList flat() const; /*!< \brief Returns br::File::flat() for each file in the list. */ | 324 | QStringList flat() const; /*!< \brief Returns br::File::flat() for each file in the list. */ |
| 298 | QStringList names() const; /*!< \brief Returns #br::File::name for each file in the list. */ | 325 | QStringList names() const; /*!< \brief Returns #br::File::name for each file in the list. */ |
| 299 | void sort(const QString& key); /*!< \brief Sort the list based on metadata. */ | 326 | void sort(const QString& key); /*!< \brief Sort the list based on metadata. */ |
| 300 | - /*!< \brief Returns values associated with the input propName for each file in the list. */ | ||
| 301 | - template<typename T> | ||
| 302 | - QList<T> get(const QString & propName) const | ||
| 303 | - { | ||
| 304 | - QList<T> values; values.reserve(size()); | ||
| 305 | - foreach (const File &f, *this) | ||
| 306 | - values.append(f.get<T>(propName)); | ||
| 307 | - return values; | ||
| 308 | - } | ||
| 309 | - template<typename T> | ||
| 310 | - QList<T> get(const QString & propName, T defaultValue) const | ||
| 311 | - { | ||
| 312 | - QList<T> values; values.reserve(size()); | ||
| 313 | - foreach (const File &f, *this) | ||
| 314 | - values.append(f.contains(propName) ? f.get<T>(propName) : defaultValue); | ||
| 315 | - return values; | ||
| 316 | - } | ||
| 317 | 327 | ||
| 318 | QList<int> crossValidationPartitions() const; /*!< \brief Returns the cross-validation partition (default=0) for each file in the list. */ | 328 | QList<int> crossValidationPartitions() const; /*!< \brief Returns the cross-validation partition (default=0) for each file in the list. */ |
| 319 | int failures() const; /*!< \brief Returns the number of files with br::File::failed(). */ | 329 | int failures() const; /*!< \brief Returns the number of files with br::File::failed(). */ |
| @@ -344,6 +354,7 @@ struct Template : public QList<cv::Mat> | @@ -344,6 +354,7 @@ struct Template : public QList<cv::Mat> | ||
| 344 | inline const cv::Mat &m() const { static const cv::Mat NullMatrix; | 354 | inline const cv::Mat &m() const { static const cv::Mat NullMatrix; |
| 345 | return isEmpty() ? qFatal("Empty template."), NullMatrix : last(); } /*!< \brief Idiom to treat the template as a matrix. */ | 355 | return isEmpty() ? qFatal("Empty template."), NullMatrix : last(); } /*!< \brief Idiom to treat the template as a matrix. */ |
| 346 | inline cv::Mat &m() { return isEmpty() ? append(cv::Mat()), last() : last(); } /*!< \brief Idiom to treat the template as a matrix. */ | 356 | inline cv::Mat &m() { return isEmpty() ? append(cv::Mat()), last() : last(); } /*!< \brief Idiom to treat the template as a matrix. */ |
| 357 | + inline const File &operator()() const { return file; } | ||
| 347 | inline cv::Mat &operator=(const cv::Mat &other) { return m() = other; } /*!< \brief Idiom to treat the template as a matrix. */ | 358 | inline cv::Mat &operator=(const cv::Mat &other) { return m() = other; } /*!< \brief Idiom to treat the template as a matrix. */ |
| 348 | inline operator const cv::Mat&() const { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ | 359 | inline operator const cv::Mat&() const { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ |
| 349 | inline operator cv::Mat&() { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ | 360 | inline operator cv::Mat&() { return m(); } /*!< \brief Idiom to treat the template as a matrix. */ |
| @@ -406,7 +417,6 @@ struct TemplateList : public QList<Template> | @@ -406,7 +417,6 @@ struct TemplateList : public QList<Template> | ||
| 406 | QList<int> indexProperty(const QString & propName, QHash<QString, int> & valueMap, QHash<int, QVariant> & reverseLookup) const; | 417 | QList<int> indexProperty(const QString & propName, QHash<QString, int> & valueMap, QHash<int, QVariant> & reverseLookup) const; |
| 407 | QList<int> applyIndex(const QString & propName, const QHash<QString, int> & valueMap) const; | 418 | QList<int> applyIndex(const QString & propName, const QHash<QString, int> & valueMap) const; |
| 408 | 419 | ||
| 409 | - | ||
| 410 | /*! | 420 | /*! |
| 411 | * \brief Returns the total number of bytes in all the templates. | 421 | * \brief Returns the total number of bytes in all the templates. |
| 412 | */ | 422 | */ |
| @@ -477,23 +487,6 @@ struct TemplateList : public QList<Template> | @@ -477,23 +487,6 @@ struct TemplateList : public QList<Template> | ||
| 477 | FileList operator()() const { return files(); } | 487 | FileList operator()() const { return files(); } |
| 478 | 488 | ||
| 479 | /*! | 489 | /*! |
| 480 | - * \brief Returns br::Template::label() for each template in the list. | ||
| 481 | - */ | ||
| 482 | - template<typename T> | ||
| 483 | - QList<T> get(const QString & propName) const | ||
| 484 | - { | ||
| 485 | - QList<T> values; values.reserve(size()); | ||
| 486 | - foreach (const Template &t, *this) values.append(t.file.get<T>(propName)); | ||
| 487 | - return values; | ||
| 488 | - } | ||
| 489 | - QList<QVariant> values(const QString & propName) const | ||
| 490 | - { | ||
| 491 | - QList<QVariant> values; values.reserve(size()); | ||
| 492 | - foreach (const Template &t, *this) values.append(t.file.value(propName)); | ||
| 493 | - return values; | ||
| 494 | - } | ||
| 495 | - | ||
| 496 | - /*! | ||
| 497 | * \brief Returns the number of occurences for each label in the list. | 490 | * \brief Returns the number of occurences for each label in the list. |
| 498 | */ | 491 | */ |
| 499 | template<typename T> | 492 | template<typename T> |
| @@ -1025,7 +1018,7 @@ public: | @@ -1025,7 +1018,7 @@ public: | ||
| 1025 | virtual TemplateList readBlock(bool *done) = 0; /*!< \brief Retrieve a portion of the stored templates. */ | 1018 | virtual TemplateList readBlock(bool *done) = 0; /*!< \brief Retrieve a portion of the stored templates. */ |
| 1026 | void writeBlock(const TemplateList &templates); /*!< \brief Serialize a template list. */ | 1019 | void writeBlock(const TemplateList &templates); /*!< \brief Serialize a template list. */ |
| 1027 | virtual void write(const Template &t) = 0; /*!< \brief Serialize a template. */ | 1020 | virtual void write(const Template &t) = 0; /*!< \brief Serialize a template. */ |
| 1028 | - static Gallery *make(const File &file); /*!< \brief Make a gallery from a file list. */ | 1021 | + static Gallery *make(const File &file); /*!< \brief Make a gallery to/from a file on disk. */ |
| 1029 | 1022 | ||
| 1030 | private: | 1023 | private: |
| 1031 | QSharedPointer<Gallery> next; | 1024 | QSharedPointer<Gallery> next; |
openbr/plugins/eigen3.cpp
| @@ -348,7 +348,7 @@ class LDATransform : public Transform | @@ -348,7 +348,7 @@ class LDATransform : public Transform | ||
| 348 | 348 | ||
| 349 | // OpenBR ensures that class values range from 0 to numClasses-1. | 349 | // OpenBR ensures that class values range from 0 to numClasses-1. |
| 350 | // Label exists because we created it earlier with relabel | 350 | // Label exists because we created it earlier with relabel |
| 351 | - QList<int> classes = trainingSet.get<int>("Label"); | 351 | + QList<int> classes = File::get<int>(trainingSet, "Label"); |
| 352 | QMap<int, int> classCounts = trainingSet.countValues<int>("Label"); | 352 | QMap<int, int> classCounts = trainingSet.countValues<int>("Label"); |
| 353 | const int numClasses = classCounts.size(); | 353 | const int numClasses = classCounts.size(); |
| 354 | 354 |
openbr/plugins/independent.cpp
| @@ -20,7 +20,7 @@ static TemplateList Downsample(const TemplateList &templates, const Transform *t | @@ -20,7 +20,7 @@ static TemplateList Downsample(const TemplateList &templates, const Transform *t | ||
| 20 | const bool atLeast = transform->instances < 0; | 20 | const bool atLeast = transform->instances < 0; |
| 21 | const int instances = abs(transform->instances); | 21 | const int instances = abs(transform->instances); |
| 22 | 22 | ||
| 23 | - QList<QString> allLabels = templates.get<QString>("Subject"); | 23 | + QList<QString> allLabels = File::get<QString>(templates, "Subject"); |
| 24 | QList<QString> uniqueLabels = allLabels.toSet().toList(); | 24 | QList<QString> uniqueLabels = allLabels.toSet().toList(); |
| 25 | qSort(uniqueLabels); | 25 | qSort(uniqueLabels); |
| 26 | 26 |
openbr/plugins/output.cpp
| @@ -146,8 +146,8 @@ class meltOutput : public MatrixOutput | @@ -146,8 +146,8 @@ class meltOutput : public MatrixOutput | ||
| 146 | QStringList lines; | 146 | QStringList lines; |
| 147 | if (file.baseName() != "terminal") lines.append(QString("Query,Target,Mask,Similarity%1").arg(keys)); | 147 | if (file.baseName() != "terminal") lines.append(QString("Query,Target,Mask,Similarity%1").arg(keys)); |
| 148 | 148 | ||
| 149 | - QList<QString> queryLabels = queryFiles.get<QString>("Subject"); | ||
| 150 | - QList<QString> targetLabels = targetFiles.get<QString>("Subject"); | 149 | + QList<QString> queryLabels = File::get<QString>(queryFiles, "Subject"); |
| 150 | + QList<QString> targetLabels = File::get<QString>(targetFiles, "Subject"); | ||
| 151 | 151 | ||
| 152 | for (int i=0; i<queryFiles.size(); i++) { | 152 | for (int i=0; i<queryFiles.size(); i++) { |
| 153 | for (int j=(selfSimilar ? i+1 : 0); j<targetFiles.size(); j++) { | 153 | for (int j=(selfSimilar ? i+1 : 0); j<targetFiles.size(); j++) { |
openbr/plugins/svm.cpp
| @@ -130,7 +130,7 @@ private: | @@ -130,7 +130,7 @@ private: | ||
| 130 | Mat lab; | 130 | Mat lab; |
| 131 | // If we are doing regression, assume subject has float values | 131 | // If we are doing regression, assume subject has float values |
| 132 | if (type == EPS_SVR || type == NU_SVR) { | 132 | if (type == EPS_SVR || type == NU_SVR) { |
| 133 | - lab = OpenCVUtils::toMat(_data.get<float>("Subject")); | 133 | + lab = OpenCVUtils::toMat(File::get<float>(_data, "Subject")); |
| 134 | } | 134 | } |
| 135 | // If we are doing classification, assume subject has discrete values, map them | 135 | // If we are doing classification, assume subject has discrete values, map them |
| 136 | // and store the mapping data | 136 | // and store the mapping data |