Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.37 KB | None | 0 0
  1. 1.Got code form Github link of opencv site
  2.  
  3. #include "opencv2/objdetect.hpp"
  4. #include "opencv2/imgcodecs.hpp"
  5. #include "opencv2/videoio.hpp"
  6. #include "opencv2/highgui.hpp"
  7. #include "opencv2/imgproc.hpp"
  8. #include "opencv2/core/utility.hpp"
  9. #include "opencv2/core/ocl.hpp"
  10.  
  11. #include <cctype>
  12. #include <iostream>
  13. #include <iterator>
  14. #include <stdio.h>
  15.  
  16. using namespace std;
  17. using namespace cv;
  18.  
  19. static void help()
  20. {
  21. cout << "\nThis program demonstrates the cascade recognizer. Now you can use Haar or LBP features.\n"
  22. "This classifier can recognize many kinds of rigid objects, once the appropriate classifier is trained.\n"
  23. "It's most known use is for faces.\n"
  24. "Usage:\n"
  25. "./facedetect [--cascade=<cascade_path> this is the primary trained classifier such as frontal face]\n"
  26. " [--nested-cascade[=nested_cascade_path this an optional secondary classifier such as eyes]]\n"
  27. " [--scale=<image scale greater or equal to 1, try 1.3 for example>]\n"
  28. " [--try-flip]\n"
  29. " [filename|camera_index]\n\n"
  30. "see facedetect.cmd for one call:\n"
  31. "./facedetect --cascade=\"../../data/haarcascades/haarcascade_frontalface_alt.xml\" --nested-cascade=\"../../data/haarcascades/haarcascade_eye.xml\" --scale=1.3\n\n"
  32. "During execution:\n\tHit any key to quit.\n"
  33. "\tUsing OpenCV version " << CV_VERSION << "\n" << endl;
  34. }
  35.  
  36. void detectAndDraw( UMat& img, Mat& canvas, CascadeClassifier& cascade,
  37. CascadeClassifier& nestedCascade,
  38. double scale, bool tryflip );
  39.  
  40. string cascadeName = "../../data/haarcascades/haarcascade_frontalface_alt.xml";
  41. string nestedCascadeName = "../../data/haarcascades/haarcascade_eye_tree_eyeglasses.xml";
  42.  
  43. int main( int argc, const char** argv )
  44. {
  45. VideoCapture capture;
  46. UMat frame, image;
  47. Mat canvas;
  48. const string scaleOpt = "--scale=";
  49. size_t scaleOptLen = scaleOpt.length();
  50. const string cascadeOpt = "--cascade=";
  51. size_t cascadeOptLen = cascadeOpt.length();
  52. const string nestedCascadeOpt = "--nested-cascade";
  53. size_t nestedCascadeOptLen = nestedCascadeOpt.length();
  54. const string tryFlipOpt = "--try-flip";
  55. size_t tryFlipOptLen = tryFlipOpt.length();
  56. String inputName;
  57. bool tryflip = false;
  58.  
  59. help();
  60.  
  61. CascadeClassifier cascade, nestedCascade;
  62. double scale = 1;
  63.  
  64. for( int i = 1; i < argc; i++ )
  65. {
  66. cout << "Processing " << i << " " << argv[i] << endl;
  67. if( cascadeOpt.compare( 0, cascadeOptLen, argv[i], cascadeOptLen ) == 0 )
  68. {
  69. cascadeName.assign( argv[i] + cascadeOptLen );
  70. cout << " from which we have cascadeName= " << cascadeName << endl;
  71. }
  72. else if( nestedCascadeOpt.compare( 0, nestedCascadeOptLen, argv[i], nestedCascadeOptLen ) == 0 )
  73. {
  74. if( argv[i][nestedCascadeOpt.length()] == '=' )
  75. nestedCascadeName.assign( argv[i] + nestedCascadeOpt.length() + 1 );
  76. if( !nestedCascade.load( nestedCascadeName ) )
  77. cerr << "WARNING: Could not load classifier cascade for nested objects" << endl;
  78. }
  79. else if( scaleOpt.compare( 0, scaleOptLen, argv[i], scaleOptLen ) == 0 )
  80. {
  81. if( !sscanf( argv[i] + scaleOpt.length(), "%lf", &scale ) || scale > 1 )
  82. scale = 1;
  83. cout << " from which we read scale = " << scale << endl;
  84. }
  85. else if( tryFlipOpt.compare( 0, tryFlipOptLen, argv[i], tryFlipOptLen ) == 0 )
  86. {
  87. tryflip = true;
  88. cout << " will try to flip image horizontally to detect assymetric objects\n";
  89. }
  90. else if( argv[i][0] == '-' )
  91. {
  92. cerr << "WARNING: Unknown option %s" << argv[i] << endl;
  93. }
  94. else
  95. inputName = argv[i];
  96. }
  97.  
  98. if( !cascade.load( cascadeName ) )
  99. {
  100. cerr << "ERROR: Could not load classifier cascade" << endl;
  101. help();
  102. return -1;
  103. }
  104.  
  105. cout << "old cascade: " << (cascade.isOldFormatCascade() ? "TRUE" : "FALSE") << endl;
  106.  
  107. if( inputName.empty() || (isdigit(inputName.c_str()[0]) && inputName.c_str()[1] == '\0') )
  108. {
  109. int c = inputName.empty() ? 0 : inputName.c_str()[0] - '0';
  110. if(!capture.open(c))
  111. cout << "Capture from camera #" << c << " didn't work" << endl;
  112. }
  113. else
  114. {
  115. if( inputName.empty() )
  116. inputName = "../data/lena.jpg";
  117. image = imread( inputName, 1 ).getUMat(ACCESS_READ);
  118. if( image.empty() )
  119. {
  120. if(!capture.open( inputName ))
  121. cout << "Could not read " << inputName << endl;
  122. }
  123. }
  124.  
  125. namedWindow( "result", 1 );
  126.  
  127. if( capture.isOpened() )
  128. {
  129. cout << "Video capturing has been started ..." << endl;
  130. for(;;)
  131. {
  132. capture >> frame;
  133. if( frame.empty() )
  134. break;
  135.  
  136. detectAndDraw( frame, canvas, cascade, nestedCascade, scale, tryflip );
  137.  
  138. if( waitKey( 10 ) >= 0 )
  139. break;
  140. }
  141. }
  142. else
  143. {
  144. cout << "Detecting face(s) in " << inputName << endl;
  145. if( !image.empty() )
  146. {
  147. detectAndDraw( image, canvas, cascade, nestedCascade, scale, tryflip );
  148. waitKey(0);
  149. }
  150. else if( !inputName.empty() )
  151. {
  152. /* assume it is a text file containing the
  153. list of the image filenames to be processed - one per line */
  154. FILE* f = fopen( inputName.c_str(), "rt" );
  155. if( f )
  156. {
  157. char buf[1000+1];
  158. while( fgets( buf, 1000, f ) )
  159. {
  160. int len = (int)strlen(buf), c;
  161. while( len > 0 && isspace(buf[len-1]) )
  162. len--;
  163. buf[len] = '\0';
  164. cout << "file " << buf << endl;
  165. image = imread( buf, 1 ).getUMat(ACCESS_READ);
  166. if( !image.empty() )
  167. {
  168. detectAndDraw( image, canvas, cascade, nestedCascade, scale, tryflip );
  169. c = waitKey(0);
  170. if( c == 27 || c == 'q' || c == 'Q' )
  171. break;
  172. }
  173. else
  174. {
  175. cerr << "Aw snap, couldn't read image " << buf << endl;
  176. }
  177. }
  178. fclose(f);
  179. }
  180. }
  181. }
  182.  
  183. return 0;
  184. }
  185.  
  186. void detectAndDraw( UMat& img, Mat& canvas, CascadeClassifier& cascade,
  187. CascadeClassifier& nestedCascade,
  188. double scale0, bool tryflip )
  189. {
  190. int i = 0;
  191. double t = 0, scale=1;
  192. vector<Rect> faces, faces2;
  193. const static Scalar colors[] =
  194. {
  195. Scalar(0,0,255),
  196. Scalar(0,128,255),
  197. Scalar(0,255,255),
  198. Scalar(0,255,0),
  199. Scalar(255,128,0),
  200. Scalar(255,255,0),
  201. Scalar(255,0,0),
  202. Scalar(255,0,255)
  203. };
  204. static UMat gray, smallImg;
  205.  
  206. t = (double)getTickCount();
  207.  
  208. resize( img, smallImg, Size(), scale0, scale0, INTER_LINEAR );
  209. cvtColor( smallImg, gray, COLOR_BGR2GRAY );
  210. equalizeHist( gray, gray );
  211.  
  212. cascade.detectMultiScale( gray, faces,
  213. 1.1, 3, 0
  214. //|CASCADE_FIND_BIGGEST_OBJECT
  215. //|CASCADE_DO_ROUGH_SEARCH
  216. |CASCADE_SCALE_IMAGE
  217. ,
  218. Size(30, 30) );
  219. if( tryflip )
  220. {
  221. flip(gray, gray, 1);
  222. cascade.detectMultiScale( gray, faces2,
  223. 1.1, 2, 0
  224. //|CASCADE_FIND_BIGGEST_OBJECT
  225. //|CASCADE_DO_ROUGH_SEARCH
  226. |CASCADE_SCALE_IMAGE
  227. ,
  228. Size(30, 30) );
  229. for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
  230. {
  231. faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
  232. }
  233. }
  234. t = (double)getTickCount() - t;
  235. smallImg.copyTo(canvas);
  236.  
  237. double fps = getTickFrequency()/t;
  238. static double avgfps = 0;
  239. static int nframes = 0;
  240. nframes++;
  241. double alpha = nframes > 50 ? 0.01 : 1./nframes;
  242. avgfps = avgfps*(1-alpha) + fps*alpha;
  243.  
  244. putText(canvas, format("OpenCL: %s, fps: %.1f", ocl::useOpenCL() ? "ON" : "OFF", avgfps), Point(50, 30),
  245. FONT_HERSHEY_SIMPLEX, 0.8, Scalar(0,255,0), 2);
  246.  
  247. for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
  248. {
  249. vector<Rect> nestedObjects;
  250. Point center;
  251. Scalar color = colors[i%8];
  252. int radius;
  253.  
  254. double aspect_ratio = (double)r->width/r->height;
  255. if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
  256. {
  257. center.x = cvRound((r->x + r->width*0.5)*scale);
  258. center.y = cvRound((r->y + r->height*0.5)*scale);
  259. radius = cvRound((r->width + r->height)*0.25*scale);
  260. circle( canvas, center, radius, color, 3, 8, 0 );
  261. }
  262. else
  263. rectangle( canvas, Point(cvRound(r->x*scale), cvRound(r->y*scale)),
  264. Point(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
  265. color, 3, 8, 0);
  266. if( nestedCascade.empty() )
  267. continue;
  268. UMat smallImgROI = gray(*r);
  269. nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
  270. 1.1, 2, 0
  271. //|CASCADE_FIND_BIGGEST_OBJECT
  272. //|CASCADE_DO_ROUGH_SEARCH
  273. //|CASCADE_DO_CANNY_PRUNING
  274. |CASCADE_SCALE_IMAGE
  275. ,
  276. Size(30, 30) );
  277. for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
  278. {
  279. center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
  280. center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
  281. radius = cvRound((nr->width + nr->height)*0.25*scale);
  282. circle( canvas, center, radius, color, 3, 8, 0 );
  283. }
  284. }
  285. imshow( "result", canvas );
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement