提交 0fb097c2 authored 作者: blu's avatar blu

init

上级 abb8c89f
{ {
"C_Cpp.intelliSenseEngineFallback": "Enabled", "C_Cpp.intelliSenseEngineFallback": "Enabled",
"python.pythonPath": "/opt/apps/conda/anaconda3/bin/python" "python.pythonPath": "/opt/apps/conda/anaconda3/bin/python",
"files.associations": {
"chrono": "cpp"
}
} }
\ No newline at end of file
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <chrono> #include <chrono>
#include <future> #include <future>
#include <vector> #include <vector>
#include <queue>
#ifdef OS_LINUX #ifdef OS_LINUX
#include <filesystem> #include <filesystem>
...@@ -43,6 +44,13 @@ struct DetectParam { ...@@ -43,6 +44,13 @@ struct DetectParam {
int post; int post;
}; };
enum EventState {
NONE,
PRE,
IN,
POST
};
class EvMLMotion: public TinyThread { class EvMLMotion: public TinyThread {
private: private:
void *pSubCtx = NULL, *pReqCtx = NULL; // for packets relay void *pSubCtx = NULL, *pReqCtx = NULL; // for packets relay
...@@ -54,6 +62,10 @@ private: ...@@ -54,6 +62,10 @@ private:
AVCodecContext *pCodecCtx = NULL; AVCodecContext *pCodecCtx = NULL;
AVDictionary *pOptsRemux = NULL; AVDictionary *pOptsRemux = NULL;
DetectParam detPara = {25,200,-1,10,3,30}; DetectParam detPara = {25,200,-1,10,3,30};
EventState evtState = EventState::NONE;
chrono::system_clock::time_point evtStartTm, evtStartTmLast;
queue<json> *evtQueue;
// load from db // load from db
int streamIdx = -1; int streamIdx = -1;
...@@ -308,7 +320,7 @@ private: ...@@ -308,7 +320,7 @@ private:
#ifdef DEBUG #ifdef DEBUG
matShow3 = gray.clone(); matShow3 = gray.clone();
#endif #endif
evtStartTm = chrono::system_clock::now();
// TODO: AVG // TODO: AVG
// cv::accumulateWeighted(gray, avg, 0.5); // cv::accumulateWeighted(gray, avg, 0.5);
cv::absdiff(gray, avg, thresh); cv::absdiff(gray, avg, thresh);
...@@ -344,7 +356,68 @@ private: ...@@ -344,7 +356,68 @@ private:
matShow2 = origin; matShow2 = origin;
#endif #endif
// business logic for event // business logic for event
auto dura = chrono::duration_cast<chrono::seconds>(evtStartTm - evtStartTmLast).count();
switch(evtState) {
case NONE: {
if(hasEvent) {
evtState = PRE;
spdlog::info("state: NONE->PRE");
evtStartTmLast = evtStartTm;
}
break;
}
case PRE: {
if(hasEvent) {
if(dura > detPara.pre) {
spdlog::info("state: PRE->PRE");
evtState = PRE;
}else{
evtState = IN;
json p;
spdlog::info("state: PRE->IN");
p["type"] = "start";
p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count();
//p["frame"] = origin.clone();
evtQueue->push(p);
}
}else{
if(dura > detPara.pre){
evtState= NONE;
spdlog::info("state: PRE->NONE");
}
}
break;
}
case IN: {
if(!hasEvent){
if(dura > (int)(detPara.post/2)){
evtState = POST;
spdlog::info("state: IN->POST");
}
}else{
evtStartTmLast = evtStartTm;
spdlog::info("state: IN->IN");
}
break;
}
case POST: {
if(!hasEvent) {
if(dura > detPara.post) {
spdlog::info("state: POST->NONE");
evtState = NONE;
json p;
p["type"] = "end";
p["ts"] = chrono::duration_cast<chrono::seconds>(evtStartTmLast.time_since_epoch()).count() + (int)(detPara.post/2);
evtQueue->push(p);
}
}else{
spdlog::info("state: POST->IN");
evtState=IN;
evtStartTmLast = evtStartTm;
}
break;
}
}
} }
protected: protected:
void run() void run()
...@@ -397,7 +470,6 @@ protected: ...@@ -397,7 +470,6 @@ protected:
gFirst = false; gFirst = false;
ret = decode_packet(true, &packet, pCodecCtx, pFrame); ret = decode_packet(true, &packet, pCodecCtx, pFrame);
} }
} }
av_packet_unref(&packet); av_packet_unref(&packet);
...@@ -409,8 +481,10 @@ protected: ...@@ -409,8 +481,10 @@ protected:
av_frame_free(&pFrame); av_frame_free(&pFrame);
} }
public: public:
EvMLMotion() EvMLMotion() = delete;
EvMLMotion(queue<json> *queue)
{ {
evtQueue = queue;
init(); init();
setupMq(); setupMq();
setupStream(); setupStream();
...@@ -421,13 +495,14 @@ public: ...@@ -421,13 +495,14 @@ public:
int main(int argc, const char *argv[]) int main(int argc, const char *argv[])
{ {
spdlog::set_level(spdlog::level::info); spdlog::set_level(spdlog::level::info);
EvMLMotion es; queue<json> evtQueue;
EvMLMotion es(&evtQueue);
es.detach();
#ifdef DEBUG #ifdef DEBUG
// TODO: remove // TODO: remove
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE ); cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
es.detach();
// TODO: remove me // TODO: remove me
while(true) { while(true) {
if(gFirst) { if(gFirst) {
...@@ -441,8 +516,15 @@ int main(int argc, const char *argv[]) ...@@ -441,8 +516,15 @@ int main(int argc, const char *argv[])
break; break;
} }
} }
#else
es.join();
#endif #endif
while(true) {
if(evtQueue.size() > 0) {
json p = evtQueue.front();
evtQueue.pop();
spdlog::info("event: {}", p.dump());
}else{
this_thread::sleep_for(chrono::duration(chrono::seconds(2)));
}
}
return 0; return 0;
} }
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论