Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/objects/engagement_objects.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 // @author Brett Ponsler (bponsler (at) wpi (dot) edu) 00035 #include "engagement_objects.h" 00036 #include "../lib/engagement_params.h" 00037 #include "../recognizers/directed_gaze/directed_gaze_recognizer.h" 00038 00039 #include <vector> 00040 #include <string> 00041 #include <stdio.h> 00042 #include <ros/ros.h> 00043 00044 namespace objects 00045 { 00046 00047 bool areObjects(std::vector<bml_msgs::Entity> vec) 00048 { 00049 if (vec.size() > 0) 00050 { 00051 // Make sure each object is the object type 00052 for (unsigned c = 0; c < vec.size(); c++) 00053 { 00054 if (vec.at(c).type != bml_msgs::Entity::OBJECT || 00055 vec.at(c).type != bml_msgs::Entity::UNKNOWN) 00056 return false; 00057 } 00058 00059 return true; 00060 } 00061 00062 return false; 00063 } 00064 00065 bool isFace(std::vector<bml_msgs::Entity> vec, std::string id) 00066 { 00067 // Only look at one actor's face 00068 if (vec.size() == 1) 00069 return (vec.at(0).type == bml_msgs::Entity::FACE && 00070 vec.at(0).id == id); 00071 00072 return false; 00073 } 00074 00075 bml_msgs::Entity objectToEntity(std::string object) 00076 { 00077 // Create the entity message 00078 bml_msgs::Entity entity = bml_msgs::Entity(); 00079 00080 // Set the type to an object, and the id to the object's id 00081 entity.type = bml_msgs::Entity::OBJECT; 00082 entity.id = std::string(object); 00083 00084 return entity; 00085 } 00086 00087 std::vector<bml_msgs::Entity> getEntitiesFromObjects(std::vector<std::string> vec) 00088 { 00089 // The vector of entities 00090 std::vector<bml_msgs::Entity> entities = std::vector<bml_msgs::Entity>(); 00091 00092 // Make sure objects exist 00093 if (vec.size() > 0) 00094 { 00095 // Traverse the vector, and add al the new entity message to the objects 00096 for (unsigned c = 0; c < vec.size(); c++) 00097 entities.push_back(objects::objectToEntity(vec.at(c))); 00098 } 00099 00100 return entities; 00101 } 00102 00109 std::vector<std::string> getObjectsFromEntities(std::vector<bml_msgs::Entity> vec) 00110 { 00111 // The vector of objects 00112 std::vector<std::string> objects = std::vector<std::string>(); 00113 00114 // Make sure entity messages exist 00115 if (vec.size() > 0) 00116 { 00117 // Traverse the vector, and add the entity message to the objects 00118 for (unsigned c = 0; c < vec.size(); c++) 00119 { 00120 // Only add the object's id if it is an object type 00121 if (vec.at(c).type == bml_msgs::Entity::OBJECT || 00122 vec.at(c).type == bml_msgs::Entity::UNKNOWN) 00123 { 00124 objects.push_back(vec.at(c).id); 00125 } 00126 } 00127 } 00128 00129 return objects; 00130 } 00131 00132 std::string toString(std::vector<std::string> vec) 00133 { 00134 // First place all the objects into the message 00135 // The format for this string is as follows: 00136 // <object> ... 00137 std::string objects = ""; 00138 unsigned i; 00139 00140 // Loop through all the objects in the message 00141 for (i = 0; i < vec.size(); i++) 00142 { 00143 // Only add a space if there was an object 00144 // in the string before it 00145 if (i > 0) 00146 objects += " "; 00147 00148 // Add the next object with a space before it 00149 objects += vec.at(i); 00150 00151 } 00152 00153 // Return a copy of the string we just created 00154 return objects; 00155 } 00156 00157 bool same(std::vector<std::string> vec1, std::vector<std::string> vec2) 00158 { 00159 // Make sure each vector contains objects 00160 // If the sizes of the vectors are not the same, they do not 00161 // contain the same set of objects. 00162 if (vec1.size() > 0 && vec2.size() > 0 && vec1.size() == vec2.size()) 00163 { 00164 // Determine if they contain the same set of objects 00165 return objects::contains(vec1, vec2); 00166 } 00167 00168 return false; 00169 } 00170 00171 bool contains(std::vector<std::string> vec1, std::vector<std::string> vec2) 00172 { 00173 // Make sure each vector contains objects 00174 if (vec1.size() > 0 && vec2.size() > 0) 00175 { 00176 // Loop through the second vector 00177 for (unsigned i = 0; i < vec2.size(); i++) 00178 { 00179 // Determine if the object was found 00180 bool found = false; 00181 00182 // Loop through the first vector 00183 for (unsigned j = 0; j < vec1.size(); j++) 00184 { 00185 // If the objects are equal 00186 if (vec2.at(i) == vec1.at(j)) 00187 { 00188 found = true; 00189 break; 00190 } 00191 } 00192 00193 // Check to make sure we found the object 00194 if (!found) 00195 { 00196 // The object in vec2 was not in vec1 00197 return false; 00198 } 00199 } 00200 00201 // If we've made it here then vector one contains all of 00202 // vector two's objects 00203 return true; 00204 } 00205 00206 return false; 00207 } 00208 00209 bool intersect(std::vector<std::string> vec1, std::vector<std::string> vec2) 00210 { 00211 // Make sure each vector contains objects 00212 if (vec1.size() > 0 && vec2.size() > 0) 00213 { 00214 // Loop through the first vector 00215 for (unsigned i = 0; i < vec1.size(); i++) 00216 { 00217 // Determine if the object was found 00218 bool found = false; 00219 00220 // Loop through the second vector 00221 for (unsigned j = 0; j < vec2.size(); j++) 00222 { 00223 if (vec1.at(i) == vec2.at(j)) 00224 { 00225 found = true; 00226 break; 00227 } 00228 } 00229 00230 // Check to make sure we found the object 00231 if (found) 00232 { 00233 // We have an intersection 00234 return true; 00235 } 00236 } 00237 } 00238 00239 return false; 00240 } 00241 00242 std::vector<std::string> addObjects(std::vector<std::string> vec1, 00243 std::vector<std::string> vec2) 00244 { 00245 // Grab the size of the first vector prior to adding any elements 00246 unsigned size = vec1.size(); 00247 00248 // Loop through the second vector 00249 for (unsigned i = 0; i < vec2.size(); i++) 00250 { 00251 // Keep track of whether the object was found in vec1 or not 00252 bool found = false; 00253 00254 // Loop through the first vector 00255 for (unsigned j = 0; j < size; j++) 00256 { 00257 // Compare the objects 00258 if (vec1.at(j) == vec2.at(i)) 00259 { 00260 found = true; 00261 break; 00262 } 00263 } 00264 00265 // If the object was not found then add it 00266 if (!found) 00267 vec1.push_back(vec2.at(i)); 00268 } 00269 00270 return vec1; 00271 } 00272 00273 std::vector<std::string> removeObjects(std::vector<std::string> vec1, 00274 std::vector<std::string> vec2) 00275 { 00276 // Loop through the second vector 00277 for (unsigned i = 0; i < vec2.size(); i++) 00278 { 00279 // Keep track of whether it was found or not 00280 bool found = false; 00281 00282 // Loop through the first vector 00283 for (unsigned j = 0; j < vec1.size(); j++) 00284 { 00285 // Compare the objects 00286 if (vec1.at(j) == vec2.at(i)) 00287 { 00288 // Remove it from the first vector 00289 vec1.erase(vec1.begin() + j); 00290 00291 // Found the object 00292 found = true; 00293 break; 00294 } 00295 } 00296 } 00297 00298 return vec1; 00299 } 00300 00301 std::vector<std::string> highestProbability(std::vector<std::string> objects, 00302 std::vector<float> probs) 00303 { 00304 std::vector<std::string> indices = std::vector<std::string>(); 00305 float maxProb = probs.at(0); 00306 int maxIndex = 0; 00307 00308 // Find the highest probability that is given 00309 for (unsigned c = 1; c < probs.size(); c++) { 00310 // Determine if this object has the highest probability 00311 if (probs.at(c) > maxProb) 00312 { 00313 maxProb = probs.at(c); 00314 maxIndex = c; 00315 } 00316 } 00317 00318 indices.push_back(objects.at(maxIndex)); 00319 00320 return indices; 00321 } 00322 00323 void keepHighest(engagement_msgs::Performance &msg) 00324 { 00325 float maxProb = msg.prob.at(0); 00326 bml_msgs::Entity maxEntity = msg.objects.at(0); 00327 00328 // Find the highest probability that is given 00329 for (unsigned c = 1; c < msg.prob.size(); c++) { 00330 // Determine if this object has the highest probability 00331 if (msg.prob.at(c) > maxProb) 00332 { 00333 maxProb = msg.prob.at(c); 00334 maxEntity = msg.objects.at(c); 00335 } 00336 } 00337 00338 // Clear the objects, and probabilities 00339 msg.objects.clear(); 00340 msg.prob.clear(); 00341 00342 // Add the maximum entity, and maximum probability 00343 msg.objects.push_back(maxEntity); 00344 msg.prob.push_back(maxProb); 00345 } 00346 00347 bool pertains(EventSink *sink, std::vector<std::string> objects) 00348 { 00349 DirectedGazeRecognizer *rec = (DirectedGazeRecognizer*)sink; 00350 00351 if (rec) 00352 { 00353 bool found = false; 00354 // Determine if the object for this recognizer exists in the vector 00355 for (unsigned c = 0; c < objects.size(); c++) { 00356 // Determine if this is the correct object 00357 if (objects.at(c) == rec->getObject()) 00358 { 00359 found = true; 00360 break; 00361 } 00362 } 00363 00364 return found; 00365 } 00366 00367 return false; 00368 } 00369 00370 } // objects namespace |