<commit_before>/**
 This file is part of Deformable Shape Tracking (DEST).
 
 Copyright <NAME> 2015
 
 Deformable Shape Tracking is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Deformable Shape Tracking is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Deformable Shape Tracking. If not, see <http://www.gnu.org/licenses/>.
 */

#include <dest/dest.h>

#include <dest/face/database_importers.h>
#include <dest/face/face_detector.h>
#include <dest/util/draw.h>
#include <dest/util/convert.h>
#include <random>
#include <opencv2/opencv.hpp>

int main(int argc, char **argv)
{
    
    dest::core::Tracker t;
    if (!t.load(argv[1])) {
        std::cout << "Failed to load tracker." << std::endl;
        return 0;
    }
    
    dest::face::FaceDetector fd;
    if (!fd.loadClassifiers("classifier_frontalface.xml")) {
        std::cout << "Failed to load classifiers." << std::endl;
        return 0;
    }
    
    cv::VideoCapture cap;
    
    if (argc > 2) {
        if (isdigit(argv[2][0])) {
            // Open capture device by index
            cap.open(atoi(argv[2]));
        } else {
            // Open video video
            cap.open(argv[2]);
        }
    } else {
        // Open default device;
        cap.open(0);
    }
    
    if (!cap.isOpened()) {
        std::cout << "Failed to open capture device." << std::endl;
        return 0;
    }
    
    cv::Mat imgCV, grayCV;
    dest::core::Image img;
    bool done = false;
    while (!done) {
        cap >> imgCV;
        
        if (imgCV.empty())
            break;
        
        //cv::resize(imgCV, imgCV, cv::Size(320, 240));
        cv::cvtColor(imgCV, grayCV, CV_BGR2GRAY);
        
        cv::Rect cvRect;
        if (!fd.detectSingleFace(imgCV, cvRect))
            continue;
        
        dest::core::Rect r;
        dest::util::toDest(grayCV, img);
        dest::util::toDest(cvRect, r);
        
        dest::core::ShapeTransform shapeToImage = dest::core::estimateSimilarityTransform(dest::core::unitRectangle(), r);
        dest::core::Shape s = t.predict(img, shapeToImage);

        //dest::util::drawShape(imgCV, steps[0], cv::Scalar(0,255,0));
        dest::util::drawShape(imgCV, s, cv::Scalar(0,0,255));
        //cv::rectangle(imgCV, cvRect, cv::Scalar(0,255,0));
        
        cv::imshow("track", imgCV);
        int key = cv::waitKey(1);
        if (key == 'x')
            done = true;
        
    }
<commit_msg>Refactor towards two tier tracking.<commit_after>/**
 This file is part of Deformable Shape Tracking (DEST).
 
 Copyright <NAME> 2015
 
 Deformable Shape Tracking is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Deformable Shape Tracking is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with Deformable Shape Tracking. If not, see <http://www.gnu.org/licenses/>.
 */

#include <dest/dest.h>

#include <dest/face/database_importers.h>
#include <dest/face/face_detector.h>
#include <dest/util/draw.h>
#include <dest/util/convert.h>
#include <random>
#include <opencv2/opencv.hpp>

int main(int argc, char **argv)
{
    
    dest::core::Tracker t[2];
    if (!t[0].load(argv[1])) {
        std::cout << "Failed to load tier 1tracker." << std::endl;
        return 0;
    }
    
    if (!t[1].load(argv[2])) {
        std::cout << "Failed to load tier 1tracker." << std::endl;
        return 0;
    }
    
    dest::face::FaceDetector fd;
    if (!fd.loadClassifiers("classifier_frontalface.xml")) {
        std::cout << "Failed to load classifiers." << std::endl;
        return 0;
    }
    
    cv::VideoCapture cap;
    
    if (argc > 3) {
        if (isdigit(argv[3][0])) {
            // Open capture device by index
            cap.open(atoi(argv[3]));
        } else {
            // Open video video
            cap.open(argv[3]);
        }
    } else {
        // Open default device;
        cap.open(0);
    }
    
    if (!cap.isOpened()) {
        std::cout << "Failed to open capture device." << std::endl;
        return 0;
    }
    
    cv::Mat imgCV, grayCV;
    dest::core::Image img;
    dest::core::Rect r;
    dest::core::Shape s;
    dest::core::ShapeTransform shapeToImage;
    bool done = false;
    bool detect = false;
    while (!done) {
        cap >> imgCV;
        
        if (imgCV.empty())
            break;
        
        cv::cvtColor(imgCV, grayCV, CV_BGR2GRAY);
        dest::util::toDest(grayCV, img);
        
        if (detect) {
            cv::Rect cvRect;
            if (!fd.detectSingleFace(imgCV, cvRect))
                continue;
            
            dest::util::toDest(cvRect, r);
            shapeToImage = dest::core::estimateSimilarityTransform(dest::core::unitRectangle(), r);
            s = t[0].predict(img, shapeToImage);
            r = dest::core::shapeBounds(s);
            detect = false;
        }
        
        for (int i = 0; i < 5; ++i) {
            shapeToImage = dest::core::estimateSimilarityTransform(dest::core::unitRectangle(), r);
            s = t[1].predict(img, shapeToImage);
            r = dest::core::shapeBounds(s);
        }

        

        //dest::util::drawShape(imgCV, steps[0], cv::Scalar(0,255,0));
        dest::util::drawShape(imgCV, s, cv::Scalar(0,0,255));
        //cv::rectangle(imgCV, cvRect, cv::Scalar(0,255,0));
        
        cv::imshow("track", imgCV);
        int key = cv::waitKey(1);
        if (key == 'x')
            done = true;
        else if (key > 0)
            detect = true;
        
    }

{'fraction_non_alphanumeric': 0.13394553514882837, 'fraction_numerical': 0.013616212792906902, 'mean_word_length': 2.2866805411030175, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1437768', 'n_tokens_mistral': 2010, 'n_tokens_neox': 1930, 'n_words': 550}