Ask Your Question

How to detect object from video using SVM

asked 2013-08-08 08:10:44 -0600

FLY gravatar image

updated 2013-08-27 15:12:36 -0600

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

    vector<Mat> descriptors = bowTrainer.getDescriptors();
    int count=0;
    for(vector<Mat>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
    cout<<"Clustering "<<count<<" features"<<endl;
    //choosing cluster's centroids as dictionary's words
    Mat dictionary = bowTrainer.cluster();
    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 
              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);
              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

edit retag flag offensive close merge delete


Please try to make a clean code format. It isn't that hard. Also please formulate your question more clearly.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-08 08:19:53 -0600 )edit

@StevenPuttemans I try to make it better ,and try to make my question clear

FLY gravatar imageFLY ( 2013-08-08 08:43:26 -0600 )edit

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 gravatar imageMathieu Barnachon ( 2013-08-10 18:04:15 -0600 )edit

@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

FLY gravatar imageFLY ( 2013-08-11 02:19:41 -0600 )edit

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.

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-08-11 04:31:29 -0600 )edit

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.

GilLevi gravatar imageGilLevi ( 2013-08-11 04:38:32 -0600 )edit

@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

FLY gravatar imageFLY ( 2013-08-11 04:40:20 -0600 )edit

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?

GilLevi gravatar imageGilLevi ( 2013-08-11 04:46:48 -0600 )edit

@GiLevi I am just going to check that , detection by parts , i didn't check that , and having no knowledge about it

FLY gravatar imageFLY ( 2013-08-11 04:50:05 -0600 )edit

@StevenPuttermans Understood! sorry about the bad usage

ximobayo gravatar imageximobayo ( 2013-08-11 16:50:38 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2013-08-18 08:53:20 -0600

If I understand your question correctly, you should implement a sliding window approach. In each window, you should apply the SVM to get candidates. Then, once you've done it for the whole image, you should merge the candidates (if you detected an object, then it is very likely that you'll detect it again in shift of a few pixels - that's the meaning of candidates).

Take a look at the V&J code at openCV or the latentSVM code (detection by parts) to see how it's done there.

By the way, I would use the LatentSVM code (detection by parts) to detect vehicles. It has trained models for cars and for buses.

Good luck.

edit flag offensive delete link more

answered 2013-08-10 11:27:43 -0600

ximobayo gravatar image

But what is the SVM learning? Histogram? Some descriptor?

edit flag offensive delete link more


Yes , keypoints using surf and histrogram

FLY gravatar imageFLY ( 2013-08-10 11:36:40 -0600 )edit

@ximobayo please stick to comments for basic questions, not answers. This is clearly stated in the FAQ, which contains the forum guidelines.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-08-10 14:45:19 -0600 )edit

Question Tools


Asked: 2013-08-08 08:10:44 -0600

Seen: 2,688 times

Last updated: Aug 27 '13