5 #include <opencv2/dnn.hpp> 7 #include "../cv-helpers.hpp" 11 const float WHRatio = inWidth / (float)inHeight;
15 "aeroplane",
"bicycle",
"bird",
"boat",
16 "bottle",
"bus",
"car",
"cat",
"chair",
17 "cow",
"diningtable",
"dog",
"horse",
18 "motorbike",
"person",
"pottedplant",
19 "sheep",
"sofa",
"train",
"tvmonitor"};
21 int main(
int argc,
char** argv)
try 24 using namespace cv::dnn;
27 Net
net = readNetFromCaffe(
"MobileNetSSD_deploy.prototxt",
28 "MobileNetSSD_deploy.caffemodel");
53 const auto window_name =
"Display Image";
54 namedWindow(window_name, WINDOW_AUTOSIZE);
56 while (getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
68 static int last_frame_number = 0;
69 if (
color_frame.get_frame_number() == last_frame_number)
continue;
70 last_frame_number =
static_cast<int>(
color_frame.get_frame_number());
77 Size(inWidth, inHeight),
meanVal,
false);
78 net.setInput(inputBlob,
"data");
79 Mat detection = net.forward(
"detection_out");
81 Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<
float>());
84 color_mat = color_mat(
crop);
85 depth_mat = depth_mat(
crop);
87 float confidenceThreshold = 0.8f;
88 for(
int i = 0;
i < detectionMat.rows;
i++)
92 if(confidence > confidenceThreshold)
94 size_t objectClass = (
size_t)(detectionMat.at<
float>(i, 1));
96 int xLeftBottom =
static_cast<int>(detectionMat.at<
float>(
i, 3) * color_mat.cols);
97 int yLeftBottom =
static_cast<int>(detectionMat.at<
float>(
i, 4) * color_mat.rows);
98 int xRightTop =
static_cast<int>(detectionMat.at<
float>(
i, 5) * color_mat.cols);
99 int yRightTop =
static_cast<int>(detectionMat.at<
float>(
i, 6) * color_mat.rows);
101 Rect
object((
int)xLeftBottom, (
int)yLeftBottom,
102 (
int)(xRightTop - xLeftBottom),
103 (
int)(yRightTop - yLeftBottom));
105 object =
object & Rect(0, 0, depth_mat.cols, depth_mat.rows);
111 Scalar
m = mean(depth_mat(
object));
113 std::ostringstream ss;
115 ss << std::setprecision(2) << m[0] <<
" meters away";
118 rectangle(color_mat,
object, Scalar(0, 255, 0));
120 Size labelSize = getTextSize(ss.str(), FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
122 auto center = (
object.br() +
object.tl())*0.5;
123 center.x = center.x - labelSize.width / 2;
125 rectangle(color_mat, Rect(
Point(center.x, center.y - labelSize.height),
126 Size(labelSize.width, labelSize.height + baseLine)),
127 Scalar(255, 255, 255), FILLED);
128 putText(color_mat, ss.str(), center,
129 FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0,0,0));
133 imshow(window_name, color_mat);
134 if (waitKey(1) >= 0)
break;
144 catch (
const std::exception& e)
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
int main(int argc, char **argv)
const float inScaleFactor
std::array< point3d, 4 > object
::std_msgs::String_< std::allocator< void > > String
static cv::Mat depth_frame_to_meters(const rs2::depth_frame &f)
const std::string & get_failed_args() const
static cv::Mat frame_to_mat(const rs2::frame &f)
const char * classNames[]
const std::string & get_failed_function() const
::geometry_msgs::Point_< std::allocator< void > > Point