提交 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
#include "inc/zmqhelper.hpp"
#include "inc/database.h"
#include "inc/json.hpp"
#include <cstdlib>
using namespace std;
using namespace httplib;
......@@ -24,6 +25,7 @@ class HttpSrv{
Server svr;
json config;
json info;
int port = 8088;
void setMonitorThread() {
......@@ -75,7 +77,10 @@ class HttpSrv{
}
HttpSrv(){
char* strPort = getenv("PORT");
if(strPort != NULL) {
port = stoi(strPort);
}
};
~HttpSrv(){};
};
......
......@@ -37,7 +37,7 @@ using namespace std;
using namespace zmqhelper;
#define URLOUT_DEFAULT "frames"
#define NUM_PKT_IGNORE 18*10
#define NUM_PKT_IGNORE 18*5
#define FRAME_SIZE 500
#define DEBUG
......@@ -56,6 +56,7 @@ struct DetectParam {
int fpsProc;
int pre;
int post;
float entropy;
};
enum EventState {
......@@ -74,7 +75,7 @@ private:
AVFormatContext *pAVFormatInput = NULL;
AVCodecContext *pCodecCtx = 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;
chrono::system_clock::time_point evtStartTm, evtStartTmLast;
queue<string> *evtQueue;
......@@ -210,6 +211,13 @@ private:
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
pSubCtx = zmq_ctx_new();
pSub = zmq_socket(pSubCtx, ZMQ_SUB);
......@@ -423,16 +431,14 @@ private:
pFrame->coded_picture_number
);
// 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);
}
detectMotion(pCodecContext->pix_fmt, pFrame, detect);
break;
}
}
return 0;
}
void detectMotion(AVPixelFormat format,AVFrame *pFrame)
void detectMotion(AVPixelFormat format,AVFrame *pFrame, bool detect = true)
{
static bool first = true;
static cv::Mat avg;
......@@ -441,6 +447,7 @@ private:
avcvhelpers::frame2mat(format, pFrame, origin);
cv::resize(origin, gray, cv::Size(FRAME_SIZE,FRAME_SIZE));
cv::cvtColor(gray, thresh, cv::COLOR_BGR2GRAY);
float fent = avcvhelpers::getEntropy(thresh);
cv::GaussianBlur(thresh, gray, cv::Size(21, 21), cv::THRESH_BINARY);
if(first) {
// avg = cv::Mat::zeros(gray.size(), CV_32FC3);
......@@ -455,10 +462,12 @@ private:
// TODO: AVG
// cv::accumulateWeighted(gray, avg, 0.5);
cv::absdiff(gray, avg, thresh);
#ifdef DEBUG
avg = gray.clone();
#endif
if(!detect || fent < detPara.entropy){
return;
}
// TODO:
cv::threshold(thresh, gray, detPara.thre, 255, cv::THRESH_BINARY);
......
......@@ -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_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
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论