| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2010-08-08 |
| 7 |
* Description : FaceEngine database interface allowing easy manipulation of face tags |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2010-2011 by Aditya Bhatt <adityabhatt1991 at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2010-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 11 |
* SPDX-FileCopyrightText: 2012-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 12 |
* SPDX-FileCopyrightText: 2024-2025 by Michael Miller <michael underscore miller at msn dot com> |
| 13 |
* |
| 14 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 15 |
* |
| 16 |
* ============================================================ */ |
| 17 |
|
| 18 |
#include "faceutils.h" |
| 19 |
|
| 20 |
// Qt includes |
| 21 |
|
| 22 |
#include <QImage> |
| 23 |
|
| 24 |
// Local includes |
| 25 |
|
| 26 |
#include "digikam_debug.h" |
| 27 |
#include "coredbaccess.h" |
| 28 |
#include "coredbconstants.h" |
| 29 |
#include "coredboperationgroup.h" |
| 30 |
#include "coredb.h" |
| 31 |
#include "dimg.h" |
| 32 |
#include "facetags.h" |
| 33 |
#include "itemtagpair.h" |
| 34 |
#include "tagproperties.h" |
| 35 |
#include "tagscache.h" |
| 36 |
#include "tagregion.h" |
| 37 |
#include "thumbnailloadthread.h" |
| 38 |
#include "albummanager.h" |
| 39 |
#include "identityprovider.h" |
| 40 |
|
| 41 |
namespace Digikam |
| 42 |
{ |
| 43 |
|
| 44 |
FaceUtils::FaceUtils(QObject* const parent) |
| 45 |
: QObject(parent) |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
// --- Mark for scanning and training --- |
| 50 |
|
| 51 |
bool FaceUtils::hasBeenScanned(qlonglong imageid) const |
| 52 |
{ |
| 53 |
return hasBeenScanned(ItemInfo(imageid)); |
| 54 |
} |
| 55 |
|
| 56 |
bool FaceUtils::hasBeenScanned(const ItemInfo& info) const |
| 57 |
{ |
| 58 |
return info.tagIds().contains(FaceTags::scannedForFacesTagId()); |
| 59 |
} |
| 60 |
|
| 61 |
bool FaceUtils::normalTagChanged() const |
| 62 |
{ |
| 63 |
return m_normalTagChanged; |
| 64 |
} |
| 65 |
|
| 66 |
void FaceUtils::markAsScanned(qlonglong imageid, bool hasBeenScanned) const |
| 67 |
{ |
| 68 |
markAsScanned(ItemInfo(imageid), hasBeenScanned); |
| 69 |
} |
| 70 |
|
| 71 |
void FaceUtils::markAsScanned(const ItemInfo& info, bool hasBeenScanned) const |
| 72 |
{ |
| 73 |
if (hasBeenScanned) |
| 74 |
{ |
| 75 |
ItemInfo(info).setTag(FaceTags::scannedForFacesTagId()); |
| 76 |
} |
| 77 |
|
| 78 |
else |
| 79 |
{ |
| 80 |
ItemInfo(info).removeTag(FaceTags::scannedForFacesTagId()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Convert between FacesEngine results and FaceTagsIface. |
| 86 |
*/ |
| 87 |
QList<FaceTagsIface> FaceUtils::toFaceTagsIfaces(qlonglong imageid, |
| 88 |
const QList<QRectF>& detectedFaces, |
| 89 |
const QList<Identity>& recognitionResults, |
| 90 |
const QSize& fullSize) const |
| 91 |
{ |
| 92 |
QList<FaceTagsIface> faces; |
| 93 |
|
| 94 |
for (int i = 0 ; i < detectedFaces.size() ; ++i) |
| 95 |
{ |
| 96 |
Identity identity; |
| 97 |
|
| 98 |
if (!recognitionResults.isEmpty()) |
| 99 |
{ |
| 100 |
identity = recognitionResults[i]; |
| 101 |
} |
| 102 |
|
| 103 |
// We'll get the unknownPersonTagId if the identity is null. |
| 104 |
|
| 105 |
int tagId = FaceTags::getOrCreateTagForIdentity(identity.attributesMap()); |
| 106 |
QRect fullSizeRect = TagRegion::relativeToAbsolute(detectedFaces[i], fullSize); |
| 107 |
FaceTagsIface::Type type = (identity.isNull() ? FaceTagsIface::UnknownName : FaceTagsIface::UnconfirmedName); |
| 108 |
|
| 109 |
if (!tagId || !fullSizeRect.isValid()) |
| 110 |
{ |
| 111 |
faces << FaceTagsIface(); |
| 112 |
continue; |
| 113 |
} |
| 114 |
/* |
| 115 |
qCDebug(DIGIKAM_GENERAL_LOG) << "New Entry" << fullSizeRect << tagId; |
| 116 |
*/ |
| 117 |
faces << FaceTagsIface(type, imageid, tagId, TagRegion(fullSizeRect)); |
| 118 |
} |
| 119 |
|
| 120 |
return faces; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Images in faces and thumbnails. |
| 125 |
*/ |
| 126 |
void FaceUtils::storeThumbnails(ThumbnailLoadThread* const thread, |
| 127 |
const QString& filePath, |
| 128 |
const QList<FaceTagsIface>& databaseFaces, |
| 129 |
const DImg& image) |
| 130 |
{ |
| 131 |
for (const FaceTagsIface& face : std::as_const(databaseFaces)) |
| 132 |
{ |
| 133 |
QList<QRect> rects; |
| 134 |
QRect orgRect = face.region().toRect(); |
| 135 |
rects << orgRect; |
| 136 |
rects << faceRectToDisplayRect(orgRect); |
| 137 |
|
| 138 |
for (const QRect& rect : std::as_const(rects)) |
| 139 |
{ |
| 140 |
QRect mapped = TagRegion::mapFromOriginalSize(image, rect); |
| 141 |
QImage detail = image.copyQImage(mapped); |
| 142 |
thread->storeDetailThumbnail(filePath, rect, detail, true); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
// --- Face detection: merging results --- |
| 148 |
|
| 149 |
QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid, |
| 150 |
const QList<QRectF>& detectedFaces, |
| 151 |
const QList<Identity>& recognitionResults, |
| 152 |
const QSize& fullSize) |
| 153 |
{ |
| 154 |
// Build list of new entries. |
| 155 |
|
| 156 |
QList<FaceTagsIface> newFaces = toFaceTagsIfaces(imageid, detectedFaces, recognitionResults, fullSize); |
| 157 |
|
| 158 |
if (newFaces.isEmpty()) |
| 159 |
{ |
| 160 |
return newFaces; |
| 161 |
} |
| 162 |
|
| 163 |
return writeUnconfirmedResults(imageid, newFaces); |
| 164 |
} |
| 165 |
|
| 166 |
QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid, |
| 167 |
QList<FaceTagsIface>& newFaces, |
| 168 |
const QList<Identity>& recognitionResults) |
| 169 |
{ |
| 170 |
for(int i = 0 ; i < newFaces.size() ; ++i) |
| 171 |
{ |
| 172 |
newFaces[i].setTagId(FaceTags::getOrCreateTagForIdentity(recognitionResults.at(i).attributesMap())); |
| 173 |
} |
| 174 |
return writeUnconfirmedResults(imageid, newFaces); |
| 175 |
} |
| 176 |
|
| 177 |
QList<FaceTagsIface> FaceUtils::writeUnconfirmedResults(qlonglong imageid, |
| 178 |
QList<FaceTagsIface>& newFaces) |
| 179 |
{ |
| 180 |
if (newFaces.isEmpty()) |
| 181 |
{ |
| 182 |
return newFaces; |
| 183 |
} |
| 184 |
|
| 185 |
// List of existing entries. |
| 186 |
|
| 187 |
QList<FaceTagsIface> currentFaces = databaseFaces(imageid); |
| 188 |
|
| 189 |
// Merge new with existing entries. |
| 190 |
|
| 191 |
for (int i = 0 ; i < newFaces.size() ; ++i) |
| 192 |
{ |
| 193 |
FaceTagsIface& newFace = newFaces[i]; |
| 194 |
QList<FaceTagsIface> overlappingEntries; |
| 195 |
|
| 196 |
for (const FaceTagsIface& oldFace : std::as_const(currentFaces)) |
| 197 |
{ |
| 198 |
double minOverlap = (oldFace.isConfirmedName() ? 0.25 : 0.5); |
| 199 |
|
| 200 |
if (oldFace.region().intersects(newFace.region(), minOverlap)) |
| 201 |
{ |
| 202 |
overlappingEntries << oldFace; |
| 203 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Entry" << oldFace.region() << oldFace.tagId() |
| 204 |
<< "overlaps" << newFace.region() << newFace.tagId() << ", skipping"; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// The purpose if the next scope is to merge entries: |
| 209 |
// A confirmed face will never be overwritten. |
| 210 |
// If a name is set to an old face, it will only be replaced by a new face with a name. |
| 211 |
|
| 212 |
if (!overlappingEntries.isEmpty()) |
| 213 |
{ |
| 214 |
if (newFace.isUnknownName()) |
| 215 |
{ |
| 216 |
// We have no name in the new face. Do we have one in the old faces? |
| 217 |
|
| 218 |
for (int j = 0 ; j < overlappingEntries.size() ; ++j) |
| 219 |
{ |
| 220 |
const FaceTagsIface& oldFace = overlappingEntries.at(j); |
| 221 |
|
| 222 |
if (oldFace.isUnknownName()) |
| 223 |
{ |
| 224 |
// Remove old face. |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
// Skip new entry if any overlapping face has a name, and we do not. |
| 229 |
|
| 230 |
newFace = FaceTagsIface(); |
| 231 |
break; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
// We have a name in the new face. Do we have names in overlapping faces? |
| 238 |
|
| 239 |
for (int j = 0 ; j < overlappingEntries.size() ; ++j) |
| 240 |
{ |
| 241 |
const FaceTagsIface& oldFace = overlappingEntries[j]; |
| 242 |
|
| 243 |
if (oldFace.isUnknownName()) |
| 244 |
{ |
| 245 |
// Remove old face. |
| 246 |
} |
| 247 |
else if (oldFace.isUnconfirmedName()) |
| 248 |
{ |
| 249 |
if (oldFace.tagId() == newFace.tagId()) |
| 250 |
{ |
| 251 |
// Remove smaller face. |
| 252 |
|
| 253 |
if (oldFace.region().intersects(newFace.region(), 1)) |
| 254 |
{ |
| 255 |
newFace = FaceTagsIface(); |
| 256 |
break; |
| 257 |
} |
| 258 |
|
| 259 |
// Else remove old face. |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
// Assume new recognition is more trained, remove older face. |
| 264 |
} |
| 265 |
} |
| 266 |
else if (oldFace.isConfirmedName()) |
| 267 |
{ |
| 268 |
// Skip new entry, confirmed has of course priority. |
| 269 |
|
| 270 |
newFace = FaceTagsIface(); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
// If we did not decide to skip this face, add is to the db now. |
| 277 |
|
| 278 |
if (!newFace.isNull()) |
| 279 |
{ |
| 280 |
// List will contain all old entries that should still be removed. |
| 281 |
|
| 282 |
removeFaces(overlappingEntries); |
| 283 |
|
| 284 |
ItemTagPair pair(imageid, newFace.tagId()); |
| 285 |
|
| 286 |
// UnconfirmedName and UnknownName have the same attribute. |
| 287 |
|
| 288 |
addFaceAndTag(pair, newFace, FaceTagsIface::attributesForFlags(FaceTagsIface::UnconfirmedName), false); |
| 289 |
|
| 290 |
// If the face is unconfirmed and the tag is not the unknown person tag, set the unconfirmed person property. |
| 291 |
|
| 292 |
if (newFace.isUnconfirmedType() && !FaceTags::isTheUnknownPerson(newFace.tagId())) |
| 293 |
{ |
| 294 |
ItemTagPair unconfirmedPair(imageid, FaceTags::unconfirmedPersonTagId()); |
| 295 |
unconfirmedPair.addProperty(ImageTagPropertyName::autodetectedPerson(), |
| 296 |
newFace.getAutodetectedPersonString()); |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return newFaces; |
| 302 |
} |
| 303 |
/* |
| 304 |
Identity FaceUtils::identityForTag(int tagId, FacialRecognitionWrapper& recognizer) const |
| 305 |
*/ |
| 306 |
Identity FaceUtils::identityForTag(int tagId) const |
| 307 |
{ |
| 308 |
QMultiMap<QString, QString> attributes = FaceTags::identityAttributes(tagId); |
| 309 |
Identity identity = IdentityProvider::instance()->findIdentity(attributes); |
| 310 |
|
| 311 |
if (!identity.isNull()) |
| 312 |
{ |
| 313 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Found FacesEngine identity" << identity.id() << "for tag" << tagId; |
| 314 |
return identity; |
| 315 |
} |
| 316 |
|
| 317 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Adding new FacesEngine identity with attributes" << attributes; |
| 318 |
identity = IdentityProvider::instance()->addIdentity(attributes); |
| 319 |
|
| 320 |
FaceTags::applyTagIdentityMapping(tagId, identity.attributesMap()); |
| 321 |
|
| 322 |
return identity; |
| 323 |
} |
| 324 |
|
| 325 |
int FaceUtils::tagForIdentity(const Identity& identity) const |
| 326 |
{ |
| 327 |
return FaceTags::getOrCreateTagForIdentity(identity.attributesMap()); |
| 328 |
} |
| 329 |
|
| 330 |
// --- Editing normal tags, reimplemented with MetadataHub --- |
| 331 |
|
| 332 |
void FaceUtils::addNormalTag(qlonglong imageId, int tagId) |
| 333 |
{ |
| 334 |
FaceTagsEditor::addNormalTag(imageId, tagId); |
| 335 |
|
| 336 |
m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tagId); |
| 337 |
} |
| 338 |
|
| 339 |
void FaceUtils::removeNormalTag(qlonglong imageId, int tagId) |
| 340 |
{ |
| 341 |
FaceTagsEditor::removeNormalTag(imageId, tagId); |
| 342 |
|
| 343 |
m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tagId); |
| 344 |
|
| 345 |
if (!FaceTags::isSystemPersonTagId(tagId)) |
| 346 |
{ |
| 347 |
qlonglong faceItemId = CoreDbAccess().db()->getFirstItemWithFaceTag(tagId); |
| 348 |
|
| 349 |
/** |
| 350 |
* If the face just removed was the final face |
| 351 |
* associated with that Tag, reset Tag Icon. |
| 352 |
*/ |
| 353 |
if (faceItemId == -1) |
| 354 |
{ |
| 355 |
TAlbum* const album = AlbumManager::instance()->findTAlbum(tagId); |
| 356 |
|
| 357 |
if (album && (album->iconId() != 0)) |
| 358 |
{ |
| 359 |
QString err; |
| 360 |
|
| 361 |
if (!AlbumManager::instance()->updateTAlbumIcon(album, QString(), |
| 362 |
0, err)) |
| 363 |
{ |
| 364 |
qCDebug(DIGIKAM_GENERAL_LOG) << err; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
void FaceUtils::removeNormalTags(qlonglong imageId, const QList<int>& tagIds) |
| 372 |
{ |
| 373 |
FaceTagsEditor::removeNormalTags(imageId, tagIds); |
| 374 |
|
| 375 |
for (int tid : tagIds) |
| 376 |
{ |
| 377 |
m_normalTagChanged |= !FaceTags::isSystemPersonTagId(tid); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
// --- Utilities --- |
| 382 |
|
| 383 |
QRect FaceUtils::faceRectToDisplayRect(const QRect& rect) |
| 384 |
{ |
| 385 |
/** |
| 386 |
* Do not change that value unless you know what you do. |
| 387 |
* There are a lot of pregenerated thumbnails in user's databases, |
| 388 |
* expensive to regenerate, depending on this very value. |
| 389 |
*/ |
| 390 |
|
| 391 |
int margin = qMax(rect.width(), rect.height()); |
| 392 |
margin /= 10; |
| 393 |
|
| 394 |
return rect.adjusted(-margin, -margin, margin, margin); |
| 395 |
} |
| 396 |
|
| 397 |
} // Namespace Digikam |
| 398 |
|
| 399 |
#include "moc_faceutils.cpp" |