digiKam
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
14 * SPDX-FileCopyrightText: 2019 by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
15 * SPDX-FileCopyrightText: 2016-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
16 *
17 * SPDX-License-Identifier: GPL-2.0-or-later
18 *
19 * ============================================================ */
20
21#pragma once
22
23// C++ includes
24
25#include <vector>
26
27// Local includes
28
29#include "digikam_opencv.h"
31#include "vectoroperations.h"
32#include "matrixoperations.h"
33#include "fullobjectdetection.h"
35
36namespace Digikam
37{
38
39namespace RedEye
40{
41
43{
44 quint64 idx1 = 0;
45 quint64 idx2 = 0;
46 float thresh = 0.0F;
47};
48
49QDataStream& operator << (QDataStream& dataStream, const SplitFeature& sp);
50QDataStream& operator >> (QDataStream& dataStream, SplitFeature& sp);
51
52// NOTE: a tree is just a std::vector<RedEye::SplitFeature>. We use this function to navigate the tree nodes.
53
57unsigned long left_child(unsigned long idx);
58
62unsigned long right_child(unsigned long idx);
63
64// ----------------------------------------------------------------------------------------
65
67{
68 std::vector<SplitFeature> splits;
69 std::vector<std::vector<float> > leaf_values;
70
71 unsigned long num_leaves() const;
72
84 const std::vector<float>& operator()(const std::vector<float>& feature_pixel_values,
85 unsigned long& i) const;
86};
87
88QDataStream& operator << (QDataStream& dataStream, const RegressionTree& regtree);
89QDataStream& operator >> (QDataStream& dataStream, RegressionTree& regtree);
90
98template<class T>
99inline std::vector<T> location(const std::vector<T>& shape,
100 unsigned long idx)
101{
102 std::vector<T> temp(2);
103 temp[0] = shape[idx * 2 ];
104 temp[1] = shape[idx * 2 + 1];
105
106 return temp;
107}
108
109// ------------------------------------------------------------------------------------
110
111unsigned long nearestShapePoint(const std::vector<float>& shape,
112 const std::vector<float>& pt);
113
114// ------------------------------------------------------------------------------------
115
126void createShapeRelativeEncoding(const std::vector<float>& shape,
127 const std::vector<std::vector<float> >& pixel_coordinates,
128 std::vector<unsigned long>& anchor_idx,
129 std::vector<std::vector<float> >& deltas);
130
131// ------------------------------------------------------------------------------------
132
133PointTransformAffine findTformBetweenShapes(const std::vector<float>& from_shape,
134 const std::vector<float>& to_shape);
135
136// ------------------------------------------------------------------------------------
137
141PointTransformAffine normalizingTform(const cv::Rect& rect);
142
143// ------------------------------------------------------------------------------------
144
148PointTransformAffine unnormalizingTform(const cv::Rect& rect);
149bool pointContained(const cv::Rect& rect, const std::vector<float>& point);
150
151// ------------------------------------------------------------------------------------
152
169void extractFeaturePixelValues(const cv::Mat& img_,
170 const cv::Rect& rect,
171 const std::vector<float>& current_shape,
172 const std::vector<float>& reference_shape,
173 const std::vector<unsigned long>& reference_pixel_anchor_idx,
174 const std::vector<std::vector<float> >& reference_pixel_deltas,
175 std::vector<float>& feature_pixel_values);
176
177// ------------------------------------------------------------------------------------
178
180{
181public:
182
183 explicit ShapePredictor();
184
185 unsigned long num_parts() const;
186 unsigned long num_features() const;
187
188 FullObjectDetection operator()(const cv::Mat& img,
189 const cv::Rect& rect) const;
190
191public:
192
193 std::vector<float> initial_shape;
194 std::vector<std::vector<RedEye::RegressionTree> > forests;
195 std::vector<std::vector<unsigned long> > anchor_idx;
196 std::vector<std::vector<std::vector<float> > > deltas;
197};
198
199QDataStream& operator << (QDataStream& dataStream, const ShapePredictor& shape);
200QDataStream& operator >> (QDataStream& dataStream, ShapePredictor& shape);
201
202} // namespace RedEye
203
204} // namespace Digikam
Definition fullobjectdetection.h:33
Definition pointtransformaffine.h:33
Definition shapepredictor.h:180
unsigned long num_features() const
Definition shapepredictor.cpp:340
std::vector< float > initial_shape
Definition shapepredictor.h:193
std::vector< std::vector< unsigned long > > anchor_idx
Definition shapepredictor.h:195
ShapePredictor()
Definition shapepredictor.cpp:331
std::vector< std::vector< std::vector< float > > > deltas
Definition shapepredictor.h:196
FullObjectDetection operator()(const cv::Mat &img, const cv::Rect &rect) const
Definition shapepredictor.cpp:355
std::vector< std::vector< RedEye::RegressionTree > > forests
Definition shapepredictor.h:194
unsigned long num_parts() const
Definition shapepredictor.cpp:335
std::vector< T > location(const std::vector< T > &shape, unsigned long idx)
Definition shapepredictor.h:99
PointTransformAffine unnormalizingTform(const cv::Rect &rect)
Definition shapepredictor.cpp:243
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:297
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:160
unsigned long right_child(unsigned long idx)
Definition shapepredictor.cpp:48
QDataStream & operator<<(QDataStream &dataStream, const SplitFeature &sp)
Definition shapepredictor.cpp:29
unsigned long nearestShapePoint(const std::vector< float > &shape, const std::vector< float > &pt)
Definition shapepredictor.cpp:135
PointTransformAffine normalizingTform(const cv::Rect &rect)
Definition shapepredictor.cpp:209
PointTransformAffine findTformBetweenShapes(const std::vector< float > &from_shape, const std::vector< float > &to_shape)
Definition shapepredictor.cpp:177
unsigned long left_child(unsigned long idx)
Definition shapepredictor.cpp:43
bool pointContained(const cv::Rect &rect, const std::vector< float > &point)
Definition shapepredictor.cpp:276
QDataStream & operator>>(QDataStream &dataStream, SplitFeature &sp)
Definition shapepredictor.cpp:36
Definition datefolderview.cpp:34
Definition shapepredictor.h:67
std::vector< SplitFeature > splits
Definition shapepredictor.h:68
std::vector< std::vector< float > > leaf_values
Definition shapepredictor.h:69
unsigned long num_leaves() const
Definition shapepredictor.cpp:55
const std::vector< float > & operator()(const std::vector< float > &feature_pixel_values, unsigned long &i) const
Definition shapepredictor.cpp:60
Definition shapepredictor.h:43
quint64 idx1
Definition shapepredictor.h:44
quint64 idx2
Definition shapepredictor.h:45
float thresh
Definition shapepredictor.h:46