📄 mshapes.cpp
字号:
// $util\mshapes.cpp 1.5 milbo$ make a combined shape file// Warning: this is raw research code -- expect it to be quite messy.// milbo petaluma sep 2005//-----------------------------------------------------------------------------// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// A copy of the GNU General Public License is available at// http://www.r-project.org/Licenses///-----------------------------------------------------------------------------#include "all.hpp"#include "bioid.hpp"#include "fast2.hpp"#include "myhebp.hpp"static char sgInFile[SLEN]; // from command linestatic char sgOutFile[SLEN] = "ms.shape"; // -ostatic char sgMergeFile[SLEN]; // -mstatic char sgOutIndexFile[SLEN] = "ms.index"; // name generated from sgOutFilestatic const char *CONF_sMamFilename = "mam/all.pts";static const char *CONF_sNnImageDir = "/a1/faces/ar/pgm";#define CONF_fWriteNnImages 0FILE *pgOut;int ngShapes;static char sgTagRegExp[SLEN]; // -p command linestatic clock_t gStartTime = clock(); // time program startedstatic const char sgUsage[] ="Usage: mshapes [-I] [-M] [-N] [-o OUT.shape] [-m UP.shape] IN.shape\n\n""-I rebuild index from IN.shape, skip everything else e.g. mshapes -I all/m3All.shape\n""-B skip BioId update (after this, you should manually insert BioId shapes and rerun mshapes -I)\n""-M skip MAM update (after this, you should insert MAM manually and rerun mshapes -I) (TODO currently always skips)\n""-N skip neural net update (after this, you should insert NN manually and rerun mshapes -I)\n""-o OUT.shape output file (default %s)\n""\n""-p PATTERN PATTERN is an egrep style pattern (not a file wildcard).\n"" Load only shapes in INFILE.shape with tags matching case-independent PATTERN.\n"" Example: -p \"abc\" matches shapes with filenames containing abc\n"" Example: -p \"^4|^8\" loads all test set shapes.\n"" Default: NULL, i.e. load all shapes\n""\n""-m UP.shape merge IN.shape and UP.shape into OUT.shape\n"" Shapes in UP.shape take precedence over those in IN.shape\n"" Example after masm -fw 1: mshapes -m _m2.shape -o shapes/_m2a.shape shapes/m2a.shape; cp shapes/_m2a.shape shapes/m2a.shape\n"" mshapes -m _m2.shape -o m2aAll.shape m2refAll.shape\n""\n""This program uses the following hardcoded names: MAM file \"%s\", NN images \"%s/*.pgm\"\n";//-------------------------------------------------------------------------------static void DoM2 (void) // copy sgInFile to sgOutFile{char s[SLEN];lprintf("Reading %s ", sgInFile);FILE *pFile = Fopen(sgInFile, "r");Fprintf(pgOut, "#-- %s ------------------------------------------------------------\n", sgInFile);int nShapes = 0;while (fgets(s, SLEN-1, pFile)) { if (strstr(s, ".bmp") || strstr(s, ".pgm") || strstr(s, ".jpg")) { nShapes++; ngShapes++; } Fprintf(pgOut, s); }fclose(pFile);lprintf("%d shapes\n", nShapes);}//-------------------------------------------------------------------------------static void Merge (void) // copy sgInFile to sgOutFile but replacing shapes that are in sgMergeFile{char s[SLEN];ShapeVec Shapes; // array[nShapes] of SHAPE, each SHAPE is nPoints x 2, original training shapesStringVec Tags; // array[nShapes] of string preceding each shape in ASM filechar sImageDirs[SLEN];char *sInFile = sgInFile;char *sMergeFile = sgMergeFile;ReadShapeFile(&Shapes, Tags, sImageDirs, sgTagRegExp, 0, 0, sMergeFile, QUIET);int nShapes1 = Shapes.size();if (nShapes1 == 0) Err("No shapes");int nPoints = Shapes[0].nrows();lprintf("got %d shape%s, first shape has %d landmarks\n", nShapes1, ((nShapes1==1)? "":"s"), nPoints);if (nPoints != CONF_nPointsXm2vts) Warn("nPoints %d != CONF_nPointsXm2vts %d", nPoints, CONF_nPointsXm2vts);bool *afInOutFile = (bool *)calloc(nShapes1, sizeof(bool));lprintf("Reading %s", sInFile);FILE *pFile = Fopen(sInFile, "r");int nShapes = 0, nMerged = 0;bool fPrint = true;while (fgets(s, SLEN-1, pFile)) // read sInFile { if (s[0] == '"' && (strstr(s, ".bmp") || strstr(s, ".pgm") || strstr(s, ".jpg"))) { // Found the string preceding each shape // If shape is in sMergeFile then replace it with the shape in MergeFile nShapes++; ngShapes++; char sStripped[SLEN]; // s with first and last quote removed strcpy(sStripped, &s[1]); sStripped[strlen(sStripped)-2] = 0; int iShape = iFindString(sStripped, Tags, 0); if (iShape >= 0) // found shape in MergeFile? { if (afInOutFile[iShape]) lprintf("\nError: %s appears twice in %s ", Tags[iShape], sInFile); else { afInOutFile[iShape] = true; fPrint = false; nMerged++; Fprintf(pgOut, "\"%s\"\n", Tags[iShape]);#if 1 Shapes[iShape].write(sgOutFile, pgOut, QUIET, NULL, "%g");#else SHAPE Shape(1,2); // ConvertShapeFromInternalFormat will resize ConvertShapeFromInternalFormat(Shape, Shapes[iShape], Tags[iShape]); Shape.write(sgOutFile, pgOut, QUIET, NULL, "%g");#endif } } } if (fPrint) Fprintf(pgOut, s); if (s[0] == '}') fPrint = true; }lprintf("\nMerged %d of %d shapes from %s and %s (shapes in %s take precedence)\n", nMerged, nShapes, sgInFile, sMergeFile, sMergeFile);if (nMerged != nShapes1) { for (int iShape = 0; iShape < nShapes1; iShape++) if (!afInOutFile[iShape]) lprintf("%s in %s but not in %s\n", Tags[iShape], sMergeFile, sInFile); lprintf("Error: %s has %d shapes but found %d of them in %s\n", sMergeFile, nShapes1, nMerged, sInFile); }fclose(pFile);}//-----------------------------------------------------------------------------#define MamX(x) ((MamMat(x,VX)) - Img.width / 2)#define MamY(y) (Img.height / 2 - (MamMat(y, VY)))// Format of mam shape array, left and right wrt viewer#define M_LCheekEar 0 // y#define M_LCheek 1 // y#define M_TipOfChin 2 // y#define M_RCheek 3 // y#define M_RCheekEar 4 // y#define M_MouthRCorner 5 // y#define M_MouthBotMidLowLip 6 // y#define M_MouthLCorner 7 // y#define M_MouthTopMidLowLip 8 // y#define M_MouthTopMidUpLip 9 // y#define M_NoseBotRNostril 10 // y#define M_NoseBotLNostril 11 // y#define M_NoseTip 12 // y#define M_REyeOut 13 // y#define M_REye 14 // y#define M_REyeIn 15 // y#define M_LEyeIn 16 // y#define M_LEye 17 // y#define M_LEyeOut 18 // ystatic void DoMam (void){char s[SLEN];int iMat = 0;Mat MamMat(19, 2);lprintf("Reading %s\n", CONF_sMamFilename);FILE *pFile = Fopen(CONF_sMamFilename, "r");Fprintf(pgOut, "#-- %s ------------------------------------------------------------\n", CONF_sMamFilename);int nShapes = 0;while (fgets(s, SLEN-1, pFile)) { if (0 == strncmp(s, "points/face", 11)) { nShapes++; ngShapes++; char sImage[FLEN]; s[strlen(s) - 1] = 0; // remove \n lprintf("%s ", s); s[strlen(s) - 5] = 0; // remove .pts sprintf(sImage, "%s.pgm", s+7); // get image width and height Image Img; char sPath[SLEN]; sprintf(sPath, "/a1/faces/nn/pgm/%s", sImage); sLoadImage(Img, sPath, VERBOSE); fgets(s, SLEN-1, pFile); if (strncmp(s, "version:", 8)) Err("Expected \"version\"\n%s", s); fgets(s, SLEN-1, pFile); if (strncmp(s, "n_points:", 9)) Err("Expected \"n_points\"\n%s", s); MamMat.sread(CONF_sMamFilename, pFile); Fprintf(pgOut, "# %d\n", iMat+1); Fprintf(pgOut, "\"%4.4x %s\"\n", 0, sImage); Fprintf(pgOut, "{\n"); Fprintf(pgOut, "%d 2\n", CONF_nPointsXm2vts); Fprintf(pgOut, "%g %g\n", MamX(M_LCheekEar), MamY(M_LCheekEar)); // 00 Fprintf(pgOut, "0 0\n"); // 01 Fprintf(pgOut, "0 0\n"); // 02 Fprintf(pgOut, "%g %g\n", MamX(M_LCheek), MamY(M_LCheek)); // 03 Fprintf(pgOut, "0 0\n"); // 04 Fprintf(pgOut, "0 0\n"); // 05 Fprintf(pgOut, "0 0\n"); // 06 Fprintf(pgOut, "%g %g\n", MamX(M_TipOfChin), MamY(M_TipOfChin)); // Chin 07 Fprintf(pgOut, "0 0\n"); // 08 Fprintf(pgOut, "0 0\n"); // 09 Fprintf(pgOut, "0 0\n"); // 10 Fprintf(pgOut, "%g %g\n", MamX(M_RCheek), MamY(M_RCheek)); // 11 Fprintf(pgOut, "0 0\n"); // 12 Fprintf(pgOut, "0 0\n"); // 13 Fprintf(pgOut, "%g %g\n", MamX(M_RCheekEar), MamY(M_RCheekEar)); // 14 Fprintf(pgOut, "%g %g\n", 0.0, 0.0); // lBrowIn 15 Fprintf(pgOut, "0 0\n"); // 16 Fprintf(pgOut, "0 0\n"); // 17 Fprintf(pgOut, "%g %g\n", 0.0, 0.0); // lBrowOut 18 Fprintf(pgOut, "0 0\n"); // 19 Fprintf(pgOut, "0 0\n"); // 20 Fprintf(pgOut, "%g %g\n", 0.0, 0.0); // rBrowOut 21 Fprintf(pgOut, "0 0\n"); // 22 Fprintf(pgOut, "0 0\n"); // 23 Fprintf(pgOut, "%g %g\n", 0.0, 0.0); // rBrowIn 24 Fprintf(pgOut, "0 0\n"); // 25 Fprintf(pgOut, "0 0\n"); // 26 Fprintf(pgOut, "%g %g\n", MamX(M_LEyeOut), MamY(M_LEyeOut)); // rEyeOut 27 Fprintf(pgOut, "0 0\n"); // 28 Fprintf(pgOut, "%g %g\n", MamX(M_LEyeIn), MamY(M_LEyeIn)); // rEyeIn 29 Fprintf(pgOut, "0 0\n"); // 30 Fprintf(pgOut, "%g %g\n", MamX(M_LEye), MamY(M_LEye)); // lEye 31 Fprintf(pgOut, "%g %g\n", MamX(M_REyeOut), MamY(M_REyeOut)); // lEyeOut 32 Fprintf(pgOut, "0 0\n"); // 33 Fprintf(pgOut, "%g %g\n", MamX(M_REyeIn), MamY(M_REyeIn)); // lEyeIn 34 Fprintf(pgOut, "0 0\n"); // 35 Fprintf(pgOut, "%g %g\n", MamX(M_REye), MamY(M_REye)); // rEye 36 Fprintf(pgOut, "0 0\n"); // 37 Fprintf(pgOut, "0 0\n"); // 38 Fprintf(pgOut, "0 0\n"); // 39 Fprintf(pgOut, "0 0\n"); // 40 Fprintf(pgOut, "0 0\n"); // 41 Fprintf(pgOut, "0 0\n"); // 42 Fprintf(pgOut, "0 0\n"); // 43 Fprintf(pgOut, "0 0\n"); // 44 Fprintf(pgOut, "0 0\n"); // 45 Fprintf(pgOut, "%g %g\n", MamX(M_NoseBotRNostril), MamY(M_NoseBotRNostril)); // 46 Fprintf(pgOut, "%g %g\n", MamX(M_NoseBotLNostril), MamY(M_NoseBotLNostril)); // 47 Fprintf(pgOut, "%g %g\n", MamX(M_MouthLCorner), MamY(M_MouthLCorner)); // rMouth 48 Fprintf(pgOut, "0 0\n"); // 49 Fprintf(pgOut, "0 0\n"); // 50 Fprintf(pgOut, "%g %g\n", MamX(M_MouthTopMidUpLip), MamY(M_MouthTopMidUpLip)); // LipUp 51 Fprintf(pgOut, "0 0\n"); // 52 Fprintf(pgOut, "0 0\n"); // 53 Fprintf(pgOut, "%g %g\n", MamX(M_MouthRCorner), MamY(M_MouthRCorner)); // lMouth 54 Fprintf(pgOut, "0 0\n"); // 55 Fprintf(pgOut, "0 0\n"); // 56 Fprintf(pgOut, "%g %g\n", MamX(M_MouthBotMidLowLip), MamY(M_MouthBotMidLowLip));// LipLow 57 Fprintf(pgOut, "0 0\n"); // 58 Fprintf(pgOut, "0 0\n"); // 59
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -