|
| 1 | +#include "input.hpp" |
| 2 | + |
| 3 | +#include "ffmpeg.hpp" |
| 4 | + |
| 5 | +#include <fstream> |
| 6 | +#include <sstream> |
| 7 | + |
| 8 | +#include <nlohmann/json.hpp> |
| 9 | +#include <lodepng/lodepng.h> |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +const std::string SEPARATOR = "/"; |
| 14 | + |
| 15 | +using json = nlohmann::json; |
| 16 | + |
| 17 | +// Format strings in printf style. |
| 18 | +template<typename ... Args> |
| 19 | +std::string stringFormat(const std::string &f, Args ... args) { |
| 20 | + int n = std::snprintf(nullptr, 0, f.c_str(), args ...) + 1; |
| 21 | + assert(n > 0); |
| 22 | + auto nn = static_cast<size_t>(n); |
| 23 | + auto buf = std::make_unique<char[]>(nn); |
| 24 | + std::snprintf(buf.get(), nn, f.c_str(), args ...); |
| 25 | + return std::string(buf.get(), buf.get() + nn - 1); |
| 26 | +} |
| 27 | + |
| 28 | +std::vector<std::string> findVideos(const std::string &folderPath, bool &isImageFolder) { |
| 29 | + isImageFolder = false; |
| 30 | + std::vector<std::string> videos; |
| 31 | + for (size_t cameraInd = 0; cameraInd < 2; ++cameraInd) { |
| 32 | + std::string videoPathNoSuffix = folderPath + SEPARATOR + "data"; |
| 33 | + if (cameraInd > 0) videoPathNoSuffix += std::to_string(cameraInd + 1); |
| 34 | + for (std::string suffix : { "mov", "avi", "mp4", "mkv" }) { |
| 35 | + const std::string videoPath = videoPathNoSuffix + "." + suffix; |
| 36 | + std::ifstream testFile(videoPath); |
| 37 | + if (testFile.is_open()) videos.push_back(videoPath); |
| 38 | + } |
| 39 | + |
| 40 | + const std::string imageFolder = stringFormat("%s/frames%d", folderPath.c_str(), cameraInd + 1); |
| 41 | + const std::string firstImage = stringFormat("%s/%08d.png", imageFolder.c_str(), 0); |
| 42 | + std::ifstream testFile(firstImage); |
| 43 | + if (testFile.is_open()) isImageFolder = true; |
| 44 | + } |
| 45 | + return videos; |
| 46 | +} |
| 47 | + |
| 48 | +// Read PNG image to buffer. |
| 49 | +bool readImage( |
| 50 | + const std::string &filePath, |
| 51 | + std::vector<uint8_t> &data, |
| 52 | + std::vector<uint8_t> &tmpBuffer, |
| 53 | + int &width, |
| 54 | + int &height |
| 55 | +) { |
| 56 | + std::ifstream file(filePath); |
| 57 | + if (!file.is_open()) { |
| 58 | + printf("No such file %s\n", filePath.c_str()); |
| 59 | + return false; |
| 60 | + } |
| 61 | + tmpBuffer.clear(); |
| 62 | + unsigned w, h; |
| 63 | + unsigned error = lodepng::decode(tmpBuffer, w, h, filePath); |
| 64 | + if (error) { |
| 65 | + printf("Error %s\n", lodepng_error_text(error)); |
| 66 | + return false; |
| 67 | + } |
| 68 | + assert(tmpBuffer.size() == 4 * w * h); |
| 69 | + data.resize(w * h); |
| 70 | + for (size_t i = 0; i < w * h; ++i) { |
| 71 | + // Get green channel from RGBA. Any reasonable gray-scale conversion should work for VIO. |
| 72 | + data[i] = tmpBuffer.at(4 * i + 1); |
| 73 | + } |
| 74 | + width = static_cast<int>(w); |
| 75 | + height = static_cast<int>(h); |
| 76 | + return true; |
| 77 | +} |
| 78 | + |
| 79 | +class InputJsonl : public Input { |
| 80 | +public: |
| 81 | + InputJsonl(const std::string &inputFolderPath) : |
| 82 | + inputFolderPath(inputFolderPath) |
| 83 | + { |
| 84 | + imu = std::make_shared<spectacularAI::Vector3d>(spectacularAI::Vector3d{ 0.0, 0.0, 0.0 }); |
| 85 | + jsonlFile.open(inputFolderPath + SEPARATOR + "data.jsonl"); |
| 86 | + if (!jsonlFile.is_open()) { |
| 87 | + printf("No data.jsonl file found. Does `%s` exist?\n", inputFolderPath.c_str()); |
| 88 | + assert(false); |
| 89 | + } |
| 90 | + videoPaths = findVideos(inputFolderPath, useImageInput); |
| 91 | + assert(!videoPaths.empty() || useImageInput); |
| 92 | + for (const std::string &videoPath : videoPaths) { |
| 93 | + videoInputs.push_back(std::make_unique<VideoInput>(videoPath)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + std::string getConfig() const final { |
| 98 | + std::ifstream configFile(inputFolderPath + SEPARATOR + "vio_config.yaml"); |
| 99 | + if (!configFile.is_open()) { |
| 100 | + printf("No vio_config.yaml provided, using default config.\n"); |
| 101 | + return ""; |
| 102 | + } |
| 103 | + std::ostringstream oss; |
| 104 | + oss << configFile.rdbuf(); |
| 105 | + return oss.str(); |
| 106 | + } |
| 107 | + |
| 108 | + std::string getCalibration() const final { |
| 109 | + std::ifstream calibrationFile(inputFolderPath + SEPARATOR + "calibration.json"); |
| 110 | + // Calibration is always required. |
| 111 | + assert(calibrationFile.is_open()); |
| 112 | + std::ostringstream oss; |
| 113 | + oss << calibrationFile.rdbuf(); |
| 114 | + return oss.str(); |
| 115 | + } |
| 116 | + |
| 117 | + bool next(Data &data) final { |
| 118 | + if (!std::getline(jsonlFile, line)) return false; |
| 119 | + data.video0 = nullptr; |
| 120 | + data.video1 = nullptr; |
| 121 | + data.accelerometer = nullptr; |
| 122 | + data.gyroscope = nullptr; |
| 123 | + |
| 124 | + json j = json::parse(line, nullptr, false); // stream, callback, allow_exceptions |
| 125 | + data.timestamp = j["time"].get<double>(); |
| 126 | + |
| 127 | + if (j.find("sensor") != j.end()) { |
| 128 | + std::array<double, 3> v = j["sensor"]["values"]; |
| 129 | + *imu = { .x = v[0], .y = v[1], .z = v[2] }; |
| 130 | + const std::string sensorType = j["sensor"]["type"]; |
| 131 | + if (sensorType == "gyroscope") { |
| 132 | + data.gyroscope = imu; |
| 133 | + } |
| 134 | + else if (sensorType == "accelerometer") { |
| 135 | + data.accelerometer = imu; |
| 136 | + } |
| 137 | + } |
| 138 | + else if (j.find("frames") != j.end()) { |
| 139 | + json jFrames = j["frames"]; |
| 140 | + size_t cameraCount = jFrames.size(); |
| 141 | + assert(cameraCount >= 1); |
| 142 | + int number = j["number"].get<int>(); |
| 143 | + for (size_t cameraInd = 0; cameraInd < cameraCount; ++cameraInd) { |
| 144 | + std::vector<uint8_t> &video = cameraInd == 0 ? video0 : video1; |
| 145 | + if (useImageInput) { |
| 146 | + std::string filePath = stringFormat("%s/frames%zu/%08zu.png", |
| 147 | + inputFolderPath.c_str(), cameraInd + 1, number); |
| 148 | + bool success = readImage(filePath, video, tmpBuffer, data.width, data.height); |
| 149 | + assert(success); |
| 150 | + } |
| 151 | + else { |
| 152 | + bool success = videoInputs.at(cameraInd)->read(video, data.width, data.height); |
| 153 | + assert(success); |
| 154 | + } |
| 155 | + uint8_t *&dataVideo = cameraInd == 0 ? data.video0 : data.video1; |
| 156 | + dataVideo = video.data(); |
| 157 | + } |
| 158 | + } |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | +private: |
| 163 | + std::ifstream jsonlFile; |
| 164 | + std::string line; |
| 165 | + std::shared_ptr<spectacularAI::Vector3d> imu; |
| 166 | + const std::string inputFolderPath; |
| 167 | + std::vector<uint8_t> video0, video1, tmpBuffer; |
| 168 | + bool useImageInput = false; |
| 169 | + std::vector<std::string> videoPaths; |
| 170 | + std::vector<std::unique_ptr<VideoInput>> videoInputs; |
| 171 | +}; |
| 172 | + |
| 173 | +class InputMock : public Input { |
| 174 | +public: |
| 175 | + int n = 0; |
| 176 | + const int width; |
| 177 | + const int height; |
| 178 | + std::vector<uint8_t> video0, video1; |
| 179 | + std::shared_ptr<spectacularAI::Vector3d> imu; |
| 180 | + |
| 181 | + InputMock() : width(640), height(480), |
| 182 | + video0(width * height), video1(width * height) |
| 183 | + { |
| 184 | + imu = std::make_shared<spectacularAI::Vector3d>(spectacularAI::Vector3d{ 0.0, 0.0, 0.0 }); |
| 185 | + printf("Using mock input, VIO may not output anything.\n"); |
| 186 | + } |
| 187 | + |
| 188 | + bool next(Data &data) final { |
| 189 | + const size_t ITERATIONS = 100; |
| 190 | + data.video0 = video0.data(); |
| 191 | + data.video1 = video1.data(); |
| 192 | + data.width = width; |
| 193 | + data.height = height; |
| 194 | + data.accelerometer = imu; |
| 195 | + data.gyroscope = imu; |
| 196 | + data.timestamp += 0.1; |
| 197 | + return n++ < ITERATIONS; |
| 198 | + } |
| 199 | + |
| 200 | + std::string getConfig() const final { |
| 201 | + return ""; |
| 202 | + } |
| 203 | + |
| 204 | + std::string getCalibration() const final { |
| 205 | + return R"({ "cameras": [ |
| 206 | + { "focalLengthX": 1.0, "focalLengthY": 1.0, "model": "pinhole" }, |
| 207 | + { "focalLengthX": 1.0, "focalLengthY": 1.0, "model": "pinhole" } |
| 208 | + ] })"; |
| 209 | + } |
| 210 | +}; |
| 211 | + |
| 212 | +} // anonymous namespace |
| 213 | + |
| 214 | +std::unique_ptr<Input> Input::buildJsonl(const std::string &inputFolderPath) { |
| 215 | + return std::unique_ptr<Input>( |
| 216 | + new InputJsonl(inputFolderPath)); |
| 217 | +} |
| 218 | + |
| 219 | +std::unique_ptr<Input> Input::buildMock() { |
| 220 | + return std::unique_ptr<Input>( |
| 221 | + new InputMock()); |
| 222 | +} |
0 commit comments