Commit 973ce6bfcd6fcbce54dd437765154032e63835da
1 parent
e964525c
generalized Convert
Showing
7 changed files
with
24 additions
and
80 deletions
sdk/core/bee.cpp
| @@ -225,64 +225,6 @@ void BEE::writeMask(const Mat &m, const QString &mask, const QString &targetSigs | @@ -225,64 +225,6 @@ void BEE::writeMask(const Mat &m, const QString &mask, const QString &targetSigs | ||
| 225 | writeMatrix<Mask_t>(m, mask, targetSigset, querySigset); | 225 | writeMatrix<Mask_t>(m, mask, targetSigset, querySigset); |
| 226 | } | 226 | } |
| 227 | 227 | ||
| 228 | -template <typename T> | ||
| 229 | -void matrixToCSV(const QString &matrix, const QString &csv) | ||
| 230 | -{ | ||
| 231 | - qDebug("Converting %s to %s", qPrintable(matrix), qPrintable(csv)); | ||
| 232 | - | ||
| 233 | - QFile out(csv); | ||
| 234 | - out.open(QFile::WriteOnly); | ||
| 235 | - | ||
| 236 | - Mat m = readMatrix<T>(matrix); | ||
| 237 | - for (int i=0; i<m.rows; i++) { | ||
| 238 | - for (int j=0; j<m.cols; j++) { | ||
| 239 | - out.write(qPrintable(QString::number(m.at<T>(i,j)))); | ||
| 240 | - out.write(","); | ||
| 241 | - } | ||
| 242 | - out.write("\n"); | ||
| 243 | - } | ||
| 244 | -} | ||
| 245 | - | ||
| 246 | -void BEE::simmatToCSV(const QString &simmat, const QString &csv) | ||
| 247 | -{ | ||
| 248 | - matrixToCSV<Simmat_t>(simmat, csv); | ||
| 249 | -} | ||
| 250 | - | ||
| 251 | -void BEE::maskToCSV(const QString &mask, const QString &csv) | ||
| 252 | -{ | ||
| 253 | - matrixToCSV<Mask_t>(mask, csv); | ||
| 254 | -} | ||
| 255 | - | ||
| 256 | -template <typename T> | ||
| 257 | -void CSVToMatrix(const QString &csv, const QString &matrix) | ||
| 258 | -{ | ||
| 259 | - qDebug("Converting %s to %s", qPrintable(csv), qPrintable(matrix)); | ||
| 260 | - | ||
| 261 | - QStringList lines = QtUtils::readLines(csv); | ||
| 262 | - Mat m(lines.size(), lines.first().split(",", QString::SkipEmptyParts).size(), OpenCVType<T,1>::make()); | ||
| 263 | - | ||
| 264 | - for (int i=0; i<lines.size(); i++) { | ||
| 265 | - const QStringList &words = lines[i].split(",", QString::SkipEmptyParts); | ||
| 266 | - for (int j=0; j<words.size(); j++) { | ||
| 267 | - bool ok; | ||
| 268 | - m.at<T>(i, j) = words[j].toFloat(&ok); | ||
| 269 | - if (!ok) qFatal("bee.cpp::CSVToMatrix failed to convert %s to floating point format.", qPrintable(words[j])); | ||
| 270 | - } | ||
| 271 | - } | ||
| 272 | - | ||
| 273 | - writeMatrix<T>(m, matrix, "Unknown_Target", "Unknown_Query"); | ||
| 274 | -} | ||
| 275 | - | ||
| 276 | -void BEE::CSVToSimmat(const QString &csv, const QString &simmat) | ||
| 277 | -{ | ||
| 278 | - CSVToMatrix<Simmat_t>(csv, simmat); | ||
| 279 | -} | ||
| 280 | - | ||
| 281 | -void BEE::CSVToMask(const QString &csv, const QString &mask) | ||
| 282 | -{ | ||
| 283 | - CSVToMatrix<Mask_t>(csv, mask); | ||
| 284 | -} | ||
| 285 | - | ||
| 286 | void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask) | 228 | void BEE::makeMask(const QString &targetInput, const QString &queryInput, const QString &mask) |
| 287 | { | 229 | { |
| 288 | qDebug("Making mask from %s and %s to %s", qPrintable(targetInput), qPrintable(queryInput), qPrintable(mask)); | 230 | qDebug("Making mask from %s and %s to %s", qPrintable(targetInput), qPrintable(queryInput), qPrintable(mask)); |
sdk/core/bee.h
| @@ -44,12 +44,6 @@ namespace BEE | @@ -44,12 +44,6 @@ namespace BEE | ||
| 44 | void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); | 44 | void writeSimmat(const cv::Mat &m, const QString &simmat, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); |
| 45 | void writeMask(const cv::Mat &m, const QString &mask, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); | 45 | void writeMask(const cv::Mat &m, const QString &mask, const QString &targetSigset = "Unknown_Target", const QString &querySigset = "Unknown_Query"); |
| 46 | 46 | ||
| 47 | - // CSV IO | ||
| 48 | - void simmatToCSV(const QString &simmat, const QString &csv); | ||
| 49 | - void maskToCSV(const QString &mask, const QString &csv); | ||
| 50 | - void CSVToSimmat(const QString &csv, const QString &simmat); | ||
| 51 | - void CSVToMask(const QString &csv, const QString &mask); | ||
| 52 | - | ||
| 53 | // Write BEE files | 47 | // Write BEE files |
| 54 | void makeMask(const QString &targetInput, const QString &queryInput, const QString &mask); | 48 | void makeMask(const QString &targetInput, const QString &queryInput, const QString &mask); |
| 55 | void combineMasks(const QStringList &inputMasks, const QString &outputMask, const QString &method); | 49 | void combineMasks(const QStringList &inputMasks, const QString &outputMask, const QString &method); |
sdk/core/core.cpp
| @@ -305,6 +305,13 @@ void br::Compare(const File &targetGallery, const File &queryGallery, const File | @@ -305,6 +305,13 @@ void br::Compare(const File &targetGallery, const File &queryGallery, const File | ||
| 305 | AlgorithmManager::getAlgorithm(output.getString("algorithm"))->compare(targetGallery, queryGallery, output); | 305 | AlgorithmManager::getAlgorithm(output.getString("algorithm"))->compare(targetGallery, queryGallery, output); |
| 306 | } | 306 | } |
| 307 | 307 | ||
| 308 | +void br::Convert(const File &src, const File &dst) | ||
| 309 | +{ | ||
| 310 | + QScopedPointer<Format> before(Factory<Format>::make(src)); | ||
| 311 | + QScopedPointer<Format> after(Factory<Format>::make(dst)); | ||
| 312 | + after->write(before->read()); | ||
| 313 | +} | ||
| 314 | + | ||
| 308 | QSharedPointer<br::Transform> br::Transform::fromAlgorithm(const QString &algorithm) | 315 | QSharedPointer<br::Transform> br::Transform::fromAlgorithm(const QString &algorithm) |
| 309 | { | 316 | { |
| 310 | return AlgorithmManager::getAlgorithm(algorithm)->transform; | 317 | return AlgorithmManager::getAlgorithm(algorithm)->transform; |
sdk/openbr.cpp
| @@ -51,17 +51,9 @@ void br_confusion(const char *file, float score, int *true_positives, int *false | @@ -51,17 +51,9 @@ void br_confusion(const char *file, float score, int *true_positives, int *false | ||
| 51 | return Confusion(file, score, *true_positives, *false_positives, *true_negatives, *false_negatives); | 51 | return Confusion(file, score, *true_positives, *false_positives, *true_negatives, *false_negatives); |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | -void br_convert(const char *input_matrix, const char *output_matrix) | ||
| 55 | -{ | ||
| 56 | - QString inputSuffix = QFileInfo(input_matrix).suffix(); | ||
| 57 | - QString outputSuffix = QFileInfo(output_matrix).suffix(); | ||
| 58 | - if (inputSuffix == "csv") { | ||
| 59 | - if (outputSuffix == "mtx") BEE::CSVToSimmat(input_matrix, output_matrix); | ||
| 60 | - else BEE::CSVToMask(input_matrix, output_matrix); | ||
| 61 | - } else { | ||
| 62 | - if (inputSuffix == "mtx") BEE::simmatToCSV(input_matrix, output_matrix); | ||
| 63 | - else BEE::maskToCSV(input_matrix, output_matrix); | ||
| 64 | - } | 54 | +void br_convert(const char *input, const char *output) |
| 55 | +{ | ||
| 56 | + Convert(File(input), File(output)); | ||
| 65 | } | 57 | } |
| 66 | 58 | ||
| 67 | void br_enroll(const char *input, const char *gallery) | 59 | void br_enroll(const char *input, const char *gallery) |
sdk/openbr.h
| @@ -123,11 +123,9 @@ BR_EXPORT void br_confusion(const char *file, float score, | @@ -123,11 +123,9 @@ BR_EXPORT void br_confusion(const char *file, float score, | ||
| 123 | int *true_positives, int *false_positives, int *true_negatives, int *false_negatives); | 123 | int *true_positives, int *false_positives, int *true_negatives, int *false_negatives); |
| 124 | 124 | ||
| 125 | /*! | 125 | /*! |
| 126 | - * \brief Converts a <i>.csv</i> file to/from a \ref simmat or \ref mask. | ||
| 127 | - * \param input_matrix The input matrix. | ||
| 128 | - * \param output_matrix The output matrix. | 126 | + * \brief Wraps br::Convert() |
| 129 | */ | 127 | */ |
| 130 | -BR_EXPORT void br_convert(const char *input_matrix, const char *output_matrix); | 128 | +BR_EXPORT void br_convert(const char *input, const char *output); |
| 131 | 129 | ||
| 132 | /*! | 130 | /*! |
| 133 | * \brief Constructs template(s) from an input. | 131 | * \brief Constructs template(s) from an input. |
sdk/openbr_plugin.h
| @@ -1080,6 +1080,13 @@ BR_EXPORT FileList Enroll(const File &input, const File &gallery = File()); | @@ -1080,6 +1080,13 @@ BR_EXPORT FileList Enroll(const File &input, const File &gallery = File()); | ||
| 1080 | */ | 1080 | */ |
| 1081 | BR_EXPORT void Compare(const File &targetGallery, const File &queryGallery, const File &output); | 1081 | BR_EXPORT void Compare(const File &targetGallery, const File &queryGallery, const File &output); |
| 1082 | 1082 | ||
| 1083 | +/*! | ||
| 1084 | + * \brief To convert between matrix/template formats. | ||
| 1085 | + * \param input The input matrix or template. | ||
| 1086 | + * \param output The output matrix or template. | ||
| 1087 | + */ | ||
| 1088 | +BR_EXPORT void Convert(const File &input, const File &output); | ||
| 1089 | + | ||
| 1083 | /*! @}*/ | 1090 | /*! @}*/ |
| 1084 | 1091 | ||
| 1085 | } // namespace br | 1092 | } // namespace br |
sdk/plugins/format.cpp
| @@ -46,12 +46,15 @@ class csvFormat : public Format | @@ -46,12 +46,15 @@ class csvFormat : public Format | ||
| 46 | QStringList lines(QString(f.readAll()).split('\n')); | 46 | QStringList lines(QString(f.readAll()).split('\n')); |
| 47 | f.close(); | 47 | f.close(); |
| 48 | 48 | ||
| 49 | + bool isUChar = true; | ||
| 49 | QList< QList<float> > valsList; | 50 | QList< QList<float> > valsList; |
| 50 | foreach (const QString &line, lines) { | 51 | foreach (const QString &line, lines) { |
| 51 | QList<float> vals; | 52 | QList<float> vals; |
| 52 | foreach (const QString &word, line.split(QRegExp(" *, *"))) { | 53 | foreach (const QString &word, line.split(QRegExp(" *, *"))) { |
| 53 | bool ok; | 54 | bool ok; |
| 54 | - vals.append(word.toFloat(&ok)); assert(ok); | 55 | + const float val = word.toFloat(&ok); |
| 56 | + vals.append(val); | ||
| 57 | + isUChar = isUChar && (val == float(uchar(val))); | ||
| 55 | } | 58 | } |
| 56 | valsList.append(vals); | 59 | valsList.append(vals); |
| 57 | } | 60 | } |
| @@ -65,6 +68,7 @@ class csvFormat : public Format | @@ -65,6 +68,7 @@ class csvFormat : public Format | ||
| 65 | } | 68 | } |
| 66 | } | 69 | } |
| 67 | 70 | ||
| 71 | + if (isUChar) m.convertTo(m, CV_8U); | ||
| 68 | return Template(m); | 72 | return Template(m); |
| 69 | } | 73 | } |
| 70 | 74 |