Commit d611f97e5909eb97df0091a6f71518ecaf6c24f7
Merge pull request #292 from biometrics/csv_fix
Fix issue #265 (csv format broken with FTE templates)
Showing
1 changed file
with
47 additions
and
12 deletions
openbr/plugins/gallery.cpp
| ... | ... | @@ -887,20 +887,27 @@ class csvGallery : public FileGallery |
| 887 | 887 | QStringList lines; |
| 888 | 888 | lines.reserve(files.size()+1); |
| 889 | 889 | |
| 890 | + QMap<QString, int> columnCounts; | |
| 891 | + | |
| 890 | 892 | { // Make header |
| 891 | 893 | QStringList words; |
| 892 | 894 | words.append("File"); |
| 893 | - foreach (const QString &key, samples.keys()) | |
| 894 | - words.append(getCSVElement(key, samples[key], true)); | |
| 895 | + foreach (const QString &key, samples.keys()) { | |
| 896 | + int count = 0; | |
| 897 | + words.append(getCSVElement(key, samples[key], true, count)); | |
| 898 | + columnCounts.insert(key, count); | |
| 899 | + } | |
| 895 | 900 | lines.append(words.join(",")); |
| 896 | 901 | } |
| 897 | - | |
| 902 | + | |
| 898 | 903 | // Make table |
| 899 | 904 | foreach (const File &file, files) { |
| 900 | 905 | QStringList words; |
| 901 | 906 | words.append(file.name); |
| 902 | - foreach (const QString &key, samples.keys()) | |
| 903 | - words.append(getCSVElement(key, file.value(key), false)); | |
| 907 | + foreach (const QString &key, samples.keys()) { | |
| 908 | + int count = columnCounts[key]; | |
| 909 | + words.append(getCSVElement(key, file.value(key), false, count)); | |
| 910 | + } | |
| 904 | 911 | lines.append(words.join(",")); |
| 905 | 912 | } |
| 906 | 913 | |
| ... | ... | @@ -950,22 +957,50 @@ class csvGallery : public FileGallery |
| 950 | 957 | files.append(t.file); |
| 951 | 958 | } |
| 952 | 959 | |
| 953 | - static QString getCSVElement(const QString &key, const QVariant &value, bool header) | |
| 960 | + static QString getCSVElement(const QString &key, const QVariant &value, bool header, int & columnCount) | |
| 954 | 961 | { |
| 962 | + if (header) | |
| 963 | + columnCount = 1; | |
| 964 | + | |
| 955 | 965 | if (value.canConvert<QString>()) { |
| 956 | 966 | if (header) return key; |
| 957 | - else return value.value<QString>(); | |
| 967 | + else { | |
| 968 | + if (columnCount != 1) | |
| 969 | + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key)); | |
| 970 | + return value.value<QString>(); | |
| 971 | + } | |
| 958 | 972 | } else if (value.canConvert<QPointF>()) { |
| 959 | 973 | const QPointF point = value.value<QPointF>(); |
| 960 | - if (header) return key+"_X,"+key+"_Y"; | |
| 961 | - else return QString::number(point.x())+","+QString::number(point.y()); | |
| 974 | + if (header) { | |
| 975 | + columnCount = 2; | |
| 976 | + return key+"_X,"+key+"_Y"; | |
| 977 | + } | |
| 978 | + else { | |
| 979 | + if (columnCount != 2) | |
| 980 | + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key)); | |
| 981 | + | |
| 982 | + return QString::number(point.x())+","+QString::number(point.y()); | |
| 983 | + } | |
| 962 | 984 | } else if (value.canConvert<QRectF>()) { |
| 963 | 985 | const QRectF rect = value.value<QRectF>(); |
| 964 | - if (header) return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height"; | |
| 965 | - else return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height()); | |
| 986 | + if (header) { | |
| 987 | + columnCount = 4; | |
| 988 | + return key+"_X,"+key+"_Y,"+key+"_Width,"+key+"_Height"; | |
| 989 | + } | |
| 990 | + else { | |
| 991 | + if (columnCount != 4) | |
| 992 | + qFatal("Inconsistent datatype for key %s, csv file cannot be generated", qPrintable(key)); | |
| 993 | + | |
| 994 | + return QString::number(rect.x())+","+QString::number(rect.y())+","+QString::number(rect.width())+","+QString::number(rect.height()); | |
| 995 | + } | |
| 966 | 996 | } else { |
| 967 | 997 | if (header) return key; |
| 968 | - else return QString::number(std::numeric_limits<float>::quiet_NaN()); | |
| 998 | + else { | |
| 999 | + QString output = QString::number(std::numeric_limits<float>::quiet_NaN()); | |
| 1000 | + for (int i = 1; i < columnCount; i++) | |
| 1001 | + output += "," + QString::number(std::numeric_limits<float>::quiet_NaN()); | |
| 1002 | + return output; | |
| 1003 | + } | |
| 969 | 1004 | } |
| 970 | 1005 | } |
| 971 | 1006 | }; | ... | ... |