//----------------------------------------------------------------------------- static void ShowFirstFewShapes (const vec_string &Tags) // in { if (Tags.size() == 0) lprintf("No shapes"); else { lprintf("First few shapes are "); unsigned iShape; for (iShape = 0; iShape < MIN(5, Tags.size()); iShape++) lprintf("%s ", sGetBase(sGetBasenameFromTag(Tags[iShape]))); if (iShape < Tags.size()) lprintf("..."); lprintf("\n"); } }
void DiscardShapesWithDifferentNbrOfPoints (vec_SHAPE &Shapes, // io: updated vec_string &Tags, // io: updated int nWantedPoints) { unsigned iShape1 = 0; unsigned nShapes = static_cast<int>(Tags.size()); ASSERT(Shapes.size() == nShapes); for (unsigned iShape = 0; iShape < nShapes; iShape++) if (Shapes[iShape].nrows() == (unsigned)nWantedPoints) { Shapes[iShape1].assign(Shapes[iShape]); Tags[iShape1] = Tags[iShape]; iShape1++; } // if (nShapes - iShape1) // lprintf("Ignored %d shape%s " // "not matching the reference number of points %d\n", // nShapes - iShape1, // (nShapes - iShape1 == 1? "": "s"), // nWantedPoints); Shapes.resize(iShape1); Tags.resize(iShape1); }
void DiscardFaceDetectorShapes (vec_SHAPE &Shapes, // io: updated vec_string &Tags, // io: updated bool fDiscardUnderscores) { unsigned iShape1 = 0; unsigned nShapes = Tags.size(); ASSERT(Shapes.size() == nShapes); for (unsigned iShape = 0; iShape < nShapes; iShape++) { const char *sTag = Tags[iShape].c_str(); // tag string should be of the form: "1234 filename" or "12345678 filename" if (sTag[4] != ' ' && sTag[8] != ' ') Err("malformed tag \"%s\"", sTag); unsigned Attr; if (1 == sscanf(sTag, "%x", &Attr) && !((Attr & FA_ViolaJones) || (Attr & FA_Rowley)) && // not a face detector shape (!fDiscardUnderscores || sGetBasenameFromTag(sTag)[0] != '_')) { Shapes[iShape1].assign(Shapes[iShape]); Tags[iShape1] = Tags[iShape]; iShape1++; } } if (nShapes - iShape1) lprintf("Ignored %d face detector%s shape%s\n", nShapes - iShape1, (fDiscardUnderscores? " and underscore": ""), (nShapes - iShape1 == 1? "": "s")); Shapes.resize(iShape1); Tags.resize(iShape1); }
//----------------------------------------------------------------------------- void ReadSelectedShapes ( vec_SHAPE &Shapes, // out: vec_string &Tags, // out: char sImageDirs[], // out: const char sShapeFile[], // in: int nMaxShapes, // in: nbr of wanted shapes, 0 for all const char sTagRegex[], // in: only read matrices whose tag // matches this regular expression unsigned Mask0, unsigned Mask1) // in: only read matrices where // Attr&Mask0 == Mask1 // (Attr is hex part of tag string) { ReadShapeFile(Shapes, Tags, sImageDirs, sTagRegex, Mask0, Mask1, sShapeFile); DiscardFaceDetectorShapes(Shapes, Tags); unsigned nShapes = Tags.size(); if (nShapes == 0) Err("no shapes"); if (unsigned(nMaxShapes) > nShapes) { lprintf("\n"); ShowFirstFewShapes(Tags); Err("want %d shapes but there are only %d shapes", nMaxShapes, nShapes); } if (nMaxShapes != 0) { SelectNShapes(Shapes, Tags, nMaxShapes, CONF_nSeed_SelectShapes); if (CONF_nSeed_SelectShapes) lprintf("Selected a random sample of %d shape%s from %d shape%s (seed=%d)\n", nMaxShapes, ((nMaxShapes==1)? "":"s"), nShapes, ((nShapes==1)? "":"s"), CONF_nSeed_SelectShapes); else lprintf("Selected the first %d shape%s of %d shape%s\n", nMaxShapes, ((nMaxShapes==1)? "":"s"), nShapes, ((nShapes==1)? "":"s")); } else lprintf("Read %d shape%s\n", nShapes, ((nShapes==1)? "":"s")); ShowFirstFewShapes(Tags); }
//----------------------------------------------------------------------------- static int iGetRefShapeIndex1 (const vec_string &Tags, unsigned Mask0, unsigned Mask1, const char sFile[]) // in: for error reporting { for (unsigned iShape = 0; iShape < Tags.size(); iShape++) { const char *sTag = Tags[iShape].c_str(); if (!sTag || sTag[0] == 0) Err("GetRefShapeIndex: shape index %d in %s does not have a tag", iShape, sFile); unsigned Tag; if (sTag[4] != ' ' && sTag[8] != ' ') Err("GetRefShapeIndex: tag %s in %s is malformed", sTag, sFile); if (1 != sscanf(sTag, "%x", &Tag)) Err("GetRefShapeIndex: tag %s in %s is not a hex number", sTag, sFile); if (fMatchAttr(Tag, Mask0, Mask1)) return iShape; // found it } return -1; // not found }
static void SelectNShapes ( vec_SHAPE &Shapes, // io vec_string &Tags, // io: also shuffled, in step with Shapes int nWantedShapes, // in: 0 means return all shapes int nSeed=0) // in: 0 means no random selection; if any other // val select randomly with rand seed=nSeed { unsigned nShapes = Tags.size(); if (nWantedShapes == 0) nWantedShapes = nShapes; nWantedShapes = MIN((unsigned)nWantedShapes, nShapes); if (nSeed) { // generate a shuffled set of indices in iShuffledShapes vec_int iShuffledShapes(nShapes); unsigned iShape; for (iShape = 0; iShape < nShapes; iShape++) iShuffledShapes[iShape] = iShape; SeedRand(nSeed); // We use our own random shuffle here because different compilers // give different results which messes up regression testing. // (I think only Visual C 6.0 is incompatible with everyone else?) // // Following code is equivalent to // random_shuffle(iShuffledShapes.begin(), iShuffledShapes.end(), // pointer_to_unary_function<int,int>(Rand)); vec_int::iterator pNext = iShuffledShapes.begin(); for (int i = 2; ++pNext != iShuffledShapes.end(); ++i) iter_swap(pNext, iShuffledShapes.begin() + Rand(i)); iShuffledShapes.resize(nWantedShapes); // sort the selected indices so we can do an in-place replacement in Shapes sort(iShuffledShapes.begin(), iShuffledShapes.end()); // keep the first nWantedShapes in iShuffledShapes for (iShape = 0; iShape < unsigned(nWantedShapes); iShape++) { unsigned iOldShape = iShuffledShapes[iShape]; if (iShape > 0 && Shapes[0].nrows() != Shapes[iOldShape].nrows()) { static bool fIssuedWarning; if (!fIssuedWarning) { fIssuedWarning = true; WarnWithNewLine("different sized shapes (%s has %d rows, %s has %d rows)\n", sGetBasenameFromTag(Tags[0].c_str()), Shapes[0].nrows(), sGetBasenameFromTag(Tags[iOldShape].c_str()), Shapes[iOldShape].nrows()); } } Shapes[iShape].assign(Shapes[iOldShape]); Tags[iShape] = Tags[iOldShape]; } } Shapes.resize(nWantedShapes); Tags.resize(nWantedShapes); }