From e38ba1999b9de2931739cd0e0bc5838f8c2039fb Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Fri, 22 Nov 2013 14:47:58 +0400 Subject: [PATCH 1/4] facedetect --- samples/ocl/facedetect.cpp | 139 ++++++++++++++++++++++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 10c6c4f4d2..7e760dacc8 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -9,6 +9,118 @@ using namespace std; using namespace cv; #define LOOP_NUM 10 +///////////////////////////////////////detectfaces with multithreading//////////////////////////////////////////// +#define MAX_THREADS 8 + +#if defined _WIN32|| defined _WIN64 + #include + #include + HANDLE handleThreads[MAX_THREADS]; +#endif + +#if defined __linux__ || defined __APPLE__ + #include + #include +#endif + +using namespace cv; + +#if defined _WIN32|| defined _WIN64 + void detectFaces(void* str) +#elif defined __linux__ || defined __APPLE__ + void* detectFaces(void* str) +#endif +{ + std::string fileName = *(std::string*)str; + ocl::OclCascadeClassifier cascade; + cascade.load("cv/cascadeandhog/cascades/haarcascade_frontalface_alt.xml" );//path to haarcascade_frontalface_alt.xml + Mat img = imread(fileName, CV_LOAD_IMAGE_COLOR); + if (img.empty()) + { + std::cout << "cann't open file " + fileName < oclfaces; + cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0)); + + for(int i = 0; irun(); + } + int idx; + std::string fileName; +protected: + Thread():thread(NULL){} +}; + +class Thread_Win : public Thread +{ +private: + friend class Thread; + Thread_Win(){} +public: + ~Thread_Win(){}; + void run() + { +#if defined _WIN32|| defined _WIN64 + handleThreads[idx] = (HANDLE)_beginthread(detectFaces, 0, (void*)&fileName); + WaitForMultipleObjects(MAX_THREADS, handleThreads, TRUE, INFINITE); +#endif + } +}; + +class Thread_Lin : public Thread +{ + private: + friend class Thread; + Thread_Lin(){} +public: + ~Thread_Lin(){}; + void run() + { +#if defined __linux__ || defined __APPLE__ + pthread_t thread; + pthread_create(&thread, NULL, detectFaces, (void*)&fileName); + pthread_join (thread, NULL); +#endif + } +}; + +Thread::Thread(int _idx, std::string _fileName) +{ +#if defined _WIN32|| defined _WIN64 + thread = new Thread_Win(); +#endif +#if defined __linux__ || defined __APPLE__ + thread = new Thread_Lin(); +#endif + thread->idx = _idx; + thread->fileName = _fileName; +} + +///////////////////////////simple-threading faces detecting/////////////////////////////// + const static Scalar colors[] = { CV_RGB(0,0,255), CV_RGB(0,128,255), CV_RGB(0,255,255), @@ -58,7 +170,7 @@ static void Draw(Mat& img, vector& faces, double scale); // Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels) double checkRectSimilarity(Size sz, vector& cpu_rst, vector& gpu_rst); -int main( int argc, const char** argv ) +int facedetect_one_thread(int argc, const char** argv ) { const char* keys = "{ h | help | false | print help message }" @@ -176,9 +288,34 @@ int main( int argc, const char** argv ) } cvDestroyWindow("result"); + std::cout<< "simple-threading sample was finished" < threads; + for(int i = 0; irun(); + } + for(int i = 0; i& faces, ocl::OclCascadeClassifier& cascade, double scale, bool calTime) From 0a1ff0d42de3b9b40d5c153a6795e04f9a9da469 Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Mon, 2 Dec 2013 11:15:46 +0400 Subject: [PATCH 2/4] multithreading facedetector --- samples/ocl/facedetect.cpp | 242 ++++++++++++++----------------------- 1 file changed, 89 insertions(+), 153 deletions(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 7e760dacc8..0f452b945f 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -5,121 +5,15 @@ #include #include +#if defined(_MSC_VER) && (_MSC_VER >= 1700) + # include +#endif + using namespace std; using namespace cv; #define LOOP_NUM 10 -///////////////////////////////////////detectfaces with multithreading//////////////////////////////////////////// -#define MAX_THREADS 8 - -#if defined _WIN32|| defined _WIN64 - #include - #include - HANDLE handleThreads[MAX_THREADS]; -#endif - -#if defined __linux__ || defined __APPLE__ - #include - #include -#endif - -using namespace cv; - -#if defined _WIN32|| defined _WIN64 - void detectFaces(void* str) -#elif defined __linux__ || defined __APPLE__ - void* detectFaces(void* str) -#endif -{ - std::string fileName = *(std::string*)str; - ocl::OclCascadeClassifier cascade; - cascade.load("cv/cascadeandhog/cascades/haarcascade_frontalface_alt.xml" );//path to haarcascade_frontalface_alt.xml - Mat img = imread(fileName, CV_LOAD_IMAGE_COLOR); - if (img.empty()) - { - std::cout << "cann't open file " + fileName < oclfaces; - cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0)); - - for(int i = 0; irun(); - } - int idx; - std::string fileName; -protected: - Thread():thread(NULL){} -}; - -class Thread_Win : public Thread -{ -private: - friend class Thread; - Thread_Win(){} -public: - ~Thread_Win(){}; - void run() - { -#if defined _WIN32|| defined _WIN64 - handleThreads[idx] = (HANDLE)_beginthread(detectFaces, 0, (void*)&fileName); - WaitForMultipleObjects(MAX_THREADS, handleThreads, TRUE, INFINITE); -#endif - } -}; - -class Thread_Lin : public Thread -{ - private: - friend class Thread; - Thread_Lin(){} -public: - ~Thread_Lin(){}; - void run() - { -#if defined __linux__ || defined __APPLE__ - pthread_t thread; - pthread_create(&thread, NULL, detectFaces, (void*)&fileName); - pthread_join (thread, NULL); -#endif - } -}; - -Thread::Thread(int _idx, std::string _fileName) -{ -#if defined _WIN32|| defined _WIN64 - thread = new Thread_Win(); -#endif -#if defined __linux__ || defined __APPLE__ - thread = new Thread_Lin(); -#endif - thread->idx = _idx; - thread->fileName = _fileName; -} - -///////////////////////////simple-threading faces detecting/////////////////////////////// +///////////////////////////single-threading faces detecting/////////////////////////////// const static Scalar colors[] = { CV_RGB(0,0,255), CV_RGB(0,128,255), @@ -134,7 +28,7 @@ const static Scalar colors[] = { CV_RGB(0,0,255), int64 work_begin = 0; int64 work_end = 0; -string outputName; +string inputName, outputName, cascadeName; static void workBegin() { @@ -170,35 +64,11 @@ static void Draw(Mat& img, vector& faces, double scale); // Else if will return (total diff of each cpu and gpu rects covered pixels)/(total cpu rects covered pixels) double checkRectSimilarity(Size sz, vector& cpu_rst, vector& gpu_rst); -int facedetect_one_thread(int argc, const char** argv ) +static int facedetect_one_thread(bool useCPU, double scale ) { - const char* keys = - "{ h | help | false | print help message }" - "{ i | input | | specify input image }" - "{ t | template | haarcascade_frontalface_alt.xml |" - " specify template file path }" - "{ c | scale | 1.0 | scale image }" - "{ s | use_cpu | false | use cpu or gpu to process the image }" - "{ o | output | facedetect_output.jpg |" - " specify output image save path(only works when input is images) }"; - - CommandLineParser cmd(argc, argv, keys); - if (cmd.get("help")) - { - cout << "Usage : facedetect [options]" << endl; - cout << "Available options:" << endl; - cmd.printParams(); - return EXIT_SUCCESS; - } - CvCapture* capture = 0; Mat frame, frameCopy, image; - bool useCPU = cmd.get("s"); - string inputName = cmd.get("i"); - outputName = cmd.get("o"); - string cascadeName = cmd.get("t"); - double scale = cmd.get("c"); ocl::OclCascadeClassifier cascade; CascadeClassifier cpu_cascade; @@ -288,32 +158,98 @@ int facedetect_one_thread(int argc, const char** argv ) } cvDestroyWindow("result"); - std::cout<< "simple-threading sample was finished" <= 1700) + +#define MAX_THREADS 10 + +static void detectFaces(std::string fileName) { - std::vector threads; - for(int i = 0; irun(); - } - for(int i = 0; i oclfaces; + cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0)); + + for(unsigned int i = 0; i threads; + for(int i = 0; i= 1 }"; + + CommandLineParser cmd(argc, argv, keys); + if (cmd.get("help")) + { + cout << "Usage : facedetect [options]" << endl; + cout << "Available options:" << endl; + cmd.printParams(); + return EXIT_SUCCESS; + } + bool useCPU = cmd.get("s"); + inputName = cmd.get("i"); + outputName = cmd.get("o"); + cascadeName = cmd.get("t"); + double scale = cmd.get("c"); + int n = cmd.get("n"); + + if(n > 1) + { +#if defined(_MSC_VER) && (_MSC_VER >= 1700) + std::cout<<"multi-threaded sample is running" <& faces, From 7a78559708776b28d33c2ff8a5d46a0699f748f2 Mon Sep 17 00:00:00 2001 From: Konstantin Matskevich Date: Fri, 6 Dec 2013 12:12:24 +0400 Subject: [PATCH 3/4] some fixes --- samples/ocl/facedetect.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 0f452b945f..78a0775151 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -74,7 +74,7 @@ static int facedetect_one_thread(bool useCPU, double scale ) if( !cascade.load( cascadeName ) || !cpu_cascade.load(cascadeName) ) { - cout << "ERROR: Could not load classifier cascade" << endl; + cout << "ERROR: Could not load classifier cascade: " << cascadeName << endl; return EXIT_FAILURE; } @@ -170,7 +170,12 @@ static int facedetect_one_thread(bool useCPU, double scale ) static void detectFaces(std::string fileName) { ocl::OclCascadeClassifier cascade; - cascade.load(cascadeName); + if(!cascade.load(cascadeName)) + { + std::cout << "ERROR: Could not load classifier cascade: " << cascadeName << std::endl; + return; + } + Mat img = imread(fileName, CV_LOAD_IMAGE_COLOR); if (img.empty()) { @@ -187,8 +192,16 @@ static void detectFaces(std::string fileName) for(unsigned int i = 0; i 0 && outputName[n-1] != '.') + n--; + if( n == 0 ) + { + std::cout << "Invalid output file name: " << outputName << std::endl; + return; + } + imwrite(outputName.substr(0,n-1) + "_" + std::to_string(_threadid) + outputName.substr(n-1, outputName.length()-1), img); } static void facedetect_multithreading(int nthreads) From 894724eaf0641a2b511797dbc28cc5a97c486fe0 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Fri, 6 Dec 2013 14:49:36 +0400 Subject: [PATCH 4/4] minor improvements --- samples/ocl/facedetect.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 78a0775151..fbb08cb1e5 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -190,18 +190,21 @@ static void detectFaces(std::string fileName) cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0)); for(unsigned int i = 0; i 0 && outputName[n-1] != '.') - n--; - if( n == 0 ) + std::string::size_type pos = outputName.rfind('.'); + std::string outputNameTid = outputName + '-' + std::to_string(_threadid); + if(pos == std::string::npos) { std::cout << "Invalid output file name: " << outputName << std::endl; - return; } - - imwrite(outputName.substr(0,n-1) + "_" + std::to_string(_threadid) + outputName.substr(n-1, outputName.length()-1), img); + else + { + outputNameTid = outputName.substr(0, pos) + "_" + std::to_string(_threadid) + outputName.substr(pos); + imwrite(outputNameTid, img); + } + imshow(outputNameTid, img); + waitKey(0); } static void facedetect_multithreading(int nthreads) @@ -212,8 +215,6 @@ static void facedetect_multithreading(int nthreads) threads.push_back(std::thread(detectFaces, inputName)); for(int i = 0; i