-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathinput_data.cpp
More file actions
136 lines (111 loc) 路 4.71 KB
/
Copy pathinput_data.cpp
File metadata and controls
136 lines (111 loc) 路 4.71 KB
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
#include <filesystem>
#include <nlohmann/json.hpp>
#include "input_data.hpp"
namespace fs = std::filesystem;
using namespace torch::indexing;
using json = nlohmann::json;
namespace ns{ InputData inputDataFromNerfStudio(const std::string &projectRoot); }
namespace cm{ InputData inputDataFromColmap(const std::string &projectRoot); }
namespace osfm { InputData inputDataFromOpenSfM(const std::string &projectRoot); }
namespace omvg { InputData inputDataFromOpenMVG(const std::string &projectRoot); }
InputData inputDataFromX(const std::string &projectRoot){
fs::path root(projectRoot);
if (fs::exists(root / "transforms.json")){
return ns::inputDataFromNerfStudio(projectRoot);
}else if (fs::exists(root / "sparse") || fs::exists(root / "cameras.bin")){
return cm::inputDataFromColmap(projectRoot);
}else if (fs::exists(root / "reconstruction.json")){
return osfm::inputDataFromOpenSfM(projectRoot);
}else if (fs::exists(root / "opensfm" / "reconstruction.json")){
return osfm::inputDataFromOpenSfM((root / "opensfm").string());
}else if (fs::exists(root / "sfm_data.json")){
return omvg::inputDataFromOpenMVG((root).string());
}
else{
throw std::runtime_error("Invalid project folder (must be either a colmap or nerfstudio or openmvg project folder)");
}
}
torch::Tensor Camera::getIntrinsicsMatrix(){
return torch::tensor({{fx, 0.0f, cx},
{0.0f, fy, cy},
{0.0f, 0.0f, 1.0f}}, torch::kFloat32);
}
bool Camera::hasDistortionParameters(){
return k1 != 0.0f || k2 != 0.0f || k3 != 0.0f || k4 != 0.0f || k5 != 0.0f || k6 != 0.0f || p1 != 0.0f || p2 != 0.0f;
}
std::string findMaskPath(const std::string &imagePath, const std::string &projectRoot){
static const char *folders[] = { "masks", "mask", "segmentation", "dynamic_masks" };
static const char *extensions[] = { ".png", ".jpg", ".jpeg", ".mask.png" };
fs::path img(imagePath);
std::string stem = img.stem().string();
std::string name = img.filename().string();
for (const char *folder : folders){
fs::path dir = fs::path(projectRoot) / folder;
if (!fs::exists(dir) || !fs::is_directory(dir)) continue;
for (const char *ext : extensions){
fs::path cand = dir / (stem + ext);
if (fs::exists(cand)) return cand.string();
cand = dir / (name + ext);
if (fs::exists(cand)) return cand.string();
}
}
return "";
}
std::tuple<std::vector<Camera>, Camera *> InputData::getCameras(bool validate, const std::string &valImage){
if (!validate) return std::make_tuple(cameras, nullptr);
else{
size_t valIdx = -1;
std::srand(42);
if (valImage == "random"){
valIdx = std::rand() % cameras.size();
}else{
for (size_t i = 0; i < cameras.size(); i++){
if (fs::path(cameras[i].filePath).filename().string() == valImage){
valIdx = i;
break;
}
}
if (valIdx == -1) throw std::runtime_error(valImage + " not in the list of cameras");
}
std::vector<Camera> cams;
Camera *valCam = nullptr;
for (size_t i = 0; i < cameras.size(); i++){
if (i != valIdx) cams.push_back(cameras[i]);
else valCam = &cameras[i];
}
return std::make_tuple(cams, valCam);
}
}
void InputData::saveCameras(const std::string &filename, bool keepCrs){
json j = json::array();
for (size_t i = 0; i < cameras.size(); i++){
Camera &cam = cameras[i];
json camera = json::object();
camera["id"] = i;
camera["img_name"] = fs::path(cam.filePath).filename().string();
camera["width"] = cam.width;
camera["height"] = cam.height;
camera["fx"] = cam.fx;
camera["fy"] = cam.fy;
torch::Tensor R = cam.camToWorld.index({Slice(None, 3), Slice(None, 3)});
torch::Tensor T = cam.camToWorld.index({Slice(None, 3), Slice(3,4)}).squeeze();
// Flip z and y
R = torch::matmul(R, torch::diag(torch::tensor({1.0f, -1.0f, -1.0f})));
if (keepCrs) T = (T / scale) + translation;
std::vector<float> position(3);
std::vector<std::vector<float>> rotation(3, std::vector<float>(3));
for (int i = 0; i < 3; i++) {
position[i] = T[i].item<float>();
for (int j = 0; j < 3; j++) {
rotation[i][j] = R[i][j].item<float>();
}
}
camera["position"] = position;
camera["rotation"] = rotation;
j.push_back(camera);
}
std::ofstream of(filename);
of << j;
of.close();
std::cout << "Wrote " << filename << std::endl;
}