5 #include <opencv2/opencv.hpp>
7 constexpr
auto FPS = 15;
9 static const std::vector<std::string>
labelMap = {
10 "person",
"bicycle",
"car",
"motorbike",
"aeroplane",
"bus",
"train",
"truck",
"boat",
11 "traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
12 "sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
13 "tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
14 "skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
15 "bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
16 "donut",
"cake",
"chair",
"sofa",
"pottedplant",
"bed",
"diningtable",
"toilet",
"tvmonitor",
17 "laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
18 "refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush"};
20 static std::atomic<bool>
syncNN{
true};
22 int main(
int argc,
char** argv) {
35 xoutRgb->setStreamName(
"rgb");
39 camRgb->setPreviewSize(416, 416);
41 camRgb->setInterleaved(
false);
50 imageAlign->setOutputSize(640, 400);
52 spatialDetectionNetwork->setBlobPath(BLOB_PATH);
53 spatialDetectionNetwork->setConfidenceThreshold(0.5f);
54 spatialDetectionNetwork->input.setBlocking(
false);
55 spatialDetectionNetwork->setBoundingBoxScaleFactor(0.5);
56 spatialDetectionNetwork->setDepthLowerThreshold(100);
57 spatialDetectionNetwork->setDepthUpperThreshold(5000);
59 spatialDetectionNetwork->setNumClasses(80);
60 spatialDetectionNetwork->setCoordinateSize(4);
61 spatialDetectionNetwork->setAnchors({10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319});
62 spatialDetectionNetwork->setAnchorMasks({{
"side26", {1, 2, 3}}, {
"side13", {3, 4, 5}}});
63 spatialDetectionNetwork->setIouThreshold(0.5f);
65 camTof->raw.link(tof->input);
66 tof->depth.link(imageAlign->input);
68 camRgb->preview.link(spatialDetectionNetwork->input);
70 spatialDetectionNetwork->passthrough.link(xoutRgb->
input);
75 spatialDetectionNetwork->out.link(xoutNN->
input);
77 camRgb->
isp.link(imageAlign->inputAlignTo);
78 imageAlign->outputAligned.link(spatialDetectionNetwork->inputDepth);
79 spatialDetectionNetwork->passthroughDepth.link(xoutDepth->
input);
84 auto detectionNNQueue = device.
getOutputQueue(
"detections", 4,
false);
87 auto startTime = std::chrono::steady_clock::now();
90 auto color = cv::Scalar(255, 255, 255);
98 cv::Mat depthFrame = depth->getFrame();
100 cv::Mat depthFrameColor;
101 cv::normalize(depthFrame, depthFrameColor, 255, 0, cv::NORM_INF, CV_8UC1);
102 cv::equalizeHist(depthFrameColor, depthFrameColor);
103 cv::applyColorMap(depthFrameColor, depthFrameColor, cv::COLORMAP_HOT);
106 auto currentTime = std::chrono::steady_clock::now();
107 auto elapsed = std::chrono::duration_cast<std::chrono::duration<float>>(currentTime - startTime);
108 if(elapsed > std::chrono::seconds(1)) {
109 fps = counter / elapsed.count();
111 startTime = currentTime;
114 auto detections = inDet->detections;
116 for(
const auto& detection : detections) {
117 auto roiData = detection.boundingBoxMapping;
118 auto roi = roiData.roi;
119 roi = roi.denormalize(depthFrameColor.cols, depthFrameColor.rows);
120 auto topLeft = roi.topLeft();
121 auto bottomRight = roi.bottomRight();
122 auto xmin =
static_cast<int>(topLeft.x);
123 auto ymin =
static_cast<int>(topLeft.y);
124 auto xmax =
static_cast<int>(bottomRight.x);
125 auto ymax =
static_cast<int>(bottomRight.y);
126 cv::rectangle(depthFrameColor, cv::Rect(cv::Point(xmin, ymin), cv::Point(xmax, ymax)), color, 1);
128 int x1 = detection.xmin * frame.cols;
129 int y1 = detection.ymin * frame.rows;
130 int x2 = detection.xmax * frame.cols;
131 int y2 = detection.ymax * frame.rows;
133 uint32_t labelIndex = detection.label;
138 cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
139 std::stringstream confStr;
140 confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
141 cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 35), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
143 std::stringstream depthX;
144 depthX <<
"X: " <<
static_cast<int>(detection.spatialCoordinates.x) <<
" mm";
145 cv::putText(frame, depthX.str(), cv::Point(x1 + 10, y1 + 50), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
146 std::stringstream depthY;
147 depthY <<
"Y: " <<
static_cast<int>(detection.spatialCoordinates.y) <<
" mm";
148 cv::putText(frame, depthY.str(), cv::Point(x1 + 10, y1 + 65), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
149 std::stringstream depthZ;
150 depthZ <<
"Z: " <<
static_cast<int>(detection.spatialCoordinates.z) <<
" mm";
151 cv::putText(frame, depthZ.str(), cv::Point(x1 + 10, y1 + 80), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
153 cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
156 std::stringstream fpsStr;
157 fpsStr << std::fixed << std::setprecision(2) <<
fps;
158 cv::putText(frame, fpsStr.str(), cv::Point(2, frame.rows - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
160 cv::imshow(
"depth", depthFrameColor);
161 cv::imshow(
"rgb", frame);
163 if(cv::waitKey(1) ==
'q') {