Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion include/detection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <memory>
#include <string>

#include "opencv2/core/core.hpp"
#include "opencv2/objdetect.hpp"

class Detector {
public:
Expand All @@ -12,3 +12,12 @@ class Detector {
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores) = 0;
};

class CascadeDetector : public Detector
{
public:
cv::CascadeClassifier ccdetector;
virtual bool Init(const std::string& model_file_path);
virtual void Detect(const cv::Mat& frame, std::vector<cv::Rect>& objects,
std::vector<double>& scores);
};
122 changes: 122 additions & 0 deletions samples/detection_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <iostream>
#include <string>

#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"

#include "detection.hpp"

using namespace std;
using namespace cv;

const char* kAbout =
"detection app";

const char* kOptions =
"{ i image | <none> | image to process }"
"{ v video | <none> | video to process }"
"{ c camera | <none> | camera to get video from }"
"{ m model | <none> | path to detector file }"
"{ h ? help usage | | print help message }";

int main(int argc, const char** argv) {
// Parse command line arguments.
CommandLineParser parser(argc, argv, kOptions);
parser.about(kAbout);

// If help option is given, print help message and exit.
if (parser.get<bool>("help")) {
parser.printMessage();
return 0;
}

// init
std::string vidName, imgName, detectName;
Mat frame;
CascadeDetector obj;
std::vector<Rect> logos;
std::vector<double> scores;
VideoCapture capture;
//

if (parser.has("m"))
{
detectName = parser.get<string>("m");
cout << detectName << endl;
obj.Init(detectName);
}

if (parser.has("i"))
{
imgName = parser.get<std::string>("i");
frame = imread(imgName);
imshow("Detection", frame);
obj.Detect(frame, logos, scores);
if (scores.size() > 0)
for (int i = 0; i < logos.size(); i++)
{
rectangle(frame, logos[i], Scalar(255, 0, 0));
imshow("Detection", frame);
}
else
std::cout << "objects haven`t been detected";
}
else
{
if (parser.has("v"))
{
vidName = parser.get<std::string>("v");

bool flag = capture.open(vidName);

if (flag)
{
flag = capture.read(frame);
while (true)
{
if (frame.empty())
{
cout << " --(!) No captured frame -- Break!";
break;
}
//Apply the classifier to the frame
obj.Detect(frame, logos, scores);
if (scores.size()> 0)
for(int i = 0; i < logos.size(); i++)
rectangle(frame, logos[i], Scalar(255, 0, 0));
imshow("Detection", frame);
capture >> frame;
int c = cv::waitKey(100);
if ((char)c == 27) break; // escape
}
capture.release();
}
else
cout << "--(!)Error opening video capture" << endl ;

}
else
if (parser.has("c"))
{
capture.open(0);
if (!capture.isOpened())
return 0;
capture.read(frame);
for (;;)
{
obj.Detect(frame, logos, scores);
if (scores.size() > 0)
for (int i = 0; i < logos.size(); i++)
rectangle(frame, logos[i], Scalar(255, 0, 0));
capture >> frame;
if (frame.empty()) break; // end of video stream
imshow("Web", frame);
if (waitKey(10) == 27) break; // stop capturing by pressing ESC
}
capture.release();
}
}
cv::waitKey(0);

return 0;
}
64 changes: 0 additions & 64 deletions samples/devtools_demo.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions samples/template_demo.cpp

This file was deleted.

39 changes: 33 additions & 6 deletions src/detection.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
#include "detection.hpp"

#include <iostream>

using std::string;
using std::shared_ptr;
using namespace cv;

shared_ptr<Detector> Detector::CreateDetector(const string& name) {
std::cerr << "Failed to create detector with name '" << name << "'"
<< std::endl;
return nullptr;
shared_ptr<Detector> Detector::CreateDetector(const string& name)
{
if (name == "cascade")
{
return std::make_shared<CascadeDetector>();
}
else
return nullptr;

}

bool CascadeDetector::Init(const std::string & model_file_path)
{
bool flag = false;
if (ccdetector.load(model_file_path))
flag = true;
return flag;
}

void CascadeDetector::Detect(const cv::Mat & frame, std::vector<cv::Rect>& ccdetectorects,
std::vector<double>& scores)
{
std::vector<int> numDetections;

if (ccdetector.empty()) return ;
if (frame.empty()) throw "Matrix is empty";

//ccdetectorects.clear();

ccdetector.detectMultiScale(frame, ccdetectorects, numDetections);

scores.assign(numDetections.begin(), numDetections.end());

}
Loading