RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
human_detector.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright 2015 RAPP
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 ******************************************************************************/
17 
19 
24 {
25 }
26 
32 cv::Mat HumanDetector::loadImage(std::string file_name)
33 {
34  cv::Mat input_img;
35  // Must check if file exists
36  input_img = cv::imread(file_name);
37  return input_img;
38 }
39 
46 std::vector<cv::Rect> HumanDetector::detectHuman2D(const cv::Mat& input_img)
47 {
48  std::vector<cv::Rect> pedestrian, upperbody, final_humans;
49  cv::Mat grayscale_img;
50  if( input_img.empty() )
51  {
52  return final_humans;
53  }
54  cv::cvtColor(input_img, grayscale_img, CV_BGR2GRAY);
55  cv::equalizeHist(grayscale_img, grayscale_img);
56 
57  // Detect Pedestrians
58  //std::vector<cv::Rect> found, found_filtered;
59  cv::HOGDescriptor hog;
60  hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
61  hog.detectMultiScale(grayscale_img, pedestrian, 0, cv::Size(8, 8), cv::Size(32, 32), 1.05, 2);
62 
63  // Detect Human Upperbody
64  std::string haar_file_path = "/usr/share/opencv/haarcascades/haarcascade_upperbody.xml";
65  upperbody = detectHuman2D( grayscale_img, haar_file_path );
66 
67  // Identify unique humans
68  final_humans = identifyUniqueHumans( pedestrian, upperbody );
69 
70  return final_humans;
71 }
72 
80 std::vector<cv::Rect> HumanDetector::detectHuman2D(const cv::Mat& input_img,
81  const std::string& haar_path)
82 {
83  std::vector<cv::Rect> found, final_humans;
84 
85  // The Haar cascade classifier
86  cv::CascadeClassifier human_cascade; // body_cascade
87  // Create the classifier
88  human_cascade.load(haar_path);
89 
90  // Parameters of detectMultiscale Cascade Classifier
91  int groundThreshold = 2;
92  double scaleStep = 1.1;
93  cv::Size minimalObjectSize = cv::Size(100, 100); // minimal size of the detected object
94  cv::Size maximalObjectSize = cv::Size(800, 800); // maximal size of the detected object
95  // Detect humans
96  human_cascade.detectMultiScale(input_img, found, scaleStep, groundThreshold, 0 | cv::CASCADE_SCALE_IMAGE, minimalObjectSize);//, maximalObjectSize);
97 
98  //human_cascade.detectMultiScale(input_img, found, 1.1, 4);
99 
100  // If no humans were found make the algorithm less strict
101  //if(found.size() == 0)
102  //{
103  // /human_cascade.detectMultiScale(input_img, found, 1.1, 3);
104  //}
105 
106  // Check the humans again to eliminate false positives
107  for(unsigned int i = 0 ; i < found.size() ; i++)
108  {
109  cv::Rect tmp_rect = found[i];
110  tmp_rect.x -= 10;
111  tmp_rect.y -= 10;
112  tmp_rect.width += 20;
113  tmp_rect.height += 20;
114  if(tmp_rect.x < 0 || tmp_rect.y < 0 ||
115  (tmp_rect.x + tmp_rect.width) > input_img.size().width ||
116  (tmp_rect.y + tmp_rect.height) > input_img.size().height)
117  {
118  continue;
119  }
120  cv::Mat temp_map = input_img(found[i]);
121  std::vector<cv::Rect> tmp_found;
122  human_cascade.detectMultiScale(temp_map, tmp_found, 1.1, 3);
123  if(tmp_found.size() == 0)
124  {
125  continue;
126  }
127  final_humans.push_back(found[i]);
128  }
129  return final_humans;
130 }
131 
140  const std::vector<cv::Rect> pedestrianVector,
141  const std::vector<cv::Rect> upperbodyVector)
142 {
143  std::vector<cv::Rect> final_vector;
144 
145  final_vector = pedestrianVector;
146 
147  final_vector.insert( final_vector.end(), upperbodyVector.begin(),
148  upperbodyVector.end() );
149 
150  int size = final_vector.size();
151  for( unsigned int i = 0; i < size; i++ )
152  {
153  final_vector.push_back( final_vector[i] );
154  }
155  cv::groupRectangles( final_vector, 1, 0.2 ); //basic set
156 
157  return final_vector;
158 }
159 
166 std::vector<cv::Rect> HumanDetector::findHuman2D(std::string file_name)
167 {
168  cv::Mat input_img;
169  input_img = loadImage(file_name);
170  //std::vector<cv::Rect> final_humans;
171  //final_humans = detectHuman2D(input_img);
172  return detectHuman2D(input_img);
173 }
174 
std::vector< cv::Rect > findHuman2D(std::string file_name)
Finds humans in an image retrieved from a file URL.
std::vector< cv::Rect > identifyUniqueHumans(const std::vector< cv::Rect > pedestrianVector, const std::vector< cv::Rect > upperbodyVector)
Identify unique humans from two sets of humans.
std::vector< cv::Rect > detectHuman2D(const cv::Mat &input_img)
Detects humans from a cv::Mat.
cv::Mat loadImage(std::string file_name)
Loads an image from a file URL.
HumanDetector(void)
Default constructor.