SVM-based pedestrian recognition training based on opencv

Source: Internet
Author: User
Tags svm

Well-written SVM + hog Classifier Training

Class mysvm: Public cvsvm {public: int get_alpha_count () {return this-> sv_total;} int get_sv_dim () {return this-> var_all;} int get_sv_count () {return this-> decision_func-> sv_count;} double * get_alpha () {return this-> decision_func-> alpha;} float ** get_sv () {return this-> Sv;} float get_rov () {return this-> decision_func-> rho;}}; void train () {char classifiersavepath [256] = "C: /pedestrianDetect-peopleFlow.txt "; string positivepath =" E: \ pictures \ train1 \ pos \ "; string negativepath =" E: \ pictures \ train1 \ neg \ "; int positivesamplecount = 4900; int negativesamplecount = 6192; int totalsamplecount = positivesamplecount + negativesamplecount; cout <"//////////////////////////////////// /// // "<Endl; cout <"totalsamplecount:" <totalsamplecount <Endl; cout <"positivesamplecount:" <positivesamplecount <Endl; cout <"negativesamplecount: "<negativesamplecount <Endl; cvmat * samplefeaturesmat = cvcreatemat (totalsamplecount, 1764, cv_32fc1); // 64*128 training samples, this matrix will be a training sample of totalsample * 3780,64*64. this matrix will be totalsample * 1764 cvsetzero (samplefeaturesmat); cvmat * samplelabelmat = cvcreatemat (totalsamplecount, 1, cv_32fc1 ); // sample ID cvsetzero (samplelabelmat ); cout <"************************************ * *********************** "<Endl; cout <"start to training positive samples... "<Endl; char positiveimgname [256]; string path; For (INT I = 0; I <positivesamplecount; I ++) {memset (positiveimgname, '\ 0 ', 256 * sizeof (char); sprintf (positiveimgname, "d.jpg", I); int Len = strlen (positiveimgname); string tempstr = positiveimgname; Path = positivepath + tempstr; CV:: mat IMG = CV: imread (PATH); If (IMG. data = NULL) {cout <"positive image sample load error:" <I <"" <path <Endl; System ("pause "); continue;} CV: hogdescriptor hog (CV: size (64, 64), CV: size (16, 16), CV: size (8, 8), CV :: size (8, 8), 9); vector <float> featurevec; hog. compute (IMG, featurevec, CV: size (8, 8); int featurevecsize = featurevec. size (); For (Int J = 0; j <featurevecsize; j ++) {cv_mat_elem (* samplefeaturesmat, float, I, j) = featurevec [J];} samplelabelmat-> data. FL [I] = 1;} cout <"End of training for positive samples... "<Endl; cout <"************************************ * ******************** "<Endl; cout <"start to train negative samples... "<Endl; char negativeimgname [256]; for (INT I = 0; I <negativesamplecount; I ++) {memset (negativeimgname, '\ 0 ', 256 * sizeof (char); sprintf (negativeimgname, "mongod.jpg", I); Path = negativepath + negativeimgname; CV: mat IMG = CV: imread (PATH ); if (IMG. data = NULL) {cout <"negative image sample load error:" <path <Endl; continue;} CV: hogdescriptor hog (CV :: size (64), CV: size (16, 16), CV: size (8, 8), CV: size (8, 8), 9); vector <float> featurevec; hog. compute (IMG, featurevec, CV: size (8, 8); // calculate the hog feature int featurevecsize = featurevec. size (); For (Int J = 0; j <featurevecsize; j ++) {cv_mat_elem (* samplefeaturesmat, float, I + positivesamplecount, j) = featurevec [J];} samplelabelmat-> data. FL [I + positivesamplecount] =-1;} cout <"End of training for negative samples... "<Endl; cout <"************************************ * ******************* "<Endl; cout <"start to train for SVM classifier... "<Endl; cvsvmparams Params; Params. svm_type = cvsvm: c_svc; Params. kernel_type = cvsvm: Linear; Params. term_crit = cvtermcriteria (cv_termcrit_iter, 1000, flt_epsilon); Params. C = 0.01; mysvm SVM; SVM. train (samplefeaturesmat, samplelabelmat, null, null, Params); // train SVM Using SVM linear classifier. save (classifiersavepath); cvreleasemat (& samplefeaturesmat); cvreleasemat (& samplelabelmat); int supportvectorsize = SVM. get_support_vector_count (); cout <"Support Vector size of SVM:" <supportvectorsize <Endl; cout <"************************ end of training for SVM ******* * *********** "<Endl; cvmat * SV, * phosphatase, * Re; // all sample feature vectors SV = cvcreatemat (supportvectorsize, 1764, cv_32fc1); phosphatase = cvcreatemat (1, supportvectorsize, cv_32fc1 ); re = cvcreatemat (1, 1764, cv_32fc1); cvmat * res = cvcreatemat (1, 1, cv_32fc1); cvsetzero (SV); cvsetzero (re ); for (INT I = 0; I <supportvectorsize; I ++) {memcpy (float *) (SV-> data. FL + I * 1764), SVM. get_support_vector (I), 1764 * sizeof (float);} double * alphago r = SVM. get_alpha (); int alphacount = SVM. get_alpha_count (); For (INT I = 0; I <supportvectorsize; I ++) {Alb-> data. FL [I] = alph316r [I];} cvmatmul (ALB, SV, RE); int poscount = 0; For (INT I = 0; I <1764; I ++) {re-> data. FL [I] * =-1;} file * fp = fopen ("C:/hogSVMDetector-peopleFlow.txt", "WB"); If (null = FP) {return 1 ;} for (INT I = 0; I <1764; I ++) {fprintf (FP, "% F \ n", re-> data. FL [I]);} float ROV = SVM. get_rov (); fprintf (FP, "% F", rock); cout <"C:/hogsvmdetector.txt saved" <Endl; // save fclose (FP); return 1 ;}

The detection code is similar to that of opencv.

void Detect(){CvCapture* cap = cvCreateFileCapture("E:\\02.avi");if (!cap){cout<<"avi file load error..."<<endl;system("pause");exit(-1);}vector<float> x;ifstream fileIn("c:/hogSVMDetector-peopleFlow.txt", ios::in);float val = 0.0f;while(!fileIn.eof()){fileIn>>val;x.push_back(val);}fileIn.close();vector<cv::Rect>  found;cv::HOGDescriptor hog(cv::Size(64,64), cv::Size(16,16), cv::Size(8,8), cv::Size(8,8), 9);hog.setSVMDetector(x);IplImage* img = NULL;cvNamedWindow("img", 0);while(img=cvQueryFrame(cap)){hog.detectMultiScale(img, found, 0, cv::Size(8,8), cv::Size(32,32), 1.05, 2);if (found.size() > 0){for (int i=0; i<found.size(); i++){CvRect tempRect = cvRect(found[i].x, found[i].y, found[i].width, found[i].height);cvRectangle(img, cvPoint(tempRect.x,tempRect.y),cvPoint(tempRect.x+tempRect.width,tempRect.y+tempRect.height),CV_RGB(255,0,0), 2);}}}cvReleaseCapture(&cap);}

SVM + hog Algorithm for pedestrian recognition, the effect is actually good, compared with Haar + Adaboost, in dm3730 ARM + DSP dual-core, I implemented two methods, it seems that SVM + hog is a little slow, and Haar + AdaBoost is still good. We have done pedestrian recognition and the classifier is trained on its own, as in 18 stages, 640x480. if you do this in dm8148 or dm8168, it should be very practical. If it is a pure DSP, it was previously made on dm6437, and it seems that the speed is not very good. At present, I am studying the dm8148 architecture, which is similar to OMAP or dm3730. I have some more openmax and so on. It is estimated that it takes care of Android development users. I still like to develop it directly based on Linux, and the code is C, the new opencv has more
Vector and so on. It is estimated that TI's DSP environment may not be supported.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.