File indexing completed on 2025-01-05 03:58:08

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2014-06-16
0007  * Description : CLI test program for training database
0008  *
0009  * SPDX-FileCopyrightText: 2014-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 // Qt includes
0016 
0017 #include <QCoreApplication>
0018 #include <QDir>
0019 #include <QImage>
0020 #include <QThreadPool>
0021 #include <QRunnable>
0022 
0023 // Local includes
0024 
0025 #include "digikam_debug.h"
0026 #include "facialrecognition_wrapper.h"
0027 #include "coredbaccess.h"
0028 #include "dbengineparameters.h"
0029 
0030 using namespace Digikam;
0031 
0032 // --------------------------------------------------------------------------------------------------
0033 
0034 const int firstMultiplier  = 20;
0035 const int secondMultiplier = 20;
0036 
0037 class Q_DECL_HIDDEN Runnable : public QRunnable
0038 {
0039 public:
0040 
0041     Runnable(int number, const FacialRecognitionWrapper& db)
0042         : number(number),
0043           db    (db)
0044     {
0045     }
0046 
0047     void run() override
0048     {
0049         QImage* const image = new QImage(256, 256, QImage::Format_ARGB32);
0050         image->fill(Qt::red);
0051 
0052         Identity identity;
0053 
0054         // Populate database.
0055 
0056         for (int i = number * secondMultiplier ; i < (number * secondMultiplier + secondMultiplier) ; ++i)
0057         {
0058             QString name                            = QString::fromLatin1("face%1").arg(i);
0059             qCDebug(DIGIKAM_TESTS_LOG) << "Record Identity " << name << " to DB";
0060             QMultiMap<QString, QString> attributes;
0061             attributes.insert(QString::fromLatin1("name"), name);
0062             identity                                = db.addIdentity(attributes);
0063             db.train(identity, image, QString::fromLatin1("test application"));
0064         }
0065 
0066         qCDebug(DIGIKAM_TESTS_LOG) << "Trained group" << number;
0067 
0068         // Check records in database.
0069 
0070         for (int i = (number * secondMultiplier) ; i < (number * secondMultiplier + secondMultiplier) ; ++i)
0071         {
0072             QString name = QString::fromLatin1("face%1").arg(i);
0073             identity     = db.findIdentity(QString::fromLatin1("name"), name);
0074 
0075             if (!identity.isNull())
0076             {
0077                 qCDebug(DIGIKAM_TESTS_LOG) << "Identity " << name << " is present in DB";
0078             }
0079             else
0080             {
0081                 qCDebug(DIGIKAM_TESTS_LOG) << "Identity " << name << " is absent in DB";
0082             }
0083         }
0084     }
0085 
0086 public:
0087 
0088     const int                number;
0089     FacialRecognitionWrapper db;
0090 
0091 private:
0092 
0093     Q_DISABLE_COPY(Runnable)
0094 
0095 };
0096 
0097 int main(int argc, char** argv)
0098 {
0099     QCoreApplication app(argc, argv);
0100 
0101     app.setApplicationName(QString::fromLatin1("digikam"));          // for DB init.
0102     DbEngineParameters prm    = DbEngineParameters::parametersFromConfig();
0103     CoreDbAccess::setParameters(prm, CoreDbAccess::MainApplication);
0104     FacialRecognitionWrapper db;
0105 
0106     QThreadPool pool;
0107     pool.setMaxThreadCount(101);
0108 
0109     for (int i = 0 ; i < firstMultiplier ; ++i)
0110     {
0111         Runnable* const r = new Runnable(i, db);
0112         pool.start(r);
0113     }
0114 
0115     pool.waitForDone();
0116 
0117     // Process recognition in database.
0118 
0119     QImage* const  image = new QImage(256, 256, QImage::Format_ARGB32);
0120     QList<Identity> list = db.recognizeFaces(QList<QImage*>() << image);
0121 
0122     if (!list.isEmpty())
0123     {
0124         Q_FOREACH (const Identity& id, list)
0125         {
0126             qCDebug(DIGIKAM_TESTS_LOG) << "Identity " << id.attribute(QString::fromLatin1("name")) << " recognized";
0127         }
0128     }
0129     else
0130     {
0131         qCDebug(DIGIKAM_TESTS_LOG) << "No Identity recognized from DB";
0132     }
0133 
0134     return 0;
0135 }