1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other materials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #include "perf_precomp.hpp"
44 
45 namespace opencv_test { namespace {
46 
47 ///////////////////////////////////////////////////////////////
48 // HOG
49 
50 DEF_PARAM_TEST_1(Image, string);
51 
52 PERF_TEST_P(Image, ObjDetect_HOG,
53             Values<string>("gpu/hog/road.png",
54                            "gpu/caltech/image_00000009_0.png",
55                            "gpu/caltech/image_00000032_0.png",
56                            "gpu/caltech/image_00000165_0.png",
57                            "gpu/caltech/image_00000261_0.png",
58                            "gpu/caltech/image_00000469_0.png",
59                            "gpu/caltech/image_00000527_0.png",
60                            "gpu/caltech/image_00000574_0.png"))
61 {
62     declare.time(300.0);
63 
64     const cv::Mat img = readImage(GetParam(), cv::IMREAD_GRAYSCALE);
65     ASSERT_FALSE(img.empty());
66 
67     if (PERF_RUN_CUDA())
68     {
69         const cv::cuda::GpuMat d_img(img);
70         std::vector<cv::Rect> gpu_found_locations;
71 
72         cv::Ptr<cv::cuda::HOG> d_hog = cv::cuda::HOG::create();
73         d_hog->setSVMDetector(d_hog->getDefaultPeopleDetector());
74 
75         TEST_CYCLE() d_hog->detectMultiScale(d_img, gpu_found_locations);
76 
77         SANITY_CHECK(gpu_found_locations);
78     }
79     else
80     {
81         std::vector<cv::Rect> cpu_found_locations;
82 
83         cv::Ptr<cv::cuda::HOG> d_hog = cv::cuda::HOG::create();
84 
85         cv::HOGDescriptor hog;
86         hog.setSVMDetector(d_hog->getDefaultPeopleDetector());
87 
88         TEST_CYCLE() hog.detectMultiScale(img, cpu_found_locations);
89 
90         SANITY_CHECK(cpu_found_locations);
91     }
92 }
93 
94 ///////////////////////////////////////////////////////////////
95 // HaarClassifier
96 
97 typedef pair<string, string> pair_string;
98 DEF_PARAM_TEST_1(ImageAndCascade, pair_string);
99 
100 PERF_TEST_P(ImageAndCascade, ObjDetect_HaarClassifier,
101             Values<pair_string>(make_pair("gpu/haarcascade/group_1_640x480_VGA.pgm", "gpu/perf/haarcascade_frontalface_alt.xml")))
102 {
103     const cv::Mat img = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
104     ASSERT_FALSE(img.empty());
105 
106     if (PERF_RUN_CUDA())
107     {
108         cv::Ptr<cv::cuda::CascadeClassifier> d_cascade =
109                 cv::cuda::CascadeClassifier::create(perf::TestBase::getDataPath(GetParam().second));
110 
111         const cv::cuda::GpuMat d_img(img);
112         cv::cuda::GpuMat objects_buffer;
113 
114         TEST_CYCLE() d_cascade->detectMultiScale(d_img, objects_buffer);
115 
116         std::vector<cv::Rect> gpu_rects;
117         d_cascade->convert(objects_buffer, gpu_rects);
118 
119         cv::groupRectangles(gpu_rects, 3, 0.2);
120         SANITY_CHECK(gpu_rects);
121     }
122     else
123     {
124         cv::CascadeClassifier cascade;
125         ASSERT_TRUE(cascade.load(perf::TestBase::getDataPath("gpu/perf/haarcascade_frontalface_alt.xml")));
126 
127         std::vector<cv::Rect> cpu_rects;
128 
129         TEST_CYCLE() cascade.detectMultiScale(img, cpu_rects);
130 
131         SANITY_CHECK(cpu_rects);
132     }
133 }
134 
135 ///////////////////////////////////////////////////////////////
136 // LBP cascade
137 
138 PERF_TEST_P(ImageAndCascade, ObjDetect_LBPClassifier,
139             Values<pair_string>(make_pair("gpu/haarcascade/group_1_640x480_VGA.pgm", "gpu/lbpcascade/lbpcascade_frontalface.xml")))
140 {
141     const cv::Mat img = readImage(GetParam().first, cv::IMREAD_GRAYSCALE);
142     ASSERT_FALSE(img.empty());
143 
144     if (PERF_RUN_CUDA())
145     {
146         cv::Ptr<cv::cuda::CascadeClassifier> d_cascade =
147                 cv::cuda::CascadeClassifier::create(perf::TestBase::getDataPath(GetParam().second));
148 
149         const cv::cuda::GpuMat d_img(img);
150         cv::cuda::GpuMat objects_buffer;
151 
152         TEST_CYCLE() d_cascade->detectMultiScale(d_img, objects_buffer);
153 
154         std::vector<cv::Rect> gpu_rects;
155         d_cascade->convert(objects_buffer, gpu_rects);
156 
157         cv::groupRectangles(gpu_rects, 3, 0.2);
158         SANITY_CHECK(gpu_rects);
159     }
160     else
161     {
162         cv::CascadeClassifier cascade;
163         ASSERT_TRUE(cascade.load(perf::TestBase::getDataPath("gpu/lbpcascade/lbpcascade_frontalface.xml")));
164 
165         std::vector<cv::Rect> cpu_rects;
166 
167         TEST_CYCLE() cascade.detectMultiScale(img, cpu_rects);
168 
169         SANITY_CHECK(cpu_rects);
170     }
171 }
172 
173 }} // namespace
174