1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2010-09-03
 * Description : Face detection benchmarker
 *
 * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * SPDX-FileCopyrightText: 2012-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "detectionbenchmarker.h"

// Local includes

#include "digikam_debug.h"
#include "tagscache.h"

namespace Digikam
{

DetectionBenchmarker::DetectionBenchmarker(FacePipeline::Private* const dd)
    : d(dd)
{
}

void DetectionBenchmarker::process(const FacePipelineExtendedPackage::Ptr& package)
{
    if (package->databaseFaces.isEmpty())
    {
        // Detection / Recognition

        qCDebug(DIGIKAM_GENERAL_LOG) << "Benchmarking image" << package->info.name();

        FaceUtils utils;
        QList<FaceTagsIface> groundTruth          = utils.databaseFaces(package->info.id());

        QList<FaceTagsIface> testedFaces          = utils.toFaceTagsIfaces(package->info.id(),
                                                                           package->detectedFaces,
                                                                           package->recognitionResults,
                                                                           package->image.originalSize());

        QList<FaceTagsIface> unmatchedTrueFaces   = groundTruth;
        QList<FaceTagsIface> unmatchedTestedFaces = testedFaces;
        QList<FaceTagsIface> matchedTrueFaces;

        int trueFaces                             = groundTruth.size();
        const double minOverlap                   = 0.75;

        qCDebug(DIGIKAM_GENERAL_LOG) << "There are" << trueFaces
                                     << "faces to be detected. The detector found" << testedFaces.size();

        ++totalImages;
        faces       += trueFaces;
        totalPixels += package->image.originalSize().width() * package->image.originalSize().height();

        for (const FaceTagsIface& trueFace : std::as_const(groundTruth))
        {
            ++faces;
            QRect rect  = trueFace.region().toRect();
            facePixels += rect.width() * rect.height();

            for (const FaceTagsIface& testedFace : std::as_const(testedFaces))
            {
                if (trueFace.region().intersects(testedFace.region(), minOverlap))
                {<--- Consider using std::any_of algorithm instead of a raw loop.
                    // cppcheck-suppress useStlAlgorithm
                    matchedTrueFaces << trueFace;
                    unmatchedTrueFaces.removeOne(trueFace);

                    break;
                }
            }
        }

        for (const FaceTagsIface& testedFace : std::as_const(testedFaces))
        {
            for (const FaceTagsIface& trueFace : std::as_const(groundTruth))
            {
                if (trueFace.region().intersects(testedFace.region(), minOverlap))
                {<--- Consider using std::any_of algorithm instead of a raw loop.
                    // cppcheck-suppress useStlAlgorithm
                    unmatchedTestedFaces.removeOne(testedFace);

                    break;
                }
            }
        }

        if (groundTruth.isEmpty())
        {
            if (testedFaces.isEmpty())
            {
                ++trueNegativeImages;
            }

            else
            {
                qCDebug(DIGIKAM_GENERAL_LOG) << "The image, truly without faces, is false-positive";
                ++falsePositiveImages;
            }
        }

        truePositiveFaces  += matchedTrueFaces.size();
        falseNegativeFaces += unmatchedTrueFaces.size();
        falsePositiveFaces += unmatchedTestedFaces.size();

        qCDebug(DIGIKAM_GENERAL_LOG) << "Faces detected correctly:"
                                     << matchedTrueFaces.size()
                                     << ", faces missed:"
                                     << unmatchedTrueFaces.size()
                                     << ", faces falsely detected:"
                                     << unmatchedTestedFaces.size();
    }

    package->processFlags  |= FacePipelinePackage::WrittenToDatabase;

    Q_EMIT processed(package);
}

/**
 * NOTE: Bench performance code. No need i18n here
 */
QString DetectionBenchmarker::result() const
{
    qCDebug(DIGIKAM_GENERAL_LOG) << "Per-image:"
                                 << trueNegativeImages
                                 << falsePositiveFaces;

    qCDebug(DIGIKAM_GENERAL_LOG) << "Per-face:"
                                 << truePositiveFaces
                                 << falseNegativeFaces
                                 << falsePositiveFaces; // 26 7 1

    int negativeImages = trueNegativeImages + falsePositiveImages;
    int trueFaces      = truePositiveFaces  + falseNegativeFaces;
    QString specificityWarning, sensitivityWarning;

    if (negativeImages < (0.2 * totalImages))
    {
        specificityWarning = QString::fromUtf8("<p><b>Note:</b><br/> "
                                               "Only %1 of the %2 test images have <i>no</i> depicted faces. "
                                               "This means the result is cannot be representative; "
                                               "it can only be used to compare preselected collections, "
                                               "and the specificity and false-positive rate have little meaning. </p>")
                             .arg(negativeImages).arg(totalImages);
        negativeImages     = qMax(negativeImages, 1);
    }

    if (trueFaces == 0)
    {
        sensitivityWarning = QString::fromUtf8("<p><b>Note:</b><br/> "
                                               "No picture in the test collection contained a face. "
                                               "This means that sensitivity and PPV have no meaning and will be zero. </p>");
        trueFaces          = 1;
    }

    // collection properties

    double pixelCoverage     = facePixels                  / totalPixels;

    // per-image

    double specificity       = double(trueNegativeImages)  / negativeImages;
    double falsePositiveRate = double(falsePositiveImages) / negativeImages;

    // per-face

    double sensitivity       = double(truePositiveFaces)   / trueFaces;
    double ppv               = double(truePositiveFaces)   / (truePositiveFaces + falsePositiveFaces);

    return QString::fromUtf8("<p>"
                             "<u>Collection Properties:</u><br/>"
                             "%1 Images <br/>"
                             "%2 Faces <br/>"
                             "%3% of pixels covered by faces <br/>"
                             "</p>"
                             "%8"
                             "%9"
                             "<p>"
                             "<u>Per-Image Performance:</u> <br/>"
                             "Specificity: %4% <br/>"
                             "False-Positive Rate: %5%"
                             "</p>"
                             "<p>"
                             "<u>Per-Face Performance:</u> <br/>"
                             "Sensitivity: %6% <br/>"
                             "Positive Predictive Value: %7% <br/>"
                             "</p>"
                             "<p>"
                             "In other words, if a face is detected as face, it will "
                             "with a probability of %7% truly be a face. "
                             "Of all true faces, %6% will be detected. "
                             "Given face with no images on it, the detector will with a probability "
                             "of %5% falsely find a face on it. "
                             "</p>")
           .arg(totalImages).arg(faces).arg(pixelCoverage * 100, 0, 'f', 1)
           .arg(specificity * 100, 0, 'f', 1).arg(falsePositiveRate * 100, 0, 'f', 1)
           .arg(sensitivity * 100, 0, 'f', 1).arg(ppv * 100, 0, 'f', 1)
           .arg(specificityWarning).arg(sensitivityWarning);
}

} // namespace Digikam

#include "moc_detectionbenchmarker.cpp"