aboutsummaryrefslogtreecommitdiff
path: root/libraries/libkface/opencv3.patch
blob: 2dfe3b3093d8ee41b6af8145147caaad91dda8ce (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
From: Gilles Caulier <caulier.gilles@gmail.com>
Date: Sun, 06 Sep 2015 08:13:52 +0000
Subject: use same OpenCV logic than digiKam and kipi-plugins
X-Git-Url: http://quickgit.kde.org/?p=libkface.git&a=commitdiff&h=dc62c0e0ea55d189d918501cce4d92f4198a1a0c
---
use same OpenCV logic than digiKam and kipi-plugins
CCBUGS: 349601
---


--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,13 +1,16 @@
 #
-# Copyright (c) 2010-2014, Gilles Caulier, <caulier dot gilles at gmail dot com>
+# Copyright (c) 2010-2015, Gilles Caulier, <caulier dot gilles at gmail dot com>
 #
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.
 
+cmake_minimum_required(VERSION 2.8.9)
 project(libkface)
 
 message(STATUS "----------------------------------------------------------------------------------")
 message(STATUS "Starting CMake configuration for: libkface")
+
+option(ENABLE_OPENCV3 "Build libkface with OpenCV3 instead OpenCV2 (default=OFF)" OFF)
 
 find_package(Qt4 4.6.0 REQUIRED)
 find_package(KDE4 REQUIRED)
@@ -29,7 +32,26 @@
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
 
 include(MacroOpenCV)
-DETECT_OPENCV(2.4.9 core highgui objdetect contrib legacy imgproc)
+
+if(ENABLE_OPENCV3)
+
+    DETECT_OPENCV(3.0.0 core face highgui objdetect imgproc)
+
+    if(${OpenCV_FOUND} AND ${OpenCV_VERSION} VERSION_LESS 3.0.0)
+        message(STATUS "ENABLE_OPENCV3 option is enabled and OpenCV < 3.0.0 have been found. Disabled ENABLE_OPENCV3")
+        set(OpenCV_FOUND FALSE)
+    endif()
+
+else()
+
+    DETECT_OPENCV(2.4.9 core highgui objdetect contrib legacy imgproc)
+
+    if(${OpenCV_FOUND} AND ${OpenCV_VERSION} VERSION_GREATER 2.4.99)
+        message(STATUS "ENABLE_OPENCV3 option is disabled and OpenCV >= 3.0.0 have been found. Enabled ENABLE_OPENCV3")
+        set(OpenCV_FOUND FALSE)
+    endif()
+
+endif()
 
 include_directories(${OpenCV_INCLUDE_DIRS})
 
@@ -48,11 +70,12 @@
     # 3.3.0 => 2.0.0     (Added a "simple" training method using image data directly for a single image)
     # 3.4.0 => 2.0.0     (Added a "simple" training method using image data directly for an image list)
     # 3.5.0 => 3.0.0     (Added d private internal container to reduce binary uncompatibility with Identity class)
+    # 3.5.1 => 3.0.0     (Added OpenCV3 support)
 
     # Library API version
     set(KFACE_LIB_MAJOR_VERSION "3")
     set(KFACE_LIB_MINOR_VERSION "5")
-    set(KFACE_LIB_PATCH_VERSION "0")
+    set(KFACE_LIB_PATCH_VERSION "1")
 
     # Suffix to add at end of version string. Usual values are:
     # "-git"   : alpha code unstable from git. Do not use in production

--- a/README
+++ b/README
@@ -20,6 +20,11 @@
 libqt      >= 4.6.x                    http://www.qtsoftware.com
 libkde     >= 4.4.x                    http://www.kde.org
 libopencv  >= 2.4.9                    http://opencv.willowgarage.com/wiki    (with opencv 'haarcascades' data files)
+
+CMake compilation options to custom libkface:
+
+Use CMake "-DENABLE_OPENCV3=on"        flag to compile libkface source code using OpenCV3 instead OpenCV2 (disabled by default).
+                                       OpenCV3 support needs extra contrib modules package, especially 'face' and 'legacy' components.
 
 -- INSTALL ------------------------------------------------------------
 

--- a/libkface/CMakeLists.txt
+++ b/libkface/CMakeLists.txt
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2010-2014, Gilles Caulier, <caulier dot gilles at gmail dot com>
+# Copyright (c) 2010-2015, Gilles Caulier, <caulier dot gilles at gmail dot com>
 #
 # Redistribution and use is allowed according to the terms of the BSD license.
 # For details see the accompanying COPYING-CMAKE-SCRIPTS file.

--- a/libkface/alignment-congealing/funnelreal.cpp
+++ b/libkface/alignment-congealing/funnelreal.cpp
@@ -426,7 +426,7 @@
 
         for(int k=0; k<numParams; k++)
         {
-            float dn = ((rand()%160)-80)/100.0f;
+            float dn = ((qrand()%160)-80)/100.0f;
             if(k>1)
             {
                 dn /= 100.0f;

--- a/libkface/detection/opencvfacedetector.cpp
+++ b/libkface/detection/opencvfacedetector.cpp
@@ -18,7 +18,7 @@
  *         <a href="alexjironkin at gmail dot com">alexjironkin at gmail dot com</a>
  * @author Copyright (C) 2010 by Aditya Bhatt
  *         <a href="adityabhatt at gmail dot com">adityabhatt at gmail dot com</a>
- * @author Copyright (C) 2010-2014 by Gilles Caulier
+ * @author Copyright (C) 2010-2015 by Gilles Caulier
  *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
  * @author Copyright (C) 2010-2013 by Marcel Wiesweg
  *         <a href="mailto:marcel dot wiesweg at gmx dot de">marcel dot wiesweg at gmx dot de</a>
@@ -136,13 +136,14 @@
 
     cv::Size getOriginalWindowSize() const
     {
+#if OPENCV_VERSION <= OPENCV_MAKE_VERSION(2,4,99)
         // This is a HACK which may break any time. Work around the fact that getOriginalWindowSize()
         // always returns (0,0) and we need these values.
         if (oldCascade)
         {
             return oldCascade->orig_window_size;
         }
-
+#endif
         return cv::Size(0, 0);
     }
 

--- a/libkface/libopencv.h.cmake.in
+++ b/libkface/libopencv.h.cmake.in
@@ -7,7 +7,7 @@
  * @date   2010-06-16
  * @brief  Wrapper for OpenCV header files
  *
- * @author Copyright (C) 2012-2014 by Gilles Caulier
+ * @author Copyright (C) 2012-2015 by Gilles Caulier
  *         <a href="mailto:caulier dot gilles at gmail dot com">caulier dot gilles at gmail dot com</a>
  *
  * This program is free software; you can redistribute it
@@ -31,16 +31,16 @@
 
 // Pragma directives to reduce warnings from OpenCV header files.
 #if not defined(__APPLE__) && defined(__GNUC__)
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
-#pragma GCC diagnostic ignored "-Woverloaded-virtual"
+#   pragma GCC diagnostic push
+#   pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
+#   pragma GCC diagnostic ignored "-Woverloaded-virtual"
 #endif
 
 #if defined(__APPLE__) && defined(__clang__)
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
-#pragma clang diagnostic ignored "-Woverloaded-virtual"
-#pragma clang diagnostic ignored "-Wcast-align"
+#   pragma clang diagnostic push
+#   pragma clang diagnostic ignored "-Wnon-virtual-dtor"
+#   pragma clang diagnostic ignored "-Woverloaded-virtual"
+#   pragma clang diagnostic ignored "-Wcast-align"
 #endif
 
 // OpenCV includes
@@ -49,25 +49,32 @@
 
 #define OPENCV_MAKE_VERSION(major,minor,patch) (((major) << 16) | ((minor) << 8) | (patch))
 #define OPENCV_VERSION                         OPENCV_MAKE_VERSION(CV_MAJOR_VERSION,CV_MINOR_VERSION,CV_SUBMINOR_VERSION)
-#define OPENCV_TEST_VERSION(major,minor,patch) ( OPENCV_VERSION >= OPENCV_MAKE_VERSION(major,minor,patch) )
+#define OPENCV_TEST_VERSION(major,minor,patch) ( OPENCV_VERSION < OPENCV_MAKE_VERSION(major,minor,patch) )
 
-#include <opencv2/core/core.hpp>
-#include <opencv2/core/internal.hpp>
-#include <opencv2/contrib/contrib.hpp>
+#if OPENCV_TEST_VERSION(3,0,0)
+#   include <opencv2/core/core.hpp>
+#   include <opencv2/core/internal.hpp>
+#   include <opencv2/contrib/contrib.hpp>
+#else
+#   include <opencv2/face.hpp>
+#   include <opencv2/core.hpp>
+#endif
 
 // for old-style code
+#if OPENCV_VERSION <= OPENCV_MAKE_VERSION(2,4,99)
+#   include <opencv2/legacy/compat.hpp>
+#endif
 #include <opencv2/opencv.hpp>
-#include <opencv2/legacy/compat.hpp>
 #include <opencv2/highgui/highgui_c.h>
 #include <opencv/cvaux.h>
 
 // Restore warnings
 #if not defined(__APPLE__) && defined(__GNUC__)
-#pragma GCC diagnostic pop
+#   pragma GCC diagnostic pop
 #endif
 
 #if defined(__APPLE__) && defined(__clang__)
-#pragma clang diagnostic pop
+#   pragma clang diagnostic pop
 #endif
 
 #endif // LIB_OPEN_CV_H

--- a/libkface/recognition-opencv-lbph/facerec_borrowed.cpp
+++ b/libkface/recognition-opencv-lbph/facerec_borrowed.cpp
@@ -531,15 +531,17 @@
     return ptr;
 }
 
-CV_INIT_ALGORITHM(LBPHFaceRecognizer, "FaceRecognizer.LBPH-KFaceIface",
-                  obj.info()->addParam(obj, "radius",     obj.m_radius);
-                  obj.info()->addParam(obj, "neighbors",  obj.m_neighbors);
-                  obj.info()->addParam(obj, "grid_x",     obj.m_grid_x);
-                  obj.info()->addParam(obj, "grid_y",     obj.m_grid_y);
-                  obj.info()->addParam(obj, "threshold",  obj.m_threshold);
-                  obj.info()->addParam(obj, "histograms", obj.m_histograms);         // modification: Make Read/Write
-                  obj.info()->addParam(obj, "labels",     obj.m_labels);             // modification: Make Read/Write
-                  obj.info()->addParam(obj, "statistic",  obj.m_statisticsMode));    // modification: Add parameter
+#if OPENCV_VERSION <= OPENCV_MAKE_VERSION(2,4,99)
+    CV_INIT_ALGORITHM(LBPHFaceRecognizer, "FaceRecognizer.LBPH-KFaceIface",
+                      obj.info()->addParam(obj, "radius",     obj.m_radius);
+                      obj.info()->addParam(obj, "neighbors",  obj.m_neighbors);
+                      obj.info()->addParam(obj, "grid_x",     obj.m_grid_x);
+                      obj.info()->addParam(obj, "grid_y",     obj.m_grid_y);
+                      obj.info()->addParam(obj, "threshold",  obj.m_threshold);
+                      obj.info()->addParam(obj, "histograms", obj.m_histograms);         // modification: Make Read/Write
+                      obj.info()->addParam(obj, "labels",     obj.m_labels);             // modification: Make Read/Write
+                      obj.info()->addParam(obj, "statistic",  obj.m_statisticsMode));    // modification: Add parameter
+#endif
 
 } // namespace KFaceIface
 

--- a/libkface/recognition-opencv-lbph/facerec_borrowed.h
+++ b/libkface/recognition-opencv-lbph/facerec_borrowed.h
@@ -45,7 +45,11 @@
 namespace KFaceIface
 {
 
+#if OPENCV_TEST_VERSION(3,0,0)
 class LBPHFaceRecognizer : public cv::FaceRecognizer
+#else
+class LBPHFaceRecognizer : public cv::face::FaceRecognizer
+#endif
 {
 public:
 
@@ -99,8 +103,13 @@
 
     ~LBPHFaceRecognizer() {}
 
+#if OPENCV_TEST_VERSION(3,0,0)
     using cv::FaceRecognizer::save;
     using cv::FaceRecognizer::load;
+#else
+    using cv::face::FaceRecognizer::save;
+    using cv::face::FaceRecognizer::load;
+#endif
 
     static cv::Ptr<LBPHFaceRecognizer> create(int radius=1, int neighbors=8, int grid_x=8, int grid_y=8, double threshold = DBL_MAX, PredictionStatistics statistics = NearestNeighbor);
 
@@ -139,6 +148,8 @@
     /**
      * Getter functions.
      */
+#if OPENCV_TEST_VERSION(3,0,0)
+
     int neighbors() const { return m_neighbors; }
     int radius()    const { return m_radius;    }
     int grid_x()    const { return m_grid_x;    }
@@ -146,6 +157,34 @@
 
     // NOTE: Implementation done through CV_INIT_ALGORITHM macro from OpenCV.
     cv::AlgorithmInfo* info() const;
+
+#else
+
+    int getNeighbors() const                             { return m_neighbors;            }
+    void setNeighbors(int _neighbors)                    { m_neighbors = _neighbors;      }
+
+    int getRadius()    const                             { return m_radius;               }
+    void setRadius(int radius)                           { m_radius = radius;             }
+
+    int getGrid_x()    const                             { return m_grid_x;               }
+    void setGrid_x(int _grid_x)                          { m_grid_x = _grid_x;            }
+
+    int getGrid_y()    const                             { return m_grid_y;               }
+    void setGrid_y(int _grid_y)                          { m_grid_y = _grid_y;            }
+
+    double getThreshold() const                          { return m_threshold;            }
+    void setThreshold(double _threshold)                 { m_threshold = _threshold;      }
+
+    void setHistograms(std::vector<cv::Mat> _histograms) { m_histograms = _histograms;    }
+    std::vector<cv::Mat> getHistograms() const           { return m_histograms;           }
+
+    void setLabels(cv::Mat _labels)                      { m_labels = _labels;            }
+    cv::Mat getLabels() const                            { return m_labels;               }
+
+    void setStatistic(int _statistic)                    { m_statisticsMode = _statistic; }
+    int getStatistic() const                             { return m_statisticsMode;       }
+
+#endif
 
 private:
 

--- a/libkface/recognition-opencv-lbph/lbphfacemodel.cpp
+++ b/libkface/recognition-opencv-lbph/lbphfacemodel.cpp
@@ -61,7 +61,11 @@
     : cv::Ptr<LBPHFaceRecognizer>(LBPHFaceRecognizer::create()),
       databaseId(0)
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("threshold", 100.0);
+#else
+    ptr()->setThreshold(100.0);
+#endif
 }
 
 LBPHFaceModel::~LBPHFaceModel()
@@ -80,9 +84,13 @@
 
 const LBPHFaceRecognizer* LBPHFaceModel::ptr() const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     const LBPHFaceRecognizer* const ptr = cv::Ptr<LBPHFaceRecognizer>::operator const KFaceIface::LBPHFaceRecognizer*();
-
-    if (!ptr) 
+#else
+    const LBPHFaceRecognizer* const ptr = cv::Ptr<LBPHFaceRecognizer>::operator KFaceIface::LBPHFaceRecognizer*();
+#endif
+
+    if (!ptr)
         kWarning() << "LBPHFaceRecognizer pointer is null";
 
     return ptr;
@@ -90,47 +98,83 @@
 
 int LBPHFaceModel::radius() const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     return ptr()->get<int>("radius");
+#else
+    return ptr()->getRadius();
+#endif
 }
 
 void LBPHFaceModel::setRadius(int radius)
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("radius", radius);
+#else
+    ptr()->setRadius(radius);
+#endif
 }
 
 int LBPHFaceModel::neighbors() const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     return ptr()->get<int>("neighbors");
+#else
+    return ptr()->getNeighbors();
+#endif
 }
 
 void LBPHFaceModel::setNeighbors(int neighbors)
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("neighbors", neighbors);
+#else
+    ptr()->setNeighbors(neighbors);
+#endif
 }
 
 int LBPHFaceModel::gridX() const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     return ptr()->get<int>("grid_x");
+#else
+    return ptr()->getGrid_x();
+#endif
 }
 
 void LBPHFaceModel::setGridX(int grid_x)
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("grid_x", grid_x);
+#else
+    ptr()->setGrid_x(grid_x);
+#endif
 }
 
 int LBPHFaceModel::gridY() const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     return ptr()->get<int>("grid_y");
+#else
+    return ptr()->getGrid_y();
+#endif
 }
 
 void LBPHFaceModel::setGridY(int grid_y)
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("grid_y", grid_y);
+#else
+    ptr()->setGrid_y(grid_y);
+#endif
 }
 
 OpenCVMatData LBPHFaceModel::histogramData(int index) const
 {
+#if OPENCV_TEST_VERSION(3,0,0)
     return OpenCVMatData(ptr()->get<std::vector<cv::Mat> >("histograms").at(index));
+#else
+    return OpenCVMatData(ptr()->getHistograms().at(index));
+#endif
 }
 
 QList<LBPHistogramMetadata> LBPHFaceModel::histogramMetadata() const
