提交 aec49ee7 authored 作者: blu's avatar blu

new feature entropy filter to ignore gray frame without iframe

上级 31b52743
...@@ -14,6 +14,7 @@ update: 2019/08/30 ...@@ -14,6 +14,7 @@ update: 2019/08/30
#include "inc/zmqhelper.hpp" #include "inc/zmqhelper.hpp"
#include "inc/database.h" #include "inc/database.h"
#include "inc/json.hpp" #include "inc/json.hpp"
#include <cstdlib>
using namespace std; using namespace std;
using namespace httplib; using namespace httplib;
...@@ -24,6 +25,7 @@ class HttpSrv{ ...@@ -24,6 +25,7 @@ class HttpSrv{
Server svr; Server svr;
json config; json config;
json info; json info;
int port = 8088;
void setMonitorThread() { void setMonitorThread() {
...@@ -75,7 +77,10 @@ class HttpSrv{ ...@@ -75,7 +77,10 @@ class HttpSrv{
} }
HttpSrv(){ HttpSrv(){
char* strPort = getenv("PORT");
if(strPort != NULL) {
port = stoi(strPort);
}
}; };
~HttpSrv(){}; ~HttpSrv(){};
}; };
......
...@@ -37,7 +37,7 @@ using namespace std; ...@@ -37,7 +37,7 @@ using namespace std;
using namespace zmqhelper; using namespace zmqhelper;
#define URLOUT_DEFAULT "frames" #define URLOUT_DEFAULT "frames"
#define NUM_PKT_IGNORE 18*10 #define NUM_PKT_IGNORE 18*5
#define FRAME_SIZE 500 #define FRAME_SIZE 500
#define DEBUG #define DEBUG
...@@ -56,6 +56,7 @@ struct DetectParam { ...@@ -56,6 +56,7 @@ struct DetectParam {
int fpsProc; int fpsProc;
int pre; int pre;
int post; int post;
float entropy;
}; };
enum EventState { enum EventState {
...@@ -74,7 +75,7 @@ private: ...@@ -74,7 +75,7 @@ private:
AVFormatContext *pAVFormatInput = NULL; AVFormatContext *pAVFormatInput = NULL;
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, 0.3};
EventState evtState = EventState::NONE; EventState evtState = EventState::NONE;
chrono::system_clock::time_point evtStartTm, evtStartTmLast; chrono::system_clock::time_point evtStartTm, evtStartTmLast;
queue<string> *evtQueue; queue<string> *evtQueue;
...@@ -210,6 +211,13 @@ private: ...@@ -210,6 +211,13 @@ private:
detPara.post = evmlmotion["post"]; detPara.post = evmlmotion["post"];
} }
if(evmlmotion.count("entropy") == 0||evmlmotion["entropy"] < 0 || evmlmotion["entropy"] >= 10) {
spdlog::warn("evmlmotion {} invalid entropy value. should be in (0, 10), default to 0.3", selfId);
detPara.entropy = 0.3;
}else{
detPara.entropy = evmlmotion["entropy"];
}
// setup sub // setup sub
pSubCtx = zmq_ctx_new(); pSubCtx = zmq_ctx_new();
pSub = zmq_socket(pSubCtx, ZMQ_SUB); pSub = zmq_socket(pSubCtx, ZMQ_SUB);
...@@ -423,16 +431,14 @@ private: ...@@ -423,16 +431,14 @@ private:
pFrame->coded_picture_number pFrame->coded_picture_number
); );
// string name = urlOut + "/"+ to_string(chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count()) + ".pgm"; // string name = urlOut + "/"+ to_string(chrono::duration_cast<chrono::seconds>(chrono::system_clock::now().time_since_epoch()).count()) + ".pgm";
if(detect) { detectMotion(pCodecContext->pix_fmt, pFrame, detect);
detectMotion(pCodecContext->pix_fmt,pFrame);
}
break; break;
} }
} }
return 0; return 0;
} }
void detectMotion(AVPixelFormat format,AVFrame *pFrame) void detectMotion(AVPixelFormat format,AVFrame *pFrame, bool detect = true)
{ {
static bool first = true; static bool first = true;
static cv::Mat avg; static cv::Mat avg;
...@@ -441,6 +447,7 @@ private: ...@@ -441,6 +447,7 @@ private:
avcvhelpers::frame2mat(format, pFrame, origin); avcvhelpers::frame2mat(format, pFrame, origin);
cv::resize(origin, gray, cv::Size(FRAME_SIZE,FRAME_SIZE)); cv::resize(origin, gray, cv::Size(FRAME_SIZE,FRAME_SIZE));
cv::cvtColor(gray, thresh, cv::COLOR_BGR2GRAY); cv::cvtColor(gray, thresh, cv::COLOR_BGR2GRAY);
float fent = avcvhelpers::getEntropy(thresh);
cv::GaussianBlur(thresh, gray, cv::Size(21, 21), cv::THRESH_BINARY); cv::GaussianBlur(thresh, gray, cv::Size(21, 21), cv::THRESH_BINARY);
if(first) { if(first) {
// avg = cv::Mat::zeros(gray.size(), CV_32FC3); // avg = cv::Mat::zeros(gray.size(), CV_32FC3);
...@@ -455,10 +462,12 @@ private: ...@@ -455,10 +462,12 @@ private:
// 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);
#ifdef DEBUG #ifdef DEBUG
avg = gray.clone(); avg = gray.clone();
#endif #endif
if(!detect || fent < detPara.entropy){
return;
}
// TODO: // TODO:
cv::threshold(thresh, gray, detPara.thre, 255, cv::THRESH_BINARY); cv::threshold(thresh, gray, detPara.thre, 255, cv::THRESH_BINARY);
......
...@@ -89,6 +89,28 @@ void frame2mat(AVPixelFormat format, const AVFrame * frame, cv::Mat& image) ...@@ -89,6 +89,28 @@ void frame2mat(AVPixelFormat format, const AVFrame * frame, cv::Mat& image)
sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes); sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
sws_freeContext(conversion); sws_freeContext(conversion);
} }
float getEntropy(cv::Mat &frame){
int histSize = 256;
/// Set the ranges ( for B,G,R) )
float range[] = { 0, 256 } ;
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
/// Compute the histograms:
cv::Mat hist;
cv::calcHist( &frame, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, true, false );
hist /= frame.total();
hist += 1e-4; //prevent 0
cv::Mat logP;
cv::log(hist,logP);
float entropy = -1*sum(hist.mul(logP)).val[0];
spdlog::debug("entropy: {}", entropy);
return entropy;
}
} }
#endif #endif
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论