STrack.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "STrack.h"
  2. STrack::STrack(vector<float> tlwh_, float score, int label, NvMOTObjToTrack *associatedObjectIn) {
  3. _tlwh.resize(4);
  4. _tlwh.assign(tlwh_.begin(), tlwh_.end());
  5. original_tlwh.resize(4);
  6. original_tlwh.assign(tlwh_.begin(), tlwh_.end());
  7. is_activated = false;
  8. track_id = 0;
  9. state = TrackState::New;
  10. tlwh.resize(4);
  11. tlbr.resize(4);
  12. static_tlwh();
  13. static_tlbr();
  14. frame_id = 0;
  15. tracklet_len = 0;
  16. this->score = score;
  17. start_frame = 0;
  18. this->label = label;
  19. this->associatedObjectIn = associatedObjectIn;
  20. }
  21. STrack::~STrack() {
  22. }
  23. void STrack::activate(byte_kalman::KalmanFilter &kalman_filter, int frame_id) {
  24. this->kalman_filter = kalman_filter;
  25. this->track_id = this->next_id();
  26. vector<float> _tlwh_tmp(4);
  27. _tlwh_tmp[0] = this->_tlwh[0];
  28. _tlwh_tmp[1] = this->_tlwh[1];
  29. _tlwh_tmp[2] = this->_tlwh[2];
  30. _tlwh_tmp[3] = this->_tlwh[3];
  31. vector<float> xyah = tlwh_to_xyah(_tlwh_tmp);
  32. DETECTBOX xyah_box;
  33. xyah_box[0] = xyah[0];
  34. xyah_box[1] = xyah[1];
  35. xyah_box[2] = xyah[2];
  36. xyah_box[3] = xyah[3];
  37. auto mc = this->kalman_filter.initiate(xyah_box);
  38. this->mean = mc.first;
  39. this->covariance = mc.second;
  40. static_tlwh();
  41. static_tlbr();
  42. this->tracklet_len = 0;
  43. this->state = TrackState::Tracked;
  44. if (frame_id == 1) {
  45. this->is_activated = true;
  46. }
  47. //this->is_activated = true;
  48. this->frame_id = frame_id;
  49. this->start_frame = frame_id;
  50. }
  51. void STrack::re_activate(STrack &new_track, int frame_id, bool new_id) {
  52. vector<float> xyah = tlwh_to_xyah(new_track.tlwh);
  53. DETECTBOX xyah_box;
  54. xyah_box[0] = xyah[0];
  55. xyah_box[1] = xyah[1];
  56. xyah_box[2] = xyah[2];
  57. xyah_box[3] = xyah[3];
  58. auto mc = this->kalman_filter.update(this->mean, this->covariance, xyah_box);
  59. this->mean = mc.first;
  60. this->covariance = mc.second;
  61. static_tlwh();
  62. static_tlbr();
  63. this->tracklet_len = 0;
  64. this->state = TrackState::Tracked;
  65. this->is_activated = true;
  66. this->frame_id = frame_id;
  67. this->score = new_track.score;
  68. this->associatedObjectIn = new_track.associatedObjectIn;
  69. this->original_tlwh.resize(4);
  70. this->original_tlwh.assign(new_track.original_tlwh.begin(), new_track.original_tlwh.end());
  71. if (new_id)
  72. this->track_id = next_id();
  73. }
  74. void STrack::update(STrack &new_track, int frame_id) {
  75. this->frame_id = frame_id;
  76. this->tracklet_len++;
  77. vector<float> xyah = tlwh_to_xyah(new_track.tlwh);
  78. DETECTBOX xyah_box;
  79. xyah_box[0] = xyah[0];
  80. xyah_box[1] = xyah[1];
  81. xyah_box[2] = xyah[2];
  82. xyah_box[3] = xyah[3];
  83. auto mc = this->kalman_filter.update(this->mean, this->covariance, xyah_box);
  84. this->mean = mc.first;
  85. this->covariance = mc.second;
  86. static_tlwh();
  87. static_tlbr();
  88. this->state = TrackState::Tracked;
  89. this->is_activated = true;
  90. this->score = new_track.score;
  91. this->associatedObjectIn = new_track.associatedObjectIn;
  92. this->original_tlwh.resize(4);
  93. this->original_tlwh.assign(new_track.original_tlwh.begin(), new_track.original_tlwh.end());
  94. }
  95. void STrack::static_tlwh() {
  96. if (this->state == TrackState::New) {
  97. tlwh[0] = _tlwh[0];
  98. tlwh[1] = _tlwh[1];
  99. tlwh[2] = _tlwh[2];
  100. tlwh[3] = _tlwh[3];
  101. return;
  102. }
  103. tlwh[0] = mean[0];
  104. tlwh[1] = mean[1];
  105. tlwh[2] = mean[2];
  106. tlwh[3] = mean[3];
  107. tlwh[2] *= tlwh[3];
  108. tlwh[0] -= tlwh[2] / 2;
  109. tlwh[1] -= tlwh[3] / 2;
  110. }
  111. void STrack::static_tlbr() {
  112. tlbr.clear();
  113. tlbr.assign(tlwh.begin(), tlwh.end());
  114. tlbr[2] += tlbr[0];
  115. tlbr[3] += tlbr[1];
  116. }
  117. vector<float> STrack::tlwh_to_xyah(vector<float> tlwh_tmp) {
  118. vector<float> tlwh_output = tlwh_tmp;
  119. tlwh_output[0] += tlwh_output[2] / 2;
  120. tlwh_output[1] += tlwh_output[3] / 2;
  121. tlwh_output[2] /= tlwh_output[3];
  122. return tlwh_output;
  123. }
  124. vector<float> STrack::to_xyah() {
  125. return tlwh_to_xyah(tlwh);
  126. }
  127. vector<float> STrack::tlbr_to_tlwh(vector<float> &tlbr) {
  128. tlbr[2] -= tlbr[0];
  129. tlbr[3] -= tlbr[1];
  130. return tlbr;
  131. }
  132. void STrack::mark_lost() {
  133. state = TrackState::Lost;
  134. }
  135. void STrack::mark_removed() {
  136. state = TrackState::Removed;
  137. }
  138. int STrack::next_id() {
  139. static int _count = 0;
  140. _count++;
  141. return _count;
  142. }
  143. int STrack::end_frame() {
  144. return this->frame_id;
  145. }
  146. void STrack::multi_predict(vector<STrack *> &stracks, byte_kalman::KalmanFilter &kalman_filter) {
  147. for (int i = 0; i < stracks.size(); i++) {
  148. if (stracks[i]->state != TrackState::Tracked) {
  149. stracks[i]->mean[7] = 0;
  150. }
  151. kalman_filter.predict(stracks[i]->mean, stracks[i]->covariance);
  152. }
  153. }