opencv/modules/softcascade/test/test_cuda_softcascade.cpp

316 lines
9.4 KiB
C++
Raw Normal View History

2012-09-20 20:22:10 +08:00
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2008-2012, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
2012-12-19 16:32:40 +08:00
#include "test_precomp.hpp"
#include "opencv2/core/gpu.hpp"
2012-09-20 20:22:10 +08:00
#ifdef HAVE_CUDA
using std::tr1::get;
2012-09-20 20:22:10 +08:00
// show detection results on input image with cv::imshow
2012-12-19 16:32:40 +08:00
//#define SHOW_DETECTIONS
2012-09-20 20:22:10 +08:00
#if defined SHOW_DETECTIONS
# define SHOW(res) \
cv::imshow(#res, res); \
cv::waitKey(0);
#else
# define SHOW(res)
#endif
2012-09-20 20:22:10 +08:00
static std::string path(std::string relative)
{
return cvtest::TS::ptr()->get_data_path() + "cascadeandhog/" + relative;
}
2012-12-19 16:32:40 +08:00
TEST(SCascadeTest, readCascade)
{
std::string xml = path("cascades/inria_caltech-17.01.2013.xml");
cv::FileStorage fs(xml, cv::FileStorage::READ);
cv::softcascade::SCascade cascade;
2012-12-19 16:32:40 +08:00
ASSERT_TRUE(fs.isOpened());
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
}
2012-12-19 16:32:40 +08:00
namespace
{
typedef cv::softcascade::Detection Detection;
2012-12-19 16:32:40 +08:00
cv::Rect getFromTable(int idx)
{
static const cv::Rect rois[] =
{
2012-11-10 04:49:51 +08:00
cv::Rect( 65 * 4, 20 * 4, 35 * 4, 80 * 4),
cv::Rect( 95 * 4, 35 * 4, 45 * 4, 40 * 4),
cv::Rect( 45 * 4, 35 * 4, 45 * 4, 40 * 4),
cv::Rect( 25 * 4, 27 * 4, 50 * 4, 45 * 4),
cv::Rect(100 * 4, 50 * 4, 45 * 4, 40 * 4),
cv::Rect( 60 * 4, 30 * 4, 45 * 4, 40 * 4),
cv::Rect( 40 * 4, 55 * 4, 50 * 4, 40 * 4),
cv::Rect( 48 * 4, 37 * 4, 72 * 4, 80 * 4),
cv::Rect( 48 * 4, 32 * 4, 85 * 4, 58 * 4),
cv::Rect( 48 * 4, 0 * 4, 32 * 4, 27 * 4)
};
return rois[idx];
}
2012-09-20 20:22:10 +08:00
2012-12-19 16:32:40 +08:00
void print(std::ostream &out, const Detection& d)
{
2012-12-19 16:32:40 +08:00
#if defined SHOW_DETECTIONS
out << "\x1b[32m[ detection]\x1b[0m ("
<< std::setw(4) << d.x
<< " "
<< std::setw(4) << d.y
<< ") ("
<< std::setw(4) << d.w
<< " "
<< std::setw(4) << d.h
<< ") "
<< std::setw(12) << d.confidence
<< std::endl;
2012-12-19 16:32:40 +08:00
#else
(void)out; (void)d;
2012-12-19 16:32:40 +08:00
#endif
}
2012-12-19 16:32:40 +08:00
void printTotal(std::ostream &out, int detbytes)
{
2012-12-19 16:32:40 +08:00
#if defined SHOW_DETECTIONS
out << "\x1b[32m[ ]\x1b[0m Total detections " << (detbytes / sizeof(Detection)) << std::endl;
2012-12-19 16:32:40 +08:00
#else
(void)out; (void)detbytes;
2012-12-19 16:32:40 +08:00
#endif
}
std::string itoa(long i)
{
static char s[65];
sprintf(s, "%ld", i);
return std::string(s);
}
#if defined SHOW_DETECTIONS
2012-12-19 16:32:40 +08:00
std::string getImageName(int level)
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%Y-%m-%d--%H-%M-%S",timeinfo);
return "gpu_rec_level_" + itoa(level)+ "_" + std::string(buffer) + ".png";
}
2012-12-19 16:32:40 +08:00
void writeResult(const cv::Mat& result, const int level)
{
std::string path = cv::tempfile(getImageName(level).c_str());
cv::imwrite(path, result);
std::cout << "\x1b[32m" << "[ ]" << std::endl << "[ stored in]"<< "\x1b[0m" << path << std::endl;
}
#endif
}
class SCascadeTestRoi : public ::testing::TestWithParam<std::tr1::tuple<cv::gpu::DeviceInfo, std::string, std::string, int> >
2012-12-19 16:32:40 +08:00
{
virtual void SetUp()
{
cv::gpu::setDevice(get<0>(GetParam()).deviceID());
}
2012-12-19 16:32:40 +08:00
};
TEST_P(SCascadeTestRoi, Detect)
{
cv::Mat coloredCpu = cv::imread(path(get<2>(GetParam())));
ASSERT_FALSE(coloredCpu.empty());
cv::softcascade::SCascade cascade;
2012-11-10 04:49:51 +08:00
cv::FileStorage fs(path(get<1>(GetParam())), cv::FileStorage::READ);
ASSERT_TRUE(fs.isOpened());
2012-11-10 04:49:51 +08:00
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
cv::gpu::GpuMat colored(coloredCpu), objectBoxes(1, 16384, CV_8UC1), rois(colored.size(), CV_8UC1);
rois.setTo(0);
2012-12-12 07:43:20 +08:00
int nroi = get<3>(GetParam());
cv::Mat result(coloredCpu);
cv::RNG rng;
for (int i = 0; i < nroi; ++i)
{
cv::Rect r = getFromTable(rng(10));
cv::gpu::GpuMat sub(rois, r);
sub.setTo(1);
cv::rectangle(result, r, cv::Scalar(0, 0, 255, 255), 1);
}
objectBoxes.setTo(0);
2012-09-20 20:22:10 +08:00
cascade.detect(colored, rois, objectBoxes);
2012-11-10 04:49:51 +08:00
cv::Mat dt(objectBoxes);
typedef cv::softcascade::Detection Detection;
Detection* dts = ((Detection*)dt.data) + 1;
int* count = dt.ptr<int>(0);
printTotal(std::cout, *count);
2012-12-18 20:59:00 +08:00
for (int i = 0; i < *count; ++i)
{
Detection d = dts[i];
print(std::cout, d);
cv::rectangle(result, cv::Rect(d.x, d.y, d.w, d.h), cv::Scalar(255, 0, 0, 255), 1);
}
SHOW(result);
}
INSTANTIATE_TEST_CASE_P(cuda_accelerated, SCascadeTestRoi, testing::Combine(
ALL_DEVICES,
testing::Values(std::string("cascades/inria_caltech-17.01.2013.xml"),
std::string("cascades/sc_cvpr_2012_to_opencv_new_format.xml")),
testing::Values(std::string("images/image_00000000_0.png")),
2012-12-19 16:32:40 +08:00
testing::Range(0, 5)));
2012-11-10 04:49:51 +08:00
2013-01-30 12:18:05 +08:00
namespace {
struct Fixture
{
std::string path;
int expected;
Fixture(){}
Fixture(std::string p, int e): path(p), expected(e) {}
};
}
typedef std::tr1::tuple<cv::gpu::DeviceInfo, Fixture> SCascadeTestAllFixture;
class SCascadeTestAll : public ::testing::TestWithParam<SCascadeTestAllFixture>
2012-12-19 16:32:40 +08:00
{
protected:
std::string xml;
2013-01-30 12:18:05 +08:00
int expected;
2012-12-19 16:32:40 +08:00
virtual void SetUp()
{
cv::gpu::setDevice(get<0>(GetParam()).deviceID());
xml = path(get<1>(GetParam()).path);
expected = get<1>(GetParam()).expected;
2012-12-19 16:32:40 +08:00
}
};
TEST_P(SCascadeTestAll, detect)
{
cv::softcascade::SCascade cascade;
2012-11-10 04:49:51 +08:00
cv::FileStorage fs(xml, cv::FileStorage::READ);
ASSERT_TRUE(fs.isOpened());
2012-11-10 04:49:51 +08:00
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
cv::Mat coloredCpu = cv::imread(path("images/image_00000000_0.png"));
ASSERT_FALSE(coloredCpu.empty());
cv::gpu::GpuMat colored(coloredCpu), objectBoxes, rois(colored.size(), CV_8UC1);
rois.setTo(1);
cascade.detect(colored, rois, objectBoxes);
2012-10-15 21:38:21 +08:00
typedef cv::softcascade::Detection Detection;
cv::Mat dt(objectBoxes);
Detection* dts = ((Detection*)dt.data) + 1;
int* count = dt.ptr<int>(0);
printTotal(std::cout, *count);
for (int i = 0; i < *count; ++i)
{
Detection d = dts[i];
print(std::cout, d);
cv::rectangle(coloredCpu, cv::Rect(d.x, d.y, d.w, d.h), cv::Scalar(255, 0, 0, 255), 1);
}
SHOW(coloredCpu);
2013-01-30 12:18:05 +08:00
ASSERT_EQ(*count, expected);
2012-10-16 01:55:57 +08:00
}
2012-11-10 07:59:09 +08:00
TEST_P(SCascadeTestAll, detectStream)
2012-11-10 07:59:09 +08:00
{
cv::softcascade::SCascade cascade;
2012-11-10 07:59:09 +08:00
cv::FileStorage fs(xml, cv::FileStorage::READ);
ASSERT_TRUE(fs.isOpened());
2012-11-10 07:59:09 +08:00
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
2012-11-10 07:59:09 +08:00
cv::Mat coloredCpu = cv::imread(path("images/image_00000000_0.png"));
ASSERT_FALSE(coloredCpu.empty());
2012-11-10 07:59:09 +08:00
cv::gpu::GpuMat colored(coloredCpu), objectBoxes(1, 100000, CV_8UC1), rois(colored.size(), CV_8UC1);
2013-01-30 12:18:05 +08:00
rois.setTo(cv::Scalar::all(1));
2012-11-10 07:59:09 +08:00
cv::gpu::Stream s;
2012-11-10 07:59:09 +08:00
objectBoxes.setTo(0);
cascade.detect(colored, rois, objectBoxes, s);
s.waitForCompletion();
2012-11-10 07:59:09 +08:00
typedef cv::softcascade::Detection Detection;
2013-01-30 12:18:05 +08:00
cv::Mat detections(objectBoxes);
int a = *(detections.ptr<int>(0));
ASSERT_EQ(a, expected);
2012-11-10 07:59:09 +08:00
}
INSTANTIATE_TEST_CASE_P(cuda_accelerated, SCascadeTestAll, testing::Combine( ALL_DEVICES,
2013-01-30 12:18:05 +08:00
testing::Values(Fixture("cascades/inria_caltech-17.01.2013.xml", 7),
Fixture("cascades/sc_cvpr_2012_to_opencv_new_format.xml", 1291))));
#endif