Add reader
authorPeng Li <seudut@gmail.com>
Tue, 10 Jul 2018 03:24:10 +0000 (11:24 +0800)
committerPeng Li <seudut@gmail.com>
Tue, 10 Jul 2018 03:24:10 +0000 (11:24 +0800)
Makefile
log4cpp.properties
main.cpp
src/Detector.h
src/Engine.cpp
src/Engine.h
src/Logger.cpp
src/Logger.h
src/Tracker.h
src/VideoReader.cpp [new file with mode: 0644]
src/VideoReader.h [new file with mode: 0644]

index cfc680f..ecdef3a 100644 (file)
--- 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 $@
index 97b5939..c24614b 100644 (file)
@@ -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 
index e8999d7..acbe99a 100644 (file)
--- 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();
index a17a5a0..a2c43cf 100644 (file)
@@ -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){};
 };
 
 }
index 7b00ce6..e51dd65 100644 (file)
@@ -1,6 +1,7 @@
+#include <mutex>
+#include <thread>
 #include "Engine.h"
 #include "Logger.h"
-#include <mutex>
 
 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)
index 66b1341..3ae02f4 100644 (file)
@@ -6,6 +6,7 @@
 #include<set>
 #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<EngineObserver *> observer_list;
     std::string videoSrc;
+    VideoReader* reader;
 };
 
 struct Person
index 06693ed..20315c0 100644 (file)
@@ -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");
index de5a0b8..a97a9a4 100644 (file)
@@ -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_ */
index 407634b..57fe765 100644 (file)
@@ -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 (file)
index 0000000..22036cb
--- /dev/null
@@ -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 (file)
index 0000000..340b548
--- /dev/null
@@ -0,0 +1,67 @@
+#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_ */