I am working in a project in which I have to detect the Motion of a human. First of all I wrote a program for Motion Detection and got it working properly. Then I moved to Human Detection using HOGDescriptor
and combined both the programs to increase the speed of the process. First I monitor for a motion and if there is any motion detected, then I crop the image by the rectangular box denoting the motion and send the cropped part alone to the Human detection function so that it can be processed quickly.
But there arises a problem. I am getting a good results some times and for sometimes I am getting a Pop Up window saying Unhandled Exception at some memory location in the .exe file.
My program is
#include <iostream>
#include <ctime>
#include<stdlib.h>
#include<vector>
#include"opencv2\opencv.hpp"
#include"opencv2\highgui\highgui.hpp"
#include"opencv2\core\core.hpp"
#include"opencv2\imgproc\imgproc.hpp"
#include<string>
#include<sstream>
using namespace std;
using namespace cv;
Mat detect1(int,VideoCapture,VideoWriter);
vector<Rect> found;
int humandet(Mat,Rect);
BackgroundSubtractorMOG2 bg[5];
int _tmain(int argc, _TCHAR* argv[])
{
Mat frame[5];
string win[5]={"Video 0","Video 1","Video 2","Video 3"};
string ip,user,pass;
stringstream ss;
string vid[5]={"D:/Recorded.avi","D:/Recorded1.avi","D:/Recorded2.avi","D:/Recorded3.avi"};
VideoWriter vidarr[5];
VideoCapture cap[5];
int n,type,j;
cout<<"Enter the no of cameras";
cin>>n;
for(int i=0,j=0;i<n;i++)
{
cout<<"Enter the camera type\n1.IP camera\n2.Non IP camera";
cin>>type;
if(type==2)
{
VideoCapture cap1(j++);
cap[i]=cap1;
cap[i].set(CV_CAP_PROP_FRAME_WIDTH,320);
cap[i].set(CV_CAP_PROP_FRAME_HEIGHT,240);
cap[i].set(CV_CAP_PROP_FPS,2);
}
else
{
cout<<"Enter the IP add:portno, username and password";
cin>>ip>>user>>pass;
ss<<"http://"<<user<<":"<<pass<<"@"<<ip<<"/axis-cgi/mjpg/video.cgi?.mjpg";
string s(ss.str());
VideoCapture cap2(s);
cap[i]=cap2;
cap[i].set(CV_CAP_PROP_FRAME_WIDTH,320);
cap[i].set(CV_CAP_PROP_FRAME_HEIGHT,240);
cap[i].set(CV_CAP_PROP_FPS,2);
}
VideoWriter video(vid[i],CV_FOURCC('D','I','V','X'),2,Size(320,240));
vidarr[i]=video;
}
while(9)
{
for(int i=0;i<n;i++)
{
frame[i]=detect1(i,cap[i],vidarr[i]);
imshow(win[i],frame[i]);
}
if(waitKey(30)==27)
break;
}
return 0;
}
Mat detect1(int j,VideoCapture cap,VideoWriter vid)
{
Mat frame;
Mat diff;
cap>>frame;
double large_area=0;
int large=0;
Rect bound_rect;
bg[j].nmixtures=3;
bg[j].bShadowDetection=true;
bg[j].nShadowDetection=0;
bg[j].fTau = 0.5;
bg[j].operator() (frame,diff);
vector<vector<Point>> contour;
findContours(diff,contour,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
for(unsigned int i=0;i<contour.size();i++)
{
double area=contourArea(contour[i]);
if(area>large_area)
{
large_area=area;
large=i;
bound_rect=boundingRect(contour[i]);
}
}
contour.clear();
if(large_area/100 > 2)
{
humandet(frame,bound_rect);
rectangle(frame,bound_rect,Scalar(0,0,255),2);
putText(frame,"Recording",Point(20,20),CV_FONT_HERSHEY_PLAIN,2,Scalar(0,255,0),2);
vid.write(frame);
return (frame);
}
else
return (frame);
}
int humandet(Mat frame1,Rect bound)
{
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
if((bound.height < 100) && (bound.width < 80))
{
Mat roi;
roi.create(Size(80,100),frame1.type());
roi.setTo(Scalar::all(0));
Mat fram=frame1(bound);
fram.copyTo(roi(Rect(0,0,(bound.height-1),(bound.width-1))));
hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
roi.release();
fram.release();
}
else if((bound.height < 200) && (bound.width < 160))
{
Mat roi;
roi.create(Size(160,200),frame1.type());
roi.setTo(Scalar::all(0));
Mat fram=frame1(bound);
fram.copyTo(roi(Rect(1,1,(bound.height-1),(bound.width-1))));
hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
roi.release();
fram.release();
}
else
{
Mat roi;
roi=frame1;
hog.detectMultiScale(roi,found,0,Size(8,8),Size(32,32),1.025);
roi.release();
}
for(unsigned int i=0;i<found.size();i++)
{
rectangle(frame1,found[i], Scalar(255,0,0), 2);
}
if(found.size())
{
frame1.release();
found.clear();
return 1;
}
else
return 0;
}
Before I used the cropping method, It was working good. i.e, when I passed the frame to the 'humandet' function without any changes and processed it as it is, there was no problem. But it was quite slow. So that I cropped the image and made the resolution constant and processed. Due to this the processing speed increased to a considerable amount. But it is often throwing an Exception. I think the problem is with the memory allocation. But I couldn't figure it out.
Give me a solution and a method to debug the error I made. Thanks in advance.