How to detect object from video using SVM
This is my code for training the dataset of for example vehicles , when it train fully , i want it to predict the data(vehicle) from video(.avi) , how to do it , i want that when the vehicle is shown in the video it count it as 1 and cout that the object is detected and if second vehicle come it increment the count as 2 , like i train it on vehicle , so when it found vehicle in the video , it count it as 1
collectclasscentroids();
vector<Mat> descriptors = bowTrainer.getDescriptors();
int count=0;
for(vector<Mat>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
{
count+=iter->rows;
}
cout<<"Clustering "<<count<<" features"<<endl;
//choosing cluster's centroids as dictionary's words
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
cout<<"extracting histograms in the form of BOW for each image "<<endl;
Mat labels(0, 1, CV_32FC1);
Mat trainingData(0, dictionarySize, CV_32FC1);
int k=0;
vector<KeyPoint> keypoint1;
Mat bowDescriptor1;
//extracting histogram in the form of bow for each image
for(j=1;j<=4;j++)
for(i=1;i<=60;i++)
{
sprintf( ch,"%s%d%s%d%s","train/",j," (",i,").jpg");
const char* imageName = ch;
img2 = cvLoadImage(imageName,0);
detector.detect(img2, keypoint1);
bowDE.compute(img2, keypoint1, bowDescriptor1);
trainingData.push_back(bowDescriptor1);
labels.push_back((float) j);
}
For those who didn't understand the question :
I want to play a movie in above code
VideoCapture cap("movie.avi");
Suppose i have a trained data which contain vehicle's , and "movie.avi" contain 5 vehicles , so it should detect that vehicles from the movie.avi and give me 5 as output
How to do this part in the above code
Please try to make a clean code format. It isn't that hard. Also please formulate your question more clearly.
@StevenPuttemans I try to make it better ,and try to make my question clear
I think you question is more: how to track a vehicle after detection. You want to count unique vehicle, so if you perform detection at each frame, you re-detect vehicle previously detected. Try to use the Kalman filter for tracking vehicle from frame to frame.
@Mathieu Barnachon But i just trained my data for vehicle detection , how i apply it on video , so the trained data like vehicle is detected from video , like we predict the image for 1 or -1 in the example of svm in opencv , like if it is in the range of (50,20) it shows 1 , otherwise -1
1 means there is a car in the image, -1 means there is no car in the image. You probably want to localize the car, aka: draw a box around it? If it is, I suggest looking at paper on car detection and localization. You could also look at the tutorial in features 2D for features matching with FLANN, but I'm not sure this is the right approach. Maybe you have to use a different approach, like LBP or HAAR.
By the way, why don't you use the detection by parts code to detect vehicles? Or at least take a look at the code to understand how the detection is made. If I understand your question right, your'e asking how to apply the SVM on video (in what scales, location, how to merge bounding boxes). Perhaps you should take a look at the detection by parts code and see how it is done there.
@Mathieu Barnachon Yes , exactly , i first delete the background from the movie , and than i want to apply the above algorithm on that video , to detect the car's from it , and than draw box around the car which it detect
OK, I'm not really sure how to do it. The best advice I can give you is to take a look at other classifiers in OpenCV to see how it is done there, for example latent svm detector and V&J. Perhaps you should wait for someone else to comment.
By the way, why don't you just use detection by parts? It has models for bikes, cars and buses. Is it not what you are interested in?
@GiLevi I am just going to check that , detection by parts , i didn't check that , and having no knowledge about it
@StevenPuttermans Understood! sorry about the bad usage