digiKam
shapepredictor.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date : 16/08/2016
7  * Description : A Shape predictor class that can predicts 68
8  * facial point including points surrounding faces
9  * eyes, that can be used for detecting human eyes
10  * positions, almost all codes are ported from dlib
11  * library (dlib.net/)
12  *
13  * Copyright (C) 2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
14  * Copyright (C) 2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
15  * Copyright (C) 2016-2022 by Gilles Caulier <caulier dot gilles at gmail dot com>
16  *
17  * This program is free software; you can redistribute it
18  * and/or modify it under the terms of the GNU General
19  * Public License as published by the Free Software Foundation;
20  * either version 2, or (at your option)
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * ============================================================ */
29 
30 #ifndef DIGIKAM_SHAPE_PREDICTOR_H
31 #define DIGIKAM_SHAPE_PREDICTOR_H
32 
33 // C++ includes
34 
35 #include <vector>
36 
37 // Local includes
38 
39 #include "digikam_opencv.h"
40 #include "pointtransformaffine.h"
41 #include "vectoroperations.h"
42 #include "matrixoperations.h"
43 #include "fullobjectdetection.h"
44 #include "qdatastreamoverloads.h"
45 
46 namespace Digikam
47 {
48 
49 namespace RedEye
50 {
51 
53 {
54  unsigned long idx1;
55  unsigned long idx2;
56  float thresh;
57 };
58 
59 QDataStream& operator << (QDataStream& dataStream, const SplitFeature& sp);
60 QDataStream& operator >> (QDataStream& dataStream, SplitFeature& sp);
61 
62 // NOTE: a tree is just a std::vector<RedEye::SplitFeature>. We use this function to navigate the tree nodes.
63 
67 unsigned long left_child(unsigned long idx);
68 
72 unsigned long right_child(unsigned long idx);
73 
74 // ----------------------------------------------------------------------------------------
75 
77 {
78  std::vector<SplitFeature> splits;
79  std::vector<std::vector<float> > leaf_values;
80 
81  unsigned long num_leaves() const;
82 
94  const std::vector<float>& operator()(const std::vector<float>& feature_pixel_values,
95  unsigned long& i) const;
96 };
97 
98 QDataStream& operator << (QDataStream& dataStream, const RegressionTree& regtree);
99 QDataStream& operator >> (QDataStream& dataStream, RegressionTree& regtree);
100 
108 template<class T>
109 inline std::vector<T> location(const std::vector<T>& shape,
110  unsigned long idx)
111 {
112  std::vector<T> temp(2);
113  temp[0] = shape[idx * 2 ];
114  temp[1] = shape[idx * 2 + 1];
115 
116  return temp;
117 }
118 
119 // ------------------------------------------------------------------------------------
120 
121 unsigned long nearestShapePoint(const std::vector<float>& shape,
122  const std::vector<float>& pt);
123 
124 // ------------------------------------------------------------------------------------
125 
136 void createShapeRelativeEncoding(const std::vector<float>& shape,
137  const std::vector<std::vector<float> >& pixel_coordinates,
138  std::vector<unsigned long>& anchor_idx,
139  std::vector<std::vector<float> >& deltas);
140 
141 // ------------------------------------------------------------------------------------
142 
143 PointTransformAffine findTformBetweenShapes(const std::vector<float>& from_shape,
144  const std::vector<float>& to_shape);
145 
146 // ------------------------------------------------------------------------------------
147 
151 PointTransformAffine normalizingTform(const cv::Rect& rect);
152 
153 // ------------------------------------------------------------------------------------
154 
158 PointTransformAffine unnormalizingTform(const cv::Rect& rect);
159 bool pointContained(const cv::Rect& rect, const std::vector<float>& point);
160 
161 // ------------------------------------------------------------------------------------
162 
179 void extractFeaturePixelValues(const cv::Mat& img_,
180  const cv::Rect& rect,
181  const std::vector<float>& current_shape,
182  const std::vector<float>& reference_shape,
183  const std::vector<unsigned long>& reference_pixel_anchor_idx,
184  const std::vector<std::vector<float> >& reference_pixel_deltas,
185  std::vector<float>& feature_pixel_values);
186 
187 // ------------------------------------------------------------------------------------
188 
190 {
191 public:
192 
193  explicit ShapePredictor();
194 
195  unsigned long num_parts() const;
196  unsigned long num_features() const;
197 
198  FullObjectDetection operator()(const cv::Mat& img,
199  const cv::Rect& rect) const;
200 
201 public:
202 
203  std::vector<float> initial_shape;
204  std::vector<std::vector<RedEye::RegressionTree> > forests;
205  std::vector<std::vector<unsigned long> > anchor_idx;
206  std::vector<std::vector<std::vector<float> > > deltas;
207 };
208 
209 QDataStream& operator << (QDataStream& dataStream, const ShapePredictor& shape);
210 QDataStream& operator >> (QDataStream& dataStream, ShapePredictor& shape);
211 
212 } // namespace RedEye
213 
214 } // namespace Digikam
215 
216 #endif // DIGIKAM_SHAPE_PREDICTOR_H
Definition: fullobjectdetection.h:43
Definition: pointtransformaffine.h:43
Definition: shapepredictor.h:190
unsigned long num_features() const
Definition: shapepredictor.cpp:345
std::vector< float > initial_shape
Definition: shapepredictor.h:203
std::vector< std::vector< unsigned long > > anchor_idx
Definition: shapepredictor.h:205
ShapePredictor()
Definition: shapepredictor.cpp:336
std::vector< std::vector< std::vector< float > > > deltas
Definition: shapepredictor.h:206
FullObjectDetection operator()(const cv::Mat &img, const cv::Rect &rect) const
Definition: shapepredictor.cpp:360
std::vector< std::vector< RedEye::RegressionTree > > forests
Definition: shapepredictor.h:204
unsigned long num_parts() const
Definition: shapepredictor.cpp:340
PointTransformAffine unnormalizingTform(const cv::Rect &rect)
Definition: shapepredictor.cpp:250
void extractFeaturePixelValues(const cv::Mat &img_, const cv::Rect &rect, const std::vector< float > &current_shape, const std::vector< float > &reference_shape, const std::vector< unsigned long > &reference_pixel_anchor_idx, const std::vector< std::vector< float > > &reference_pixel_deltas, std::vector< float > &feature_pixel_values)
Definition: shapepredictor.cpp:302
void createShapeRelativeEncoding(const std::vector< float > &shape, const std::vector< std::vector< float > > &pixel_coordinates, std::vector< unsigned long > &anchor_idx, std::vector< std::vector< float > > &deltas)
Definition: shapepredictor.cpp:169
unsigned long right_child(unsigned long idx)
Definition: shapepredictor.cpp:57
QDataStream & operator<<(QDataStream &dataStream, const SplitFeature &sp)
Definition: shapepredictor.cpp:38
unsigned long nearestShapePoint(const std::vector< float > &shape, const std::vector< float > &pt)
Definition: shapepredictor.cpp:144
std::vector< T > location(const std::vector< T > &shape, unsigned long idx)
Definition: shapepredictor.h:109
PointTransformAffine normalizingTform(const cv::Rect &rect)
Definition: shapepredictor.cpp:216
PointTransformAffine findTformBetweenShapes(const std::vector< float > &from_shape, const std::vector< float > &to_shape)
Definition: shapepredictor.cpp:186
unsigned long left_child(unsigned long idx)
Definition: shapepredictor.cpp:52
bool pointContained(const cv::Rect &rect, const std::vector< float > &point)
Definition: shapepredictor.cpp:283
QDataStream & operator>>(QDataStream &dataStream, SplitFeature &sp)
Definition: shapepredictor.cpp:45
Definition: datefolderview.cpp:43
Definition: shapepredictor.h:77
std::vector< SplitFeature > splits
Definition: shapepredictor.h:78
std::vector< std::vector< float > > leaf_values
Definition: shapepredictor.h:79
unsigned long num_leaves() const
Definition: shapepredictor.cpp:64
const std::vector< float > & operator()(const std::vector< float > &feature_pixel_values, unsigned long &i) const
Definition: shapepredictor.cpp:69
Definition: shapepredictor.h:53
unsigned long idx1
Definition: shapepredictor.h:54
float thresh
Definition: shapepredictor.h:56
unsigned long idx2
Definition: shapepredictor.h:55