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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2010-08-08
 * Description : FaceEngine database interface allowing easy manipulation of face tags
 *
 * SPDX-FileCopyrightText: 2010-2011 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
 * SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * SPDX-FileCopyrightText: 2012-2026 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2024-2025 by Michael Miller <michael underscore miller at msn dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "faceutils.h"

// Qt includes

#include <QImage>

// Local includes

#include "digikam_debug.h"
#include "coredbaccess.h"
#include "coredbconstants.h"
#include "coredboperationgroup.h"
#include "coredb.h"
#include "dimg.h"
#include "facetags.h"
#include "itemtagpair.h"
#include "tagproperties.h"
#include "tagscache.h"
#include "tagregion.h"
#include "thumbnailloadthread.h"
#include "albummanager.h"
#include "identityprovider.h"

namespace Digikam
{

FaceUtils::FaceUtils(QObject* const parent)
    : QObject(parent)
{
}

// --- Mark for scanning and training ---

bool FaceUtils::hasBeenScanned(qlonglong imageid) const
{
    return hasBeenScanned(ItemInfo(imageid));
}

bool FaceUtils::hasBeenScanned(const ItemInfo& info) const
{
    return info.tagIds().contains(FaceTags::scannedForFacesTagId());
}

bool FaceUtils::normalTagChanged() const
{
    return m_normalTagChanged;
}

void FaceUtils::markAsScanned(qlonglong imageid, bool hasBeenScanned) const<--- Shadow argument
{
    markAsScanned(ItemInfo(imageid), hasBeenScanned);
}

void FaceUtils::markAsScanned(const ItemInfo& info, bool hasBeenScanned) const<--- Shadow argument
{
    if (hasBeenScanned)
    {
        ItemInfo(info).setTag(FaceTags::scannedForFacesTagId());
    }

    else
    {
        ItemInfo(info).removeTag(FaceTags::scannedForFacesTagId());
    }
}

/**
 * Convert between FacesEngine results and FaceTagsIface.
 */
QList<FaceTagsIface> FaceUtils::toFaceTagsIfaces(qlonglong imageid,
                                                 const QList<QRectF>& detectedFaces,
                                                 const QList<Identity>& recognitionResults,
                                                 const QSize& fullSize) const
{
    QList<FaceTagsIface> faces;

    for (int i = 0 ; i < detectedFaces.size() ; ++i)
    {
        Identity identity;

        if (!recognitionResults.isEmpty())
        {
            identity = recognitionResults[i];
        }

        // We'll get the unknownPersonTagId if the identity is null.

        int tagId                = FaceTags::getOrCreateTagForIdentity(identity.attributesMap());
        QRect fullSizeRect       = TagRegion::relativeToAbsolute(detectedFaces[i], fullSize);
        FaceTagsIface::Type type = (identity.isNull() ? FaceTagsIface::UnknownName : FaceTagsIface::UnconfirmedName);

        if (!tagId || !fullSizeRect.isValid())
        {
            faces << FaceTagsIface();
            continue;
        }
/*
        qCDebug(DIGIKAM_GENERAL_LOG) << "New Entry" << fullSizeRect << tagId;
*/
        faces << FaceTagsIface(type, imageid, tagId, TagRegion(fullSizeRect));
    }

    return faces;
}

/**
 * Images in faces and thumbnails.
 */
void FaceUtils::storeThumbnails(ThumbnailLoadThread* const thread,
                                const QString& filePath,
                                const QList<FaceTagsIface>& databaseFaces,
                                const DImg& image)
{
    for (const FaceTagsIface& face : std::as_const(databaseFaces))
    {
        QList<QRect> rects;
        QRect orgRect = face.region().toRect();
        rects << orgRect;
        rects << faceRectToDisplayRect(orgRect);

        for (const QRect& rect : std::as_const(rects))
        {
            QRect mapped  = TagRegion::mapFromOriginalSize(image, rect);
            QImage detail = image.copyQImage(mapped);
            thread->storeDetailThumbnail(filePath, rect, detail, true);
        }
    }
}

// --- Face detection: merging results ---

QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid,
                                                        const QList<QRectF>& detectedFaces,
                                                        const QList<Identity>& recognitionResults,
                                                        const QSize& fullSize)
{
    // Build list of new entries.

    QList<FaceTagsIface> newFaces = toFaceTagsIfaces(imageid, detectedFaces, recognitionResults, fullSize);

    if (newFaces.isEmpty())
    {
        return newFaces;
    }

    return writeUnconfirmedResults(imageid, newFaces);
}

QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid,
                                                        QList<FaceTagsIface>& newFaces,
                                                        const QList<Identity>& recognitionResults)
{
    for(int i = 0 ; i < newFaces.size() ; ++i)
    {
        newFaces[i].setTagId(FaceTags::getOrCreateTagForIdentity(recognitionResults.at(i).attributesMap()));
    }
    return writeUnconfirmedResults(imageid, newFaces);
}

QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid,
                                                        QList<FaceTagsIface>& newFaces)
{
    if (newFaces.isEmpty())
    {
        return newFaces;
    }

    // List of existing entries.

    QList<FaceTagsIface> currentFaces = databaseFaces(imageid);

    // Merge new with existing entries.

    for (int i = 0 ; i < newFaces.size() ; ++i)
    {
        FaceTagsIface& newFace = newFaces[i];
        QList<FaceTagsIface> overlappingEntries;

        for (const FaceTagsIface& oldFace : std::as_const(currentFaces))
        {
            double minOverlap = (oldFace.isConfirmedName() ? 0.25 : 0.5);

            if (oldFace.region().intersects(newFace.region(), minOverlap))
            {
                overlappingEntries << oldFace;
                qCDebug(DIGIKAM_GENERAL_LOG) << "Entry" << oldFace.region() << oldFace.tagId()
                                             << "overlaps" << newFace.region() << newFace.tagId() << ", skipping";
            }
        }

        // The purpose if the next scope is to merge entries:
        // A confirmed face will never be overwritten.
        // If a name is set to an old face, it will only be replaced by a new face with a name.

        if (!overlappingEntries.isEmpty())
        {
            if (newFace.isUnknownName())
            {
                // We have no name in the new face. Do we have one in the old faces?

                for (int j = 0 ; j < overlappingEntries.size() ; ++j)
                {
                    const FaceTagsIface& oldFace = overlappingEntries.at(j);

                    if (oldFace.isUnknownName())
                    {
                        // Remove old face.
                    }
                    else
                    {
                        // Skip new entry if any overlapping face has a name, and we do not.

                        newFace = FaceTagsIface();
                        break;
                    }
                }
            }
            else
            {
                // We have a name in the new face. Do we have names in overlapping faces?

                for (int j = 0 ; j < overlappingEntries.size() ; ++j)
                {
                    const FaceTagsIface& oldFace = overlappingEntries[j];

                    if      (oldFace.isUnknownName())
                    {
                        // Remove old face.
                    }
                    else if (oldFace.isUnconfirmedName())
                    {
                        if (oldFace.tagId() == newFace.tagId())
                        {
                            // Remove smaller face.

                            if (oldFace.region().intersects(newFace.region(), 1))
                            {
                                newFace = FaceTagsIface();
                                break;
                            }

                            // Else remove old face.
                        }
                        else
                        {
                            // Assume new recognition is more trained, remove older face.
                        }
                    }
                    else if (oldFace.isConfirmedName())
                    {
                        // Skip new entry, confirmed has of course priority.

                        newFace = FaceTagsIface();
                    }
                }
            }
        }

        // If we did not decide to skip this face, add is to the db now.

        if (!newFace.isNull())
        {
            // List will contain all old entries that should still be removed.

            removeFaces(overlappingEntries);

            ItemTagPair pair(imageid, newFace.tagId());

            // UnconfirmedName and UnknownName have the same attribute.

            addFaceAndTag(pair, newFace, FaceTagsIface::attributesForFlags(FaceTagsIface::UnconfirmedName), false);

            // If the face is unconfirmed and the tag is not the unknown person tag, set the unconfirmed person property.

            if (newFace.isUnconfirmedType() && !FaceTags::isTheUnknownPerson(newFace.tagId()))
            {
                ItemTagPair unconfirmedPair(imageid, FaceTags::unconfirmedPersonTagId());
                unconfirmedPair.addProperty(ImageTagPropertyName::autodetectedPerson(),
                                            newFace.getAutodetectedPersonString());
            }
        }
    }

    return newFaces;
}
/*
Identity FaceUtils::identityForTag(int tagId, FacialRecognitionWrapper& recognizer) const
*/
Identity FaceUtils::identityForTag(int tagId) const
{
    QMultiMap<QString, QString> attributes = FaceTags::identityAttributes(tagId);
    Identity identity                      = IdentityProvider::instance()->findIdentity(attributes);

    if (!identity.isNull())
    {
        qCDebug(DIGIKAM_GENERAL_LOG) << "Found FacesEngine identity" << identity.id() << "for tag" << tagId;
        return identity;
    }

    qCDebug(DIGIKAM_GENERAL_LOG) << "Adding new FacesEngine identity with attributes" << attributes;
    identity                               = IdentityProvider::instance()->addIdentity(attributes);

    FaceTags::applyTagIdentityMapping(tagId, identity.attributesMap());

    return identity;
}

int FaceUtils::tagForIdentity(const Identity& identity) const
{
    return FaceTags::getOrCreateTagForIdentity(identity.attributesMap());
}

// --- Editing normal tags, reimplemented with MetadataHub ---

void FaceUtils::addNormalTag(qlonglong imageId, int tagId)
{
    FaceTagsEditor::addNormalTag(imageId, tagId);

    m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tagId);
}

void FaceUtils::removeNormalTag(qlonglong imageId, int tagId)
{
    FaceTagsEditor::removeNormalTag(imageId, tagId);

    m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tagId);

    if (!FaceTags::isSystemPersonTagId(tagId))
    {
        qlonglong faceItemId = CoreDbAccess().db()->getFirstItemWithFaceTag(tagId);

        /**
         * If the face just removed was the final face
         * associated with that Tag, reset Tag Icon.
         */
        if (faceItemId == -1)
        {
            TAlbum* const album = AlbumManager::instance()->findTAlbum(tagId);

            if (album && (album->iconId() != 0))
            {
                QString err;

                if (!AlbumManager::instance()->updateTAlbumIcon(album, QString(),
                                                                0, err))
                {
                    qCDebug(DIGIKAM_GENERAL_LOG) << err;
                }
            }
        }
    }
}

void FaceUtils::removeNormalTags(qlonglong imageId, const QList<int>& tagIds)
{
    FaceTagsEditor::removeNormalTags(imageId, tagIds);

    for (int tid : tagIds)
    {
        m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tid);
    }
}

// --- Utilities ---

QRect FaceUtils::faceRectToDisplayRect(const QRect& rect)
{
    /**
     * Do not change that value unless you know what you do.
     * There are a lot of pregenerated thumbnails in user's databases,
     * expensive to regenerate, depending on this very value.
     */

    int margin = qMax(rect.width(), rect.height());
    margin    /= 10;

    return rect.adjusted(-margin, -margin, margin, margin);
}

} // Namespace Digikam

#include "moc_faceutils.cpp"