| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2010-09-03 |
| 7 |
* Description : Face detection benchmarker |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 10 |
* SPDX-FileCopyrightText: 2012-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* |
| 12 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 13 |
* |
| 14 |
* ============================================================ */ |
| 15 |
|
| 16 |
#include "detectionbenchmarker.h" |
| 17 |
|
| 18 |
// Local includes |
| 19 |
|
| 20 |
#include "digikam_debug.h" |
| 21 |
#include "tagscache.h" |
| 22 |
|
| 23 |
namespace Digikam |
| 24 |
{ |
| 25 |
|
| 26 |
DetectionBenchmarker::DetectionBenchmarker(FacePipeline::Private* const dd) |
| 27 |
: d(dd) |
| 28 |
{ |
| 29 |
} |
| 30 |
|
| 31 |
void DetectionBenchmarker::process(const FacePipelineExtendedPackage::Ptr& package) |
| 32 |
{ |
| 33 |
if (package->databaseFaces.isEmpty()) |
| 34 |
{ |
| 35 |
// Detection / Recognition |
| 36 |
|
| 37 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Benchmarking image" << package->info.name(); |
| 38 |
|
| 39 |
FaceUtils utils; |
| 40 |
QList<FaceTagsIface> groundTruth = utils.databaseFaces(package->info.id()); |
| 41 |
|
| 42 |
QList<FaceTagsIface> testedFaces = utils.toFaceTagsIfaces(package->info.id(), |
| 43 |
package->detectedFaces, |
| 44 |
package->recognitionResults, |
| 45 |
package->image.originalSize()); |
| 46 |
|
| 47 |
QList<FaceTagsIface> unmatchedTrueFaces = groundTruth; |
| 48 |
QList<FaceTagsIface> unmatchedTestedFaces = testedFaces; |
| 49 |
QList<FaceTagsIface> matchedTrueFaces; |
| 50 |
|
| 51 |
int trueFaces = groundTruth.size(); |
| 52 |
const double minOverlap = 0.75; |
| 53 |
|
| 54 |
qCDebug(DIGIKAM_GENERAL_LOG) << "There are" << trueFaces |
| 55 |
<< "faces to be detected. The detector found" << testedFaces.size(); |
| 56 |
|
| 57 |
++totalImages; |
| 58 |
faces += trueFaces; |
| 59 |
totalPixels += package->image.originalSize().width() * package->image.originalSize().height(); |
| 60 |
|
| 61 |
for (const FaceTagsIface& trueFace : std::as_const(groundTruth)) |
| 62 |
{ |
| 63 |
++faces; |
| 64 |
QRect rect = trueFace.region().toRect(); |
| 65 |
facePixels += rect.width() * rect.height(); |
| 66 |
|
| 67 |
for (const FaceTagsIface& testedFace : std::as_const(testedFaces)) |
| 68 |
{ |
| 69 |
if (trueFace.region().intersects(testedFace.region(), minOverlap)) |
| 70 |
{ |
| 71 |
// cppcheck-suppress useStlAlgorithm |
| 72 |
matchedTrueFaces << trueFace; |
| 73 |
unmatchedTrueFaces.removeOne(trueFace); |
| 74 |
|
| 75 |
break; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
for (const FaceTagsIface& testedFace : std::as_const(testedFaces)) |
| 81 |
{ |
| 82 |
for (const FaceTagsIface& trueFace : std::as_const(groundTruth)) |
| 83 |
{ |
| 84 |
if (trueFace.region().intersects(testedFace.region(), minOverlap)) |
| 85 |
{ |
| 86 |
// cppcheck-suppress useStlAlgorithm |
| 87 |
unmatchedTestedFaces.removeOne(testedFace); |
| 88 |
|
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (groundTruth.isEmpty()) |
| 95 |
{ |
| 96 |
if (testedFaces.isEmpty()) |
| 97 |
{ |
| 98 |
++trueNegativeImages; |
| 99 |
} |
| 100 |
|
| 101 |
else |
| 102 |
{ |
| 103 |
qCDebug(DIGIKAM_GENERAL_LOG) << "The image, truly without faces, is false-positive"; |
| 104 |
++falsePositiveImages; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
truePositiveFaces += matchedTrueFaces.size(); |
| 109 |
falseNegativeFaces += unmatchedTrueFaces.size(); |
| 110 |
falsePositiveFaces += unmatchedTestedFaces.size(); |
| 111 |
|
| 112 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Faces detected correctly:" |
| 113 |
<< matchedTrueFaces.size() |
| 114 |
<< ", faces missed:" |
| 115 |
<< unmatchedTrueFaces.size() |
| 116 |
<< ", faces falsely detected:" |
| 117 |
<< unmatchedTestedFaces.size(); |
| 118 |
} |
| 119 |
|
| 120 |
package->processFlags |= FacePipelinePackage::WrittenToDatabase; |
| 121 |
|
| 122 |
Q_EMIT processed(package); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* NOTE: Bench performance code. No need i18n here |
| 127 |
*/ |
| 128 |
QString DetectionBenchmarker::result() const |
| 129 |
{ |
| 130 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Per-image:" |
| 131 |
<< trueNegativeImages |
| 132 |
<< falsePositiveFaces; |
| 133 |
|
| 134 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Per-face:" |
| 135 |
<< truePositiveFaces |
| 136 |
<< falseNegativeFaces |
| 137 |
<< falsePositiveFaces; // 26 7 1 |
| 138 |
|
| 139 |
int negativeImages = trueNegativeImages + falsePositiveImages; |
| 140 |
int trueFaces = truePositiveFaces + falseNegativeFaces; |
| 141 |
QString specificityWarning, sensitivityWarning; |
| 142 |
|
| 143 |
if (negativeImages < (0.2 * totalImages)) |
| 144 |
{ |
| 145 |
specificityWarning = QString::fromUtf8("<p><b>Note:</b><br> " |
| 146 |
"Only %1 of the %2 test images have <i>no</i> depicted faces. " |
| 147 |
"This means the result is cannot be representative; " |
| 148 |
"it can only be used to compare preselected collections, " |
| 149 |
"and the specificity and false-positive rate have little meaning. </p>") |
| 150 |
.arg(negativeImages).arg(totalImages); |
| 151 |
negativeImages = qMax(negativeImages, 1); |
| 152 |
} |
| 153 |
|
| 154 |
if (trueFaces == 0) |
| 155 |
{ |
| 156 |
sensitivityWarning = QString::fromUtf8("<p><b>Note:</b><br> " |
| 157 |
"No picture in the test collection contained a face. " |
| 158 |
"This means that sensitivity and PPV have no meaning and will be zero. </p>"); |
| 159 |
trueFaces = 1; |
| 160 |
} |
| 161 |
|
| 162 |
// collection properties |
| 163 |
|
| 164 |
double pixelCoverage = facePixels / totalPixels; |
| 165 |
|
| 166 |
// per-image |
| 167 |
|
| 168 |
double specificity = double(trueNegativeImages) / negativeImages; |
| 169 |
double falsePositiveRate = double(falsePositiveImages) / negativeImages; |
| 170 |
|
| 171 |
// per-face |
| 172 |
|
| 173 |
double sensitivity = double(truePositiveFaces) / trueFaces; |
| 174 |
double ppv = double(truePositiveFaces) / (truePositiveFaces + falsePositiveFaces); |
| 175 |
|
| 176 |
return QString::fromUtf8("<p>" |
| 177 |
"<u>Collection Properties:</u><br>" |
| 178 |
"%1 Images <br>" |
| 179 |
"%2 Faces <br>" |
| 180 |
"%3% of pixels covered by faces <br>" |
| 181 |
"</p>" |
| 182 |
"%8" |
| 183 |
"%9" |
| 184 |
"<p>" |
| 185 |
"<u>Per-Image Performance:</u> <br>" |
| 186 |
"Specificity: %4% <br>" |
| 187 |
"False-Positive Rate: %5%" |
| 188 |
"</p>" |
| 189 |
"<p>" |
| 190 |
"<u>Per-Face Performance:</u> <br>" |
| 191 |
"Sensitivity: %6% <br>" |
| 192 |
"Positive Predictive Value: %7% <br>" |
| 193 |
"</p>" |
| 194 |
"<p>" |
| 195 |
"In other words, if a face is detected as face, it will " |
| 196 |
"with a probability of %7% truly be a face. " |
| 197 |
"Of all true faces, %6% will be detected. " |
| 198 |
"Given face with no images on it, the detector will with a probability " |
| 199 |
"of %5% falsely find a face on it. " |
| 200 |
"</p>") |
| 201 |
.arg(totalImages).arg(faces).arg(pixelCoverage * 100, 0, 'f', 1) |
| 202 |
.arg(specificity * 100, 0, 'f', 1).arg(falsePositiveRate * 100, 0, 'f', 1) |
| 203 |
.arg(sensitivity * 100, 0, 'f', 1).arg(ppv * 100, 0, 'f', 1) |
| 204 |
.arg(specificityWarning).arg(sensitivityWarning); |
| 205 |
} |
| 206 |
|
| 207 |
} // namespace Digikam |
| 208 |
|
| 209 |
#include "moc_detectionbenchmarker.cpp" |