- opencv - 3.4.1
Run `script/install_opencv.sh` to build and install opencv into /usr/local
-Refer this page <https://docs.opencv.org/3.4.1/d7/d9f/tutorial_linux_install.html>
+
+ Refer this page <https://docs.opencv.org/3.4.1/d7/d9f/tutorial_linux_install.html>
- log4cpp : logger utils
+
- eigen : matrix library of C++
+
- scons
+
`apt-get install liblog4cpp5-dev libeigen3-dev scons`
- boost-python
+
- `apt-get install libpython-dev python-dev`
- build boost with python
- `pip install scipy numpy sklearn`
## Build with `scons`
- Run `scons`
+
To build folder `src/` and `main.cpp`. `libtracker.a` and `main` will be generated
- Run `scons --all`
+
To build folder `src/` and `test/` and `main.cpp`. `libtracker.a` , `main`,
and `test/TestMain` will be generated. Run `TestMain` to run all the unit
test
- Run `scons -c` or `scons --all -c`
+
To clean
### Run
`./main`
### Log
+
Logger config file is located at `config/log4cpp.properties`. By default, log
file is `/tmp/trackerpp.log`
Detector();
virtual ~Detector();
// TODO
- unsigned int detect(const cv::Mat& frame, Detection* detections){return 0;}
+ unsigned int detect(const cv::Mat& frame, Detection* detections){return 2;}
};
struct Detection
// 检测目标的分数,可以不填
float score;
// 检测目标的坐标,包括物体中心的x、y坐标,物体的高和宽
- unsigned int center_x;
- unsigned int center_y;
- unsigned int height;
- unsigned int width;
+ unsigned int center_x = 100;
+ unsigned int center_y = 100;
+ unsigned int height = 200;
+ unsigned int width = 200;
// 检测目标的特征向量
unsigned int feature_size;
float * feature;
Engine();
void run();
WorkerThread eventThread {"EventThread"};
+ WorkerThread writeImgThread {"WriteImgThread"};
DetectorPtr detector;
MultiTrackerPtr multiTracker;
std::set<EngineObserver *> observer_list;
+++ /dev/null
-#ifndef _METRICS_H_
-#define _METRICS_H_
-
-#include <string>
-#include <opencv2/opencv.hpp>
-#include "SharedPtr.h"
-
-namespace suanzi {
-
- TK_DECLARE_PTR(Metrics);
- //TK_DECLARE_PTR(Patch);
-// struct Patch;
- class Metrics
- {
- public:
- Metrics(const std::string& cl_path = "");
- ~Metrics(){}
- const static long int MaxCost = 100000;
- const static int MaxPatch = 5;
- //double distance()
- private:
- cv::HOGDescriptor descriptor = {cv::Size(64, 128), cv::Size(16, 16), cv::Size(8, 8), cv::Size(8, 8), 9};
- };
-
-// struct Patch
-// {
-// // bb_ltrb
-// //
-// // image_crop
-// cv::Mat image_crop;
-// //
-// // features
-// };
-}
-
-#endif /* _METRICS_H_ */
int max_id = 0;
PatchPtr createPatch(const cv::Mat& image, const Detection& d);
double distance(TrackerPtr t, const cv::Mat& image, const Detection& d);
+ void addTracker(TrackerPtr t);
PredictorWrapperPtr predictor;
cv::HOGDescriptor descriptor;
EngineWPtr engine;
+++ /dev/null
-#include
-
#include <opencv2/opencv.hpp>
#include <string>
#include <vector>
-#include "Metrics.h"
#include "SharedPtr.h"
#include "MultiTracker.h"
#include "Detector.h"
LOG_DEBUG(TAG, "==================================");
EnginePtr e = Engine::create();
e->addObserver(new Callback());
- //e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.1.75:554/stream1");
- e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.56.101:5454/test.mp4");
+ e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.1.75:554/stream1");
+ //e->setVideoSrc(VideoSrcType::URL, "rtsp://192.168.56.101:5454/test.mp4");
e->start();
e->destroy();
log4cpp::Category::shutdown();
f = open(fname, 'rb')
predictors = pickle.load(f)
f.close()
+ return True;
def dump():
global predictors
#include "MultiTracker.h"
-#include "Metrics.h"
#include <algorithm>
#include "hungarian.h"
#include "Logger.h"
#include "Utils.h"
+#include <cstdlib>
+#include <stdexcept>
using namespace suanzi;
using namespace cv;
static const cv::Size PREFERRED_SIZE = Size(64, 128);
static const double MaxCost = 100000;
static const double ProbThreshold = 0.05;
+static const int MaxTrackers = 100;
MultiTracker::MultiTracker(EngineWPtr e)
: engine(e)
cv::Mat im2(PREFERRED_SIZE, p2->image_crop.type());
cv::resize(p1->image_crop, im1, im1.size());
cv::resize(p2->image_crop, im2, im2.size());
- cv::Mat result;
+
+ cv::Mat result = Mat::zeros(1,1, CV_64F);
+
cv::matchTemplate(im1, im2, result, CV_TM_CCOEFF_NORMED);
feature.push_back(result.at<double>(0, 0));
cv::matchTemplate(im1, im2, result, CV_TM_CCORR_NORMED);
feature.push_back(calc_iou_ratio(getRectInDetection(d1), getRectInDetection(d2)));
+// for (auto i : feature){
+// cout << i << "\t";
+// if (isnan(i)){
+// throw overflow_error("Nan in feature");
+// }
+// }
+// cout << endl;
+//
return feature;
}
ss = similarity(i, patch);
features.insert(features.end(), ss.begin(), ss.end());
}
- double prob = predictor->predict(Tracker::MaxPatch - 1, features); // TODO why is MaxPatch-1
+ double prob = predictor->predict(tracker->patches.size() - 1, features); // TODO why is MaxPatch-1
if (prob > ProbThreshold)
return -log(prob);
else
for (auto i : unmatch_bbs_indices){
TrackerPtr new_tracker (new Tracker(image, detections[i]));
new_tracker->addPatch(createPatch(image, detections[i]));
- this->trackers.push_back(new_tracker);
+ addTracker(new_tracker);
Person test; // TODO
inPersons.push_back(test);
}
patch->features = std::make_pair(feature_hog_double, hist);
return patch;
}
+
+void MultiTracker::addTracker(TrackerPtr t)
+{
+ trackers.insert(trackers.begin(), t);
+ if(trackers.size() > MaxTrackers){
+ LOG_ERROR(TAG, "trackers reaches the maximum " + to_string(MaxTrackers));
+ trackers.pop_back();
+ }
+}
ret_val = py::extract<decltype(ret_val)>(py_ret); \
}catch(boost::python::error_already_set const &){ \
LOG_ERROR(TAG, PyWrapper::parse_python_exception()); \
+ throw runtime_error("Python error"); \
} \
{
LOG_DEBUG(TAG, "load " + fname);
py::object func = m_module.attr("init");
- string ret;
+ bool ret = true;
CALL_WITH_EXCEPTION(func(fname.c_str()), ret);
- return true;
+ return ret;
}
void PredictorWrapper::dump()
{
patches.insert(patches.begin(), p);
if (patches.size() > MaxPatch){
- patches.erase(patches.end());
+ patches.pop_back();
}
}
#include "Utils.h"
+#include <iostream>
+#include <stdexcept>
using namespace suanzi;
using namespace cv;
+using namespace std;
double distance_cosine(const Eigen::VectorXd& u, const Eigen::VectorXd& v)
{
+ if (u.dot(u) * v.dot(v) == 0){
+ throw overflow_error("zero vector for cosine distance");
+ }
return (1 - u.dot(v) / std::sqrt(u.dot(u) * v.dot(v)));
}
static long count = 0;
-static void printImg(const cv::Mat& mat)
-{
- std::cout << mat.at<int>(0,0) << std::endl;
-}
-
bool UrlReader::read(cv::Mat& mat)
{
bool ret = vcap.read(mat);
#include "gtest/gtest.h"
#include "PredictorWrapper.h"
+#include <iostream>
using namespace suanzi;
+using namespace std;
TEST(Predictor, load)
{
predictor->dump();
std::vector<double> ff (40, 1);
double prob = predictor->predict(4, ff);
+ cout << prob << endl;
}
+
+
+//
+//TEST(Predictor, predict)
+//{
+// PredictorWrapperPtr predictor (new PredictorWrapper("predictor", "../python"));
+// predictor->load("../resources/model.pkl");
+// predictor->dump();
+// //std::vector<double> ff (40, 1);
+//
+// std::vector<double> ff = { 6.9154247185294e-310, 6.91542471861144e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.9154247185294e-310, 6.91542471861144e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, -3.0949108904043924e+217, -3.0949108904066356e+217, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.91542471837167e-310, 6.9154247185979e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 6.9154247183334e-310, 6.9154247185946e-310, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 };
+// int index = 4;
+// double prob = predictor->predict(index, ff);
+// cout << prob << endl;
+//}
+//