81afa789c3a5dcfa25a454c6210a0d6b04ba6084
[platform/upstream/opencv.git] / samples / cpp / detection_based_tracker_sample.cpp
1 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
2
3 #include <opencv2/core.hpp>
4 #include <opencv2/core/utility.hpp>
5 #include <opencv2/imgproc.hpp>
6 #include <opencv2/highgui.hpp>
7 #include <opencv2/objdetect.hpp>
8 #include "opencv2/contrib/detection_based_tracker.hpp"
9
10 #include <vector>
11 #include <iostream>
12 #include <stdio.h>
13
14 #define DEBUGLOGS 1
15
16
17 #ifdef ANDROID
18 #include <android/log.h>
19 #define LOG_TAG "DETECTIONBASEDTRACKER__TEST_APPLICAT"
20 #define LOGD0(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
21 #define LOGI0(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
22 #define LOGW0(...) ((void)__android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__))
23 #define LOGE0(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
24 #else
25
26 #include <stdio.h>
27
28 #define LOGD0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
29 #define LOGI0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
30 #define LOGW0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
31 #define LOGE0(_str, ...) do{printf(_str , ## __VA_ARGS__); printf("\n");fflush(stdout);} while(0)
32 #endif
33
34 #if DEBUGLOGS
35 #define LOGD(_str, ...) LOGD0(_str , ## __VA_ARGS__)
36 #define LOGI(_str, ...) LOGI0(_str , ## __VA_ARGS__)
37 #define LOGW(_str, ...) LOGW0(_str , ## __VA_ARGS__)
38 #define LOGE(_str, ...) LOGE0(_str , ## __VA_ARGS__)
39 #else
40 #define LOGD(...) do{} while(0)
41 #define LOGI(...) do{} while(0)
42 #define LOGW(...) do{} while(0)
43 #define LOGE(...) do{} while(0)
44 #endif
45
46 using namespace cv;
47 using namespace std;
48
49 #define ORIGINAL 0
50 #define SHOULD_USE_EXTERNAL_BUFFERS 1
51
52 static void usage()
53 {
54     LOGE0("usage: filepattern outfilepattern cascadefile");
55     LOGE0("\t where ");
56     LOGE0("\t filepattern --- pattern for the paths to the source images");
57     LOGE0("\t       (e.g.\"./Videos/FACESJPG2/Faces2_%%08d.jpg\" ");
58     LOGE0("\t outfilepattern --- pattern for the paths for images which will be generated");
59     LOGE0("\t       (e.g.\"./resFaces2_%%08d.jpg\" ");
60     LOGE0("\t cascadefile --- path to the cascade file");
61     LOGE0("\t       (e.g.\"opencv/data/lbpcascades/lbpcascade_frontalface.xml\" ");
62 }
63
64 class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
65 {
66     public:
67         CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
68             Detector(detector)
69         {
70             CV_Assert(detector);
71         }
72
73         void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
74         {
75             Detector->detectMultiScale(Image, objects, 1.1, 3, 0, minObjSize, maxObjSize);
76         }
77         virtual ~CascadeDetectorAdapter()
78         {}
79
80     private:
81         CascadeDetectorAdapter();
82         cv::Ptr<cv::CascadeClassifier> Detector;
83  };
84
85 static int test_FaceDetector(int argc, char *argv[])
86 {
87     if (argc < 4)
88     {
89         usage();
90         return -1;
91     }
92
93     const char* filepattern=argv[1];
94     const char* outfilepattern=argv[2];
95     const char* cascadefile=argv[3];
96     LOGD0("filepattern='%s'", filepattern);
97     LOGD0("outfilepattern='%s'", outfilepattern);
98     LOGD0("cascadefile='%s'", cascadefile);
99
100     vector<Mat> images;
101     {
102         char filename[256];
103         for(int n=1; ; n++)
104         {
105             snprintf(filename, sizeof(filename), filepattern, n);
106             LOGD("filename='%s'", filename);
107             Mat m0;
108             m0=imread(filename);
109             if (m0.empty())
110             {
111                 LOGI0("Cannot read the file --- break");
112                 break;
113             }
114             images.push_back(m0);
115         }
116         LOGD("read %d images", (int)images.size());
117     }
118
119     std::string cascadeFrontalfilename=cascadefile;
120     cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
121     cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);
122
123     cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
124     cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);
125
126     DetectionBasedTracker::Parameters params;
127     DetectionBasedTracker fd(MainDetector, TrackingDetector, params);
128
129     fd.run();
130
131     Mat gray;
132     Mat m;
133
134     int64 tprev=getTickCount();
135     double freq=getTickFrequency();
136
137     int num_images=images.size();
138     for(int n=1; n <= num_images; n++)
139     {
140         int64 tcur=getTickCount();
141         int64 dt=tcur-tprev;
142         tprev=tcur;
143         double t_ms=((double)dt)/freq * 1000.0;
144         LOGD("\n\nSTEP n=%d        from prev step %f ms\n", n, t_ms);
145         m=images[n-1];
146         CV_Assert(! m.empty());
147         cvtColor(m, gray, COLOR_BGR2GRAY);
148
149         fd.process(gray);
150
151         vector<Rect> result;
152         fd.getObjects(result);
153
154         for(size_t i=0; i < result.size(); i++)
155         {
156             Rect r=result[i];
157             CV_Assert(r.area() > 0);
158             Point tl=r.tl();
159             Point br=r.br();
160             Scalar color=Scalar(0, 250, 0);
161             rectangle(m, tl, br, color, 3);
162         }
163     }
164
165     char outfilename[256];
166     for(int n=1; n <= num_images; n++)
167     {
168         snprintf(outfilename, sizeof(outfilename), outfilepattern, n);
169         LOGD("outfilename='%s'", outfilename);
170         m=images[n-1];
171         imwrite(outfilename, m);
172     }
173
174     fd.stop();
175
176     return 0;
177 }
178
179 int main(int argc, char *argv[])
180 {
181     return test_FaceDetector(argc, argv);
182 }
183
184 #else // #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
185
186 #include <stdio.h>
187 int main()
188 {
189     printf("This sample works for UNIX or ANDROID only\n");
190     return 0;
191 }
192
193 #endif