Merge pull request #661 from cuda-geek:merge-cpu-gpu-detections
This commit is contained in:
commit
3f8d87d82c
@ -49,18 +49,21 @@
|
|||||||
namespace cv { namespace softcascade {
|
namespace cv { namespace softcascade {
|
||||||
|
|
||||||
// Representation of detectors result.
|
// Representation of detectors result.
|
||||||
|
// We assume that image is less then 2^16x2^16.
|
||||||
struct CV_EXPORTS Detection
|
struct CV_EXPORTS Detection
|
||||||
{
|
{
|
||||||
// Default object type.
|
|
||||||
enum {PEDESTRIAN = 1};
|
|
||||||
|
|
||||||
// Creates Detection from an object bounding box and confidence.
|
// Creates Detection from an object bounding box and confidence.
|
||||||
// Param b is a bounding box
|
// Param b is a bounding box
|
||||||
// Param c is a confidence that object belongs to class k
|
// Param c is a confidence that object belongs to class k
|
||||||
// Param k is an object class
|
// Param k is an object class
|
||||||
Detection(const cv::Rect& b, const float c, int k = PEDESTRIAN) : bb(b), confidence(c), kind(k) {}
|
Detection(const cv::Rect& b, const float c, int k = PEDESTRIAN);
|
||||||
|
cv::Rect bb() const;
|
||||||
|
enum {PEDESTRIAN = 1};
|
||||||
|
|
||||||
cv::Rect bb;
|
ushort x;
|
||||||
|
ushort y;
|
||||||
|
ushort w;
|
||||||
|
ushort h;
|
||||||
float confidence;
|
float confidence;
|
||||||
int kind;
|
int kind;
|
||||||
};
|
};
|
||||||
@ -247,19 +250,6 @@ class CV_EXPORTS SCascade : public cv::Algorithm
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// Representation of detectors result.
|
|
||||||
struct CV_EXPORTS Detection
|
|
||||||
{
|
|
||||||
ushort x;
|
|
||||||
ushort y;
|
|
||||||
ushort w;
|
|
||||||
ushort h;
|
|
||||||
float confidence;
|
|
||||||
int kind;
|
|
||||||
|
|
||||||
enum {PEDESTRIAN = 0};
|
|
||||||
};
|
|
||||||
|
|
||||||
enum { NO_REJECT = 1, DOLLAR = 2, /*PASCAL = 4,*/ DEFAULT = NO_REJECT, NMS_MASK = 0xF};
|
enum { NO_REJECT = 1, DOLLAR = 2, /*PASCAL = 4,*/ DEFAULT = NO_REJECT, NMS_MASK = 0xF};
|
||||||
|
|
||||||
// An empty cascade will be created.
|
// An empty cascade will be created.
|
||||||
|
@ -27,8 +27,8 @@ void fixture##_##name::__cpu() { FAIL() << "No such CPU implementation analogy";
|
|||||||
namespace {
|
namespace {
|
||||||
struct DetectionLess
|
struct DetectionLess
|
||||||
{
|
{
|
||||||
bool operator()(const cv::softcascade::SCascade::Detection& a,
|
bool operator()(const cv::softcascade::Detection& a,
|
||||||
const cv::softcascade::SCascade::Detection& b) const
|
const cv::softcascade::Detection& b) const
|
||||||
{
|
{
|
||||||
if (a.x != b.x) return a.x < b.x;
|
if (a.x != b.x) return a.x < b.x;
|
||||||
else if (a.y != b.y) return a.y < b.y;
|
else if (a.y != b.y) return a.y < b.y;
|
||||||
@ -41,7 +41,7 @@ namespace {
|
|||||||
{
|
{
|
||||||
cv::Mat detections(objects);
|
cv::Mat detections(objects);
|
||||||
|
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
typedef cv::softcascade::Detection Detection;
|
||||||
Detection* begin = (Detection*)(detections.ptr<char>(0));
|
Detection* begin = (Detection*)(detections.ptr<char>(0));
|
||||||
Detection* end = (Detection*)(detections.ptr<char>(0) + detections.cols);
|
Detection* end = (Detection*)(detections.ptr<char>(0) + detections.cols);
|
||||||
std::sort(begin, end, DetectionLess());
|
std::sort(begin, end, DetectionLess());
|
||||||
@ -73,7 +73,7 @@ RUN_GPU(SCascadeTest, detect)
|
|||||||
|
|
||||||
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
||||||
|
|
||||||
cv::gpu::GpuMat objectBoxes(1, 10000 * sizeof(cv::softcascade::SCascade::Detection), CV_8UC1), rois(colored.size(), CV_8UC1);
|
cv::gpu::GpuMat objectBoxes(1, 10000 * sizeof(cv::softcascade::Detection), CV_8UC1), rois(colored.size(), CV_8UC1);
|
||||||
rois.setTo(1);
|
rois.setTo(1);
|
||||||
|
|
||||||
cascade.detect(colored, rois, objectBoxes);
|
cascade.detect(colored, rois, objectBoxes);
|
||||||
@ -215,7 +215,7 @@ RUN_GPU(SCascadeTest, detectStream)
|
|||||||
|
|
||||||
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
ASSERT_TRUE(cascade.load(fs.getFirstTopLevelNode()));
|
||||||
|
|
||||||
cv::gpu::GpuMat objectBoxes(1, 10000 * sizeof(cv::softcascade::SCascade::Detection), CV_8UC1), rois(colored.size(), CV_8UC1);
|
cv::gpu::GpuMat objectBoxes(1, 10000 * sizeof(cv::softcascade::Detection), CV_8UC1), rois(colored.size(), CV_8UC1);
|
||||||
rois.setTo(1);
|
rois.setTo(1);
|
||||||
|
|
||||||
cv::gpu::Stream s;
|
cv::gpu::Stream s;
|
||||||
|
@ -17,7 +17,7 @@ void extractRacts(std::vector<Detection> objectBoxes, std::vector<Rect>& rects)
|
|||||||
{
|
{
|
||||||
rects.clear();
|
rects.clear();
|
||||||
for (int i = 0; i < (int)objectBoxes.size(); ++i)
|
for (int i = 0; i < (int)objectBoxes.size(); ++i)
|
||||||
rects.push_back(objectBoxes[i].bb);
|
rects.push_back(objectBoxes[i].bb());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -42,11 +42,14 @@
|
|||||||
|
|
||||||
#include "precomp.hpp"
|
#include "precomp.hpp"
|
||||||
|
|
||||||
using cv::softcascade::Detection;
|
cv::softcascade::Detection::Detection(const cv::Rect& b, const float c, int k)
|
||||||
using cv::softcascade::Detector;
|
: x(static_cast<ushort>(b.x)), y(static_cast<ushort>(b.y)),
|
||||||
using cv::softcascade::ChannelFeatureBuilder;
|
w(static_cast<ushort>(b.width)), h(static_cast<ushort>(b.height)), confidence(c), kind(k) {}
|
||||||
|
|
||||||
using namespace cv;
|
cv::Rect cv::softcascade::Detection::bb() const
|
||||||
|
{
|
||||||
|
return cv::Rect(x, y, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -151,13 +154,13 @@ struct Level
|
|||||||
scaleshift = static_cast<int>(relScale * (1 << 16));
|
scaleshift = static_cast<int>(relScale * (1 << 16));
|
||||||
}
|
}
|
||||||
|
|
||||||
void addDetection(const int x, const int y, float confidence, std::vector<Detection>& detections) const
|
void addDetection(const int x, const int y, float confidence, std::vector<cv::softcascade::Detection>& detections) const
|
||||||
{
|
{
|
||||||
// fix me
|
// fix me
|
||||||
int shrinkage = 4;//(*octave).shrinkage;
|
int shrinkage = 4;//(*octave).shrinkage;
|
||||||
cv::Rect rect(cvRound(x * shrinkage), cvRound(y * shrinkage), objSize.width, objSize.height);
|
cv::Rect rect(cvRound(x * shrinkage), cvRound(y * shrinkage), objSize.width, objSize.height);
|
||||||
|
|
||||||
detections.push_back(Detection(rect, confidence));
|
detections.push_back(cv::softcascade::Detection(rect, confidence));
|
||||||
}
|
}
|
||||||
|
|
||||||
float rescale(cv::Rect& scaledRect, const float threshold, int idx) const
|
float rescale(cv::Rect& scaledRect, const float threshold, int idx) const
|
||||||
@ -183,7 +186,7 @@ struct ChannelStorage
|
|||||||
size_t step;
|
size_t step;
|
||||||
int model_height;
|
int model_height;
|
||||||
|
|
||||||
cv::Ptr<ChannelFeatureBuilder> builder;
|
cv::Ptr<cv::softcascade::ChannelFeatureBuilder> builder;
|
||||||
|
|
||||||
enum {HOG_BINS = 6, HOG_LUV_BINS = 10};
|
enum {HOG_BINS = 6, HOG_LUV_BINS = 10};
|
||||||
|
|
||||||
@ -192,7 +195,7 @@ struct ChannelStorage
|
|||||||
model_height = cvRound(colored.rows / (float)shrinkage);
|
model_height = cvRound(colored.rows / (float)shrinkage);
|
||||||
if (featureTypeStr == "ICF") featureTypeStr = "HOG6MagLuv";
|
if (featureTypeStr == "ICF") featureTypeStr = "HOG6MagLuv";
|
||||||
|
|
||||||
builder = ChannelFeatureBuilder::create(featureTypeStr);
|
builder = cv::softcascade::ChannelFeatureBuilder::create(featureTypeStr);
|
||||||
(*builder)(colored, hog, cv::Size(cvRound(colored.cols / (float)shrinkage), model_height));
|
(*builder)(colored, hog, cv::Size(cvRound(colored.cols / (float)shrinkage), model_height));
|
||||||
|
|
||||||
step = hog.step1();
|
step = hog.step1();
|
||||||
@ -213,8 +216,7 @@ struct ChannelStorage
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct cv::softcascade::Detector::Fields
|
||||||
struct Detector::Fields
|
|
||||||
{
|
{
|
||||||
float minScale;
|
float minScale;
|
||||||
float maxScale;
|
float maxScale;
|
||||||
@ -421,17 +423,17 @@ struct Detector::Fields
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Detector::Detector(const double mins, const double maxs, const int nsc, const int rej)
|
cv::softcascade::Detector::Detector(const double mins, const double maxs, const int nsc, const int rej)
|
||||||
: fields(0), minScale(mins), maxScale(maxs), scales(nsc), rejCriteria(rej) {}
|
: fields(0), minScale(mins), maxScale(maxs), scales(nsc), rejCriteria(rej) {}
|
||||||
|
|
||||||
Detector::~Detector() { delete fields;}
|
cv::softcascade::Detector::~Detector() { delete fields;}
|
||||||
|
|
||||||
void Detector::read(const cv::FileNode& fn)
|
void cv::softcascade::Detector::read(const cv::FileNode& fn)
|
||||||
{
|
{
|
||||||
Algorithm::read(fn);
|
Algorithm::read(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Detector::load(const cv::FileNode& fn)
|
bool cv::softcascade::Detector::load(const cv::FileNode& fn)
|
||||||
{
|
{
|
||||||
if (fields) delete fields;
|
if (fields) delete fields;
|
||||||
|
|
||||||
@ -473,7 +475,7 @@ void DollarNMS(dvector& objects)
|
|||||||
{
|
{
|
||||||
const Detection &b = *next;
|
const Detection &b = *next;
|
||||||
|
|
||||||
const float ovl = overlap(a.bb, b.bb) / std::min(a.bb.area(), b.bb.area());
|
const float ovl = overlap(a.bb(), b.bb()) / std::min(a.bb().area(), b.bb().area());
|
||||||
|
|
||||||
if (ovl > DollarThreshold)
|
if (ovl > DollarThreshold)
|
||||||
next = objects.erase(next);
|
next = objects.erase(next);
|
||||||
@ -485,13 +487,13 @@ void DollarNMS(dvector& objects)
|
|||||||
|
|
||||||
static void suppress(int type, std::vector<Detection>& objects)
|
static void suppress(int type, std::vector<Detection>& objects)
|
||||||
{
|
{
|
||||||
CV_Assert(type == Detector::DOLLAR);
|
CV_Assert(type == cv::softcascade::Detector::DOLLAR);
|
||||||
DollarNMS(objects);
|
DollarNMS(objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Detector::detectNoRoi(const cv::Mat& image, std::vector<Detection>& objects) const
|
void cv::softcascade::Detector::detectNoRoi(const cv::Mat& image, std::vector<Detection>& objects) const
|
||||||
{
|
{
|
||||||
Fields& fld = *fields;
|
Fields& fld = *fields;
|
||||||
// create integrals
|
// create integrals
|
||||||
@ -518,7 +520,7 @@ void Detector::detectNoRoi(const cv::Mat& image, std::vector<Detection>& objects
|
|||||||
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
|
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Detector::detect(cv::InputArray _image, cv::InputArray _rois, std::vector<Detection>& objects) const
|
void cv::softcascade::Detector::detect(cv::InputArray _image, cv::InputArray _rois, std::vector<Detection>& objects) const
|
||||||
{
|
{
|
||||||
// only color images are suppered
|
// only color images are suppered
|
||||||
cv::Mat image = _image.getMat();
|
cv::Mat image = _image.getMat();
|
||||||
@ -570,7 +572,7 @@ void Detector::detect(cv::InputArray _image, cv::InputArray _rois, std::vector<D
|
|||||||
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
|
if (rejCriteria != NO_REJECT) suppress(rejCriteria, objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Detector::detect(InputArray _image, InputArray _rois, OutputArray _rects, OutputArray _confs) const
|
void cv::softcascade::Detector::detect(InputArray _image, InputArray _rois, OutputArray _rects, OutputArray _confs) const
|
||||||
{
|
{
|
||||||
std::vector<Detection> objects;
|
std::vector<Detection> objects;
|
||||||
detect( _image, _rois, objects);
|
detect( _image, _rois, objects);
|
||||||
@ -588,7 +590,7 @@ void Detector::detect(InputArray _image, InputArray _rois, OutputArray _rects,
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (IDet it = objects.begin(); it != objects.end(); ++it, ++i)
|
for (IDet it = objects.begin(); it != objects.end(); ++it, ++i)
|
||||||
{
|
{
|
||||||
rectPtr[i] = (*it).bb;
|
rectPtr[i] = (*it).bb();
|
||||||
confPtr[i] = (*it).confidence;
|
confPtr[i] = (*it).confidence;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -76,7 +76,7 @@ TEST(SCascadeTest, readCascade)
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
typedef cv::softcascade::Detection Detection;
|
||||||
|
|
||||||
cv::Rect getFromTable(int idx)
|
cv::Rect getFromTable(int idx)
|
||||||
{
|
{
|
||||||
@ -194,7 +194,7 @@ TEST_P(SCascadeTestRoi, Detect)
|
|||||||
cascade.detect(colored, rois, objectBoxes);
|
cascade.detect(colored, rois, objectBoxes);
|
||||||
|
|
||||||
cv::Mat dt(objectBoxes);
|
cv::Mat dt(objectBoxes);
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
typedef cv::softcascade::Detection Detection;
|
||||||
|
|
||||||
Detection* dts = ((Detection*)dt.data) + 1;
|
Detection* dts = ((Detection*)dt.data) + 1;
|
||||||
int* count = dt.ptr<int>(0);
|
int* count = dt.ptr<int>(0);
|
||||||
@ -262,7 +262,7 @@ TEST_P(SCascadeTestAll, detect)
|
|||||||
|
|
||||||
cascade.detect(colored, rois, objectBoxes);
|
cascade.detect(colored, rois, objectBoxes);
|
||||||
|
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
typedef cv::softcascade::Detection Detection;
|
||||||
cv::Mat dt(objectBoxes);
|
cv::Mat dt(objectBoxes);
|
||||||
|
|
||||||
|
|
||||||
@ -303,7 +303,7 @@ TEST_P(SCascadeTestAll, detectStream)
|
|||||||
cascade.detect(colored, rois, objectBoxes, s);
|
cascade.detect(colored, rois, objectBoxes, s);
|
||||||
s.waitForCompletion();
|
s.waitForCompletion();
|
||||||
|
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
typedef cv::softcascade::Detection Detection;
|
||||||
cv::Mat detections(objectBoxes);
|
cv::Mat detections(objectBoxes);
|
||||||
int a = *(detections.ptr<int>(0));
|
int a = *(detections.ptr<int>(0));
|
||||||
ASSERT_EQ(a, expected);
|
ASSERT_EQ(a, expected);
|
||||||
|
@ -139,12 +139,11 @@ int main(int argc, char** argv)
|
|||||||
std::stringstream conf(std::stringstream::in | std::stringstream::out);
|
std::stringstream conf(std::stringstream::in | std::stringstream::out);
|
||||||
conf << d.confidence;
|
conf << d.confidence;
|
||||||
|
|
||||||
cv::rectangle(frame, cv::Rect(d.bb.x, d.bb.y, d.bb.width, d.bb.height), cv::Scalar(b, 0, 255 - b, 255), 2);
|
cv::rectangle(frame, cv::Rect((int)d.x, (int)d.y, (int)d.w, (int)d.h), cv::Scalar(b, 0, 255 - b, 255), 2);
|
||||||
cv::putText(frame, conf.str() , cv::Point(d.bb.x + 10, d.bb.y - 5),1, 1.1, cv::Scalar(25, 133, 255, 0), 1, CV_AA);
|
cv::putText(frame, conf.str() , cv::Point((int)d.x + 10, (int)d.y - 5),1, 1.1, cv::Scalar(25, 133, 255, 0), 1, CV_AA);
|
||||||
|
|
||||||
if (wf)
|
if (wf)
|
||||||
myfile << d.bb.x << "," << d.bb.y << ","
|
myfile << d.x << "," << d.y << "," << d.w << "," << d.h << "," << d.confidence << "\n";
|
||||||
<< d.bb.width << "," << d.bb.height << "," << d.confidence << "\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
#include <opencv2/highgui.hpp>
|
#include <opencv2/highgui.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
typedef cv::softcascade::Detection Detection;
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
const std::string keys =
|
const std::string keys =
|
||||||
@ -64,7 +66,7 @@ int main(int argc, char** argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cv::gpu::GpuMat objects(1, sizeof(SCascade::Detection) * 10000, CV_8UC1);
|
cv::gpu::GpuMat objects(1, sizeof(Detection) * 10000, CV_8UC1);
|
||||||
cv::gpu::printShortCudaDeviceInfo(parser.get<int>("device"));
|
cv::gpu::printShortCudaDeviceInfo(parser.get<int>("device"));
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
@ -80,7 +82,6 @@ int main(int argc, char** argv)
|
|||||||
cascade.detect(dframe, roi, objects);
|
cascade.detect(dframe, roi, objects);
|
||||||
|
|
||||||
cv::Mat dt(objects);
|
cv::Mat dt(objects);
|
||||||
typedef cv::softcascade::SCascade::Detection Detection;
|
|
||||||
|
|
||||||
Detection* dts = ((Detection*)dt.data) + 1;
|
Detection* dts = ((Detection*)dt.data) + 1;
|
||||||
int* count = dt.ptr<int>(0);
|
int* count = dt.ptr<int>(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user