From 642ffd9803b9be0439124d1aebe2780fcbabeb07 Mon Sep 17 00:00:00 2001 From: Peng Li Date: Tue, 10 Jul 2018 11:24:10 +0800 Subject: [PATCH] Add reader --- Makefile | 4 +++- log4cpp.properties | 2 +- main.cpp | 4 +--- src/Detector.h | 4 +++- src/Engine.cpp | 27 +++++++++++++++++---- src/Engine.h | 3 +++ src/Logger.cpp | 2 +- src/Logger.h | 2 +- src/Tracker.h | 5 +--- src/VideoReader.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/VideoReader.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 172 insertions(+), 17 deletions(-) create mode 100644 src/VideoReader.cpp create mode 100644 src/VideoReader.h diff --git a/Makefile b/Makefile index cfc680f..ecdef3a 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,12 @@ CXXLAGS += -Wall -std=c++11 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 $@ diff --git a/log4cpp.properties b/log4cpp.properties index 97b5939..c24614b 100644 --- a/log4cpp.properties +++ b/log4cpp.properties @@ -9,4 +9,4 @@ log4cpp.appender.rootAppender.maxFileSize=400000 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 diff --git a/main.cpp b/main.cpp index e8999d7..acbe99a 100644 --- a/main.cpp +++ b/main.cpp @@ -10,13 +10,11 @@ using namespace suanzi; 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()) }; }; @@ -25,7 +23,7 @@ class Callback : public EngineObserver int main(int argc, char* argv[]) { - InitLogger("log4cpp.properties"); + initLogger("log4cpp.properties"); LOG_DEBUG(TAG, "=================================="); Engine* e = Engine::create(); diff --git a/src/Detector.h b/src/Detector.h index a17a5a0..a2c43cf 100644 --- a/src/Detector.h +++ b/src/Detector.h @@ -1,5 +1,6 @@ #ifndef _DETECTOR_H_ #define _DETECTOR_H_ +#include "VideoReader.h" namespace suanzi { @@ -7,7 +8,8 @@ class Detector { public: Detector(); - ~Detector(); + virtual ~Detector(); + void detect(cv::Mat& frame){}; }; } diff --git a/src/Engine.cpp b/src/Engine.cpp index 7b00ce6..e51dd65 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -1,6 +1,7 @@ +#include +#include #include "Engine.h" #include "Logger.h" -#include using namespace suanzi; @@ -11,6 +12,8 @@ static Engine* g_instance = nullptr; Engine::Engine() { + detector = new Detector(); + tracker = new Tracker(); } Engine* Engine::create() @@ -31,21 +34,35 @@ void Engine::destroy() } 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) diff --git a/src/Engine.h b/src/Engine.h index 66b1341..3ae02f4 100644 --- a/src/Engine.h +++ b/src/Engine.h @@ -6,6 +6,7 @@ #include #include "Tracker.h" #include "Detector.h" +#include "VideoReader.h" namespace suanzi{ @@ -23,10 +24,12 @@ public: private: Engine(); virtual ~Engine(); + void run(); Tracker* tracker; Detector* detector; std::set observer_list; std::string videoSrc; + VideoReader* reader; }; struct Person diff --git a/src/Logger.cpp b/src/Logger.cpp index 06693ed..20315c0 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -2,7 +2,7 @@ log4cpp::Category * gLogger; -void InitLogger(const char* fname) +void initLogger(const char* fname) { log4cpp::PropertyConfigurator::configure(fname); log4cpp::Category& logger = log4cpp::Category::getInstance("rootAppender"); diff --git a/src/Logger.h b/src/Logger.h index de5a0b8..a97a9a4 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -12,6 +12,6 @@ extern log4cpp::Category * gLogger; #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_ */ diff --git a/src/Tracker.h b/src/Tracker.h index 407634b..57fe765 100644 --- a/src/Tracker.h +++ b/src/Tracker.h @@ -7,10 +7,7 @@ class Tracker { public: Tracker(); - ~Tracker(); - - virtual void createMetrics() = 0; - virtual void display() = 0; + virtual ~Tracker(); }; } diff --git a/src/VideoReader.cpp b/src/VideoReader.cpp new file mode 100644 index 0000000..22036cb --- /dev/null +++ b/src/VideoReader.cpp @@ -0,0 +1,69 @@ +#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() +//{ +//} diff --git a/src/VideoReader.h b/src/VideoReader.h new file mode 100644 index 0000000..340b548 --- /dev/null +++ b/src/VideoReader.h @@ -0,0 +1,67 @@ +#ifndef _IVIDEO_READER_H_ +#define _IVIDEO_READER_H_ + +#include +#include + +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_ */ -- 2.11.0