From 89b9823b24a1fd933aeb8c2e3f16024eeb07ba85 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Tue, 27 May 2014 16:26:43 -0400 Subject: [PATCH] generalized -evalDetection --- openbr/core/eval.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------ 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/openbr/core/eval.cpp b/openbr/core/eval.cpp index d2f2f67..2c98852 100644 --- a/openbr/core/eval.cpp +++ b/openbr/core/eval.cpp @@ -427,84 +427,106 @@ static QStringList computeDetectionResults(const QList &detec return lines; } -QString getDetectKey(const TemplateList &templates) +struct DetectionKey : public QString { - const File &f = templates.first().file; - foreach (const QString &key, f.localKeys()) { - // first check for single detections + enum Type { + Invalid, + Rect, + RectList, + XYWidthHeight + } type; + + DetectionKey(const QString &key = "", Type type = Invalid) + : QString(key), type(type) {} +}; + +static DetectionKey getDetectKey(const FileList &files) +{ + if (files.empty()) + return DetectionKey(); + + const File &f = files.first(); + const QStringList localKeys = f.localKeys(); + + // first check for single detections + foreach (const QString &key, localKeys) if (!f.get(key, QRectF()).isNull()) - return key; - } + return DetectionKey(key, DetectionKey::Rect); + // and then multiple if (!f.rects().empty()) - return "Rects"; - return ""; -} + return DetectionKey("Rects", DetectionKey::RectList); -bool detectKeyIsList(QString key, const TemplateList &templates) -{ - return templates.first().file.get(key, QRectF()).isNull(); + // check for _X, _Y, _Width, _Height + foreach (const QString &localKey, localKeys) { + if (!localKey.endsWith("_X")) + continue; + const QString key = localKey.mid(0, localKey.size()-2); + if (localKeys.contains(key+"_Y") && + localKeys.contains(key+"_Width") && + localKeys.contains(key+"_Height")) + return DetectionKey(key, DetectionKey::XYWidthHeight); + } + + return DetectionKey(); } -// return a list of detections whether the template holds -// multiple detections or a single detection -QList getDetections(QString key, const Template &t, bool isList, bool isTruth) +// return a list of detections independent of the detection key format +static QList getDetections(const DetectionKey &key, const File &f, bool isTruth) { - File f = t.file; QList dets; - if (isList) { + if (key.type == DetectionKey::RectList) { QList rects = f.rects(); QList confidences = f.getList("Confidences", QList()); if (!isTruth && rects.size() != confidences.size()) qFatal("You don't have enough confidence. I mean, your detections don't all have confidence measures."); for (int i=0; i(key))); - else - dets.append(Detection(f.get(key), f.get("Confidence", -1))); + } else if (key.type == DetectionKey::Rect) { + dets.append(Detection(f.get(key), isTruth ? -1 : f.get("Confidence", -1))); + } else if (key.type == DetectionKey::XYWidthHeight) { + const QRectF rect(f.get(key+"_X"), f.get(key+"_Y"), f.get(key+"_Width"), f.get(key+"_Height")); + dets.append(Detection(rect, isTruth ? -1 : f.get("Confidence", -1))); } return dets; } -QMap getDetections(const TemplateList &predicted, const TemplateList &truth) +static QMap getDetections(const File &predictedGallery, const File &truthGallery) { + const FileList predicted = TemplateList::fromGallery(predictedGallery).files(); + const FileList truth = TemplateList::fromGallery(truthGallery).files(); + // Figure out which metadata field contains a bounding box - QString truthDetectKey = getDetectKey(truth); - if (truthDetectKey.isEmpty()) qFatal("No suitable ground truth metadata key found."); - QString predictedDetectKey = getDetectKey(predicted); - if (predictedDetectKey.isEmpty()) qFatal("No suitable predicted metadata key found."); + DetectionKey truthDetectKey = getDetectKey(truth); + if (truthDetectKey.isEmpty()) + qFatal("No suitable ground truth metadata key found."); + + DetectionKey predictedDetectKey = getDetectKey(predicted); + if (predictedDetectKey.isEmpty()) + qFatal("No suitable predicted metadata key found."); + qDebug("Using metadata key: %s%s", qPrintable(predictedDetectKey), qPrintable(predictedDetectKey == truthDetectKey ? QString() : "/"+truthDetectKey)); QMap allDetections; - bool predKeyIsList = detectKeyIsList(predictedDetectKey, predicted); - bool truthKeyIsList = detectKeyIsList(truthDetectKey, truth); - foreach (const Template &t, predicted) { - QList dets = getDetections(predictedDetectKey, t, predKeyIsList, false); - allDetections[t.file.baseName()].predicted.append(dets); - } - foreach (const Template &t, truth) { - QList dets = getDetections(truthDetectKey, t, truthKeyIsList, true); - allDetections[t.file.baseName()].truth.append(dets); - } + foreach (const File &f, predicted) + allDetections[f.baseName()].predicted.append(getDetections(predictedDetectKey, f, false)); + foreach (const File &f, truth) + allDetections[f.baseName()].truth.append(getDetections(truthDetectKey, f, true)); return allDetections; } float EvalDetection(const QString &predictedGallery, const QString &truthGallery, const QString &csv) { qDebug("Evaluating detection of %s against %s", qPrintable(predictedGallery), qPrintable(truthGallery)); - const TemplateList predicted(TemplateList::fromGallery(predictedGallery)); - const TemplateList truth(TemplateList::fromGallery(truthGallery)); // Organized by file, QMap used to preserve order - QMap allDetections = getDetections(predicted, truth); + QMap allDetections = getDetections(predictedGallery, truthGallery); QList resolvedDetections, falseNegativeDetections; int totalTrueDetections = 0; -- libgit2 0.21.4