@@ -168,12 +212,24 @@
         m_histogramMetadata << metadata;
     }
 
+#if OPENCV_TEST_VERSION(3,0,0)
     std::vector<cv::Mat> currentHistograms = ptr()->get<std::vector<cv::Mat> >("histograms");
     cv::Mat currentLabels                  = ptr()->get<cv::Mat>("labels");
+#else
+    std::vector<cv::Mat> currentHistograms = ptr()->getHistograms();
+    cv::Mat currentLabels                  = ptr()->getLabels();
+#endif
+
     currentHistograms.insert(currentHistograms.end(), newHistograms.begin(), newHistograms.end());
     currentLabels.push_back(newLabels);
+
+#if OPENCV_TEST_VERSION(3,0,0)
     ptr()->set("histograms", currentHistograms);
-    ptr()->set("labels", currentLabels);
+    ptr()->set("labels",     currentLabels);
+#else
+    ptr()->setHistograms(currentHistograms);
+    ptr()->setLabels(currentLabels);
+#endif
 
 /*
     //Most cumbersome and inefficient way through a file storage which we were forced to use if we used standard OpenCV
@@ -215,7 +271,11 @@
 
     // Update local information
     // We assume new labels are simply appended
+#if OPENCV_TEST_VERSION(3,0,0)
     cv::Mat currentLabels = ptr()->get<cv::Mat>("labels");
+#else
+    cv::Mat currentLabels = ptr()->getLabels();
+#endif
 
     for (int i = m_histogramMetadata.size() ; i < currentLabels.rows ; i++)
     {

--- a/tests/preprocess.cpp
+++ b/tests/preprocess.cpp
@@ -93,7 +93,7 @@
         // Draw images side-by-side for later display
         QSize size(left.cols, left.rows);
         size.scale(uiSize, uiSize, Qt::KeepAspectRatio);
-        cv::Size scaleSize(size.height(), size.width());
+        cv::Size scaleSize(size.width(), size.height());
 
         const int top = currentRow*uiSize;
         cv::Mat scaledLeft, scaledRight;