MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / parse_image

Method parse_image

lite/load_and_run/src/helpers/data_parser.cpp:110–149  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108}
109
110void DataParser::parse_image(const std::string& name, const std::string& path) {
111 // load binary ppm/pgm
112 std::ifstream fin;
113 fin.open(path, std::ifstream::binary | std::ifstream::in);
114 mgb_assert(fin.is_open(), "open file %s failed for --input", path.c_str());
115
116 size_t w = 0, h = 0, channel = 0;
117 char buf[128] = {0};
118
119 fin.getline(buf, 128);
120 if ('5' == buf[1]) {
121 channel = 1;
122 } else if ('6' == buf[1]) {
123 channel = 3;
124 } else {
125 mgb_assert(0, "not a formal ppm/pgm");
126 }
127
128 while (fin.getline(buf, 128)) {
129 if (buf[0] == '#') {
130 continue;
131 }
132 break;
133 }
134 std::stringstream ss;
135 ss << std::string(buf);
136 ss >> w;
137 ss >> h;
138
139 mgb_assert(w > 0 and h > 0);
140
141 mgb::HostTensorND hv;
142 hv.comp_node(mgb::CompNode::default_cpu(), true)
143 .dtype(mgb::dtype::Uint8())
144 .resize({1, h, w, channel});
145
146 fin.read((char*)(hv.raw_ptr()), hv.layout().total_nr_elems());
147 fin.close();
148 inputs.insert(std::make_pair(name, std::move(hv)));
149}
150
151void DataParser::parse_npy(const std::string& name, const std::string& path) {
152 std::string type_str;

Callers

nothing calls this directly

Calls 10

openMethod · 0.80
resizeMethod · 0.80
dtypeMethod · 0.45
comp_nodeMethod · 0.45
readMethod · 0.45
raw_ptrMethod · 0.45
total_nr_elemsMethod · 0.45
layoutMethod · 0.45
closeMethod · 0.45
insertMethod · 0.45

Tested by

no test coverage detected