You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.3 KiB

#include <boost/filesystem.hpp>
#include <iostream>
#include <string>
#include "math.hpp"
struct path_leaf_string {
std::string operator()(const boost::filesystem::directory_entry& entry) const
{
return entry.path().leaf().string();
}
};
void read_directory(const std::string& name, std::vector<std::string>& v) {
boost::filesystem::path p(name);
boost::filesystem::directory_iterator start(p);
boost::filesystem::directory_iterator end;
std::transform(start, end, std::back_inserter(v), path_leaf_string());
}
void save_as_csv(const math::dataset& dataset, std::string filename) {
// file pointer
std::fstream fout;
// opens an existing csv file or creates a new file.
fout.open(filename, std::ios::out);
std::string name;
// Insert the data to file
for (auto p: dataset) {
for (auto x: p.first) {
fout << x.real() << ','
<< x.imag() << ',';
}
fout << p.second;
fout << '\n';
}
}
math::dataset& load_csv(std::string filename, int size) {
math::dataset* d = new math::dataset();
std::fstream fin;
// Open an existing file
fin.open(filename, std::ios::in);
std::string line, word, temp;
while (fin >> temp) {
std::pair<math::csignal, std::string> p;
std::getline(fin, line);
std::stringstream s(temp);
int count = 0;
while (std::getline(s, word, ',')) {
if (count == size) {
p.second = word;
} else {
double a = atof(word.c_str());
std::getline(s, word, ',');
double b = atof(word.c_str());
p.first.push_back(math::complex(a, b));
}
count++;
}
d->push_back(p);
}
return *d;
}
math::dataset get_data(std::string path, int size, int cmax, int threshold) {
math::dataset res;
std::vector<std::string> dirs;
read_directory(path, dirs);
for (auto dir: dirs) {
std::vector<std::string> files;
read_directory(path+"/"+dir, files);
std::string label = dir;
int count = 0;
for (int i=0; count<size/4 && i<files.size(); ++i) {
try {
math::csignal d = math::img2desc(path+"/"+dir+"/"+files[i], cmax, threshold);
res.push_back({d, label});
count++;
} catch (std::length_error& e) {
std::cout << "No contour: image skiped." << std::endl;
}
}
std::cout << res.size() << std::endl;
}
return res;
}