blob: 82a88862233156ccf4919ec83046ecdcc1f4e4d8 [file] [log] [blame]
Parker Schuhf2a34932019-02-16 20:39:19 -08001#include "aos/vision/image/image_dataset.h"
2
3#include <fstream>
4
5#include "aos/vision/image/image_types.h"
6
Stephan Pleinesf63bde82024-01-13 15:59:33 -08007namespace aos::vision {
Parker Schuhf2a34932019-02-16 20:39:19 -08008
9namespace {
10std::string GetFileContents(const std::string &filename) {
11 std::ifstream in(filename, std::ios::in | std::ios::binary);
12 if (in) {
13 std::string contents;
14 in.seekg(0, std::ios::end);
15 contents.resize(in.tellg());
16 in.seekg(0, std::ios::beg);
17 in.read(&contents[0], contents.size());
18 in.close();
19 return (contents);
20 }
21 fprintf(stderr, "Could not read file: %s\n", filename.c_str());
22 exit(-1);
23}
24
25std::vector<std::string> Split(DataRef inp, char delim) {
26 size_t i = 0;
27 std::vector<size_t> pos;
28 while (i < inp.size()) {
29 i = inp.find(delim, i);
30 if (i == std::string::npos) break;
31 // fprintf(stderr, "k=%d, i=%d\n", k, (int)i);
32 pos.emplace_back(i);
33 i = i + 1;
34 }
35 std::vector<std::string> res;
36 res.reserve(pos.size() + 1);
37 i = 0;
38 for (auto p : pos) {
James Kuszmaul3ae42262019-11-08 12:33:41 -080039 res.emplace_back(inp.substr(i, p - i));
Parker Schuhf2a34932019-02-16 20:39:19 -080040 i = p + 1;
41 }
James Kuszmaul3ae42262019-11-08 12:33:41 -080042 res.emplace_back(inp.substr(i));
Parker Schuhf2a34932019-02-16 20:39:19 -080043 return res;
44}
45} // namespace
46
Parker Schuh9e1d1692019-02-24 14:34:04 -080047DatasetFrame LoadFile(const std::string &jpeg_filename) {
48 bool is_jpeg = true;
49 size_t l = jpeg_filename.size();
50 if (l > 4 && jpeg_filename[l - 1] == 'v') {
51 is_jpeg = false;
52 }
53 return DatasetFrame{is_jpeg, GetFileContents(jpeg_filename)};
54}
55
Parker Schuhf2a34932019-02-16 20:39:19 -080056std::vector<DatasetFrame> LoadDataset(const std::string &jpeg_list_filename) {
57 std::vector<DatasetFrame> images;
58 auto contents = GetFileContents(jpeg_list_filename);
59
60 std::string basename;
61 auto it = jpeg_list_filename.find_last_of('/');
62 if (it != std::string::npos) {
63 basename = jpeg_list_filename.substr(0, it + 1);
64 }
65
66 for (const auto &jpeg_filename : Split(contents, '\n')) {
67 [&]() {
68 if (jpeg_filename.empty()) return;
69 for (std::size_t i = 0; i < jpeg_filename.size(); ++i) {
70 if (jpeg_filename[i] == '#') return;
71 if (jpeg_filename[i] != ' ') break;
72 }
Parker Schuhf2a34932019-02-16 20:39:19 -080073 if (jpeg_filename[0] == '/') {
Parker Schuh9e1d1692019-02-24 14:34:04 -080074 images.emplace_back(LoadFile(jpeg_filename));
Parker Schuhf2a34932019-02-16 20:39:19 -080075 } else {
Parker Schuh9e1d1692019-02-24 14:34:04 -080076 images.emplace_back(LoadFile(basename + jpeg_filename));
Parker Schuhf2a34932019-02-16 20:39:19 -080077 }
78 }();
79 }
80
81 return images;
82}
83
Stephan Pleinesf63bde82024-01-13 15:59:33 -080084} // namespace aos::vision