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 | /* ============================================================
*
* This file is a part of digiKam
*
* Date : 2010-06-16
* Description : Identity management of recognition wrapper
*
* SPDX-FileCopyrightText: 2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
* SPDX-FileCopyrightText: 2010-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
* SPDX-FileCopyrightText: 2020 by Nghia Duong <minhnghiaduong997 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 "facialrecognition_wrapper_p.h"
namespace Digikam
{
/**
* NOTE: Takes care that there may be multiple values of attribute in identity's attributes.
*/
bool FacialRecognitionWrapper::Private::identityContains(const Identity& identity,
const QString& attribute,
const QString& value)
{
const QMultiMap<QString, QString> map = identity.attributesMap();
QMultiMap<QString, QString>::const_iterator it = map.constFind(attribute);
for (; (it != map.constEnd()) && (it.key() == attribute) ; ++it)
{
if (it.value() == value)
{
return true;
}
}
return false;
}
Identity FacialRecognitionWrapper::Private::findByAttribute(const QString& attribute,
const QString& value) const
{
for (const Identity& identity : std::as_const(identityCache))
{
if (identityContains(identity, attribute, value))
{<--- Consider using std::find_if algorithm instead of a raw loop.
// cppcheck-suppress useStlAlgorithm
return identity;
}
}
return Identity();
}
/**
* NOTE: Takes care that there may be multiple values of attribute in valueMap.
*/
Identity FacialRecognitionWrapper::Private::findByAttributes(const QString& attribute,
const QMultiMap<QString, QString>& valueMap) const
{
QMultiMap<QString, QString>::const_iterator it = valueMap.find(attribute);
for (; (it != valueMap.end()) && (it.key() == attribute) ; ++it)
{
for (const Identity& identity : std::as_const(identityCache))
{
if (identityContains(identity, attribute, it.value()))
{
// cppcheck-suppress useStlAlgorithm
return identity;
}
}
}
return Identity();
}
// -----------------------------------------------------------------------
QList<Identity> FacialRecognitionWrapper::allIdentities() const
{
if (!d || !d->dbAvailable)
{
return QList<Identity>();
}
d->trainingLock.lockForRead();
QList<Identity> result = (d->identityCache.values());
d->trainingLock.unlock();
return result;
}
Identity FacialRecognitionWrapper::identity(int id) const
{
if (!d || !d->dbAvailable)
{
return Identity();
}
d->trainingLock.lockForRead();
Identity result = d->identityCache.value(id);
d->trainingLock.unlock();
return result;
}
Identity FacialRecognitionWrapper::findIdentity(const QString& attribute, const QString& value) const
{
if (!d || !d->dbAvailable || attribute.isEmpty())
{
return Identity();
}
d->trainingLock.lockForRead();
Identity result = d->findByAttribute(attribute, value);
d->trainingLock.unlock();
return result;
}
Identity FacialRecognitionWrapper::findIdentity(const QMultiMap<QString, QString>& attributes) const
{
if (!d || !d->dbAvailable || attributes.isEmpty())
{
return Identity();
}
d->trainingLock.lockForRead();
Identity match;
// First and foremost, UUID.
QString uuid = attributes.value(QLatin1String("uuid"));
match = d->findByAttribute(QLatin1String("uuid"), uuid);
if (!match.isNull())
{
d->trainingLock.unlock();
return match;
}
// A negative UUID match, with a given UUID, precludes any further search.
if (!uuid.isNull())
{
d->trainingLock.unlock();
return Identity();
}
// Full name.
match = d->findByAttributes(QLatin1String("fullName"), attributes);
if (!match.isNull())
{
d->trainingLock.unlock();
return match;
}
// Name.
match = d->findByAttributes(QLatin1String("name"), attributes);
if (!match.isNull())
{
d->trainingLock.unlock();
return match;
}
QMultiMap<QString, QString>::const_iterator it;
for (it = attributes.begin() ; it != attributes.end() ; ++it)
{
if (
(it.key() == QLatin1String("uuid")) ||
(it.key() == QLatin1String("fullName")) ||
(it.key() == QLatin1String("name"))
)
{
continue;
}
match = d->findByAttribute(it.key(), it.value());
if (!match.isNull())
{
d->trainingLock.unlock();
return match;
}
}
d->trainingLock.unlock();
return Identity();
}
Identity FacialRecognitionWrapper::addIdentity(const QMultiMap<QString, QString>& attributes)
{
if (!d || !d->dbAvailable)
{
return Identity();
}
if (attributes.contains(QLatin1String("uuid")))
{
d->trainingLock.lockForRead();
Identity matchByUuid = findIdentity(QLatin1String("uuid"), attributes.value(QLatin1String("uuid")));
d->trainingLock.unlock();
if (!matchByUuid.isNull())
{
// This situation is not well defined.
qCDebug(DIGIKAM_FACESENGINE_LOG) << "Called addIdentity with a given UUID, and there is such a UUID already in the database."
<< "The existing identity is returned without adjusting properties!";
return matchByUuid;
}
}
Identity identity;<--- Shadow variable
{
FaceDbOperationGroup group;
d->trainingLock.lockForWrite();
int id = FaceDbAccess().db()->addIdentity();
identity.setId(id);
identity.setAttributesMap(attributes);
identity.setAttribute(QLatin1String("uuid"), QUuid::createUuid().toString());
FaceDbAccess().db()->updateIdentity(identity);
d->trainingLock.unlock();
}
d->identityCache[identity.id()] = identity;
return identity;
}
Identity FacialRecognitionWrapper::addIdentityDebug(const QMultiMap<QString, QString>& attributes)
{
Identity identity;<--- Shadow variable
{
identity.setId(attributes.value(QLatin1String("name")).toInt());
identity.setAttributesMap(attributes);
identity.setAttribute(QLatin1String("uuid"), QUuid::createUuid().toString());
}
d->identityCache[identity.id()] = identity;
d->trainingLock.unlock();
return identity;
}
void FacialRecognitionWrapper::addIdentityAttributes(int id, const QMultiMap<QString, QString>& attributes)
{
if (!d || !d->dbAvailable)
{
return;
}
d->trainingLock.lockForWrite();
QHash<int, Identity>::iterator it = d->identityCache.find(id);
if (it != d->identityCache.end())
{
QMultiMap<QString, QString> map = it->attributesMap();
map.unite(attributes);
it->setAttributesMap(map);
FaceDbAccess().db()->updateIdentity(*it);
}
d->trainingLock.unlock();
}
void FacialRecognitionWrapper::addIdentityAttribute(int id, const QString& attribute, const QString& value)
{
if (!d || !d->dbAvailable)
{
return;
}
d->trainingLock.lockForWrite();
QHash<int, Identity>::iterator it = d->identityCache.find(id);
if (it != d->identityCache.end())
{
QMultiMap<QString, QString> map = it->attributesMap();
map.insert(attribute, value);
it->setAttributesMap(map);
FaceDbAccess().db()->updateIdentity(*it);
}
d->trainingLock.unlock();
}
void FacialRecognitionWrapper::setIdentityAttributes(int id, const QMultiMap<QString, QString>& attributes)
{
if (!d || !d->dbAvailable)
{
return;
}
d->trainingLock.lockForWrite();
QHash<int, Identity>::iterator it = d->identityCache.find(id);
if (it != d->identityCache.end())
{
it->setAttributesMap(attributes);
FaceDbAccess().db()->updateIdentity(*it);
}
d->trainingLock.unlock();
}
void FacialRecognitionWrapper::deleteIdentity(const Identity& identityToBeDeleted)
{
if (!d || !d->dbAvailable || identityToBeDeleted.isNull())
{
return;
}
d->trainingLock.lockForWrite();
FaceDbAccess().db()->deleteIdentity(identityToBeDeleted.id());
d->identityCache.remove(identityToBeDeleted.id());
d->trainingLock.unlock();
}
void FacialRecognitionWrapper::deleteIdentities(QList<Identity> identitiesToBeDeleted)
{
QList<Identity>::iterator identity = identitiesToBeDeleted.begin();<--- Shadow variable
while (identity != identitiesToBeDeleted.end())
{
deleteIdentity(*identity);
identity = identitiesToBeDeleted.erase(identity);
}
}
} // namespace Digikam
|