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 xoutRgb->setStreamName(
"rgb");
45 camRgb->setPreviewSize(300, 300);
47 camRgb->setInterleaved(
false);
51 monoLeft->setCamera(
"left");
59 stereo->setOutputSize(monoLeft->getResolutionWidth(), monoLeft->getResolutionHeight());
61 spatialDetectionNetwork->setBlobPath(nnPath);
62 spatialDetectionNetwork->setConfidenceThreshold(0.5f);
63 spatialDetectionNetwork->input.setBlocking(
false);
64 spatialDetectionNetwork->setBoundingBoxScaleFactor(0.5);
65 spatialDetectionNetwork->setDepthLowerThreshold(100);
66 spatialDetectionNetwork->setDepthUpperThreshold(5000);
69 monoLeft->out.link(stereo->left);
70 monoRight->
out.
link(stereo->right);
72 camRgb->preview.link(spatialDetectionNetwork->input);
74 spatialDetectionNetwork->passthrough.link(xoutRgb->input);
79 spatialDetectionNetwork->out.link(xoutNN->
input);
81 stereo->depth.link(spatialDetectionNetwork->inputDepth);
82 spatialDetectionNetwork->passthroughDepth.link(xoutDepth->
input);
88 auto detectionNNQueue = device.
getOutputQueue(
"detections", 4,
false);
91 auto startTime = steady_clock::now();
94 auto color = cv::Scalar(255, 255, 255);
102 auto currentTime = steady_clock::now();
103 auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
104 if(elapsed > seconds(1)) {
105 fps = counter / elapsed.count();
107 startTime = currentTime;
111 cv::Mat depthFrame = depth->getFrame();
113 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(
const 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 * frame.cols;
133 int y1 = detection.ymin * frame.rows;
134 int x2 = detection.xmax * frame.cols;
135 int y2 = detection.ymax * frame.rows;
137 uint32_t labelIndex = detection.label;
138 std::string labelStr =
to_string(labelIndex);
142 cv::putText(frame, 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(frame, 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(frame, 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(frame, 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(frame, depthZ.str(), cv::Point(x1 + 10, y1 + 80), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
157 cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
160 std::stringstream fpsStr;
161 fpsStr <<
"NN fps: " << std::fixed << std::setprecision(2) <<
fps;
162 cv::putText(frame, fpsStr.str(), cv::Point(2, inPreview->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
164 cv::imshow(
"depth", depthFrameColor);
165 cv::imshow(
"preview", frame);
167 int key = cv::waitKey(1);
168 if(key ==
'q' || key ==
'Q') {