intg pascal_dataset<Tdata>::count_samples() { total_difficult = 0; total_truncated = 0; total_occluded = 0; total_ignored = 0; total_samples = 0; std::string xmlpath; boost::filesystem::path p(annroot); if (!boost::filesystem::exists(p)) eblthrow("Annotation path " << annroot << " does not exist."); std::cout << "Counting number of samples in " << annroot << " ..." << std::endl; // find all xml files recursively std::list<std::string> *files = find_fullfiles(annroot, XML_PATTERN, NULL, false, true); if (!files || files->size() == 0) eblthrow("no xml files found in " << annroot << " using file pattern " << XML_PATTERN); std::cout << "Found " << files->size() << " xml files." << std::endl; for (std::list<std::string>::iterator i = files->begin(); i != files->end(); ++i) { xmlpath = *i; // parse xml DomParser parser; parser.parse_file(xmlpath); if (parser) { // initialize root node and list const Node* pNode = parser.get_document()->get_root_node(); Node::NodeList list = pNode->get_children(); // parse all objects in image for(Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) { if (!strcmp((*iter)->get_name().c_str(), "object")) { // check for difficult flag in object node Node::NodeList olist = (*iter)->get_children(); count_sample(olist); } } } } if (files) delete files; std::cout << "Found: " << total_samples << " samples, including "; std::cout << total_difficult << " difficult, " << total_truncated; std::cout << " truncated and " << total_occluded << " occluded." << std::endl; ignore_difficult ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " difficult samples." << std::endl; ignore_truncated ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " truncated samples." << std::endl; ignore_occluded ? std::cout << "Ignoring" : std::cout << "Using"; std::cout << " occluded samples." << std::endl; total_samples = total_samples - total_ignored; return total_samples; }
idx<T> matlab::load_matrix(const char *name) { if (!fp) eblthrow("null file pointer"); #ifdef __MATLAB__ // get variable corresponding to name mxArray *var = matGetVariable(fp, name); if (!var) eblthrow("variable \"" << name << "\" not found in matlab object"); int ndims = mxGetNumberOfDimensions(var); if (ndims > MAXDIMS) eblthrow("cannot load matrix with " << ndims << " dimensions, libidx was compiled to support " << MAXDIMS << " at most. Modify MAXDIMS and recompile."); intg nelements = mxGetNumberOfElements(var); const mwSize *dims = mxGetDimensions(var); // allocate matrix idxdim d; for (uint i = 0; i < ndims; ++i) d.insert_dim(i, dims[i]); idx<T> m(d); // load data mxClassID type = mxGetClassID(var); switch (type) { case mxCELL_CLASS: eblthrow("cannot load matrix from type cell"); break ; case mxSTRUCT_CLASS: eblthrow("cannot load matrix from type struct"); break ; case mxLOGICAL_CLASS: eblthrow("cannot load matrix from type logical"); break ; case mxFUNCTION_CLASS: eblthrow("cannot load matrix from type function"); break ; case mxINT8_CLASS: case mxUINT8_CLASS: case mxCHAR_CLASS: read_cast_matrix<ubyte>(var, m); break ; case mxINT16_CLASS: read_cast_matrix<int16>(var, m); break ; case mxUINT16_CLASS: read_cast_matrix<uint16>(var, m); break ; case mxINT32_CLASS: read_cast_matrix<uint32>(var, m); break ; case mxUINT32_CLASS: read_cast_matrix<uint32>(var, m); break ; case mxINT64_CLASS: read_cast_matrix<int64>(var, m); break ; case mxUINT64_CLASS: read_cast_matrix<uint64>(var, m); break ; case mxSINGLE_CLASS: read_cast_matrix<float>(var, m); break ; case mxDOUBLE_CLASS: read_cast_matrix<double>(var, m); break ; case mxVOID_CLASS: eblthrow("unsupported type: void"); case mxUNKNOWN_CLASS: eblthrow("unknown type"); break ; } // delete array if (var) mxDestroyArray(var); #else idx<T> m; #endif return m; }
idxdim get_matrix_dims(const char *filename) { // open file FILE *fp = fopen(filename, "rb"); if (!fp) eblthrow("failed to open " << filename); // read it int magic; idxdim d = read_matrix_header(fp, magic); fclose(fp); return d; }
bool is_matrix(const char *filename) { try { // open file FILE *fp = fopen(filename, "rb"); if (!fp) eblthrow("failed to open " << filename); int magic = 0; read_matrix_header(fp, magic); } catch (eblexception &e) { return false; } return true; }
string& string::operator=(char *v) { if (!v) return *this; size_t lv = strlen(v); if (lv == 0) return *this; if (!resize(lv + 1)) eblthrow("failed to resize string"); strcpy(s, v); return *this; }
string& string::operator<<(const char *v) { if (v == NULL) return *this; size_t lv = strlen(v), le = size(); if (lv == 0) return *this; lv += le + 1; if (!resize(lv)) eblthrow("failed to resize string"); strcpy(s + le, v); return *this; }
idxdim read_matrix_header(FILE *fp, int &magic) { int ndim, v, magic_vincent; int ndim_min = 3; // std header requires at least 3 dims even empty ones. idxdim dims; // read magic number if (fread(&magic, sizeof (int), 1, fp) != 1) { fclose(fp); eblthrow("cannot read magic number"); } magic_vincent = endian(magic); magic_vincent &= ~0xF; // magic contained in higher bits // read number of dimensions if (is_magic(magic)) { // regular magic number, read next number if (fread(&ndim, sizeof (int), 1, fp) != 1) { fclose(fp); eblthrow("cannot read number of dimensions"); } // check number is valid if (ndim > MAXDIMS) { fclose(fp); eblthrow("too many dimensions: " << ndim << " (MAXDIMS = " << MAXDIMS << ")."); } } else if (is_magic_vincent(magic_vincent)) { // vincent magic number // ndim is contained in lower bits of the magic number ndim = endian(magic) & 0xF; ndim_min = ndim; magic = magic_vincent; } else { // unkown magic fclose(fp); eblthrow("unknown magic number: " << reinterpret_cast<void*>(magic) << " or " << magic << " vincent: " << magic_vincent); } // read each dimension for (int i = 0; (i < ndim) || (i < ndim_min); ++i) { if (fread(&v, sizeof (int), 1, fp) != 1) { fclose(fp); eblthrow("failed to read matrix dimensions"); } // if vincent, convert to endian first if (is_magic_vincent(magic_vincent)) v = endian(v); if (i < ndim) { // ndim may be less than ndim_min if (v <= 0) { // check that dimension is valid fclose(fp); eblthrow("dimension is negative or zero"); } dims.insert_dim(i, v); // insert dimension } } return dims; }
int get_matrix_type(const char *filename) { // open file FILE *fp = fopen(filename, "rb"); if (!fp) eblthrow("failed to open " << filename); int magic = 0; if (has_multiple_matrices(filename)) { // first read offsets matrix idx<int64> p = load_matrix<int64>(fp); // read 2nd matrix header try { read_matrix_header(fp, magic); } catch (ebl::eblexception &e) {} return magic; } else { // header: read magic number try { read_matrix_header(fp, magic); } catch (ebl::eblexception &e) {} return magic; } }
file::file(const char *filename, const char *mode) : refcounter(0) { fp = fopen(filename, mode); if (!fp) eblthrow("failed to open " << filename); }