12 static const std::vector<std::string>
labelMap = {
13 "person",
"bicycle",
"car",
"motorbike",
"aeroplane",
"bus",
"train",
"truck",
"boat",
14 "traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
15 "sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
16 "tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
17 "skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
18 "bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
19 "donut",
"cake",
"chair",
"sofa",
"pottedplant",
"bed",
"diningtable",
"toilet",
"tvmonitor",
20 "laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
21 "refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush"};
23 static std::atomic<bool>
syncNN{
true};
25 int main(
int argc,
char** argv) {
27 using namespace std::chrono;
28 std::string nnPath(BLOB_PATH);
32 nnPath = std::string(argv[1]);
36 printf(
"Using blob at path: %s\n", nnPath.c_str());
47 xoutRgb->setStreamName(
"rgb");
51 camRgb->setPreviewSize(416, 416);
53 camRgb->setInterleaved(
false);
58 detectionNetwork->setConfidenceThreshold(0.5f);
59 detectionNetwork->setNumClasses(80);
60 detectionNetwork->setCoordinateSize(4);
61 detectionNetwork->setAnchors({10, 14, 23, 27, 37, 58, 81, 82, 135, 169, 344, 319});
62 detectionNetwork->setAnchorMasks({{
"side26", {1, 2, 3}}, {
"side13", {3, 4, 5}}});
63 detectionNetwork->setIouThreshold(0.5f);
64 detectionNetwork->setBlobPath(nnPath);
65 detectionNetwork->setNumInferenceThreads(2);
66 detectionNetwork->input.setBlocking(
false);
71 detectionNetwork->passthrough.link(xoutRgb->input);
76 detectionNetwork->out.link(nnOut->
input);
86 std::vector<dai::ImgDetection> detections;
87 auto startTime = steady_clock::now();
90 auto color2 = cv::Scalar(255, 255, 255);
93 auto displayFrame = [](std::string name, cv::Mat frame, std::vector<dai::ImgDetection>& detections) {
94 auto color = cv::Scalar(255, 0, 0);
96 for(
auto& detection : detections) {
97 int x1 = detection.xmin * frame.cols;
98 int y1 = detection.ymin * frame.rows;
99 int x2 = detection.xmax * frame.cols;
100 int y2 = detection.ymax * frame.rows;
102 uint32_t labelIndex = detection.label;
103 std::string labelStr =
to_string(labelIndex);
107 cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
108 std::stringstream confStr;
109 confStr << std::fixed << std::setprecision(2) << detection.confidence * 100;
110 cv::putText(frame, confStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, 255);
111 cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
114 cv::imshow(name, frame);
118 std::shared_ptr<dai::ImgFrame> inRgb;
119 std::shared_ptr<dai::ImgDetections> inDet;
130 auto currentTime = steady_clock::now();
131 auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
132 if(elapsed > seconds(1)) {
133 fps = counter / elapsed.count();
135 startTime = currentTime;
139 frame = inRgb->getCvFrame();
140 std::stringstream fpsStr;
141 fpsStr <<
"NN fps: " << std::fixed << std::setprecision(2) <<
fps;
142 cv::putText(frame, fpsStr.str(), cv::Point(2, inRgb->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color2);
146 detections = inDet->detections;
150 displayFrame(
"rgb", frame, detections);
153 int key = cv::waitKey(1);
154 if(key ==
'q' || key ==
'Q') {