File: waldboost_detector.cpp

package info (click to toggle)
opencv 3.2.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 238,480 kB
  • sloc: xml: 901,650; cpp: 703,419; lisp: 20,142; java: 17,843; python: 17,641; ansic: 603; cs: 601; sh: 516; perl: 494; makefile: 117
file content (46 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <opencv2/xobjdetect.hpp>
#include <opencv2/imgcodecs/imgcodecs_c.h>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <cstdio>
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;
        cerr << "       " << argv[0] << " detect <model_filename> <img_filename> <out_filename> <labelling_filename>" << endl;
        return 0;
    }

    string mode = argv[1];
    Ptr<WBDetector> detector = WBDetector::create();
    if (mode == "train") {
        assert(argc == 5);
        detector->train(argv[3], argv[4]);
        FileStorage fs(argv[2], FileStorage::WRITE);
        fs << "waldboost";
        detector->write(fs);
    } else if (mode == "detect") {
        assert(argc == 6);
        vector<Rect> bboxes;
        vector<double> confidences;
        Mat img = imread(argv[3], CV_LOAD_IMAGE_GRAYSCALE);
        FileStorage fs(argv[2], FileStorage::READ);
        detector->read(fs.getFirstTopLevelNode());
        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]);
        }
        for (size_t i = 0; i < bboxes.size(); ++i) {
            rectangle(img, bboxes[i], Scalar(255, 0, 0));
        }
        imwrite(argv[4], img);
    }
}