1 #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
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"
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__))
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)
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__)
40 #define LOGD(...) do{} while(0)
41 #define LOGI(...) do{} while(0)
42 #define LOGW(...) do{} while(0)
43 #define LOGE(...) do{} while(0)
50 #define SHOULD_USE_EXTERNAL_BUFFERS 1
54 LOGE0("usage: filepattern outfilepattern cascadefile");
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\" ");
64 class CascadeDetectorAdapter: public DetectionBasedTracker::IDetector
67 CascadeDetectorAdapter(cv::Ptr<cv::CascadeClassifier> detector):
73 void detect(const cv::Mat &Image, std::vector<cv::Rect> &objects)
75 Detector->detectMultiScale(Image, objects, 1.1, 3, 0, minObjSize, maxObjSize);
77 virtual ~CascadeDetectorAdapter()
81 CascadeDetectorAdapter();
82 cv::Ptr<cv::CascadeClassifier> Detector;
85 static int test_FaceDetector(int argc, char *argv[])
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);
105 snprintf(filename, sizeof(filename), filepattern, n);
106 LOGD("filename='%s'", filename);
111 LOGI0("Cannot read the file --- break");
114 images.push_back(m0);
116 LOGD("read %d images", (int)images.size());
119 std::string cascadeFrontalfilename=cascadefile;
120 cv::Ptr<cv::CascadeClassifier> cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
121 cv::Ptr<DetectionBasedTracker::IDetector> MainDetector = makePtr<CascadeDetectorAdapter>(cascade);
123 cascade = makePtr<cv::CascadeClassifier>(cascadeFrontalfilename);
124 cv::Ptr<DetectionBasedTracker::IDetector> TrackingDetector = makePtr<CascadeDetectorAdapter>(cascade);
126 DetectionBasedTracker::Parameters params;
127 DetectionBasedTracker fd(MainDetector, TrackingDetector, params);
134 int64 tprev=getTickCount();
135 double freq=getTickFrequency();
137 int num_images=images.size();
138 for(int n=1; n <= num_images; n++)
140 int64 tcur=getTickCount();
143 double t_ms=((double)dt)/freq * 1000.0;
144 LOGD("\n\nSTEP n=%d from prev step %f ms\n", n, t_ms);
146 CV_Assert(! m.empty());
147 cvtColor(m, gray, COLOR_BGR2GRAY);
152 fd.getObjects(result);
154 for(size_t i=0; i < result.size(); i++)
157 CV_Assert(r.area() > 0);
160 Scalar color=Scalar(0, 250, 0);
161 rectangle(m, tl, br, color, 3);
165 char outfilename[256];
166 for(int n=1; n <= num_images; n++)
168 snprintf(outfilename, sizeof(outfilename), outfilepattern, n);
169 LOGD("outfilename='%s'", outfilename);
171 imwrite(outfilename, m);
179 int main(int argc, char *argv[])
181 return test_FaceDetector(argc, argv);
184 #else // #if defined(__linux__) || defined(LINUX) || defined(__APPLE__) || defined(ANDROID)
189 printf("This sample works for UNIX or ANDROID only\n");