File size: 5,392 Bytes
2b7d279 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | #include <filesystem>
#include <iostream>
#include <fstream>
#include<random>
#include "BYTETracker.cpp"
#include "GlobalMotionCompensation.cpp"
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/eigen.h>
namespace py = pybind11;
PYBIND11_MODULE(adaptbytetrack, m) {
// Adaptive ByteTracker
py::class_<BYTETracker>(m, "BYTETracker")
.def(py::init<int, int, float, float, bool > ())
.def("update", &BYTETracker::update);
}
using namespace std;
using namespace Eigen;
MatrixXd read_txt(string file_name = "", char delim = ' ') {
std::ifstream inRowCol(file_name);
std::string lineRowCol;
int nrows = 0;
int ncols = 0;
while (std::getline(inRowCol, lineRowCol)) {
nrows++;
// Split the line by comma and count the number of elements
std::vector<std::string> res;
std::stringstream ss(lineRowCol);
std::string element;
ncols = 0;
while (std::getline(ss, element, delim)) {
ncols++;
}
}
if (ncols == 0) {
return MatrixXd(nrows, ncols); // column vector, no delimiter
}
std::ifstream in(file_name);
std::string line;
int row = 0;
int col = 0;
Eigen::MatrixXd res = Eigen::MatrixXd(nrows, ncols);
if (in.is_open()) {
while (std::getline(in, line)) {
char *ptr = (char *) line.c_str();
int len = line.length();
col = 0;
char *start = ptr;
for (int i = 0; i < len; i++) {
if (ptr[i] == delim) {
res(row, col++) = atof(start);
start = ptr + i + 1;
}
}
res(row, col) = atof(start);
row++;
}
in.close();
}
return res;
}
void write_results(const string &filename, const vector<vector<float>> &results) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: cannot open file " << filename << std::endl;
return;
}
for (const vector<float> &result: results) {
int frame_id = result[0];
int track_id = result[1];
if (track_id < 0) continue;
float x1 = result[2];
float y1 = result[3];
float w = result[4];
float h = result[5];
float s = 1; // default score of a track
file << frame_id << ","
<< track_id << ","
<< std::fixed << std::setprecision(1)
<< x1 << "," << y1 << "," << w << "," << h << ","
<< std::setprecision(2) << s << ",-1,-1,-1\n";
}
file.close();
std::cout << "Saved results to " << filename << std::endl;
}
cv::Scalar get_color(int idx) {
idx = (idx + 1) * 50;
int r = (37 * idx) % 255;
int g = (17 * idx) % 255;
int b = (29 * idx) % 255;
return cv::Scalar(b, g, r); // OpenCV uses BGR color format
}
//int main() {
// MatrixXd arr = read_txt("../detection/bytetrack/MOT16-02.txt", ',');
// int n_frames = arr.col(0).maxCoeff();
//
// BYTETracker byteTracker(30, 30);
// vector<cv::String> fn;
// glob("/media/ubuntu/2715608D71CBF6FC/datasets/mot/MOT16/train/MOT16-02/img1/*.jpg", fn, false);
//
// vector<vector<float>> results;
// for (int frame = 0; frame < n_frames; frame++) {
// std::vector<vector<float>> rects;
// for (int i = 0; i < arr.rows(); i++) {
// if (arr(i, 0) == frame) {
// float left = static_cast<float>(arr(i, 1));
// float top = static_cast<float>(arr(i, 2));
// float right = static_cast<float>(arr(i, 3));
// float bottom = static_cast<float>(arr(i, 4));
// float conf = static_cast<float>(arr(i, 5));
// vector<float> row = {left, top, right, bottom, conf};
// rects.emplace_back(row);
// }
// }
//
// cv::Mat img = cv::imread(fn[frame]);
// vector<vector<float>> output_tracks = byteTracker.update(rects, fn[frame]);
//
// cv::putText(img, //target image
// "Frame " + std::to_string(frame), //text
// cv::Point(30, 30), //top-left position
// cv::FONT_HERSHEY_DUPLEX,
// 1,
// CV_RGB(255, 0, 0), //font color
// 2);
// for (vector<float> track: output_tracks) {
// Scalar c = get_color((int) track[0]);
// cv::Rect rect(track[1], track[2], track[3], track[4]);
// cv::rectangle(img, rect, c, 2);
// cv::putText(img, //target image
// std::to_string(int(track[0])), //text
// cv::Point(track[1] + 5, track[2] + 20), //top-left position
// cv::FONT_HERSHEY_DUPLEX,
// 0.8,
// c, //font color
// 1);
// vector<float> f_track = {(float) (frame), track[0], track[1], track[2], track[3], track[4]};
// results.emplace_back(f_track);
// }
// cv::imshow("Image", img);
// cv::waitKey(10);
// }
// write_results("MOT16-02.txt", results);
//
// return 0;
//} |