~ubuntu-branches/ubuntu/quantal/digikam/quantal-proposed

« back to all changes in this revision

Viewing changes to core/utilities/maintenance/facedetector.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-06-08 00:31:19 UTC
  • mfrom: (1.2.39) (3.2.23 sid)
  • Revision ID: package-import@ubuntu.com-20120608003119-qn0l1eo3rf5jdh9w
Tags: 4:2.6.0-0ubuntu1
* Merge from debian, remaining changes (LP: #885324, #1009943)
  - keep seperate binary packages:
    + libkface1, libkface-data, libkface-dev
    + libkgeomap1, libkgeomap-data, libkgeomap-dev
    + libvkontakte1, libkvkontakte-dev
    + libmediawiki1, libmediawiki-dev
  - keep patches:
    + kubuntu_mysqld_executable_name.diff
  - don't fail on missing files in dh_install
  - install oxygen icons for kipi-plugins
  - digikam breaks/replaces kipi-plugins-common << 4:2.5.0~
  - digikam-doc breaks/replaces digikam-data << 4:2.5.0~
  - digikam-data breaks/replaces kipi-plugins << 4:2.5.0~
  - kipi-plugins-common breaks/replaces kipi-plugins << 4:2.5.0~
* Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2010-07-18
 
7
 * Description : batch face detection
 
8
 *
 
9
 * Copyright (C) 2010 by Aditya Bhatt <adityabhatt1991 at gmail dot com>
 
10
 * Copyright (C) 2010-2012 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
11
 * Copyright (C) 2012 by Andi Clemens <andi dot clemens at googlemail dot com>
 
12
 *
 
13
 * This program is free software; you can redistribute it
 
14
 * and/or modify it under the terms of the GNU General
 
15
 * Public License as published by the Free Software Foundation;
 
16
 * either version 2, or (at your option)
 
17
 * any later version.
 
18
 *
 
19
 * This program is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU General Public License for more details.
 
23
 *
 
24
 * ============================================================ */
 
25
 
 
26
#include "facedetector.moc"
 
27
 
 
28
// Qt includes
 
29
 
 
30
#include <QClipboard>
 
31
#include <QVBoxLayout>
 
32
#include <QTimer>
 
33
 
 
34
// KDE includes
 
35
 
 
36
#include <kicon.h>
 
37
#include <kconfig.h>
 
38
#include <kdebug.h>
 
39
#include <klocale.h>
 
40
#include <kpushbutton.h>
 
41
#include <kstandarddirs.h>
 
42
#include <kstandardguiitem.h>
 
43
#include <ktextedit.h>
 
44
#include <kapplication.h>
 
45
 
 
46
// KFace includes
 
47
 
 
48
#include <libkface/recognitiondatabase.h>
 
49
 
 
50
// Local includes
 
51
 
 
52
#include "albumdb.h"
 
53
#include "album.h"
 
54
#include "albummanager.h"
 
55
#include "facepipeline.h"
 
56
#include "facescansettings.h"
 
57
#include "imageinfo.h"
 
58
#include "imageinfojob.h"
 
59
 
 
60
namespace Digikam
 
61
{
 
62
 
 
63
class BenchmarkMessageDisplay : public QWidget
 
64
{
 
65
public:
 
66
 
 
67
    BenchmarkMessageDisplay(const QString& richText)
 
68
        : QWidget(0)
 
69
    {
 
70
        setAttribute(Qt::WA_DeleteOnClose);
 
71
 
 
72
        QVBoxLayout* vbox     = new QVBoxLayout;
 
73
        KTextEdit* edit       = new KTextEdit;
 
74
        vbox->addWidget(edit, 1);
 
75
        KPushButton* okButton = new KPushButton(KStandardGuiItem::ok());
 
76
        vbox->addWidget(okButton, 0, Qt::AlignRight);
 
77
 
 
78
        setLayout(vbox);
 
79
 
 
80
        connect(okButton, SIGNAL(clicked()),
 
81
                this, SLOT(close()));
 
82
 
 
83
        edit->setHtml(richText);
 
84
        QApplication::clipboard()->setText(edit->toPlainText());
 
85
 
 
86
        resize(500, 400);
 
87
        show();
 
88
        raise();
 
89
    }
 
90
};
 
91
 
 
92
// --------------------------------------------------------------------------
 
93
 
 
94
class FaceDetector::FaceDetectorPriv
 
95
{
 
96
public:
 
97
 
 
98
    FaceDetectorPriv()
 
99
    {
 
100
        benchmark  = false;
 
101
        total      = 0;
 
102
    }
 
103
 
 
104
    bool               benchmark;
 
105
 
 
106
    int                total;
 
107
 
 
108
    AlbumPointerList<> albumTodoList;
 
109
    ImageInfoJob       albumListing;
 
110
    FacePipeline       pipeline;
 
111
};
 
112
 
 
113
FaceDetector::FaceDetector(const FaceScanSettings& settings, ProgressItem* parent)
 
114
    : MaintenanceTool("FaceDetector", parent),
 
115
      d(new FaceDetectorPriv)
 
116
{
 
117
    if (settings.task == FaceScanSettings::RetrainAll)
 
118
    {
 
119
        KFaceIface::RecognitionDatabase::addDatabase();
 
120
        d->pipeline.plugRetrainingDatabaseFilter();
 
121
        d->pipeline.plugTrainer();
 
122
        d->pipeline.construct();
 
123
    }
 
124
    else if (settings.task == FaceScanSettings::Benchmark)
 
125
    {
 
126
        d->benchmark = true;
 
127
        d->pipeline.plugDatabaseFilter(FacePipeline::ScanAll);
 
128
        d->pipeline.plugPreviewLoader();
 
129
 
 
130
        if (settings.useFullCpu)
 
131
        {
 
132
            d->pipeline.plugParallelFaceDetectors();
 
133
        }
 
134
        else
 
135
        {
 
136
            d->pipeline.plugFaceDetector();
 
137
        }
 
138
 
 
139
        d->pipeline.plugBenchmarker();
 
140
        d->pipeline.construct();
 
141
    }
 
142
    else
 
143
    {
 
144
        FacePipeline::FilterMode filterMode;
 
145
        FacePipeline::WriteMode  writeMode;
 
146
 
 
147
        if (settings.task == FaceScanSettings::DetectAndRecognize)
 
148
        {
 
149
            if (settings.alreadyScannedHandling == FaceScanSettings::Skip)
 
150
            {
 
151
                filterMode = FacePipeline::SkipAlreadyScanned;
 
152
                writeMode  = FacePipeline::NormalWrite;
 
153
            }
 
154
            else if (settings.alreadyScannedHandling == FaceScanSettings::Rescan)
 
155
            {
 
156
                filterMode = FacePipeline::ScanAll;
 
157
                writeMode  = FacePipeline::OverwriteUnconfirmed;
 
158
            }
 
159
            else // if (settings.alreadyScannedHandling == FaceScanSettings::Merge)
 
160
            {
 
161
                filterMode = FacePipeline::ScanAll;
 
162
                writeMode  = FacePipeline::NormalWrite;
 
163
            }
 
164
        }
 
165
        else // if (settings.task == FaceScanSettings::RecognizeMarkedFaces)
 
166
        {
 
167
            filterMode = FacePipeline::ReadUnconfirmedFaces;
 
168
            writeMode  = FacePipeline::NormalWrite;
 
169
        }
 
170
 
 
171
        d->pipeline.plugDatabaseFilter(filterMode);
 
172
 
 
173
        if (settings.task == FaceScanSettings::DetectAndRecognize)
 
174
        {
 
175
            d->pipeline.plugPreviewLoader();
 
176
 
 
177
            if (settings.useFullCpu)
 
178
            {
 
179
                d->pipeline.plugParallelFaceDetectors();
 
180
            }
 
181
            else
 
182
            {
 
183
                d->pipeline.plugFaceDetector();
 
184
            }
 
185
        }
 
186
 
 
187
        d->pipeline.plugFaceRecognizer();
 
188
        d->pipeline.plugDatabaseWriter(writeMode);
 
189
        d->pipeline.construct();
 
190
        d->pipeline.setDetectionAccuracy(settings.accuracy);
 
191
    }
 
192
 
 
193
    connect(&d->albumListing, SIGNAL(signalItemsInfo(ImageInfoList)),
 
194
            this, SLOT(slotItemsInfo(ImageInfoList)));
 
195
 
 
196
    connect(&d->albumListing, SIGNAL(signalCompleted()),
 
197
            this, SLOT(slotContinueAlbumListing()));
 
198
 
 
199
    connect(&d->pipeline, SIGNAL(finished()),
 
200
            this, SLOT(slotContinueAlbumListing()));
 
201
 
 
202
    connect(&d->pipeline, SIGNAL(processed(FacePipelinePackage)),
 
203
            this, SLOT(slotShowOneDetected(FacePipelinePackage)));
 
204
 
 
205
    connect(&d->pipeline, SIGNAL(skipped(QList<ImageInfo>)),
 
206
            this, SLOT(slotImagesSkipped(QList<ImageInfo>)));
 
207
 
 
208
    connect(this, SIGNAL(progressItemCanceled(ProgressItem*)),
 
209
            this, SLOT(slotCancel()));
 
210
 
 
211
    if (settings.albums.isEmpty() || settings.task == FaceScanSettings::RetrainAll)
 
212
    {
 
213
        d->albumTodoList = AlbumManager::instance()->allPAlbums();
 
214
    }
 
215
    else
 
216
    {
 
217
        d->albumTodoList = settings.albums;
 
218
    }
 
219
}
 
220
 
 
221
FaceDetector::~FaceDetector()
 
222
{
 
223
    delete d;
 
224
}
 
225
 
 
226
void FaceDetector::slotStart()
 
227
{
 
228
    MaintenanceTool::slotStart();
 
229
 
 
230
    setThumbnail(KIcon("edit-image-face-show").pixmap(22));
 
231
    setUsesBusyIndicator(true);
 
232
 
 
233
    // get total count, cached by AlbumManager
 
234
    QMap<int, int> palbumCounts, talbumCounts;
 
235
    bool hasPAlbums = false;
 
236
    bool hasTAlbums = false;
 
237
 
 
238
    foreach(Album* album, d->albumTodoList)
 
239
    {
 
240
        if (album->type() == Album::PHYSICAL)
 
241
        {
 
242
            hasPAlbums = true;
 
243
        }
 
244
        else
 
245
        {
 
246
            hasTAlbums = true;
 
247
        }
 
248
    }
 
249
 
 
250
    palbumCounts = AlbumManager::instance()->getPAlbumsCount();
 
251
    talbumCounts = AlbumManager::instance()->getTAlbumsCount();
 
252
 
 
253
    if (palbumCounts.isEmpty() && hasPAlbums)
 
254
    {
 
255
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
256
        palbumCounts = DatabaseAccess().db()->getNumberOfImagesInAlbums();
 
257
        QApplication::restoreOverrideCursor();
 
258
    }
 
259
 
 
260
    if (talbumCounts.isEmpty() && hasTAlbums)
 
261
    {
 
262
        QApplication::setOverrideCursor(Qt::WaitCursor);
 
263
        talbumCounts = DatabaseAccess().db()->getNumberOfImagesInTags();
 
264
        QApplication::restoreOverrideCursor();
 
265
    }
 
266
 
 
267
    d->total = 0;
 
268
 
 
269
    foreach(Album* album, d->albumTodoList)
 
270
    {
 
271
        if (album->type() == Album::PHYSICAL)
 
272
        {
 
273
            d->total += palbumCounts.value(album->id());
 
274
        }
 
275
        else
 
276
            // this is possibly broken of course because we do not know if images have multiple tags,
 
277
            // but there's no better solution without expensive operation
 
278
        {
 
279
            d->total += talbumCounts.value(album->id());
 
280
        }
 
281
    }
 
282
 
 
283
    kDebug() << "Total is" << d->total;
 
284
 
 
285
    d->total = qMax(1, d->total);
 
286
 
 
287
    setUsesBusyIndicator(false);
 
288
    setLabel(i18n("Updating faces database."));
 
289
    setTotalItems(d->total);
 
290
 
 
291
    slotContinueAlbumListing();
 
292
}
 
293
 
 
294
void FaceDetector::slotContinueAlbumListing()
 
295
{
 
296
    kDebug() << d->albumListing.isRunning() << !d->pipeline.hasFinished();
 
297
 
 
298
    // we get here by the finished signal from both, and want both to have finished to continue
 
299
    if (d->albumListing.isRunning() || !d->pipeline.hasFinished())
 
300
    {
 
301
        return;
 
302
    }
 
303
 
 
304
    // list can have null pointer if album was deleted recently
 
305
    Album* album = 0;
 
306
 
 
307
    do
 
308
    {
 
309
        if (d->albumTodoList.isEmpty())
 
310
        {
 
311
            return slotDone();
 
312
        }
 
313
 
 
314
        album = d->albumTodoList.takeFirst();
 
315
    }
 
316
    while (!album);
 
317
 
 
318
    d->albumListing.allItemsFromAlbum(album);
 
319
}
 
320
 
 
321
void FaceDetector::slotItemsInfo(const ImageInfoList& items)
 
322
{
 
323
    d->pipeline.process(items);
 
324
}
 
325
 
 
326
void FaceDetector::slotDone()
 
327
{
 
328
    if (d->benchmark)
 
329
    {
 
330
        new BenchmarkMessageDisplay(d->pipeline.benchmarkResult());
 
331
    }
 
332
 
 
333
    // Switch on scanned for faces flag on digiKam config file.
 
334
    KGlobal::config()->group("General Settings").writeEntry("Face Scanner First Run", true);
 
335
 
 
336
    MaintenanceTool::slotDone();
 
337
}
 
338
 
 
339
void FaceDetector::slotCancel()
 
340
{
 
341
    d->pipeline.cancel();
 
342
    MaintenanceTool::slotCancel();
 
343
}
 
344
 
 
345
void FaceDetector::slotImagesSkipped(const QList<ImageInfo>& infos)
 
346
{
 
347
    advance(infos.size());
 
348
}
 
349
 
 
350
void FaceDetector::slotShowOneDetected(const FacePipelinePackage& package)
 
351
{
 
352
    QPixmap pix;
 
353
 
 
354
    if (!package.image.isNull())
 
355
    {
 
356
        pix = package.image.smoothScale(22, 22, Qt::KeepAspectRatio).convertToPixmap();
 
357
    }
 
358
    else if (!package.faces.isEmpty())
 
359
    {
 
360
        pix = QPixmap::fromImage(package.faces.first().image().toQImage().scaled(22, 22, Qt::KeepAspectRatio));
 
361
    }
 
362
 
 
363
    setThumbnail(pix);
 
364
    advance(1);
 
365
}
 
366
 
 
367
} // namespace Digikam