Skip to content

Commit

Permalink
Use one vector of data combined with vector of data size instead of u…
Browse files Browse the repository at this point in the history
…sing multiple save with different attribute names.
  • Loading branch information
s-trinh committed Dec 5, 2023
1 parent 13beef0 commit ea3a87f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ std::string toString(const std::string &name, int val)
}
}

int main(int argc, char **argv)
int main(int /*argc*/, char **/*argv*/)
{
const std::string npz_filename = "npz_tracking_teabox.npz";
visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
Expand Down Expand Up @@ -53,6 +53,13 @@ int main(int argc, char **argv)
int nb_data = *arr_nb_data.data<int>();
std::cout << "Number of images: " << nb_data << std::endl;

// Load all the images data
visp::cnpy::NpyArray arr_vec_img_data_size = npz_data["vec_img_data_size"];
int* vec_img_data_size_ptr = arr_vec_img_data_size.data<int>();
visp::cnpy::NpyArray arr_vec_img = npz_data["vec_img"];
unsigned char* vec_img_ptr = arr_vec_img.data<unsigned char>();
size_t img_data_offset = 0;

// Load all the poses
visp::cnpy::NpyArray arr_vec_poses = npz_data["vec_poses"];
double* vec_poses_ptr = arr_vec_poses.data<double>();
Expand All @@ -61,10 +68,8 @@ int main(int argc, char **argv)
size_t pose_size = arr_vec_poses.shape[1];

for (int iter = 0; iter < nb_data; iter++) {
std::string img_data_str = toString("png_image_%06d", iter);
visp::cnpy::NpyArray arr_nb_data = npz_data[img_data_str];

vpImageIo::readPNGfromMem(arr_nb_data.data<unsigned char>(), arr_nb_data.shape[0], I);
vpImageIo::readPNGfromMem(vec_img_ptr + img_data_offset, vec_img_data_size_ptr[iter], I);
img_data_offset += vec_img_data_size_ptr[iter];
vpImageConvert::convert(I, I_display);

const std::string str_model_iter_sz = toString("model_%06d", iter) + "_sz";
Expand All @@ -88,11 +93,6 @@ int main(int argc, char **argv)
}
}

// const std::string vec_pose_str = toString("vec_pose_%06d", iter);
// visp::cnpy::NpyArray arr_vec_pose = npz_data[vec_pose_str];
// vpHomogeneousMatrix cMo(vpTranslationVector(arr_vec_pose.data<double>()[3], arr_vec_pose.data<double>()[4], arr_vec_pose.data<double>()[5]),
// vpThetaUVector(arr_vec_pose.data<double>()[0], arr_vec_pose.data<double>()[1], arr_vec_pose.data<double>()[2])
// );
vpHomogeneousMatrix cMo(vpTranslationVector(vec_poses_ptr[pose_size*iter + 3], vec_poses_ptr[pose_size*iter + 4], vec_poses_ptr[pose_size*iter + 5]),
vpThetaUVector(vec_poses_ptr[pose_size*iter], vec_poses_ptr[pose_size*iter + 1], vec_poses_ptr[pose_size*iter + 2])
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::string toString(const std::string &name, int val)
}
}

int main(int argc, char **argv)
int main(int /*argc*/, char **/*argv*/)
{
std::string opt_videoname = "model/teabox/teabox.mp4";
std::string opt_modelname = "model/teabox/teabox.cao";
Expand Down Expand Up @@ -70,10 +70,6 @@ int main(int argc, char **argv)
const std::string npz_filename = "npz_tracking_teabox.npz";
const std::string camera_name = "Camera";
std::vector<char> vec_camera_name(camera_name.begin(), camera_name.end());
for (size_t i = 0; i < vec_camera_name.size(); i++) {
std::cout << i << "): " << vec_camera_name[i] << " ; " << int(vec_camera_name[i]) << " ; "
<< camera_name.c_str()[i] << " ; " << int(camera_name.c_str()[i]) << std::endl;
}

visp::cnpy::npz_save(npz_filename, "camera_name", &vec_camera_name[0], { vec_camera_name.size() }, "w"); // overwrite
visp::cnpy::npz_save(npz_filename, "height", &height, { 1 }, "a"); // append
Expand All @@ -89,6 +85,11 @@ int main(int argc, char **argv)
std::vector<double> vec_poses;
vec_poses.reserve(g.getLastFrameIndex() * 6);

std::vector<int> vec_img_data_size;
vec_img_data_size.reserve(g.getLastFrameIndex());
std::vector<unsigned char> vec_img_data;
vec_img_data.reserve(g.getLastFrameIndex() * height * width);

int iter = 0;
while (!g.end()) {
g.acquire(I);
Expand All @@ -98,18 +99,17 @@ int main(int argc, char **argv)

int last_pos = 0;
vpImageIo::writePNGtoMem(I, last_pos, img_buffer.data());
visp::cnpy::npz_save(npz_filename, toString("png_image_%06d", iter), &img_buffer[0], { (unsigned int) last_pos }, "a");
vec_img_data_size.push_back(last_pos);
vec_img_data.insert(vec_img_data.end(), img_buffer.data(), img_buffer.data() + last_pos);

std::vector<double> vec_pose = poseToVec(cMo);
vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
// Commented, use instead a "multidimensional" array
// visp::cnpy::npz_save(npz_filename, toString("vec_pose_%06d", iter), &vec_pose[0], { vec_pose.size() }, "a");

std::map<std::string, std::vector<std::vector<double> > > mapOfModels;
std::map<std::string, unsigned int> mapOfW;
mapOfW[camera_name] = I.getWidth();
std::map<std::string, unsigned int> mapOfH;
mapOfH[camera_name] = I.getHeight();;
mapOfH[camera_name] = I.getHeight();
std::map<std::string, vpHomogeneousMatrix> mapOfcMos;
mapOfcMos[camera_name] = cMo;
std::map<std::string, vpCameraParameters> mapOfCams;
Expand All @@ -135,6 +135,8 @@ int main(int argc, char **argv)
iter++;
}

visp::cnpy::npz_save(npz_filename, "vec_img_data_size", &vec_img_data_size[0], { vec_img_data_size.size() }, "a");
visp::cnpy::npz_save(npz_filename, "vec_img", &vec_img_data[0], { vec_img_data.size() }, "a");
// Show how to save a "multidimensional" array
visp::cnpy::npz_save(npz_filename, "vec_poses", &vec_poses[0], { static_cast<size_t>(iter), 6 }, "a");

Expand Down

0 comments on commit ea3a87f

Please sign in to comment.