Skip to content

Commit

Permalink
Merge pull request Smorodov#70 from Nuzhny007/master
Browse files Browse the repository at this point in the history
External API for set detectors settings
  • Loading branch information
Smorodov authored Dec 30, 2017
2 parents c51d3b7 + 607cb60 commit 09fcf88
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 63 deletions.
69 changes: 31 additions & 38 deletions Detector/BaseDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,64 @@
///
BaseDetector* CreateDetector(
tracking::Detectors detectorType,
const BaseDetector::config_t& config,
bool collectPoints,
cv::UMat& gray
)
{
BaseDetector* detector = nullptr;

switch (detectorType)
{
case tracking::Motion_VIBE:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_VIBE, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_VIBE, collectPoints, gray);
break;

case tracking::Motion_MOG:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_MOG, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_MOG, collectPoints, gray);
break;

case tracking::Motion_GMG:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_GMG, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_GMG, collectPoints, gray);
break;

case tracking::Motion_CNT:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_CNT, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_CNT, collectPoints, gray);
break;

case tracking::Motion_SuBSENSE:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_SuBSENSE, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_SuBSENSE, collectPoints, gray);
break;

case tracking::Motion_LOBSTER:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_LOBSTER, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_LOBSTER, collectPoints, gray);
break;

case tracking::Motion_MOG2:
return new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_MOG2, collectPoints, gray);
detector = new MotionDetector(BackgroundSubtract::BGFG_ALGS::ALG_MOG2, collectPoints, gray);
break;

case tracking::Face_HAAR:
{
FaceDetector* detector = new FaceDetector(collectPoints, gray);
if (!detector->Init("../data/haarcascade_frontalface_alt2.xml"))
{
delete detector;
detector = nullptr;
}
return detector;
}
detector = new FaceDetector(collectPoints, gray);
break;

case tracking::Pedestrian_HOG:
case tracking::Pedestrian_C4:
{
PedestrianDetector* detector = new PedestrianDetector(collectPoints, gray);
if (!detector->Init((detectorType == tracking::Pedestrian_HOG) ? PedestrianDetector::HOG : PedestrianDetector::C4,
"../data/combined.txt.model", "../data/combined.txt.model_"))
{
delete detector;
detector = nullptr;
}
return detector;
}
detector = new PedestrianDetector(collectPoints, gray);
break;

case tracking::DNN:
{
DNNDetector* detector = new DNNDetector(collectPoints, gray);
if (!detector->Init("../data/MobileNetSSD_deploy.prototxt", "../data/MobileNetSSD_deploy.caffemodel"))
{
delete detector;
detector = nullptr;
}
return detector;
}

detector = new DNNDetector(collectPoints, gray);
break;

default:
return nullptr;
break;
}

if (!detector->Init(config))
{
delete detector;
detector = nullptr;
}
return nullptr;
return detector;
}
39 changes: 38 additions & 1 deletion Detector/BaseDetector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <memory>
#include <map>
#include <string>
#include "defines.h"

///
Expand All @@ -9,28 +11,59 @@
class BaseDetector
{
public:
///
/// \brief BaseDetector
/// \param collectPoints
/// \param frame
///
BaseDetector(bool collectPoints, cv::UMat& frame)
: m_collectPoints(collectPoints)
{
m_minObjectSize.width = std::max(5, frame.cols / 100);
m_minObjectSize.height = m_minObjectSize.width;
}
///
/// \brief ~BaseDetector
///
virtual ~BaseDetector(void)
{
}

typedef std::map<std::string, std::string> config_t;
///
/// \brief Init
/// \param config
///
virtual bool Init(const config_t& config) = 0;

///
/// \brief Detect
/// \param frame
///
virtual void Detect(cv::UMat& frame) = 0;

///
/// \brief SetMinObjectSize
/// \param minObjectSize
///
void SetMinObjectSize(cv::Size minObjectSize)
{
m_minObjectSize = minObjectSize;
}

///
/// \brief GetDetects
/// \return
///
const regions_t& GetDetects() const
{
return m_regions;
}

///
/// \brief CollectPoints
/// \param region
///
virtual void CollectPoints(CRegion& region)
{
const int yStep = 5;
Expand All @@ -53,6 +86,10 @@ class BaseDetector
}
}

