Renesas GR-PEACH OpenCV Development / gr-peach-opencv-project-sd-card_update

Fork of gr-peach-opencv-project-sd-card by the do

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers predict_collector.cpp Source File

predict_collector.cpp

00001 /*
00002 By downloading, copying, installing or using the software you agree to this license.
00003 If you do not agree to this license, do not download, install,
00004 copy or use the software.
00005 
00006 
00007                           License Agreement
00008                For Open Source Computer Vision Library
00009                        (3-clause BSD License)
00010 
00011 Copyright (C) 2000-2015, Intel Corporation, all rights reserved.
00012 Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
00013 Copyright (C) 2009-2015, NVIDIA Corporation, all rights reserved.
00014 Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
00015 Copyright (C) 2015, OpenCV Foundation, all rights reserved.
00016 Copyright (C) 2015, Itseez Inc., all rights reserved.
00017 Third party copyrights are property of their respective owners.
00018 
00019 Redistribution and use in source and binary forms, with or without modification,
00020 are permitted provided that the following conditions are met:
00021 
00022   * Redistributions of source code must retain the above copyright notice,
00023     this list of conditions and the following disclaimer.
00024 
00025   * Redistributions in binary form must reproduce the above copyright notice,
00026     this list of conditions and the following disclaimer in the documentation
00027     and/or other materials provided with the distribution.
00028 
00029   * Neither the names of the copyright holders nor the names of the contributors
00030     may be used to endorse or promote products derived from this software
00031     without specific prior written permission.
00032 
00033 This software is provided by the copyright holders and contributors "as is" and
00034 any express or implied warranties, including, but not limited to, the implied
00035 warranties of merchantability and fitness for a particular purpose are disclaimed.
00036 In no event shall copyright holders or contributors be liable for any direct,
00037 indirect, incidental, special, exemplary, or consequential damages
00038 (including, but not limited to, procurement of substitute goods or services;
00039 loss of use, data, or profits; or business interruption) however caused
00040 and on any theory of liability, whether in contract, strict liability,
00041 or tort (including negligence or otherwise) arising in any way out of
00042 the use of this software, even if advised of the possibility of such damage.
00043 */
00044 #include "opencv2/face/predict_collector.hpp"
00045 
00046 namespace cv {namespace face {
00047 
00048 static std::pair<int, double> toPair(const StandardCollector::PredictResult & val) {
00049     return std::make_pair(val.label, val.distance);
00050 }
00051 
00052 static bool pairLess(const std::pair<int, double> & lhs, const std::pair<int, double> & rhs) {
00053     return lhs.second < rhs.second;
00054 }
00055 
00056 //===================================
00057 
00058 StandardCollector::StandardCollector(double threshold_) : threshold(threshold_) {
00059     init(0);
00060 }
00061 
00062 void StandardCollector::init(size_t size) {
00063     minRes = PredictResult();
00064     data.clear();
00065     data.reserve(size);
00066 }
00067 
00068 bool StandardCollector::collect(int label, double dist) {
00069     if (dist < threshold)
00070     {
00071         PredictResult res(label, dist);
00072         if (res.distance < minRes.distance)
00073             minRes = res;
00074         data.push_back(res);
00075     }
00076     return true;
00077 }
00078 
00079 int StandardCollector::getMinLabel() const {
00080     return minRes.label;
00081 }
00082 
00083 double StandardCollector::getMinDist() const {
00084     return minRes.distance;
00085 }
00086 
00087 std::vector< std::pair<int, double> > StandardCollector::getResults(bool sorted) const {
00088     std::vector< std::pair<int, double> > res(data.size());
00089     std::transform(data.begin(), data.end(), res.begin(), &toPair);
00090     if (sorted)
00091     {
00092         std::sort(res.begin(), res.end(), &pairLess);
00093     }
00094     return res;
00095 }
00096 
00097 std::map<int, double> StandardCollector::getResultsMap() const {
00098     std::map<int, double> res;
00099     for (std::vector<PredictResult>::const_iterator i = data.begin(); i != data.end(); ++i) {
00100         std::map<int, double>::iterator j = res.find(i->label);
00101         if (j == res.end()) {
00102             res.insert(toPair(*i));
00103         } else if (i->distance < j->second) {
00104             j->second = i->distance;
00105         }
00106     }
00107     return res;
00108 }
00109 
00110 Ptr<StandardCollector> StandardCollector::create(double threshold) {
00111     return makePtr<StandardCollector>(threshold);
00112 }
00113 
00114 }} // cv::face::
00115