7 static const std::vector<std::string>
labelMap = {
"background",
"aeroplane",
"bicycle",
"bird",
"boat",
"bottle",
"bus",
8 "car",
"cat",
"chair",
"cow",
"diningtable",
"dog",
"horse",
9 "motorbike",
"person",
"pottedplant",
"sheep",
"sofa",
"train",
"tvmonitor"};
11 static std::atomic<bool>
syncNN{
true};
13 int main(
int argc,
char** argv) {
15 using namespace std::chrono;
16 std::string nnPath(BLOB_PATH);
20 nnPath = std::string(argv[1]);
24 printf(
"Using blob at path: %s\n", nnPath.c_str());
40 xoutManip->setStreamName(
"right");
45 imageManip->initialConfig.setResize(300, 300);
50 monoLeft->setCamera(
"left");
59 spatialDetectionNetwork->setBlobPath(nnPath);
60 spatialDetectionNetwork->input.setBlocking(
false);
61 spatialDetectionNetwork->setBoundingBoxScaleFactor(0.5);
62 spatialDetectionNetwork->setDepthLowerThreshold(100);
63 spatialDetectionNetwork->setDepthUpperThreshold(5000);
66 monoLeft->out.link(stereo->left);
67 monoRight->
out.
link(stereo->right);
69 imageManip->out.link(spatialDetectionNetwork->input);
71 spatialDetectionNetwork->passthrough.link(xoutManip->input);
73 imageManip->out.link(xoutManip->input);
76 spatialDetectionNetwork->out.link(nnOut->
input);
78 stereo->rectifiedRight.link(imageManip->inputImage);
79 stereo->depth.link(spatialDetectionNetwork->inputDepth);
80 spatialDetectionNetwork->passthroughDepth.link(xoutDepth->
input);
87 auto detectionNNQueue = device.
getOutputQueue(
"detections", 4,
false);
90 auto startTime = steady_clock::now();
93 auto color = cv::Scalar(255, 255, 255);
101 auto currentTime = steady_clock::now();
102 auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
103 if(elapsed > seconds(1)) {
104 fps = counter / elapsed.count();
106 startTime = currentTime;
109 cv::Mat rectifiedRight = inRectified->
getCvFrame();
111 cv::Mat depthFrame = inDepth->getFrame();
112 cv::Mat depthFrameColor;
114 cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
115 cv::equalizeHist(depthFrameColor, depthFrameColor);
116 cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
118 auto detections = inDet->detections;
120 for(
auto& detection : detections) {
121 auto roiData = detection.boundingBoxMapping;
122 auto roi = roiData.roi;
123 roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
124 auto topLeft = roi.topLeft();
125 auto bottomRight = roi.bottomRight();
126 auto xmin = (int)topLeft.x;
127 auto ymin = (
int)topLeft.y;
128 auto xmax = (int)bottomRight.x;
129 auto ymax = (
int)bottomRight.y;
130 cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, cv::FONT_HERSHEY_SIMPLEX);
132 int x1 = detection.xmin * rectifiedRight.cols;
133 int y1 = detection.ymin * rectifiedRight.rows;
134 int x2 = detection.xmax * rectifiedRight.cols;
135 int y2 = detection.ymax * rectifiedRight.rows;
137 uint32_t labelIndex = detection.label;
138 std::string labelStr =
to_string(labelIndex);
142 cv::putText(rectifiedRight, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
143 std::stringstream confStr;
144 confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
145 cv::putText(rectifiedRight, confStr.str(), cv::Point(x1 + 10, y1 + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
147 std::stringstream depthX;
148 depthX <<
"X: " << (int)detection.spatialCoordinates.x <<
" mm";
149 cv::putText(rectifiedRight, depthX.str(), cv::Point(x1 + 10, y1 + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
150 std::stringstream depthY;
151 depthY <<
"Y: " << (int)detection.spatialCoordinates.y <<
" mm";
152 cv::putText(rectifiedRight, depthY.str(), cv::Point(x1 + 10, y1 + 65), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
153 std::stringstream depthZ;
154 depthZ <<
"Z: " << (int)detection.spatialCoordinates.z <<
" mm";
155 cv::putText(rectifiedRight, depthZ.str(), cv::Point(x1 + 10, y1 + 80), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
157 cv::rectangle(rectifiedRight, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
160 std::stringstream fpsStr;
161 fpsStr << std::fixed << std::setprecision(2) <<
fps;
162 cv::putText(rectifiedRight, fpsStr.str(), cv::Point(2, rectifiedRight.rows - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
164 cv::imshow(
"depth", depthFrameColor);
165 cv::imshow(
"rectified right", rectifiedRight);
167 int key = cv::waitKey(1);
168 if(key ==
'q' || key ==
'Q') {