树莓PI远程控制摄像头请参考前文:http://www.cnblogs.com/yuliyang/p/3561209.html
参考:http://answers.opencv.org/question/133/how-do-i-access-an-ip-camera/
http://blog.youtueye.com/work/opencv-hog-peopledetector-trainning.html
项目环境:opencv2.8 ,debian, QT
代码:
运行:
yuliyang@debian-yuliyang:~/build-peopledetect-桌面-Debug$ ./peopledetect
/home/yuliyang/OLTbinaries/INRIAPerson/HOG/model_4BiSVMLight.alt
model_4BiSVMLight.alt文件是INRIAPerson行人检测库HOG文件夹下的modle文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 |
#include <fstream> #include <iostream> #include <vector> #include <stdio.h> #include <string.h> #include <ctype.h> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" using
namespace std; using
namespace cv; vector< float > load_lear_model( const
char * model_file) { vector< float > detector; FILE
*modelfl; if
((modelfl = fopen
(model_file, "rb" )) == NULL) { cout<< "Unable to open the modelfile" <<endl; return
detector; } char
version_buffer[10]; if
(! fread
(&version_buffer, sizeof ( char ),10,modelfl)) { cout<< "Unable to read version" <<endl; return
detector; } if ( strcmp (version_buffer, "V6.01" )) { cout<< "Version of model-file does not match version of svm_classify!" <<endl; return
detector; } // read version number int
version = 0; if
(! fread
(&version, sizeof ( int ),1,modelfl)) { cout<< "Unable to read version number" <<endl; return
detector; } if
(version < 200) { cout<< "Does not support model file compiled for light version" <<endl; return
detector; } long
kernel_type; fread (&(kernel_type), sizeof ( long ),1,modelfl); { // ignore these long
poly_degree; fread (&(poly_degree), sizeof ( long ),1,modelfl); double
rbf_gamma; fread (&(rbf_gamma), sizeof ( double ),1,modelfl); double
coef_lin; fread (&(coef_lin), sizeof ( double ),1,modelfl); double
coef_const; fread (&(coef_const), sizeof ( double ),1,modelfl); long
l; fread (&l, sizeof ( long ),1,modelfl); char * custom = new
char [l]; fread (custom, sizeof ( char ),l,modelfl); delete [] custom; } long
totwords; fread (&(totwords), sizeof ( long ),1,modelfl); { // ignore these long
totdoc; fread (&(totdoc), sizeof ( long ),1,modelfl); long
sv_num; fread (&(sv_num), sizeof ( long ),1,modelfl); } double
linearbias = 0.0; fread (&linearbias, sizeof ( double ),1,modelfl); if (kernel_type == 0) { /* linear kernel */ /* save linear wts also */ double * linearwt = new
double [totwords+1]; int
length = totwords; fread (linearwt, sizeof ( double ),totwords+1,modelfl); for ( int
i = 0;i<totwords;i++){ float
term = linearwt[i]; detector.push_back(term); } float
term = -linearbias; detector.push_back(term); delete
[] linearwt; } else
{ cout<< "Only supports linear SVM model files" <<endl; } fclose (modelfl); return
detector; } void
help() { printf ( "\nDemonstrate the use of the HoG descriptor using\n" " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n" "Usage:\n" "./peopledetect (<image_filename> | <image_list>.txt)\n\n" ); } int
main( int
argc, char ** argv) { VideoCapture cap; cap.open( "http://192.168.1.102:8001/?action=stream?dummy=param.mjpg" );//在浏览器里输入http://192.168.1.102:8001/?action=stream cap.set(CV_CAP_PROP_FRAME_WIDTH, 320); cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240); if
(!cap.isOpened()) return
-1; Mat img; FILE * f = 0; // char _filename[1024]; // if( argc != 3 ) // { // cout<<"ERROR"<<endl; // return 0; // } // img = imread(argv[1]); // cap >> img; // if( img.data ) // { // strcpy(_filename, argv[1]); // } // else // { // f = fopen(argv[1], "rt"); // if(!f) // { // fprintf( stderr, "ERROR: the specified file could not be loaded\n"); // return -1; // } // } HOGDescriptor hog; //hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); vector< float > detector = load_lear_model(argv[1]); hog.setSVMDetector(detector); namedWindow( "people detector" , 1); while
( true ) { cap >> img; if
(!img.data) continue ; vector<Rect> found, found_filtered; hog.detectMultiScale(img, found, 0, Size(4,4), Size(0,0), 1.05, 2); size_t
i, j; for
(i=0; i<found.size(); i++) { Rect r = found[i]; for
(j=0; j<found.size(); j++) if
(j!=i && (r & found[j])==r) break ; if
(j==found.size()) found_filtered.push_back(r); } for
(i=0; i<found_filtered.size(); i++) { Rect r = found_filtered[i]; r.x += cvRound(r.width*0.1); r.width = cvRound(r.width*0.8); r.y += cvRound(r.height*0.06); r.height = cvRound(r.height*0.9); rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2); } imshow( "people detector" , img); if
(waitKey(20) >= 0) break ; } // for(;;) // { // char* filename = _filename; // if(f) // { // if(!fgets(filename, (int)sizeof(_filename)-2, f)) // break; // //while(*filename && isspace(*filename)) // // ++filename; // if(filename[0] == ‘#‘) // continue; // int l = strlen(filename); // while(l > 0 && isspace(filename[l-1])) // --l; // filename[l] = ‘\0‘; // img = imread(filename); // } // printf("%s:\n", filename); // if(!img.data) // continue; // fflush(stdout); // vector<Rect> found, found_filtered; // double t = (double)getTickCount(); // // run the detector with default parameters. to get a higher hit-rate // // (and more false alarms, respectively), decrease the hitThreshold and // // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). // hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); // t = (double)getTickCount() - t; // printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); // size_t i, j; // for( i = 0; i < found.size(); i++ ) // { // Rect r = found[i]; // for( j = 0; j < found.size(); j++ ) // if( j != i && (r & found[j]) == r) // break; // if( j == found.size() ) // found_filtered.push_back(r); // } // for( i = 0; i < found_filtered.size(); i++ ) // { // Rect r = found_filtered[i]; // // the HOG detector returns slightly larger rectangles than the real objects. // // so we slightly shrink the rectangles to get a nicer output. // r.x += cvRound(r.width*0.1); // r.width = cvRound(r.width*0.8); // r.y += cvRound(r.height*0.07); // r.height = cvRound(r.height*0.8); // rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); // } // imshow("people detector", img); // int c = waitKey(0) & 255; // if( c == ‘q‘ || c == ‘Q‘ || !f) // break; // } if (f) fclose (f); return
0; } //#include "opencv2/imgproc/imgproc.hpp" //#include "opencv2/objdetect/objdetect.hpp" //#include "opencv2/highgui/highgui.hpp" //#include <stdio.h> //#include <string.h> //#include <ctype.h> //using namespace cv; //using namespace std; //void help() //{ // printf( // "\nDemonstrate the use of the HoG descriptor using\n" // " HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n" // "Usage:\n" // "./peopledetect (<image_filename> | <image_list>.txt)\n\n"); //} //int main(int argc, char** argv) //{ // Mat img; // FILE* f = 0; // char _filename[1024]; // if( argc == 1 ) // { // printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n"); // return 0; // } // img = imread(argv[1]); // if( img.data ) // { // strcpy(_filename, argv[1]); // } // else // { // f = fopen(argv[1], "rt"); // if(!f) // { // fprintf( stderr, "ERROR: the specified file could not be loaded\n"); // return -1; // } // } // HOGDescriptor hog; // hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); // namedWindow("people detector", 1); // for(;;) // { // char* filename = _filename; // if(f) // { // if(!fgets(filename, (int)sizeof(_filename)-2, f)) // break; // //while(*filename && isspace(*filename)) // // ++filename; // if(filename[0] == ‘#‘) // continue; // int l = strlen(filename); // while(l > 0 && isspace(filename[l-1])) // --l; // filename[l] = ‘\0‘; // img = imread(filename); // } // printf("%s:\n", filename); // if(!img.data) // continue; // fflush(stdout); // vector<Rect> found, found_filtered; // double t = (double)getTickCount(); // // run the detector with default parameters. to get a higher hit-rate // // (and more false alarms, respectively), decrease the hitThreshold and // // groupThreshold (set groupThreshold to 0 to turn off the grouping completely). // hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2); // t = (double)getTickCount() - t; // printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency()); // size_t i, j; // for( i = 0; i < found.size(); i++ ) // { // Rect r = found[i]; // for( j = 0; j < found.size(); j++ ) // if( j != i && (r & found[j]) == r) // break; // if( j == found.size() ) // found_filtered.push_back(r); // } // for( i = 0; i < found_filtered.size(); i++ ) // { // Rect r = found_filtered[i]; // // the HOG detector returns slightly larger rectangles than the real objects. // // so we slightly shrink the rectangles to get a nicer output. // r.x += cvRound(r.width*0.1); // r.width = cvRound(r.width*0.8); // r.y += cvRound(r.height*0.07); // r.height = cvRound(r.height*0.8); // rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3); // } // imshow("people detector", img); // int c = waitKey(0) & 255; // if( c == ‘q‘ || c == ‘Q‘ || !f) // break; // } // if(f) // fclose(f); // return 0; //} |
效果:
opencv+树莓PI的基于HOG特征的行人检测,布布扣,bubuko.com
原文:http://www.cnblogs.com/yuliyang/p/3594002.html