Factor out jpeg list image dataset loading for use in calibration.
Change-Id: Ie546a9458699fd80cb4359c703b8cacc32e2e52a
diff --git a/aos/vision/image/image_dataset.cc b/aos/vision/image/image_dataset.cc
new file mode 100644
index 0000000..cd3ec46
--- /dev/null
+++ b/aos/vision/image/image_dataset.cc
@@ -0,0 +1,84 @@
+#include "aos/vision/image/image_dataset.h"
+
+#include <fstream>
+
+#include "aos/vision/image/image_types.h"
+
+namespace aos {
+namespace vision {
+
+namespace {
+std::string GetFileContents(const std::string &filename) {
+ std::ifstream in(filename, std::ios::in | std::ios::binary);
+ if (in) {
+ std::string contents;
+ in.seekg(0, std::ios::end);
+ contents.resize(in.tellg());
+ in.seekg(0, std::ios::beg);
+ in.read(&contents[0], contents.size());
+ in.close();
+ return (contents);
+ }
+ fprintf(stderr, "Could not read file: %s\n", filename.c_str());
+ exit(-1);
+}
+
+std::vector<std::string> Split(DataRef inp, char delim) {
+ size_t i = 0;
+ std::vector<size_t> pos;
+ while (i < inp.size()) {
+ i = inp.find(delim, i);
+ if (i == std::string::npos) break;
+ // fprintf(stderr, "k=%d, i=%d\n", k, (int)i);
+ pos.emplace_back(i);
+ i = i + 1;
+ }
+ std::vector<std::string> res;
+ res.reserve(pos.size() + 1);
+ i = 0;
+ for (auto p : pos) {
+ res.emplace_back(inp.substr(i, p - i).to_string());
+ i = p + 1;
+ }
+ res.emplace_back(inp.substr(i).to_string());
+ return res;
+}
+} // namespace
+
+std::vector<DatasetFrame> LoadDataset(const std::string &jpeg_list_filename) {
+ std::vector<DatasetFrame> images;
+ auto contents = GetFileContents(jpeg_list_filename);
+
+ std::string basename;
+ auto it = jpeg_list_filename.find_last_of('/');
+ if (it != std::string::npos) {
+ basename = jpeg_list_filename.substr(0, it + 1);
+ }
+
+ for (const auto &jpeg_filename : Split(contents, '\n')) {
+ [&]() {
+ if (jpeg_filename.empty()) return;
+ for (std::size_t i = 0; i < jpeg_filename.size(); ++i) {
+ if (jpeg_filename[i] == '#') return;
+ if (jpeg_filename[i] != ' ') break;
+ }
+ bool is_jpeg = true;
+ size_t l = jpeg_filename.size();
+ if (l > 4 && jpeg_filename[l - 1] == 'v') {
+ is_jpeg = false;
+ }
+ if (jpeg_filename[0] == '/') {
+ images.emplace_back(
+ DatasetFrame{is_jpeg, GetFileContents(jpeg_filename)});
+ } else {
+ images.emplace_back(
+ DatasetFrame{is_jpeg, GetFileContents(basename + jpeg_filename)});
+ }
+ }();
+ }
+
+ return images;
+}
+
+} // namespace vision
+} // namespace aos