7 #include <opencv2/core/utils/filesystem.hpp> 8 namespace fs = cv::utils::fs;
14 #ifdef BUILD_SHARED_LIBS 34 std::shared_ptr< openvino_helpers::object_detection >
detector;
50 catch(
const std::exception &
e )
53 LOG(
WARNING) <<
"Failed to load labels: " << e.what();
66 std::vector< detector_and_labels > & detectors,
67 openvino::Core & engine,
71 std::vector< std::string > xmls;
72 fs::glob_relative(
".",
"*.xml", xmls );
73 for(
auto path_to_xml : xmls )
78 detector->load_into( engine, device_name );
81 LOG(
INFO) <<
" ... press '" << char(
'0' + detectors.size() ) <<
"' to switch to it";
83 catch(
const std::exception &
e )
91 LOG(ERROR) <<
"Failed to load model: " << e.what();
109 cv::Mat
const &
image,
110 std::vector< openvino_helpers::object_detection::Result >
const & results,
111 std::vector< std::string > & labels,
118 for(
auto const &
result : results )
123 rect = rect & cv::Rect( 0, 0, image.cols, image.rows );
129 if(
result.label < labels.size() )
130 label = labels[
result.label];
131 object_ptr = std::make_shared< openvino_helpers::detected_object >( next_id++, label, rect );
136 object_ptr->move( rect );
138 objects.push_back( object_ptr );
152 cv::Scalar
const green( 0, 255, 0 );
153 cv::Scalar
const white( 255, 255, 255 );
155 for(
auto &&
object : objects )
157 auto r =
object->get_location();
158 cv::rectangle( image,
r, green );
161 auto center_x =
r.x +
r.width / 2;
162 auto center_y =
r.y +
r.height / 2;
166 std::ostringstream ss;
167 ss <<
object->get_label() <<
" ";
168 ss << std::setprecision( 2 ) <<
d;
169 ss <<
" meters away";
170 cv::putText( image, ss.str(),
cv::Point(
r.x + 5,
r.y +
r.height - 5 ), cv::FONT_HERSHEY_SIMPLEX, 0.4,
white );
182 size_t current_detector,
183 high_resolution_clock::time_point switch_time
187 if( ms_since_switch > 1000 )
188 ms_since_switch = 1000;
189 double alpha = ( 1000 - ms_since_switch ) / 1000.;
191 auto size = cv::getTextSize( str, cv::FONT_HERSHEY_SIMPLEX, 3, 1,
nullptr );
192 cv::Point center{ image.cols / 2, image.rows / 2 };
193 cv::Rect
r{ center.x -
size.width, center.y -
size.height,
size.width * 2,
size.height * 2 };
195 cv::Mat overlay( roi.size(), CV_8UC3, cv::Scalar( 32, 32, 32 ) );
196 cv::putText( overlay, str,
cv::Point{
r.width / 2 -
size.width / 2,
r.height / 2 +
size.height / 2 }, cv::FONT_HERSHEY_SIMPLEX, 3, cv::Scalar{ 255, 255, 255 } );
197 cv::addWeighted( overlay, alpha, roi, 1 - alpha, 0, roi );
201 int main(
int argc,
char * argv[])
try 217 openvino::Core engine;
219 engine.SetLogCallback( error_listener );
223 engine.AddExtension(std::make_shared< openvino::Extensions::Cpu::CpuExtensions >(), device_name);
226 std::vector< detector_and_labels > detectors;
228 if( detectors.empty() )
230 LOG(ERROR) <<
"No detectors available in: " << fs::getcwd();
234 size_t current_detector = 0;
235 for(
size_t i = 1;
i < detectors.size(); ++
i )
237 if( detectors[
i]->pathToModel ==
"mobilenet-ssd.xml" )
239 current_detector =
i;
243 auto p_detector = detectors[current_detector].detector;
245 auto p_labels = &detectors[current_detector].labels;
247 const auto window_name =
"OpenVINO DNN sample";
248 cv::namedWindow( window_name, cv::WINDOW_AUTOSIZE );
253 uint64 last_frame_number = 0;
256 while( cv::getWindowProperty( window_name, cv::WND_PROP_AUTOSIZE ) >= 0 )
269 if(
color_frame.get_frame_number() == last_frame_number )
271 last_frame_number =
color_frame.get_frame_number();
276 if( ! p_detector->_request )
278 p_detector->enqueue(
image );
279 p_detector->submit_request();
286 auto const results = p_detector->fetch_results();
289 p_detector->enqueue(
image );
290 p_detector->submit_request();
301 imshow( window_name,
image );
304 const int key = cv::waitKey( 1 );
307 if( key >=
'1' && key <
'1' + detectors.size() )
309 size_t detector_index = key -
'1';
310 if( detector_index != current_detector )
312 current_detector = detector_index;
313 p_detector = detectors[current_detector].detector;
314 p_labels = &detectors[current_detector].labels;
329 catch (
const std::exception& e)
331 LOG(ERROR) <<
"Unknown exception caught: " << e.what();
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
static const ImVec4 white
static Logger * reconfigureLogger(Logger *logger, const Configurations &configurations)
Reconfigures specified logger with new configurations.
int main(int argc, char *argv[])
std::string remove_ext(const std::string &filepath)
std::list< detected_object::ptr > detected_objects
void set(Level level, ConfigurationType configurationType, const std::string &value)
Sets value of configuration for specified level.
LOG(INFO)<< "Log message to default logger"
GLsizei const GLchar *const * string
GLenum GLenum GLsizei void * image
const std::string & get_failed_args() const
Determines format of logging corresponding level and logger.
GLfloat GLfloat GLfloat alpha
#define INITIALIZE_EASYLOGGINGPP
frameset process(frameset frames)
void draw_detector_overlay(cv::Mat &image, size_t current_detector, high_resolution_clock::time_point switch_time)
void load_detectors_into(std::vector< detector_and_labels > &detectors, openvino::Core &engine, std::string const &device_name)
openvino_helpers::object_detection * operator->()
void draw_objects(cv::Mat &image, rs2::depth_frame depth_frame, openvino_helpers::detected_objects const &objects)
std::vector< std::string > read_labels(std::string const &filename)
void log_to_console(rs2_log_severity min_severity)
Thread-safe Configuration repository.
float get_distance(int x, int y) const
GLuint GLsizei const GLchar * label
detected_object::ptr find_object(cv::Rect rect, detected_objects const &objects)
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
detector_and_labels(std::string const &path_to_xml)
Generic level that represents all the levels. Useful when setting global configuration for all levels...
std::vector< std::string > labels
void detect_objects(cv::Mat const &image, std::vector< openvino_helpers::object_detection::Result > const &results, std::vector< std::string > &labels, size_t &next_id, openvino_helpers::detected_objects &objects)
static cv::Mat frame_to_mat(const rs2::frame &f)
const std::string & get_failed_function() const
std::shared_ptr< openvino_helpers::object_detection > detector
::geometry_msgs::Point_< std::allocator< void > > Point