File indexing completed on 2025-03-09 03:54:59

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 16/08/2016
0007  * Description : Full object detection class representing the output of the
0008  *               shape predictor class, containing 64 facial point including
0009  *               eye, nose, and mouth.
0010  *
0011  * SPDX-FileCopyrightText:      2016 by Omar Amin <Omar dot moh dot amin at gmail dot com>
0012  * SPDX-FileCopyrightText: 2016-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #include "fullobjectdetection.h"
0019 
0020 namespace Digikam
0021 {
0022 
0023 FullObjectDetection::FullObjectDetection(const cv::Rect& rect_,
0024                                          const std::vector<std::vector<float> >& parts_)
0025     : rect (rect_),
0026       parts(parts_)
0027 {
0028 }
0029 
0030 FullObjectDetection::FullObjectDetection()
0031 {
0032 }
0033 
0034 FullObjectDetection::FullObjectDetection(const cv::Rect& rect_)
0035     : rect(rect_)
0036 {
0037 }
0038 
0039 const cv::Rect& FullObjectDetection::get_rect() const
0040 {
0041     return rect;
0042 }
0043 
0044 cv::Rect& FullObjectDetection::get_rect()
0045 {
0046     return rect;
0047 }
0048 
0049 unsigned long FullObjectDetection::num_parts() const
0050 {
0051     return (unsigned long)parts.size();
0052 }
0053 
0054 const std::vector<float>& FullObjectDetection::part(unsigned long idx) const
0055 {
0056     return parts[idx];
0057 }
0058 
0059 std::vector<float>& FullObjectDetection::part(unsigned long idx)
0060 {
0061     return parts[idx];
0062 }
0063 
0064 // -------------------------------------------------------------------
0065 
0066 std::vector<cv::Rect> getEyes(const FullObjectDetection& shape)
0067 {
0068     std::vector<cv::Rect> eyes;
0069 
0070     for (int j = 0 ; j < 2 ; ++j)
0071     {
0072         int start = j ? 36 : 42;
0073         int end   = j ? 41 : 47;
0074         int tlx, tly, brx, bry;
0075 
0076         // initializing
0077 
0078         std::vector<float> firstpoint = shape.part(start);
0079         tlx                           = (int)firstpoint[0];
0080         brx                           = (int)firstpoint[0];
0081         tly                           = (int)firstpoint[1];
0082         bry                           = (int)firstpoint[1];
0083 
0084         for (int i = start ; i <= end ; ++i)
0085         {
0086             std::vector<float> x = shape.part(i);
0087 
0088             if      (x[0] < tlx)
0089             {
0090                 tlx = (int)x[0];
0091             }
0092             else if (x[0] > brx)
0093             {
0094                 brx = (int)x[0];
0095             }
0096 
0097             if      (x[1] < tly)
0098             {
0099                 tly = (int)x[1];
0100             }
0101             else if (x[1] > bry)
0102             {
0103                 bry = (int)x[1];
0104             }
0105         }
0106 
0107         eyes.push_back(cv::Rect(cv::Point(tlx, tly),
0108                                 cv::Point(brx, bry)));
0109     }
0110 
0111     return eyes;
0112 }
0113 
0114 } // namespace Digikam