7 changed files with 133 additions and 54 deletions
@ -0,0 +1,28 @@ |
|||||
|
#include <math.hpp> |
||||
|
#include <fstream> |
||||
|
#include <iostream> |
||||
|
#include <file.hpp> |
||||
|
#include <math.hpp> |
||||
|
|
||||
|
int main(int argc, char** argv) { |
||||
|
math::complex c1 = {0.1, 0.2}; |
||||
|
math::complex c2 = {0.3, 0.4}; |
||||
|
math::complex c3 = {0.5, 0.6}; |
||||
|
math::complex c4 = {0.7, 0.8}; |
||||
|
math::csignal s1 = {c1, c2}; |
||||
|
math::csignal s2 = {c3, c4}; |
||||
|
std::pair<math::csignal, std::string> p1 = {s1, "label1"}; |
||||
|
std::pair<math::csignal, std::string> p2 = {s2, "label2"}; |
||||
|
math::dataset d = {p1, p2}; |
||||
|
save_as_csv(d, "test.csv"); |
||||
|
|
||||
|
math::dataset d2 = load_csv("test.csv", s1.size()); |
||||
|
|
||||
|
for (auto p: d2) { |
||||
|
for (auto x: p.first) { |
||||
|
std::cout << x << " "; |
||||
|
} |
||||
|
std::cout << p.second << std::endl; |
||||
|
} |
||||
|
return 0; |
||||
|
} |
||||
@ -1,10 +1,8 @@ |
|||||
add_executable(traitement traitement.cpp) |
add_executable(traitement traitement.cpp) |
||||
target_link_libraries(traitement ${OpenCV_LIBS}) |
target_link_libraries(traitement ${OpenCV_LIBS}) |
||||
|
|
||||
find_package(Boost COMPONENTS system filesystem REQUIRED) |
|
||||
|
|
||||
add_executable(knn knn.cpp) |
add_executable(knn knn.cpp) |
||||
target_link_libraries(knn ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) |
target_link_libraries(knn ${OpenCV_LIBS} ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY}) |
||||
|
|
||||
add_executable(neural_network neural_network.cpp) |
add_executable(neural_network neural_network.cpp) |
||||
target_link_libraries(neural_network ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) |
target_link_libraries(neural_network ${OpenCV_LIBS} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}) |
||||
|
|||||
@ -0,0 +1,93 @@ |
|||||
|
#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; |
||||
|
} |
||||
Loading…
Reference in new issue