Meta Byte Track
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

STrack.cpp 3.7KB

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