diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 10c6c4f4d..7e760dacc 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)