File indexing completed on 2025-03-09 03:55:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam
0004  *
0005  * Date        : 2010-06-16
0006  * Description : The recognition wrapper
0007  *
0008  * SPDX-FileCopyrightText:      2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
0009  * SPDX-FileCopyrightText:      2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
0010  * SPDX-FileCopyrightText: 2010-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText:      2020 by Nghia Duong <minhnghiaduong997 at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #ifndef FACIAL_RECOGNITION_WRAPPER_H
0018 #define FACIAL_RECOGNITION_WRAPPER_H
0019 
0020 // Qt includes
0021 
0022 #include <QExplicitlySharedDataPointer>
0023 #include <QImage>
0024 #include <QList>
0025 #include <QMultiMap>
0026 #include <QVariant>
0027 
0028 // Local includes
0029 
0030 #include "digikam_export.h"
0031 #include "identity.h"
0032 #include "dataproviders.h"
0033 
0034 namespace Digikam
0035 {
0036 
0037 class DIGIKAM_GUI_EXPORT FacialRecognitionWrapper
0038 {
0039 public:
0040 
0041     explicit FacialRecognitionWrapper();
0042     FacialRecognitionWrapper(const FacialRecognitionWrapper&);
0043 
0044     ~FacialRecognitionWrapper();
0045 
0046 public:
0047 
0048     /**
0049      * Checks the integrity and returns true if everything is fine.
0050      */
0051     bool integrityCheck();
0052 
0053     /**
0054      * Shrinks the database.
0055      */
0056     void vacuum();
0057 
0058 public:
0059 
0060     // --- Backend parameters (facesengine_interface_setup.cpp) --------------------------
0061     /**
0062      * Tunes backend parameters.
0063      * Available parameters:
0064      * "accuracy", synonymous: "threshold", range: 0-1, type: float
0065      * Determines recognition threshold, 0->accept very insecure recognitions, 1-> be very sure about a recognition.
0066      *
0067      * "k-nearest" : limit the number of nearest neighbors for KNN
0068      */
0069     void        setParameter(const QString& parameter, const QVariant& value);
0070     void        setParameters(const QVariantMap& parameters);
0071     QVariantMap parameters()                                                const;
0072 
0073     // --- Identity management (facesengine_interface_identity.cpp) -----------------------------------------
0074 
0075     /// NOTE: For the documentation of standard attributes, see identity.h
0076 
0077     /**
0078      * Returns all identities known to the database
0079      */
0080     QList<Identity> allIdentities()                                         const;
0081     Identity        identity(int id)                                        const;
0082 
0083     /**
0084      * Finds the first identity with matching attribute - value.
0085      * Returns a null identity if no match is found or attribute is empty.
0086      */
0087     Identity findIdentity(const QString& attribute, const QString& value)   const;
0088 
0089     /**
0090      * Finds the identity matching the given attributes.
0091      * Attributes are first checked with knowledge of their meaning.
0092      * Secondly, all unknown attributes are used.
0093      * Returns a null Identity if no match is possible or the map is empty.
0094      */
0095     Identity findIdentity(const QMultiMap<QString, QString>& attributes)         const;
0096 
0097     /**
0098      * Adds a new identity with the specified attributes.
0099      * Please note that a UUID is automatically generated.
0100      */
0101     Identity addIdentity(const QMultiMap<QString, QString>& attributes);
0102 
0103     /**
0104      * This is the debug version of addIdentity, so the identity is only added
0105      * to identityCache, but not into the recognition database.
0106      */
0107     Identity addIdentityDebug(const QMultiMap<QString, QString>& attributes);
0108 
0109     /**
0110      * Adds or sets, resp., the attributes of an identity.
0111      */
0112     void addIdentityAttributes(int id, const QMultiMap<QString, QString>& attributes);
0113     void addIdentityAttribute(int id, const QString& attribute, const QString& value);
0114     void setIdentityAttributes(int id, const QMultiMap<QString, QString>& attributes);
0115 
0116     /**
0117      * Deletes an identity from the database.
0118      */
0119     void deleteIdentity(const Identity& identityToBeDeleted);
0120 
0121     /**
0122      * Deletes a list of identities from the database.
0123      */
0124     void deleteIdentities(QList<Identity> identitiesToBeDeleted);
0125 
0126     // --- Faces Training management (facesengine_interface_training.cpp) ----------------------------------------------------
0127     /**
0128      * Performs training.
0129      * The identities which have new images to be trained are given.
0130      * An empty list means that all identities are checked.
0131      *
0132      * All needed data will be queried from the provider.
0133      *
0134      * An identifier for the current training context is given,
0135      * which can identify the application or group of collections.
0136      * (It is assumed that training from different contexts is based on
0137      * non-overlapping collections of images. Keep it always constant for your app.)
0138      */
0139     void train(const QList<Identity>& identitiesToBeTrained,
0140                TrainingDataProvider* const data,
0141                const QString& trainingContext);
0142     void train(const Identity& identityToBeTrained,
0143                TrainingDataProvider* const data,
0144                const QString& trainingContext);
0145 
0146     /**
0147      * Performs training by using image data directly.
0148      *
0149      * These are convenience functions for simple setups.
0150      * If you want good performance and/or a more versatile implementation, be sure to
0151      * implement your own TrainingDataProvider and use one of the above functions.
0152      */
0153     void train(const Identity& identityToBeTrained,
0154                QImage* image,
0155                const QString& trainingContext);
0156     void train(const Identity& identityToBeTrained,
0157                const QList<QImage*>& images,
0158                const QString& trainingContext);
0159 
0160     /**
0161      * Deletes the training data for all identities,
0162      * leaving the identities as such in the database.
0163      */
0164     void clearAllTraining(const QString& trainingContext = QString());
0165 
0166     /**
0167      * Deletes the training data for the given identity,
0168      * leaving the identity as such in the database.
0169      */
0170     void clearTraining(const QList<Identity>& identitiesToClean,
0171                        const QString& trainingContext = QString());
0172 
0173     // --- Recognition management (facesengine_interface_recognize.cpp) -------------------
0174 
0175     /**
0176      * Returns the recommended size if you want to scale face images for recognition.
0177      * Larger images can be passed, but may be downscaled.
0178      */
0179     // TODO : review to see if this function is necessary
0180     //int recommendedImageSize(const QSize& availableSize = QSize()) const;
0181 
0182     /**
0183      * Performs recognition.
0184      * The face details to be recognized are passed by the provider.
0185      * For each entry in the provider, in 1-to-1 mapping,
0186      * a recognized identity or the null identity is returned.
0187      */
0188     QList<Identity> recognizeFaces(ImageListProvider* const images);
0189     QList<Identity> recognizeFaces(const QList<QImage*>& images);
0190     Identity        recognizeFace(QImage* const image);
0191 
0192 private:
0193 
0194     // Disable
0195     FacialRecognitionWrapper& operator=(const FacialRecognitionWrapper&) = delete;
0196 
0197 private:
0198 
0199     class Private;
0200     static Private* d;
0201 };
0202 
0203 } // namespace Digikam
0204 
0205 #endif // FACIAL_RECOGNITION_WRAPPER_H