Commit 0ef776fc authored by wanghailong's avatar wanghailong

修改参数,增加策略减少误检

parent 72b18292
Pipeline #611 failed with stages
...@@ -51,10 +51,10 @@ CBM_model::~CBM_model() ...@@ -51,10 +51,10 @@ CBM_model::~CBM_model()
} }
void CBM_model::Initialize() void CBM_model::Initialize()
{ {
//_writer6.open("long.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),0); // _writer6.open("long.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),0);
//_writer7.open("short.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),0); // _writer7.open("short.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),0);
//_writer8.open("static.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1); // _writer8.open("static.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1);
//_writer10.open("DPM.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1); // _writer10.open("DPM.avi",cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1);
mog_fg = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1); mog_fg = cv::Mat::zeros(cv::Size(new_width, new_height), CV_8UC1);
...@@ -106,10 +106,10 @@ void CBM_model::Initialize() ...@@ -106,10 +106,10 @@ void CBM_model::Initialize()
void CBM_model::Uninitialize() void CBM_model::Uninitialize()
{ {
#ifdef DEBUG #ifdef DEBUG
//_writer6.release(); // _writer6.release();
//_writer7.release(); // _writer7.release();
//_writer8.release(); // _writer8.release();
//_writer10.release(); // _writer10.release();
#endif #endif
// cvReleaseImage(&mog_fg); // cvReleaseImage(&mog_fg);
...@@ -166,9 +166,9 @@ bool CBM_model::Motion_Detection(myImage *img) ...@@ -166,9 +166,9 @@ bool CBM_model::Motion_Detection(myImage *img)
frame_count++; frame_count++;
//_writer6.write(mog_fg); // _writer6.write(mog_fg);
//_writer7.write(mog_fg2); // _writer7.write(mog_fg2);
//_writer8.write(imgStatic); // _writer8.write(imgStatic);
return false; return false;
} }
else{ else{
...@@ -180,8 +180,8 @@ bool CBM_model::Motion_Detection(myImage *img) ...@@ -180,8 +180,8 @@ bool CBM_model::Motion_Detection(myImage *img)
_myGMM2->process(_Previous_Img[FG_count],input_temp); _myGMM2->process(_Previous_Img[FG_count],input_temp);
myImageAND(input_temp,maskROI,my_mog_fg2); myImageAND(input_temp,maskROI,my_mog_fg2);
myDiladeitself( my_mog_fg, 5); myDiladeitself( my_mog_fg, 3);
myDiladeitself( my_mog_fg2, 5); myDiladeitself( my_mog_fg2, 3);
if (check_foreground2(my_mog_fg)>(my_mog_fg->width*my_mog_fg->height*0.30)){//if motion detection cannot work well if (check_foreground2(my_mog_fg)>(my_mog_fg->width*my_mog_fg->height*0.30)){//if motion detection cannot work well
_myGMM->ChangeLearningRate(0.02);//speed up long-term model's learning rate to adapt the lighting changes. _myGMM->ChangeLearningRate(0.02);//speed up long-term model's learning rate to adapt the lighting changes.
...@@ -214,9 +214,9 @@ bool CBM_model::Motion_Detection(myImage *img) ...@@ -214,9 +214,9 @@ bool CBM_model::Motion_Detection(myImage *img)
} }
//_writer6.write(mog_fg); // _writer6.write(mog_fg);
//_writer7.write(mog_fg2); // _writer7.write(mog_fg2);
//_writer8.write(imgStatic); // _writer8.write(imgStatic);
FG_count = FG_count + 1; FG_count = FG_count + 1;
FG_count = FG_count%TEMPORAL_RULE; FG_count = FG_count%TEMPORAL_RULE;
...@@ -297,10 +297,10 @@ int CBM_model::GetLabeling2( myImage *pImg1, int areaThreshold, int option) ...@@ -297,10 +297,10 @@ int CBM_model::GetLabeling2( myImage *pImg1, int areaThreshold, int option)
vector<int> color_labels; vector<int> color_labels;
color_labels.clear(); color_labels.clear();
for (int i = 0; i < pImg1->width*pImg1->height; i++){ for (int i = 0; i < pImg1->width*pImg1->height; i++){
// if(out[i] > 2473){ if(out[i] > 2473){
// std::cout << "error number : " << out[i] <<std::endl; //std::cout << "error number : " << out[i] <<std::endl;
// continue; continue;
// } }
constant_template[out[i]] = true; constant_template[out[i]] = true;
} }
for (int i = 0; i < 2474; i++){ for (int i = 0; i < 2474; i++){
......
...@@ -29,7 +29,9 @@ double INPUT_RESIZE; ...@@ -29,7 +29,9 @@ double INPUT_RESIZE;
bool DetectAnomaly::detectAnomaly(cv::Mat img, vector<vector<int>>* resultLocation) bool DetectAnomaly::detectAnomaly(cv::Mat img, vector<vector<int>>* resultLocation)
{ {
yolo_test = img.clone(); yolo_test = img.clone();
cv::resize(img, img,cv::Size(2688*0.25, 1520*0.25), cv::INTER_CUBIC); cv::Rect rect(0,750,2400,770);
img = img(rect);
cv::resize(img, img,cv::Size(2400*0.25, 770*0.25), cv::INTER_CUBIC);
img.copyTo(myimg); img.copyTo(myimg);
medianBlur(mat_myimg, mat_myimg, 3); medianBlur(mat_myimg, mat_myimg, 3);
opencv_2_myImage(myimg, myimg1); //transfer opencv data to myimage data opencv_2_myImage(myimg, myimg1); //transfer opencv data to myimage data
...@@ -83,14 +85,14 @@ DetectAnomaly::DetectAnomaly() ...@@ -83,14 +85,14 @@ DetectAnomaly::DetectAnomaly()
// // INPUT_RESIZE = 0.25; // // INPUT_RESIZE = 0.25;
cfgAnomaly.input_resize = 0.25; cfgAnomaly.input_resize = 0.25;
cfgAnomaly.owner_search_roi = 10; cfgAnomaly.owner_search_roi = 10;
cfgAnomaly.gmm_learn_frame = 1000; cfgAnomaly.gmm_learn_frame = 300;
cfgAnomaly.max_sfg = 15625; cfgAnomaly.max_sfg = 15625;
cfgAnomaly.min_sfg = 50; cfgAnomaly.min_sfg = 15;
cfgAnomaly.max_fg = 75000; cfgAnomaly.max_fg = 75000;
cfgAnomaly.min_fg = 100; cfgAnomaly.min_fg = 50;
cfgAnomaly.buffer_length = 300; cfgAnomaly.buffer_length = 100;
cfgAnomaly.image_height = 1520 * cfgAnomaly.input_resize; cfgAnomaly.image_height = 770 * cfgAnomaly.input_resize;
cfgAnomaly.image_width = 2688 * cfgAnomaly.input_resize; cfgAnomaly.image_width = 2400 * cfgAnomaly.input_resize;
cfgAnomaly.gmm_long_learn_rate = 0.0001; cfgAnomaly.gmm_long_learn_rate = 0.0001;
cfgAnomaly.gmm_short_learn_rate = 0.002; cfgAnomaly.gmm_short_learn_rate = 0.002;
...@@ -99,24 +101,24 @@ DetectAnomaly::DetectAnomaly() ...@@ -99,24 +101,24 @@ DetectAnomaly::DetectAnomaly()
// set gloabl var // set gloabl var
INPUT_RESIZE = 1.0; INPUT_RESIZE = 1.0;
OWNER_SEARCH_ROI = 10; OWNER_SEARCH_ROI = 10;
GMM_LEARN_FRAME = 1000; //frame number for GMM initialization GMM_LEARN_FRAME = 300; //frame number for GMM initialization
MAX_SFG = 15625; MAX_SFG = 15625;
MIN_SFG = 50; MIN_SFG = 15;
MAX_FG = 75000; MAX_FG = 75000;
MIN_FG = 100; MIN_FG = 50;
BUFFER_LENGTH = 300; BUFFER_LENGTH = 100;
GMM_LONG_LEARN_RATE = 0.0001; GMM_LONG_LEARN_RATE = 0.0001;
GMM_SHORT_LEARN_RATE = 0.002; GMM_SHORT_LEARN_RATE = 0.002;
// INPUT_RESIZE = 0.25; // INPUT_RESIZE = 0.25;
cfgAnomaly.top_left_x = 759 * cfgAnomaly.input_resize; cfgAnomaly.top_left_x = 759 * cfgAnomaly.input_resize;
cfgAnomaly.top_left_y = 785 * cfgAnomaly.input_resize; cfgAnomaly.top_left_y = 35 * cfgAnomaly.input_resize;
cfgAnomaly.top_right_x = 1581 * cfgAnomaly.input_resize; cfgAnomaly.top_right_x = 1581 * cfgAnomaly.input_resize;
cfgAnomaly.top_right_y = 771 * cfgAnomaly.input_resize; cfgAnomaly.top_right_y = 21 * cfgAnomaly.input_resize;
cfgAnomaly.bottom_left_x = 73 * cfgAnomaly.input_resize; cfgAnomaly.bottom_left_x = 73 * cfgAnomaly.input_resize;
cfgAnomaly.bottom_left_y = 1489 * cfgAnomaly.input_resize; cfgAnomaly.bottom_left_y = 739 * cfgAnomaly.input_resize;
cfgAnomaly.bottom_right_x = 2376 * cfgAnomaly.input_resize; cfgAnomaly.bottom_right_x = 2376 * cfgAnomaly.input_resize;
cfgAnomaly.bottom_right_y = 1411 * cfgAnomaly.input_resize; cfgAnomaly.bottom_right_y = 661 * cfgAnomaly.input_resize;
obj_left = false; obj_left = false;
arr[0] = 4; arr[0] = 4;
...@@ -148,7 +150,7 @@ DetectAnomaly::DetectAnomaly() ...@@ -148,7 +150,7 @@ DetectAnomaly::DetectAnomaly()
opencv_2_myImage(mask, mymask); opencv_2_myImage(mask, mymask);
#ifdef DEBUG #ifdef DEBUG
cv::imwrite("mask.jpg", Image1); //cv::imwrite("mask.jpg", Image1);
cv::waitKey(1); cv::waitKey(1);
//cvReleaseImage(&Image1); //cvReleaseImage(&Image1);
#endif #endif
......
...@@ -13,8 +13,7 @@ ObjLeftDetect::ObjLeftDetect(myImage * input, int set_MOG_LearnFrame, int set_mi ...@@ -13,8 +13,7 @@ ObjLeftDetect::ObjLeftDetect(myImage * input, int set_MOG_LearnFrame, int set_mi
_CBM_model = new CBM_model(input, set_MOG_LearnFrame, set_min_area, set_buffer_len, INPUT_RESIZE, mask); _CBM_model = new CBM_model(input, set_MOG_LearnFrame, set_min_area, set_buffer_len, INPUT_RESIZE, mask);
new_width = (int)(input->width*INPUT_RESIZE); new_width = (int)(input->width*INPUT_RESIZE);
new_height = (int)(input->height*INPUT_RESIZE); new_height = (int)(input->height*INPUT_RESIZE);
// new_width = (int)(input->width);
// new_height = (int)(input->height);
initialize(); initialize();
alarmList.clear(); alarmList.clear();
} }
...@@ -28,6 +27,17 @@ void ObjLeftDetect::ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_m ...@@ -28,6 +27,17 @@ void ObjLeftDetect::ObjLeftSet(myImage *input, int set_MOG_LearnFrame, int set_m
_CBM_model = new CBM_model(input, set_MOG_LearnFrame, set_min_area, set_buffer_len, INPUT_RESIZE, mask); _CBM_model = new CBM_model(input, set_MOG_LearnFrame, set_min_area, set_buffer_len, INPUT_RESIZE, mask);
new_width = (int)(input->width * INPUT_RESIZE); new_width = (int)(input->width * INPUT_RESIZE);
new_height = (int)(input->height * INPUT_RESIZE); new_height = (int)(input->height * INPUT_RESIZE);
objmask = cv::Mat(cv::Size(new_width,new_height),CV_8UC1);
myImage_2_opencv(mask, objmask);
cv::findContours(objmask,contours,cv::noArray(),cv::RETR_EXTERNAL,cv::CHAIN_APPROX_SIMPLE);
cv::Mat showpic = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC1);
drawContours(showpic, contours, 0, Scalar::all(255));
// cv::imwrite("show.jpg",showpic);
// new_width = (int)(input->width);
// new_height = (int)(input->height);
// std::cout << "contours size : " << contours.size() << " "<< contours[0].size() <<std::endl;
// new_width = (int)(input->width); // new_width = (int)(input->width);
// new_height = (int)(input->height); // new_height = (int)(input->height);
initialize(); initialize();
...@@ -61,6 +71,7 @@ void ObjLeftDetect::initialize() ...@@ -61,6 +71,7 @@ void ObjLeftDetect::initialize()
A = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3); A = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
B = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3); B = cv::Mat::zeros(cv::Size(new_width,new_height),CV_8UC3);
// _writer1.open("static_to_track.avi", cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1); // _writer1.open("static_to_track.avi", cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1);
// _writer2.open("summary.avi", cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1); // _writer2.open("summary.avi", cv::VideoWriter::fourcc('X','V','I','D'),30,cv::Size(new_width,new_height),1);
...@@ -72,8 +83,8 @@ bool ObjLeftDetect::process(myImage * input, cv::Mat show_pic, vector<vector<int ...@@ -72,8 +83,8 @@ bool ObjLeftDetect::process(myImage * input, cv::Mat show_pic, vector<vector<int
cv::Mat test; cv::Mat test;
test = cv::Mat(cv::Size(new_width, new_height),CV_8UC3); test = cv::Mat(cv::Size(new_width, new_height),CV_8UC3);
set_alarm = false; set_alarm = false;
myResize(input,myimg2); //myResize(input,myimg2);
object_detected = _CBM_model->Motion_Detection(myimg2); object_detected = _CBM_model->Motion_Detection(input);
if (object_detected == true) if (object_detected == true)
{ {
...@@ -100,16 +111,32 @@ bool ObjLeftDetect::process(myImage * input, cv::Mat show_pic, vector<vector<int ...@@ -100,16 +111,32 @@ bool ObjLeftDetect::process(myImage * input, cv::Mat show_pic, vector<vector<int
auto result = results_map.at(0); auto result = results_map.at(0);
auto obj = LeftLocation[i]; auto obj = LeftLocation[i];
int objx0 = (obj->x - obj->width/2) * 4; int objx0 = (obj->x - obj->width);
int objy0 = (obj->y - obj->height/2) * 4; int objy0 = (obj->y - obj->height);
int objx1 = (obj->x + obj->width/2) * 4; int objx1 = (obj->x + obj->width);
int objy1 = (obj->y + obj->height/2) * 4; int objy1 = (obj->y + obj->height);
vector<int> ojblocation = {objx0, objy0, objx1, objy1}; vector<int> ojblocation = {objx0, objy0, objx1, objy1};
if( ((objx1 - objx0) > 200) || ((objy1 - objy0) >200) ){ //check points location
cv::Point2f point1(objx0,objy0);
cv::Point2f point2(objx1,objy1);
auto check1 = cv::pointPolygonTest(contours[0],point1,false);
auto check2 = cv::pointPolygonTest(contours[0],point2,false);
std::cout << check1 << " " <<check2<<std::endl;
if( (check1 == -1) || (check2==-1) ){
continue;
}
if( ((objx1 - objx0) > 50) || ((objy1 - objy0) >50) ){
continue; continue;
} }
bool iscar = false; bool iscar = false;
for (int j = 0; j < result->facility.size(); j++) { for (int j = 0; j < result->facility.size(); j++) {
......
...@@ -69,6 +69,10 @@ private: ...@@ -69,6 +69,10 @@ private:
bool set_alarm; bool set_alarm;
int pic_num = 0; int pic_num = 0;
//get mask points
cv::Mat objmask;
std::vector<std::vector<cv::Point>> contours;
}; };
#endif #endif
...@@ -42,7 +42,7 @@ int main() ...@@ -42,7 +42,7 @@ int main()
int count = 0; int count = 0;
// char test_video[200] = "zhao.mp4"; // char test_video[200] = "zhao.mp4";
char test_video[200] = "../test_video/out.avi"; char test_video[200] = "/media/6542bc52-3d7a-4bea-b9b6-f9343ca8560f/AB/test_video/test-6.avi";
VideoDetails *_video; VideoDetails *_video;
_video = new VideoDetails(test_video); _video = new VideoDetails(test_video);
//VideoCapture capture; //VideoCapture capture;
...@@ -52,6 +52,7 @@ int main() ...@@ -52,6 +52,7 @@ int main()
cv::Mat qImg; cv::Mat qImg;
// config opt; // config opt;
DetectAnomaly detect; DetectAnomaly detect;
int pic_num = 0;
#ifdef DEBUG #ifdef DEBUG
//cv::VideoWriter _writer; //cv::VideoWriter _writer;
//_writer.open("alarm.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), 30, cv::Size(2448*0.25, 2048*0.25), 1); //_writer.open("alarm.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), 30, cv::Size(2448*0.25, 2048*0.25), 1);
...@@ -60,22 +61,22 @@ int main() ...@@ -60,22 +61,22 @@ int main()
/* main loop */ /* main loop */
/************************************************************************/ /************************************************************************/
bool obj_left = false; bool obj_left = false;
//calculate time
struct timeval start, end;
float time = 0;
gettimeofday(&start, NULL);
while (_video->_file.read(qImg)) while (_video->_file.read(qImg))
{ {
// cvResize(src_img,des_img,CV_INTER_LINEAR); // cvResize(src_img,des_img,CV_INTER_LINEAR);
vector<vector<int>> resultLocation; vector<vector<int>> resultLocation;
//calculate time pic_num++;
struct timeval start, end; if(pic_num % 2 == 0){
float time = 0; continue;
gettimeofday(&start, NULL); }
obj_left = detect.detectAnomaly(qImg,&resultLocation); obj_left = detect.detectAnomaly(qImg,&resultLocation);
gettimeofday(&end, NULL);
time = (1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec) /
(1000.0);
cout << "total time = " << time << "ms" << endl;
//yolo_infer //yolo_infer
...@@ -94,10 +95,10 @@ int main() ...@@ -94,10 +95,10 @@ int main()
printf("alram!!\n"); printf("alram!!\n");
for(int i = 0; i < resultLocation.size();i++){ for(int i = 0; i < resultLocation.size();i++){
int x0 = resultLocation[i][0]; int x0 = resultLocation[i][0] * 4;
int y0 = resultLocation[i][1]; int y0 = resultLocation[i][1] * 4 + 750;
int x1 = resultLocation[i][2]; int x1 = resultLocation[i][2] * 4;
int y1 = resultLocation[i][3]; int y1 = resultLocation[i][3] * 4 + 750;
cv::Point p1(x0, y0); cv::Point p1(x0, y0);
cv::Point p2(x1, y1); cv::Point p2(x1, y1);
...@@ -116,6 +117,10 @@ int main() ...@@ -116,6 +117,10 @@ int main()
} }
#endif #endif
} }
gettimeofday(&end, NULL);
time = (1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec) /
(1000.0);
cout << "total time = " << time << "ms" << endl;
#ifdef DEBUG #ifdef DEBUG
printf("Finished detec, then release ...\n"); printf("Finished detec, then release ...\n");
......
...@@ -28,7 +28,7 @@ void*** Memory_Allocate(int width, int height, int depth) ...@@ -28,7 +28,7 @@ void*** Memory_Allocate(int width, int height, int depth)
for( int i = 0; i < width; i++) for( int i = 0; i < width; i++)
for( int j = 0; j < height; j++) for( int j = 0; j < height; j++)
for( int k = 0; k < depth; k++) for( int k = 0; k < depth; k++)
pixel_index[i][j][k] = NULL; pixel_index[i][j][k] = ' ';
return (void***)pixel_index; return (void***)pixel_index;
} }
...@@ -53,7 +53,7 @@ void* Memory_Allocate1D(int width, int height, int depth) ...@@ -53,7 +53,7 @@ void* Memory_Allocate1D(int width, int height, int depth)
pixel_index = (unsigned char *)malloc(width*height*depth*sizeof(unsigned char)); pixel_index = (unsigned char *)malloc(width*height*depth*sizeof(unsigned char));
for( int i = 0; i < width*height*depth; i++) for( int i = 0; i < width*height*depth; i++)
pixel_index[i] = NULL; pixel_index[i] = ' ';
return (void*)pixel_index; return (void*)pixel_index;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment