From e719bee2b80639d09acafac9551fc20e9f082d2c Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Wed, 18 Dec 2013 00:15:02 +0400 Subject: [PATCH 1/2] minor refactoring, no functional changes --- samples/ocl/facedetect.cpp | 50 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index fbb08cb1e5..9fafbf3ce1 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -11,7 +11,10 @@ using namespace std; using namespace cv; + #define LOOP_NUM 10 +#define MAX_THREADS 10 + ///////////////////////////single-threading faces detecting/////////////////////////////// @@ -26,23 +29,23 @@ const static Scalar colors[] = { CV_RGB(0,0,255), } ; -int64 work_begin = 0; -int64 work_end = 0; +int64 work_begin[MAX_THREADS] = {0}; +int64 work_end[MAX_THREADS] = {0}; string inputName, outputName, cascadeName; -static void workBegin() +static void workBegin(int i = 0) { - work_begin = getTickCount(); + work_begin[i] = getTickCount(); } -static void workEnd() +static void workEnd(int i = 0) { - work_end += (getTickCount() - work_begin); + work_end[i] += (getTickCount() - work_begin[i]); } -static double getTime() +static double getTime(int i = 0) { - return work_end /((double)cvGetTickFrequency() * 1000.); + return work_end[i] /getTickFrequency() * 1000.; } @@ -96,7 +99,6 @@ static int facedetect_one_thread(bool useCPU, double scale ) } } - cvNamedWindow( "result", 1 ); if( capture ) { cout << "In capture ..." << endl; @@ -125,34 +127,34 @@ static int facedetect_one_thread(bool useCPU, double scale ) } else { - cout << "In image read" << endl; + cout << "In image read " << image.size() << endl; vector faces; vector ref_rst; double accuracy = 0.; - for(int i = 0; i <= LOOP_NUM; i ++) + cout << "loops: "; + for(int i = 0; i <= LOOP_NUM; i++) { - cout << "loop" << i << endl; + cout << i << ", "; if(useCPU) - detectCPU(image, faces, cpu_cascade, scale, i==0?false:true); + detectCPU(image, faces, cpu_cascade, scale, i!=0); else { - detect(image, faces, cascade, scale, i==0?false:true); + detect(image, faces, cascade, scale, i!=0); if(i == 0) { detectCPU(image, ref_rst, cpu_cascade, scale, false); accuracy = checkRectSimilarity(image.size(), ref_rst, faces); } } - if (i == LOOP_NUM) - { - if (useCPU) - cout << "average CPU time (noCamera) : "; - else - cout << "average GPU time (noCamera) : "; - cout << getTime() / LOOP_NUM << " ms" << endl; - cout << "accuracy value: " << accuracy <= 1700) -#define MAX_THREADS 10 - static void detectFaces(std::string fileName) { ocl::OclCascadeClassifier cascade; From 8c6049867394da89e0b5ed3dd5dc98187a87a2b6 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Wed, 18 Dec 2013 17:02:39 +0400 Subject: [PATCH 2/2] adding timing for multi-threaded case --- samples/ocl/facedetect.cpp | 57 +++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/samples/ocl/facedetect.cpp b/samples/ocl/facedetect.cpp index 9fafbf3ce1..378105906e 100644 --- a/samples/ocl/facedetect.cpp +++ b/samples/ocl/facedetect.cpp @@ -30,7 +30,7 @@ const static Scalar colors[] = { CV_RGB(0,0,255), int64 work_begin[MAX_THREADS] = {0}; -int64 work_end[MAX_THREADS] = {0}; +int64 work_total[MAX_THREADS] = {0}; string inputName, outputName, cascadeName; static void workBegin(int i = 0) @@ -40,12 +40,12 @@ static void workBegin(int i = 0) static void workEnd(int i = 0) { - work_end[i] += (getTickCount() - work_begin[i]); + work_total[i] += (getTickCount() - work_begin[i]); } -static double getTime(int i = 0) +static double getTotalTime(int i = 0) { - return work_end[i] /getTickFrequency() * 1000.; + return work_total[i] /getTickFrequency() * 1000.; } @@ -152,7 +152,7 @@ static int facedetect_one_thread(bool useCPU, double scale ) cout << "average CPU time (noCamera) : "; else cout << "average GPU time (noCamera) : "; - cout << getTime() / LOOP_NUM << " ms" << endl; + cout << getTotalTime() / LOOP_NUM << " ms" << endl; cout << "accuracy value: " << accuracy <= 1700) -static void detectFaces(std::string fileName) +static void detectFaces(std::string fileName, int threadNum) { ocl::OclCascadeClassifier cascade; if(!cascade.load(cascadeName)) @@ -179,7 +179,7 @@ static void detectFaces(std::string fileName) 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)); + std::thread::id tid = std::this_thread::get_id(); + std::cout << '[' << threadNum << "] " + << "ThreadID = " << tid + << ", CommandQueue = " << *(void**)ocl::getClCommandQueuePtr() + << endl; + for(int i = 0; i <= LOOP_NUM; i++) + { + if(i>0) workBegin(threadNum); + cascade.detectMultiScale(d_img, oclfaces, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30), Size(0, 0)); + if(i>0) workEnd(threadNum); + } + std::cout << '[' << threadNum << "] " << "Average time = " << getTotalTime(threadNum) / LOOP_NUM << " ms" << endl; for(unsigned int i = 0; i threads; for(int i = 0; i= 1 }"; CommandLineParser cmd(argc, argv, keys); @@ -314,8 +327,8 @@ void Draw(Mat& img, vector& faces, double scale) radius = cvRound((r->width + r->height)*0.25*scale); circle( img, center, radius, color, 3, 8, 0 ); } - imwrite( outputName, img ); - if(abs(scale-1.0)>.001) + if( !outputName.empty() ) imwrite( outputName, img ); + if( abs(scale-1.0)>.001 ) { resize(img, img, Size((int)(img.cols/scale), (int)(img.rows/scale))); }