///
/// \brief CalcMotionMap
/// \param frame
///
virtual void CalcMotionMap(cv::Mat frame)
{
if (m_motionMap.size() != frame.size())
Expand Down Expand Up @@ -110,4 +147,4 @@ class BaseDetector
/// \param gray
/// \return
///
BaseDetector* CreateDetector(tracking::Detectors detectorType, bool collectPoints, cv::UMat& gray);
BaseDetector* CreateDetector(tracking::Detectors detectorType, const BaseDetector::config_t& config, bool collectPoints, cv::UMat& gray);
15 changes: 13 additions & 2 deletions Detector/DNNDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,20 @@ DNNDetector::~DNNDetector(void)
/// \brief DNNDetector::Init
/// \return
///
bool DNNDetector::Init(std::string modelConfiguration, std::string modelBinary)
bool DNNDetector::Init(const config_t& config)
{
m_net = cv::dnn::readNetFromCaffe(modelConfiguration, modelBinary);
auto modelConfiguration = config.find("modelConfiguration");
auto modelBinary = config.find("modelBinary");
if (modelConfiguration != config.end() && modelBinary != config.end())
{
m_net = cv::dnn::readNetFromCaffe(modelConfiguration->second, modelBinary->second);
}

auto confidenceThreshold = config.find("confidenceThreshold");
if (confidenceThreshold != config.end())
{
m_confidenceThreshold = std::stof(confidenceThreshold->second);
}

return !m_net.empty();
}
Expand Down
2 changes: 1 addition & 1 deletion Detector/DNNDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DNNDetector : public BaseDetector
DNNDetector(bool collectPoints, cv::UMat& colorFrame);
~DNNDetector(void);

bool Init(std::string modelConfiguration = "../data/MobileNetSSD_deploy.prototxt", std::string modelBinary = "../data/MobileNetSSD_deploy.caffemodel");
bool Init(const config_t& config);

void Detect(cv::UMat& colorFrame);

Expand Down
9 changes: 5 additions & 4 deletions Detector/FaceDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ FaceDetector::~FaceDetector(void)
/// \param cascadeFileName
/// \return
///
bool FaceDetector::Init(std::string cascadeFileName)
bool FaceDetector::Init(const config_t& config)
{
m_cascade.load(cascadeFileName);
if (m_cascade.empty())
auto cascadeFileName = config.find("cascadeFileName");
if (cascadeFileName != config.end() &&
(!m_cascade.load(cascadeFileName->second) || m_cascade.empty()))
{
std::cerr << "Cascade not opened!" << std::endl;
std::cerr << "Cascade " << cascadeFileName->second << " not opened!" << std::endl;
return false;
}
return true;
Expand Down
2 changes: 1 addition & 1 deletion Detector/FaceDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FaceDetector : public BaseDetector
FaceDetector(bool collectPoints, cv::UMat& gray);
~FaceDetector(void);

bool Init(std::string cascadeFileName = "../data/haarcascade_frontalface_alt2.xml");
bool Init(const config_t& config);

void Detect(cv::UMat& gray);

Expand Down
10 changes: 10 additions & 0 deletions Detector/MotionDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ MotionDetector::~MotionDetector(void)
{
}

///
/// \brief MotionDetector::Init
/// \param config
/// \return
///
bool MotionDetector::Init(const config_t& config)
{
return true;
}

///
/// \brief MotionDetector::DetectContour
///
Expand Down
2 changes: 2 additions & 0 deletions Detector/MotionDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class MotionDetector : public BaseDetector
MotionDetector(BackgroundSubtract::BGFG_ALGS algType, bool collectPoints, cv::UMat& gray);
~MotionDetector(void);

bool Init(const config_t& config);

void Detect(cv::UMat& gray);

void CalcMotionMap(cv::Mat frame);
Expand Down
28 changes: 24 additions & 4 deletions Detector/PedestrianDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ PedestrianDetector::~PedestrianDetector(void)
/// \param cascadeFileName
/// \return
///
bool PedestrianDetector::Init(DetectorTypes detectorType, std::string cascadeFileName1, std::string cascadeFileName2)
bool PedestrianDetector::Init(const config_t& config)
{
m_detectorType = detectorType;
auto detectorType = config.find("detectorType");

if (detectorType == config.end())
{
m_detectorType = HOG;
}
else
{
m_detectorType = (detectorType->second == "HOG") ? HOG : C4;
}

switch (m_detectorType)
{
Expand All @@ -39,8 +48,19 @@ bool PedestrianDetector::Init(DetectorTypes detectorType, std::string cascadeFil
return true;

case C4:
LoadCascade(cascadeFileName1, cascadeFileName2, m_scannerC4);
return true;
{
auto cascadeFileName1 = config.find("cascadeFileName1");
auto cascadeFileName2 = config.find("cascadeFileName2");
if (cascadeFileName1 == config.end() || cascadeFileName2 == config.end())
{
return false;
}
else
{
LoadCascade(cascadeFileName1->second, cascadeFileName2->second, m_scannerC4);
return true;
}
}

default:
return false;
Expand Down
2 changes: 1 addition & 1 deletion Detector/PedestrianDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PedestrianDetector : public BaseDetector
PedestrianDetector(bool collectPoints, cv::UMat& gray);
~PedestrianDetector(void);

bool Init(DetectorTypes detectorType, std::string cascadeFileName1 = "../data/combined.txt.model", std::string cascadeFileName2 = "../data/combined.txt.model_");
bool Init(const config_t& config);

void Detect(cv::UMat& gray);

Expand Down
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
1. External API for the detectors options: for Background subtractors, confidenceThreshold for DNNDetector etc.
2. New Multitarget tracking algorithm based on Discrete-Continuous Energy Minimization: https://bitbucket.org/amilan/dctracking
3. Deep SORT: https://github.com/nwojke/deep_sort
+ New Multitarget tracking algorithm based on Discrete-Continuous Energy Minimization: https://bitbucket.org/amilan/dctracking
+ Deep SORT: https://github.com/nwojke/deep_sort
Loading

0 comments on commit 09fcf88

Please sign in to comment.