FileAnnotationWriter::FileAnnotationWriter(const ssi_char_t *filename, const ssi_char_t *label, const ssi_char_t *tier) : _file(0), _label(0), _tier(0), _meta(0), _time(0), _duration(0), _filename(0) { if (filename) { FilePath fp(filename); if (ssi_strcmp(fp.getExtension(), ".csv")) { _filename = ssi_strcpy(filename); } else { _filename = ssi_strcat(filename, ".csv"); } _file = fopen(_filename, "w"); if (_file == 0) { ssi_err("could not open file %s", _filename); return; } } else { _file = stdout; } if (label) { _label = ssi_strcpy(label); } if (tier) { _tier = ssi_strcpy(tier); } _meta = new ssi_char_t[2056]; }
bool Chain::parse (const ssi_char_t *filepath) { release (); if (filepath == 0 || filepath[0] == '\0') { ssi_wrn ("file is empty"); return false; } FilePath fp (filepath); ssi_char_t *filepath_with_ext = 0; if (strcmp (fp.getExtension (), SSI_FILE_TYPE_CHAIN) != 0) { filepath_with_ext = ssi_strcat (filepath, SSI_FILE_TYPE_CHAIN); } else { filepath_with_ext = ssi_strcpy (filepath); } if (!ssi_exists (filepath_with_ext)) { ssi_wrn ("file not found '%s", filepath_with_ext); return false; } ssi_msg (SSI_LOG_LEVEL_BASIC, "load '%s'", filepath_with_ext); TiXmlDocument doc; if (!doc.LoadFile (filepath_with_ext)) { ssi_wrn ("failed loading chain from file '%s'", filepath_with_ext); delete[] filepath_with_ext; return false; } TiXmlElement *body = doc.FirstChildElement(); if (!body || strcmp (body->Value (), "chain") != 0) { ssi_wrn ("tag <chain> missing"); delete[] filepath_with_ext; return false; } TiXmlElement *filter = body->FirstChildElement ("filter"); if (filter) { if (!parseFilter (filter)) { ssi_wrn ("failed parsing <filter> tag"); return false; } } TiXmlElement *feature = body->FirstChildElement ("feature"); if (feature) { if (!parseFeature (feature)) { ssi_wrn ("failed parsing <feature> tag"); return false; } } _parsed = feature || filter; if (!_parsed) { ssi_wrn ("parsing failed because no feature/filter were loaded"); } return _parsed; }
ssi_msg (SSI_LOG_LEVEL_BASIC, "start 'path=%s'", _options.path); if (ssi_log_level >= SSI_LOG_LEVEL_BASIC) { ssi_print (" sample rate\t= %.2lf Hz\n\ sample dim\t\t= %u\n\ sample bytes\t= %u\n\ sample number\t= %u\n\ stream length\t= %.2fs\n", _stream.sr, _stream.dim, _stream.byte, _sample_number_total, _sample_number_total/_stream.sr); } // set thread name ssi_char_t *thread_name = ssi_strcat ("ssi_sensor_FileReader@", _options.path); Thread::setName (thread_name); delete[] thread_name; return true; } void FileReader::run () { if (_stopped) { ::Sleep (100); return; } if (_is_providing) { if (_file_stream_in.read (_stream) == FileStreamIn::READ_ERROR) {
bool FileSamplesOut::open (ISamples &data, const ssi_char_t *path, File::TYPE type, File::VERSION version) { ssi_msg (SSI_LOG_LEVEL_DETAIL, "open files '%s'", path); _version = version; if (_version < File::V2) { ssi_wrn ("version < V2 not supported"); return false; } if (_file_info || _file_data) { ssi_wrn ("samples already open"); return false; } _n_users = data.getUserSize (); _users = new ssi_char_t *[_n_users]; _n_per_user = new ssi_size_t[_n_users]; for (ssi_size_t i = 0; i < _n_users; i++) { _users[i] = ssi_strcpy (data.getUserName (i)); _n_per_user[i] = 0; } _n_classes = data.getClassSize (); _classes = new ssi_char_t *[_n_classes]; _n_per_class = new ssi_size_t[_n_classes]; for (ssi_size_t i = 0; i < _n_classes; i++) { _classes[i] = ssi_strcpy (data.getClassName (i)); _n_per_class[i] = 0; } _n_streams = data.getStreamSize (); _streams = new ssi_stream_t[_n_streams]; for (ssi_size_t i = 0; i < _n_streams; i++) { ssi_stream_t s = data.getStream (i); ssi_stream_init (_streams[i], 0, s.dim, s.byte, s.type, s.sr, 0); } _has_missing_data = false; if (path == 0 || path[0] == '\0') { _console = true; } if (_console) { _file_data = File::CreateAndOpen (type, File::WRITE, ""); if (!_file_data) { ssi_wrn ("could not open console"); return false; } } else { FilePath fp (path); ssi_char_t *path_info = 0; if (strcmp (fp.getExtension (), SSI_FILE_TYPE_SAMPLES) != 0) { path_info = ssi_strcat (path, SSI_FILE_TYPE_SAMPLES); } else { path_info = ssi_strcpy (path); } _path = ssi_strcpy (path_info); _file_info = File::CreateAndOpen (File::ASCII, File::WRITE, path_info); if (!_file_info) { ssi_wrn ("could not open info file '%s'", path_info); return false; } ssi_sprint (_string, "<?xml version=\"1.0\" ?>\n<samples ssi-v=\"%d\">", version); _file_info->writeLine (_string); ssi_char_t *path_data = ssi_strcat (path_info, "~"); _file_data = File::CreateAndOpen (type, File::WRITE, path_data); if (!_file_data) { ssi_wrn ("could not open data file '%s'", path_data); return false; } if (_version == File::V3) { _file_streams = new FileStreamOut[_n_streams]; ssi_char_t string[SSI_MAX_CHAR]; for (ssi_size_t i = 0; i < _n_streams; i++) { ssi_sprint (string, "%s.#%u", path_info, i); _file_streams[i].open (_streams[i], string, type); } } delete[] path_info; delete[] path_data; } return true; };
bool ISNorm::LoadParams(const ssi_char_t *path, Params ¶ms) { FilePath fp(path); ssi_char_t *path_xml; ssi_char_t *path_data; if (ssi_strcmp(fp.getExtension(), ".norm", false)) { path_xml = ssi_strcpy(path); } else { path_xml = ssi_strcat(path, ".norm"); } path_data = ssi_strcat(path_xml, "~"); TiXmlDocument doc; if (!doc.LoadFile(path_xml)) { ssi_wrn("failed loading trainer from file '%s' (r:%d,c:%d)", path_xml, doc.ErrorRow(), doc.ErrorCol()); return false; } TiXmlElement *body = doc.FirstChildElement(); if (!body || strcmp(body->Value(), "norm") != 0) { ssi_wrn("tag <norm> missing"); return false; } TiXmlElement *element = 0; element = body->FirstChildElement("method"); const ssi_char_t *method_s = element->GetText(); bool found = false; METHOD::List method; for (ssi_size_t i = 0; i < METHOD::NUM; i++) { if (ssi_strcmp(method_s, METHOD_NAMES[i], false)) { method = (METHOD::List) i; found = true; } } if (!found) { ssi_wrn("unkown method '%s'", method_s); return false; } ISNorm::ZeroParams(params, method); element = body->FirstChildElement("dim"); const ssi_char_t *dim_s = element->GetText(); ssi_size_t dim; if (sscanf(dim_s, "%u", &dim) != 1) { ssi_wrn("could not parse dimension '%s'", dim_s); return false; } ISNorm::InitParams(params, dim); if (method == METHOD::SCALE) { element = body->FirstChildElement("limits"); const ssi_char_t *limits_s = element->GetText(); if (sscanf(limits_s, "%f %f", params.limits, params.limits + 1) != 2) { ssi_wrn("could not parse limits '%s'", limits_s); return false; } } element = body->FirstChildElement("type"); const ssi_char_t *type_s = element->GetText(); found = false; File::TYPE type; if (ssi_strcmp(type_s, File::TYPE_NAMES[File::ASCII], false)) { type = File::ASCII; found = true; } else if (ssi_strcmp(type_s, File::TYPE_NAMES[File::BINARY], false)) { type = File::BINARY; found = true; } if (!found) { ssi_wrn("unkown type '%s'", type_s); return false; } if (method != METHOD::NONE) { FILE *fp = fopen(path_data, type == File::BINARY ? "rb" : "r"); if (fp) { switch (params.method) { case METHOD::NONE: break; case METHOD::SCALE: if (type == File::BINARY) { fread(params.mins, params.n_features, sizeof(ssi_real_t), fp); fread(params.maxs, params.n_features, sizeof(ssi_real_t), fp); } else { ssi_real_t *mins = params.mins; ssi_real_t *maxs = params.maxs; for (ssi_size_t i = 0; i < params.n_features; i++) { fscanf(fp, "%f %f\n", mins++, maxs++); } } break; case METHOD::ZSCORE: if (type == File::BINARY) { fread(params.mean, params.n_features, sizeof(ssi_real_t), fp); fread(params.stdv, params.n_features, sizeof(ssi_real_t), fp); } else { ssi_real_t *mean = params.mean; ssi_real_t *stdv = params.stdv; for (ssi_size_t i = 0; i < params.n_features; i++) { fscanf(fp, "%f %f\n", mean++, stdv++); } } break; } fclose(fp); } else { ssi_wrn("could not open file '%s'", path_data); return false; } } delete[] path_xml; delete[] path_data; return true; }
bool ISNorm::SaveParams(const ssi_char_t *path, Params ¶ms, File::TYPE type) { ssi_char_t string[SSI_MAX_CHAR]; FilePath fp(path); ssi_char_t *path_xml; ssi_char_t *path_data; if (ssi_strcmp(fp.getExtension(), "norm", false)) { path_xml = ssi_strcpy(path); } else { path_xml = ssi_strcat(path, ".norm"); } path_data = ssi_strcat(path_xml, "~"); TiXmlElement norm("norm"); TiXmlElement norm_method("method"); norm_method.InsertEndChild(TiXmlText(METHOD_NAMES[params.method])); norm.InsertEndChild(norm_method); if (params.method == METHOD::SCALE) { TiXmlElement norm_limits("limits"); ssi_sprint(string, "%f %f", params.limits[0], params.limits[1]); norm_limits.InsertEndChild(TiXmlText(string)); norm.InsertEndChild(norm_limits); } TiXmlElement norm_dim("dim"); ssi_sprint(string, "%u", params.n_features); norm_dim.InsertEndChild(TiXmlText(string)); norm.InsertEndChild(norm_dim); TiXmlElement norm_type("type"); norm_type.InsertEndChild(TiXmlText(File::TYPE_NAMES[type])); norm.InsertEndChild(norm_type); TiXmlDocument doc; TiXmlDeclaration head("1.0", "", ""); doc.InsertEndChild(head); doc.InsertEndChild(norm); doc.SaveFile(path_xml); if (params.method != METHOD::NONE) { FILE *fp = fopen(path_data, type == File::BINARY ? "wb" : "w"); if (fp) { switch (params.method) { case METHOD::SCALE: if (type == File::BINARY) { fwrite(params.mins, params.n_features, sizeof(ssi_real_t), fp); fwrite(params.maxs, params.n_features, sizeof(ssi_real_t), fp); } else { ssi_real_t *mins = params.mins; ssi_real_t *maxs = params.maxs; for (ssi_size_t i = 0; i < params.n_features; i++) { ssi_fprint(fp, "%f %f\n", *mins++, *maxs++); } } break; case METHOD::ZSCORE: if (type == File::BINARY) { fwrite(params.mean, params.n_features, sizeof(ssi_real_t), fp); fwrite(params.stdv, params.n_features, sizeof(ssi_real_t), fp); } else { ssi_real_t *mean = params.mean; ssi_real_t *stdv = params.stdv; for (ssi_size_t i = 0; i < params.n_features; i++) { ssi_fprint(fp, "%f %f\n", *mean++, *stdv++); } } break; } } else { ssi_wrn("could not open file '%s'", path_data); return false; } fclose(fp); } delete[] path_data; delete[] path_xml; return true; }
bool FileStreamIn::open (ssi_stream_t &data, const ssi_char_t *path, ssi_size_t &n_meta, void **meta) { ssi_msg (SSI_LOG_LEVEL_DETAIL, "open stream file '%s'", path); if (_file_info || _file_data) { ssi_wrn ("stream already open"); return false; } if (path == 0 || path[0] == '\0') { ssi_wrn ("'%s' is not a valid path", path); return false; } FilePath fp (path); ssi_char_t *path_info = 0; if (strcmp (fp.getExtension (), SSI_FILE_TYPE_STREAM) != 0) { path_info = ssi_strcat (path, SSI_FILE_TYPE_STREAM); } else { path_info = ssi_strcpy (path); } _path = ssi_strcpy (path_info); _file_info = File::CreateAndOpen (File::ASCII, File::READ, path_info); if (!_file_info) { ssi_wrn ("could not open info file '%s'", path_info); return false; } TiXmlDocument doc; if (!doc.LoadFile (_file_info->getFile ())) { ssi_wrn ("failed loading stream from file '%s'", path_info); return false; } TiXmlElement *body = doc.FirstChildElement(); if (!body || strcmp (body->Value (), "stream") != 0) { ssi_wrn ("tag <stream> missing"); return false; } int v = 0; if (body->QueryIntAttribute ("ssi-v", &v) != TIXML_SUCCESS) { ssi_wrn ("attribute <ssi-v> in tag <stream> missing"); return false; } _version = ssi_cast (File::VERSION, v); if (_version < File::V2) { ssi_wrn ("version < V2 not supported"); return false; } TiXmlElement *element = body->FirstChildElement ("info"); if (!element || strcmp (element->Value (), "info") != 0) { ssi_wrn ("tag <info> missing"); return false; } ssi_time_t sample_rate = 0; if (element->QueryDoubleAttribute ("sr", &sample_rate) != TIXML_SUCCESS) { ssi_wrn ("attribute <sr> missing in tag <info>"); return false; } int sample_dimension = 0; if (element->QueryIntAttribute ("dim", &sample_dimension) != TIXML_SUCCESS) { ssi_wrn ("attribute <dim> missing in tag <info>"); return false; } int sample_byte = 0; if (element->QueryIntAttribute ("byte", &sample_byte) != TIXML_SUCCESS) { ssi_wrn ("attribute <byte> missing in tag <info>"); return false; } const char *type = element->Attribute ("type"); if (!type) { ssi_wrn ("attribute <type> missing in tag <info>"); return false; } ssi_type_t sample_type; if (!ssi_name2type (type, sample_type)) { ssi_wrn ("could not parse type from '%s'", type); return false; } const char *ftype_name = element->Attribute ("ftype"); if (!ftype_name) { ssi_wrn ("attribute <ftype> missing in tag <info>"); return false; } File::TYPE ftype; if (strcmp (ftype_name, File::TYPE_NAMES[0]) == 0) { ftype = File::BINARY; } else if (strcmp (ftype_name, File::TYPE_NAMES[1]) == 0) { ftype = File::ASCII; } else { ssi_wrn ("attribute <ftype> has invalid value '%s' in tag <info>", ftype_name); return false; } *meta = 0; n_meta = 0; element = body->FirstChildElement ("meta"); if (element && strcmp (element->Value (), "meta") == 0) { switch (sample_type) { case SSI_IMAGE: { n_meta = sizeof (ssi_video_params_t); ssi_video_params_t *params = new ssi_video_params_t (); *meta = params; if (element->QueryIntAttribute ("width", ¶ms->widthInPixels) != TIXML_SUCCESS) { ssi_wrn ("attribute <width> missing in tag <meta>"); return false; } if (element->QueryIntAttribute ("height", ¶ms->heightInPixels) != TIXML_SUCCESS) { ssi_wrn ("attribute <height> missing in tag <meta>"); return false; } if (element->QueryIntAttribute ("depth", ¶ms->depthInBitsPerChannel) != TIXML_SUCCESS) { ssi_wrn ("attribute <depth> missing in tag <meta>"); return false; } if (element->QueryIntAttribute ("channels", ¶ms->numOfChannels) != TIXML_SUCCESS) { ssi_wrn ("attribute <channels> missing in tag <meta>"); return false; } int flip = 0; if (element->QueryIntAttribute ("channels", &flip) != TIXML_SUCCESS) { ssi_wrn ("attribute <flip> missing in tag <meta>"); } params->flipImage = flip != 0; break; } default: ssi_wrn ("type '%s' doesn't support meta information", SSI_TYPE_NAMES[sample_type]); break; } } ssi_char_t *path_data = ssi_strcat (path_info, "~"); _file_data = File::CreateAndOpen (ftype, File::READ, path_data); if (!_file_data) { ssi_wrn ("could not open data file '%s'", path_data); return false; } ssi_stream_init (data, 0, sample_dimension, sample_byte, sample_type, sample_rate, 0); ssi_stream_init (_stream, 0, sample_dimension, sample_byte, sample_type, sample_rate, 0); // count chunks _n_chunks = 0; _next_chunk = 0; element = body->FirstChildElement ("chunk"); if (element) { do { _n_chunks++; element = element->NextSiblingElement ("chunk"); } while (element); } // read in chunks _n_samples = 0; _samples = new ssi_size_t[_n_chunks]; _time = new ssi_time_t[_n_chunks]; _bytes = new ssi_size_t[_n_chunks]; element = body->FirstChildElement ("chunk"); ssi_size_t n = 0; if (element) { do { ssi_time_t time = 0; if (element->QueryDoubleAttribute ("from", &time) != TIXML_SUCCESS) { ssi_wrn ("attribute <time> missing in tag <chunk>"); return false; } _time[n] = time; int bytes = 0; if (element->QueryIntAttribute ("byte", &bytes) != TIXML_SUCCESS) { ssi_wrn ("attribute <bytes> missing in tag <chunk>"); return false; } _bytes[n] = ssi_cast (ssi_size_t, bytes); int samples = 0; if (element->QueryIntAttribute ("num", &samples) != TIXML_SUCCESS) { ssi_wrn ("attribute <time> missing in tag <chunk>"); return false; } _n_samples += ssi_cast (ssi_size_t, samples); _samples[n++] = ssi_cast (ssi_size_t, samples); element = element->NextSiblingElement ("chunk"); } while (element); } delete[] path_info; delete[] path_data; return true; };