Line data Source code
1 : #include "TrainingDataReader.h" 2 : #include "Manager.h" 3 : #include "SimpleLogger.h" 4 : #include "csv_parser.h" 5 : #include "exception/FileReaderException.h" 6 : #include <filesystem> 7 : #include <fstream> 8 : #include <memory> 9 : #include <string> 10 : 11 : // for csv_parser doc, see https://github.com/ashaduri/csv-parser 12 : 13 : constexpr int MAX_ERRORS = 5; 14 : 15 : using namespace sipai; 16 : 17 1 : std::vector<Data> TrainingDataReader::loadTrainingDataPaths() { 18 : const auto &training_data_file = 19 1 : Manager::getInstance().app_params.training_data_file; 20 1 : if (training_data_file.empty()) { 21 0 : throw FileReaderException("empty file path"); 22 : } 23 : 24 : // Create a file stream object using RAII (Resource Acquisition Is 25 : // Initialization) 26 1 : std::ifstream file(training_data_file); 27 1 : if (!file.is_open()) { 28 0 : throw FileReaderException("Failed to open file: " + training_data_file); 29 : } 30 : 31 1 : std::vector<Data> datas; 32 1 : Csv::Parser csvParser; 33 1 : std::vector<std::vector<Csv::CellReference>> cell_refs; 34 1 : std::string line; 35 1 : int lineNumber = 1; 36 1 : int totalErrors = 0; 37 12 : while (std::getline(file, line)) { 38 : try { 39 11 : if (line.empty()) { 40 1 : continue; 41 : } 42 10 : std::string_view str_view(line); 43 10 : csvParser.parseTo2DVector(str_view, cell_refs); 44 10 : if (cell_refs.empty() || cell_refs.size() != 2) { 45 0 : throw FileReaderException("invalid column numbers, at line " + 46 0 : std::to_string(lineNumber)); 47 : } 48 10 : Data data; 49 10 : data.file_input = cell_refs[0][0].getCleanString().value(); 50 10 : data.file_target = cell_refs[1][0].getCleanString().value(); 51 10 : datas.push_back(data); 52 10 : lineNumber++; 53 10 : } catch (Csv::ParseError &ex) { 54 0 : totalErrors++; 55 0 : if (totalErrors < MAX_ERRORS) { 56 0 : SimpleLogger::LOG_ERROR("CSV parsing error at line (", lineNumber, 57 0 : "): ", ex.what()); 58 : } else { 59 0 : throw FileReaderException("Too many parsing errors."); 60 : } 61 0 : } 62 : } 63 2 : return datas; 64 1 : } 65 : 66 2 : std::vector<Data> TrainingDataReader::loadTrainingDataFolder() { 67 : const auto &training_data_folder = 68 2 : Manager::getInstance().app_params.training_data_folder; 69 2 : if (training_data_folder.empty()) { 70 0 : throw FileReaderException("empty folder path"); 71 : } 72 : 73 2 : std::vector<Data> datas; 74 : // Add images paths from the folder 75 2 : for (const auto &entry : 76 42 : std::filesystem::directory_iterator(training_data_folder)) { 77 20 : if (entry.is_regular_file()) { 78 : // Convert the extension to lowercase 79 20 : std::string extension = entry.path().extension().string(); 80 20 : std::transform(extension.begin(), extension.end(), extension.begin(), 81 : ::tolower); 82 : // Check if the file is an image by checking its extension 83 20 : if (valid_extensions.find(extension) != valid_extensions.end()) { 84 20 : Data data; 85 20 : data.file_target = entry.path().string(); 86 20 : datas.push_back(data); 87 20 : } 88 20 : } 89 2 : } 90 2 : return datas; 91 0 : }