waldboost_detector.cpp 1.54 KB
Newer Older
1 2 3 4
#include <opencv2/xobjdetect.hpp>
#include <opencv2/imgcodecs/imgcodecs_c.h>
#include <opencv2/imgproc.hpp>
#include <iostream>
5
#include <cstdio>
6 7 8 9 10 11 12 13
using namespace std;
using namespace cv;
using namespace cv::xobjdetect;

int main(int argc, char **argv)
{
    if (argc < 5) {
        cerr << "Usage: " << argv[0] << " train <model_filename> <pos_path> <neg_path>" << endl;
14
        cerr << "       " << argv[0] << " detect <model_filename> <img_filename> <out_filename> <labelling_filename>" << endl;
15 16 17 18
        return 0;
    }

    string mode = argv[1];
19
    Ptr<WBDetector> detector = WBDetector::create();
20
    if (mode == "train") {
21 22 23
        assert(argc == 5);
        detector->train(argv[3], argv[4]);
        FileStorage fs(argv[2], FileStorage::WRITE);
24
        fs << "waldboost";
25
        detector->write(fs);
26
    } else if (mode == "detect") {
27
        assert(argc == 6);
28 29 30
        vector<Rect> bboxes;
        vector<double> confidences;
        Mat img = imread(argv[3], CV_LOAD_IMAGE_GRAYSCALE);
31
        FileStorage fs(argv[2], FileStorage::READ);
32
        detector->read(fs.getFirstTopLevelNode());
33 34 35 36 37 38 39 40
        detector->detect(img, bboxes, confidences);

        FILE *fhandle = fopen(argv[5], "a");
        for (size_t i = 0; i < bboxes.size(); ++i) {
            Rect o = bboxes[i];
            fprintf(fhandle, "%s;%u;%u;%u;%u;%lf\n",
                argv[3], o.x, o.y, o.width, o.height, confidences[i]);
        }
41 42 43 44 45 46
        for (size_t i = 0; i < bboxes.size(); ++i) {
            rectangle(img, bboxes[i], Scalar(255, 0, 0));
        }
        imwrite(argv[4], img);
    }
}