SRC := $(wildcard src/*.cpp *.cpp)
OBJS := $(patsubst %.cpp,%.o, $(SRC))
+LIBS += `pkg-config --libs opencv`
+LIBS += -llog4cpp -lpthread
.PHONY: all clean
all:$(OBJS)
- $(CC) $(CFLAGS) -o main $(OBJS) -llog4cpp
+ $(CC) $(CFLAGS) -o main $(OBJS) $(LIBS)
$(OBJS):%.o:%.cpp
$(CC) -c $(CFLAGS) $< -o $@
log4cpp.appender.rootAppender.maxBackupIndex=3
log4cpp.appender.rootAppender.fileName=logs/log.txt
log4cpp.appender.rootAppender.layout=PatternLayout
-log4cpp.appender.rootAppender.layout.ConversionPattern=%d [%p] %m%n
+log4cpp.appender.rootAppender.layout.ConversionPattern=%d [%t] [%p] %m%n
class Callback : public EngineObserver
{
void onPersonIn(Person& p){
-// std::cout << "onPersonIn " << p.toString() << std::endl;
LOG_DEBUG(TAG, "OnPersonIn " << p.toString())
};
void onPersonOut(Person& p) {
-// std::cout << "onPersonOut " << std::endl;
LOG_DEBUG(TAG, "OnPersonIn " << p.toString())
};
};
int main(int argc, char* argv[])
{
- InitLogger("log4cpp.properties");
+ initLogger("log4cpp.properties");
LOG_DEBUG(TAG, "==================================");
Engine* e = Engine::create();
#ifndef _DETECTOR_H_
#define _DETECTOR_H_
+#include "VideoReader.h"
namespace suanzi {
{
public:
Detector();
- ~Detector();
+ virtual ~Detector();
+ void detect(cv::Mat& frame){};
};
}
+#include <mutex>
+#include <thread>
#include "Engine.h"
#include "Logger.h"
-#include <mutex>
using namespace suanzi;
Engine::Engine()
{
+ detector = new Detector();
+ tracker = new Tracker();
}
Engine* Engine::create()
}
Engine::~Engine()
-{
+{
+ delete detector;
+ delete tracker;
}
void Engine::setVideoSrc(const std::string& url)
{
videoSrc = url;
+ reader = VideoReaderFactory::createVideoReader(VideoSrcType::URL,"rtsp://192.168.1.75:554/stream1");
+}
+
+void Engine::run()
+{
+ LOG_DEBUG(TAG, "run");
+ cv::Mat frame;
+ while (reader->read(frame)){
+ detector->detect(frame);
+ }
}
void Engine::start()
{
LOG_DEBUG(TAG, "start");
- Person p;
- for(auto& o: observer_list){
- o->onPersonIn(p);
+ if (!reader){
+ LOG_ERROR(TAG, "reader is null. exit");
+ return;
}
+ std::thread t(&Engine::run, this);
+ t.join();
}
void Engine::addObserver(EngineObserver *observer)
#include<set>
#include "Tracker.h"
#include "Detector.h"
+#include "VideoReader.h"
namespace suanzi{
private:
Engine();
virtual ~Engine();
+ void run();
Tracker* tracker;
Detector* detector;
std::set<EngineObserver *> observer_list;
std::string videoSrc;
+ VideoReader* reader;
};
struct Person
log4cpp::Category * gLogger;
-void InitLogger(const char* fname)
+void initLogger(const char* fname)
{
log4cpp::PropertyConfigurator::configure(fname);
log4cpp::Category& logger = log4cpp::Category::getInstance("rootAppender");
#define LOG_ERROR(TAG, msg) LOG4CPP_ERROR_S((*gLogger)) << '[' << TAG << ']' <<" ("<<__FILE__<<":"<<__LINE__<<") - " << msg;
#define LOG_FATAL(TAG, msg) LOG4CPP_FATAL_S((*gLogger)) << '[' << TAG << ']' <<" ("<<__FILE__<<":"<<__LINE__<<") - " << msg;
-void InitLogger(const char* fname);
+void initLogger(const char* fname);
#endif /* _LOGGER_H_ */
{
public:
Tracker();
- ~Tracker();
-
- virtual void createMetrics() = 0;
- virtual void display() = 0;
+ virtual ~Tracker();
};
}
--- /dev/null
+#include "VideoReader.h"
+#include "Logger.h"
+
+using namespace suanzi;
+using namespace cv;
+
+const static std::string TAG = "VideoReader";
+
+VideoReader* VideoReaderFactory::createVideoReader(VideoSrcType type, const std::string& url)
+{
+ VideoReader* v = nullptr;
+ switch(type){
+ case VideoSrcType::URL:
+ v = new UrlReader(type, url);
+ break;
+ case VideoSrcType::File:
+ v = new FileReader(type, url);
+ break;
+ case VideoSrcType::USB:
+ v = new UsbReader(type, url);
+ break;
+ default:
+ break;
+ }
+ return v;
+}
+
+VideoReader::~VideoReader()
+{
+ LOG_DEBUG("video", "init");
+}
+
+
+UrlReader::UrlReader(VideoSrcType type, const std::string& url) : VideoReader(type, url)
+{
+ LOG_DEBUG(TAG, "UrlReader, open " + url);
+ vcap.open(url);
+ if (!vcap.isOpened()){
+ LOG_ERROR(TAG, "open video " + url);
+ throw std::runtime_error("Cannot open video url " + url);
+ }
+}
+
+UrlReader::~UrlReader()
+{
+}
+
+bool UrlReader::read(cv::Mat& mat)
+{
+ bool ret = vcap.read(mat);
+ if (mat.empty()){
+ LOG_ERROR(TAG, "blank frame grabbed");
+ return false;
+ }
+ return ret;
+}
+
+//
+//void UrlReader::read()
+//{
+//}
+//
+//void FileReader::read()
+//{
+//}
+//
+//void UsbReader::read()
+//{
+//}
--- /dev/null
+#ifndef _IVIDEO_READER_H_
+#define _IVIDEO_READER_H_
+
+#include <string>
+#include <opencv2/opencv.hpp>
+
+namespace suanzi {
+
+typedef enum {
+ URL,
+ File,
+ USB
+} VideoSrcType;
+
+class VideoReader;
+
+class VideoReaderFactory
+{
+public:
+ static VideoReader* createVideoReader(VideoSrcType type, const std::string& url);
+};
+
+class VideoReader
+{
+public:
+ VideoReader(VideoSrcType type, const std::string& url) : type(type), url(url){}
+
+ virtual ~VideoReader();
+ virtual bool read(cv::Mat& mat){return true;}
+
+private:
+ VideoSrcType type;
+
+protected:
+ std::string url;
+};
+
+
+class UrlReader : public VideoReader
+{
+public:
+ UrlReader(VideoSrcType type, const std::string& url);
+ virtual ~UrlReader();
+ bool read(cv::Mat& mat);
+private:
+ cv::VideoCapture vcap;
+};
+
+class FileReader : public VideoReader
+{
+public:
+ FileReader(VideoSrcType type, const std::string& url):VideoReader(type, url){}
+// void read(){};
+};
+
+class UsbReader : public VideoReader
+{
+public:
+ UsbReader(VideoSrcType type, const std::string& url):VideoReader(type, url){}
+// void read(){};
+};
+
+
+}
+
+
+#endif /* _IVIDEO_READER_H_ */