1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
19 // Jia Haipeng, jiahaipeng95@gmail.com
20 // Sen Liu, swjutls1987@126.com
22 // Redistribution and use in source and binary forms, with or without modification,
23 // are permitted provided that the following conditions are met:
25 // * Redistribution's of source code must retain the above copyright notice,
26 // this list of conditions and the following disclaimer.
28 // * Redistribution's in binary form must reproduce the above copyright notice,
29 // this list of conditions and the following disclaimer in the documentation
30 // and/or other oclMaterials provided with the distribution.
32 // * The name of the copyright holders may not be used to endorse or promote products
33 // derived from this software without specific prior written permission.
35 // This software is provided by the copyright holders and contributors "as is" and
36 // any express or implied warranties, including, but not limited to, the implied
37 // warranties of merchantability and fitness for a particular purpose are disclaimed.
38 // In no event shall the Intel Corporation or contributors be liable for any direct,
39 // indirect, incidental, special, exemplary, or consequential damages
40 // (including, but not limited to, procurement of substitute goods or services;
41 // loss of use, data, or profits; or business interruption) however caused
42 // and on any theory of liability, whether in contract, strict liability,
43 // or tort (including negligence or otherwise) arising in any way out of
44 // the use of this software, even if advised of the possibility of such damage.
48 #include "opencv2/objdetect/objdetect.hpp"
49 #include "precomp.hpp"
53 using namespace cvtest;
54 using namespace testing;
57 extern string workdir;
61 IMPLEMENT_PARAM_CLASS(CascadeName, std::string);
62 CascadeName cascade_frontalface_alt(std::string("haarcascade_frontalface_alt.xml"));
63 CascadeName cascade_frontalface_alt2(std::string("haarcascade_frontalface_alt2.xml"));
66 Rect operator ()(const CvAvgComp &e) const
73 PARAM_TEST_CASE(Haar, double, int, CascadeName)
75 cv::ocl::OclCascadeClassifier cascade, nestedCascade;
76 cv::CascadeClassifier cpucascade, cpunestedCascade;
80 std::string cascadeName;
86 cascadeName = (workdir + "../../data/haarcascades/").append(GET_PARAM(2));
88 if( (!cascade.load( cascadeName )) || (!cpucascade.load(cascadeName)) )
90 cout << "ERROR: Could not load classifier cascade" << endl;
96 ////////////////////////////////faceDetect/////////////////////////////////////////////////
97 TEST_P(Haar, FaceDetect)
99 string imgName = workdir + "lena.jpg";
100 Mat img = imread( imgName, 1 );
104 std::cout << "Couldn't read " << imgName << std::endl;
108 vector<Rect> faces, oclfaces;
110 Mat gray, smallImg(cvRound (img.rows / scale), cvRound(img.cols / scale), CV_8UC1 );
111 MemStorage storage(cvCreateMemStorage(0));
112 cvtColor( img, gray, CV_BGR2GRAY );
113 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
114 equalizeHist( smallImg, smallImg );
116 cv::ocl::oclMat image;
118 image.upload(smallImg);
119 _objects = cascade.oclHaarDetectObjects( image, storage, 1.1,
120 3, flags, Size(30, 30), Size(0, 0) );
121 vector<CvAvgComp> vecAvgComp;
122 Seq<CvAvgComp>(_objects).copyTo(vecAvgComp);
123 oclfaces.resize(vecAvgComp.size());
124 std::transform(vecAvgComp.begin(), vecAvgComp.end(), oclfaces.begin(), getRect());
126 cpucascade.detectMultiScale( smallImg, faces, 1.1, 3,
128 Size(30, 30), Size(0, 0) );
129 EXPECT_EQ(faces.size(), oclfaces.size());
132 TEST_P(Haar, FaceDetectUseBuf)
134 string imgName = workdir + "lena.jpg";
135 Mat img = imread( imgName, 1 );
139 std::cout << "Couldn't read " << imgName << std::endl;
143 vector<Rect> faces, oclfaces;
145 Mat gray, smallImg(cvRound (img.rows / scale), cvRound(img.cols / scale), CV_8UC1 );
146 cvtColor( img, gray, CV_BGR2GRAY );
147 resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
148 equalizeHist( smallImg, smallImg );
150 cv::ocl::oclMat image;
151 image.upload(smallImg);
153 cv::ocl::OclCascadeClassifierBuf cascadebuf;
154 if( !cascadebuf.load( cascadeName ) )
156 cout << "ERROR: Could not load classifier cascade for FaceDetectUseBuf!" << endl;
159 cascadebuf.detectMultiScale( image, oclfaces, 1.1, 3,
161 Size(30, 30), Size(0, 0) );
163 cpucascade.detectMultiScale( smallImg, faces, 1.1, 3,
165 Size(30, 30), Size(0, 0) );
166 EXPECT_EQ(faces.size(), oclfaces.size());
168 // intentionally run ocl facedetect again and check if it still works after the first run
169 cascadebuf.detectMultiScale( image, oclfaces, 1.1, 3,
172 cascadebuf.release();
173 EXPECT_EQ(faces.size(), oclfaces.size());
176 INSTANTIATE_TEST_CASE_P(FaceDetect, Haar,
178 Values(CV_HAAR_SCALE_IMAGE, 0), Values(cascade_frontalface_alt, cascade_frontalface_alt2)));
180 #endif // HAVE_OPENCL