peopledetect.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #include <opencv2/objdetect.hpp>
  5. #include <opencv2/highgui.hpp>
  6. #include <opencv2/imgproc.hpp>
  7. #include <opencv2/videoio.hpp>
  8. #include <iostream>
  9. #include <iomanip>
  10. using namespace cv;
  11. using namespace std;
  12. class Detector
  13. {
  14. enum Mode { Default, Daimler } m;
  15. HOGDescriptor hog, hog_d;
  16. public:
  17. Detector() : m(Default), hog(), hog_d(Size(48, 96), Size(16, 16), Size(8, 8), Size(8, 8), 9)
  18. {
  19. hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
  20. hog_d.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());
  21. }
  22. void toggleMode() { m = (m == Default ? Daimler : Default); }
  23. string modeName() const { return (m == Default ? "Default" : "Daimler"); }
  24. vector<Rect> detect(InputArray img)
  25. {
  26. // Run the detector with default parameters. to get a higher hit-rate
  27. // (and more false alarms, respectively), decrease the hitThreshold and
  28. // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
  29. vector<Rect> found;
  30. if (m == Default)
  31. hog.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, false);
  32. else if (m == Daimler)
  33. hog_d.detectMultiScale(img, found, 0, Size(8,8), Size(), 1.05, 2, true);
  34. return found;
  35. }
  36. void adjustRect(Rect & r) const
  37. {
  38. // The HOG detector returns slightly larger rectangles than the real objects,
  39. // so we slightly shrink the rectangles to get a nicer output.
  40. r.x += cvRound(r.width*0.1);
  41. r.width = cvRound(r.width*0.8);
  42. r.y += cvRound(r.height*0.07);
  43. r.height = cvRound(r.height*0.8);
  44. }
  45. };
  46. static const string keys = "{ help h | | print help message }"
  47. "{ camera c | 0 | capture video from camera (device index starting from 0) }"
  48. "{ video v | | use video as input }";
  49. int main(int argc, char** argv)
  50. {
  51. CommandLineParser parser(argc, argv, keys);
  52. parser.about("This sample demonstrates the use of the HoG descriptor.");
  53. if (parser.has("help"))
  54. {
  55. parser.printMessage();
  56. return 0;
  57. }
  58. int camera = parser.get<int>("camera");
  59. string file = parser.get<string>("video");
  60. if (!parser.check())
  61. {
  62. parser.printErrors();
  63. return 1;
  64. }
  65. VideoCapture cap;
  66. if (file.empty())
  67. cap.open(camera);
  68. else
  69. {
  70. file = samples::findFileOrKeep(file);
  71. cap.open(file);
  72. }
  73. if (!cap.isOpened())
  74. {
  75. cout << "Can not open video stream: '" << (file.empty() ? "<camera>" : file) << "'" << endl;
  76. return 2;
  77. }
  78. cout << "Press 'q' or <ESC> to quit." << endl;
  79. cout << "Press <space> to toggle between Default and Daimler detector" << endl;
  80. Detector detector;
  81. Mat frame;
  82. for (;;)
  83. {
  84. cap >> frame;
  85. if (frame.empty())
  86. {
  87. cout << "Finished reading: empty frame" << endl;
  88. break;
  89. }
  90. int64 t = getTickCount();
  91. vector<Rect> found = detector.detect(frame);
  92. t = getTickCount() - t;
  93. // show the window
  94. {
  95. ostringstream buf;
  96. buf << "Mode: " << detector.modeName() << " ||| "
  97. << "FPS: " << fixed << setprecision(1) << (getTickFrequency() / (double)t);
  98. putText(frame, buf.str(), Point(10, 30), FONT_HERSHEY_PLAIN, 2.0, Scalar(0, 0, 255), 2, LINE_AA);
  99. }
  100. for (vector<Rect>::iterator i = found.begin(); i != found.end(); ++i)
  101. {
  102. Rect &r = *i;
  103. detector.adjustRect(r);
  104. rectangle(frame, r.tl(), r.br(), cv::Scalar(0, 255, 0), 2);
  105. }
  106. imshow("People detector", frame);
  107. // interact with user
  108. const char key = (char)waitKey(1);
  109. if (key == 27 || key == 'q') // ESC
  110. {
  111. cout << "Exit requested" << endl;
  112. break;
  113. }
  114. else if (key == ' ')
  115. {
  116. detector.toggleMode();
  117. }
  118. }
  119. return 0;
  120. }