Parker Schuh | f2a3493 | 2019-02-16 20:39:19 -0800 | [diff] [blame] | 1 | #include "aos/vision/image/image_dataset.h" |
| 2 | |
| 3 | #include <fstream> |
| 4 | |
| 5 | #include "aos/vision/image/image_types.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | namespace vision { |
| 9 | |
| 10 | namespace { |
| 11 | std::string GetFileContents(const std::string &filename) { |
| 12 | std::ifstream in(filename, std::ios::in | std::ios::binary); |
| 13 | if (in) { |
| 14 | std::string contents; |
| 15 | in.seekg(0, std::ios::end); |
| 16 | contents.resize(in.tellg()); |
| 17 | in.seekg(0, std::ios::beg); |
| 18 | in.read(&contents[0], contents.size()); |
| 19 | in.close(); |
| 20 | return (contents); |
| 21 | } |
| 22 | fprintf(stderr, "Could not read file: %s\n", filename.c_str()); |
| 23 | exit(-1); |
| 24 | } |
| 25 | |
| 26 | std::vector<std::string> Split(DataRef inp, char delim) { |
| 27 | size_t i = 0; |
| 28 | std::vector<size_t> pos; |
| 29 | while (i < inp.size()) { |
| 30 | i = inp.find(delim, i); |
| 31 | if (i == std::string::npos) break; |
| 32 | // fprintf(stderr, "k=%d, i=%d\n", k, (int)i); |
| 33 | pos.emplace_back(i); |
| 34 | i = i + 1; |
| 35 | } |
| 36 | std::vector<std::string> res; |
| 37 | res.reserve(pos.size() + 1); |
| 38 | i = 0; |
| 39 | for (auto p : pos) { |
| 40 | res.emplace_back(inp.substr(i, p - i).to_string()); |
| 41 | i = p + 1; |
| 42 | } |
| 43 | res.emplace_back(inp.substr(i).to_string()); |
| 44 | return res; |
| 45 | } |
| 46 | } // namespace |
| 47 | |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame^] | 48 | DatasetFrame LoadFile(const std::string &jpeg_filename) { |
| 49 | bool is_jpeg = true; |
| 50 | size_t l = jpeg_filename.size(); |
| 51 | if (l > 4 && jpeg_filename[l - 1] == 'v') { |
| 52 | is_jpeg = false; |
| 53 | } |
| 54 | return DatasetFrame{is_jpeg, GetFileContents(jpeg_filename)}; |
| 55 | } |
| 56 | |
Parker Schuh | f2a3493 | 2019-02-16 20:39:19 -0800 | [diff] [blame] | 57 | std::vector<DatasetFrame> LoadDataset(const std::string &jpeg_list_filename) { |
| 58 | std::vector<DatasetFrame> images; |
| 59 | auto contents = GetFileContents(jpeg_list_filename); |
| 60 | |
| 61 | std::string basename; |
| 62 | auto it = jpeg_list_filename.find_last_of('/'); |
| 63 | if (it != std::string::npos) { |
| 64 | basename = jpeg_list_filename.substr(0, it + 1); |
| 65 | } |
| 66 | |
| 67 | for (const auto &jpeg_filename : Split(contents, '\n')) { |
| 68 | [&]() { |
| 69 | if (jpeg_filename.empty()) return; |
| 70 | for (std::size_t i = 0; i < jpeg_filename.size(); ++i) { |
| 71 | if (jpeg_filename[i] == '#') return; |
| 72 | if (jpeg_filename[i] != ' ') break; |
| 73 | } |
Parker Schuh | f2a3493 | 2019-02-16 20:39:19 -0800 | [diff] [blame] | 74 | if (jpeg_filename[0] == '/') { |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame^] | 75 | images.emplace_back(LoadFile(jpeg_filename)); |
Parker Schuh | f2a3493 | 2019-02-16 20:39:19 -0800 | [diff] [blame] | 76 | } else { |
Parker Schuh | 9e1d169 | 2019-02-24 14:34:04 -0800 | [diff] [blame^] | 77 | images.emplace_back(LoadFile(basename + jpeg_filename)); |
Parker Schuh | f2a3493 | 2019-02-16 20:39:19 -0800 | [diff] [blame] | 78 | } |
| 79 | }(); |
| 80 | } |
| 81 | |
| 82 | return images; |
| 83 | } |
| 84 | |
| 85 | } // namespace vision |
| 86 | } // namespace aos |