object_tracker.cpp
Go to the documentation of this file.
1 #include <chrono>
2 #include <iostream>
3 
4 // Includes common necessary includes for development using depthai library
5 #include "depthai/depthai.hpp"
6 
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"};
10 
11 static std::atomic<bool> fullFrameTracking{false};
12 
13 int main(int argc, char** argv) {
14  using namespace std;
15  using namespace std::chrono;
16  std::string nnPath(BLOB_PATH);
17 
18  // If path to blob specified, use that
19  if(argc > 1) {
20  nnPath = std::string(argv[1]);
21  }
22 
23  // Print which blob we are using
24  printf("Using blob at path: %s\n", nnPath.c_str());
25 
26  // Create pipeline
27  dai::Pipeline pipeline;
28 
29  // Define sources and outputs
30  auto camRgb = pipeline.create<dai::node::ColorCamera>();
31  auto detectionNetwork = pipeline.create<dai::node::MobileNetDetectionNetwork>();
32  auto objectTracker = pipeline.create<dai::node::ObjectTracker>();
33 
34  auto xlinkOut = pipeline.create<dai::node::XLinkOut>();
35  auto trackerOut = pipeline.create<dai::node::XLinkOut>();
36 
37  xlinkOut->setStreamName("preview");
38  trackerOut->setStreamName("tracklets");
39 
40  // Properties
41  camRgb->setPreviewSize(300, 300);
43  camRgb->setInterleaved(false);
44  camRgb->setColorOrder(dai::ColorCameraProperties::ColorOrder::BGR);
45  camRgb->setFps(40);
46 
47  // testing MobileNet DetectionNetwork
48  detectionNetwork->setBlobPath(nnPath);
49  detectionNetwork->setConfidenceThreshold(0.5f);
50  detectionNetwork->input.setBlocking(false);
51 
52  objectTracker->setDetectionLabelsToTrack({15}); // track only person
53  // possible tracking types: ZERO_TERM_COLOR_HISTOGRAM, ZERO_TERM_IMAGELESS, SHORT_TERM_IMAGELESS, SHORT_TERM_KCF
54  objectTracker->setTrackerType(dai::TrackerType::ZERO_TERM_COLOR_HISTOGRAM);
55  // take the smallest ID when new object is tracked, possible options: SMALLEST_ID, UNIQUE_ID
56  objectTracker->setTrackerIdAssignmentPolicy(dai::TrackerIdAssignmentPolicy::SMALLEST_ID);
57 
58  // Linking
59  camRgb->preview.link(detectionNetwork->input);
60  objectTracker->passthroughTrackerFrame.link(xlinkOut->input);
61 
62  if(fullFrameTracking) {
63  camRgb->video.link(objectTracker->inputTrackerFrame);
64  } else {
65  detectionNetwork->passthrough.link(objectTracker->inputTrackerFrame);
66  }
67 
68  detectionNetwork->passthrough.link(objectTracker->inputDetectionFrame);
69  detectionNetwork->out.link(objectTracker->inputDetections);
70  objectTracker->out.link(trackerOut->input);
71 
72  // Connect to device and start pipeline
73  dai::Device device(pipeline);
74 
75  auto preview = device.getOutputQueue("preview", 4, false);
76  auto tracklets = device.getOutputQueue("tracklets", 4, false);
77 
78  auto startTime = steady_clock::now();
79  int counter = 0;
80  float fps = 0;
81 
82  while(true) {
83  auto imgFrame = preview->get<dai::ImgFrame>();
84  auto track = tracklets->get<dai::Tracklets>();
85 
86  counter++;
87  auto currentTime = steady_clock::now();
88  auto elapsed = duration_cast<duration<float>>(currentTime - startTime);
89  if(elapsed > seconds(1)) {
90  fps = counter / elapsed.count();
91  counter = 0;
92  startTime = currentTime;
93  }
94 
95  auto color = cv::Scalar(255, 0, 0);
96  cv::Mat frame = imgFrame->getCvFrame();
97  auto trackletsData = track->tracklets;
98  for(auto& t : trackletsData) {
99  auto roi = t.roi.denormalize(frame.cols, frame.rows);
100  int x1 = roi.topLeft().x;
101  int y1 = roi.topLeft().y;
102  int x2 = roi.bottomRight().x;
103  int y2 = roi.bottomRight().y;
104 
105  uint32_t labelIndex = t.label;
106  std::string labelStr = to_string(labelIndex);
107  if(labelIndex < labelMap.size()) {
108  labelStr = labelMap[labelIndex];
109  }
110  cv::putText(frame, labelStr, cv::Point(x1 + 10, y1 + 20), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
111 
112  std::stringstream idStr;
113  idStr << "ID: " << t.id;
114  cv::putText(frame, idStr.str(), cv::Point(x1 + 10, y1 + 40), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
115  std::stringstream statusStr;
116  statusStr << "Status: " << t.status;
117  cv::putText(frame, statusStr.str(), cv::Point(x1 + 10, y1 + 60), cv::FONT_HERSHEY_TRIPLEX, 0.5, color);
118 
119  cv::rectangle(frame, cv::Rect(cv::Point(x1, y1), cv::Point(x2, y2)), color, cv::FONT_HERSHEY_SIMPLEX);
120  }
121 
122  std::stringstream fpsStr;
123  fpsStr << "NN fps:" << std::fixed << std::setprecision(2) << fps;
124  cv::putText(frame, fpsStr.str(), cv::Point(2, imgFrame->getHeight() - 4), cv::FONT_HERSHEY_TRIPLEX, 0.4, color);
125 
126  cv::imshow("tracker", frame);
127 
128  int key = cv::waitKey(1);
129  if(key == 'q' || key == 'Q') {
130  return 0;
131  }
132  }
133  return 0;
134 }
dai::node::XLinkOut
XLinkOut node. Sends messages over XLink.
Definition: XLinkOut.hpp:14
dai::Pipeline
Represents the pipeline, set of nodes and connections between them.
Definition: Pipeline.hpp:100
fps
static constexpr int fps
Definition: rgb_depth_aligned.cpp:12
dai::TrackerIdAssignmentPolicy::SMALLEST_ID
@ SMALLEST_ID
Take the smallest available ID.
dai::node::ColorCamera
ColorCamera node. For use with color sensors.
Definition: ColorCamera.hpp:16
dai::Tracklets
Definition: Tracklets.hpp:15
dai::TrackerType::ZERO_TERM_COLOR_HISTOGRAM
@ ZERO_TERM_COLOR_HISTOGRAM
Tracking using image data too.
main
int main(int argc, char **argv)
Definition: object_tracker.cpp:13
dai::Device::getOutputQueue
std::shared_ptr< DataOutputQueue > getOutputQueue(const std::string &name)
Definition: Device.cpp:86
dai::node::XLinkOut::input
Input input
Definition: XLinkOut.hpp:27
depthai.hpp
dai::Pipeline::create
std::shared_ptr< N > create()
Definition: Pipeline.hpp:145
dai::node::ObjectTracker
ObjectTracker node. Performs object tracking using Kalman filter and hungarian algorithm.
Definition: ObjectTracker.hpp:19
dai::ColorCameraProperties::ColorOrder::BGR
@ BGR
dai::ImgFrame
Definition: ImgFrame.hpp:25
nanorpc::core::exception::to_string
std::string to_string(std::exception const &e)
Definition: exception.h:46
dai::ColorCameraProperties::SensorResolution::THE_1080_P
@ THE_1080_P
1920 × 1080
dai::Device
Definition: Device.hpp:21
std
Definition: Node.hpp:366
fullFrameTracking
static std::atomic< bool > fullFrameTracking
Definition: object_tracker.cpp:11
labelMap
static const std::vector< std::string > labelMap
Definition: object_tracker.cpp:7
dai::node::MobileNetDetectionNetwork
MobileNetDetectionNetwork node. Parses MobileNet results.
Definition: DetectionNetwork.hpp:56
dai::node::XLinkOut::setStreamName
void setStreamName(const std::string &name)
Definition: XLinkOut.cpp:13


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19