📄 mnn.cpp
字号:
// $masm\mnn.cpp 1.5 milbo$ interface between masm and neural net// Warning: this is raw research code -- expect it to be quite messy.//-----------------------------------------------------------------------------// 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"//-----------------------------------------------------------------------------static void WriteNnImage (Image &Img, const char sImagePath[], int nFaces, const tFaceEyeLocation *pLocations, SHAPE &Shape){char sOutPath[SLEN];char sDrive[_MAX_DRIVE], sDir[_MAX_DIR], sFname[_MAX_FNAME], sExt[_MAX_EXT];_splitpath(sImagePath, sDrive, sDir, sFname, sExt);sprintf(sOutPath, "%s/nn_%s.bmp", CONF_sOutDir, sFname);RgbImage RgbImg(Img); // convert gray to colorfor (int iFace = 0; iFace < nFaces; iFace++) DrawNnResult(RgbImg, pLocations[iFace]);WriteBmp(RgbImg, sOutPath);}//-----------------------------------------------------------------------------// Returns true if found 1 face with 2 eyes, with face in Shape.// Face is a "NN shape", with NBR_ROWLEY_POINTS points -- see NN_ defs in atland.hpp for format// If it can't find a face (with two eyes), we print an error message and return false// The caller can decide whether to exit the program or notbool fNnFindFace (SHAPE &Shape, // out Image &Img, const char sImageFile[], bool fWriteImage) // in{bool fSuccess = false;Shape.dimClear(NBR_ROWLEY_POINTS, 2);// do the neural net searchtFaceEyeLocation *pLocations; // found face and eye coords go into this arrayconst char *sDataDir = "../../stasm/stasm/data";int nFaces = Track_FindAllFaces(&pLocations, Img, sDataDir);// choose biggest face with both eyesint iBest = 0, nMaxWidth = 0;for (int iFace = 0; iFace < nFaces; iFace++) if (pLocations[iFace].xLeftEye > 0 && pLocations[iFace].xRightEye > 0 && pLocations[iFace].x2 - pLocations[iFace].x1 > nMaxWidth) { iBest = iFace; nMaxWidth = pLocations[iFace].x2 - pLocations[iFace].x1; }if (nFaces < 1) lprintf("Rowley detector found no faces in %s\n", sImageFile);else if (pLocations[iBest].xLeftEye <= 0) lprintf("Rowley detector didn't find the left eye in %s\n", sImageFile);else if (pLocations[iBest].xRightEye <= 0) lprintf("Rowley detector didn't find the right eye in %s\n", sImageFile);else { fSuccess = true;#if 0 if (nFaces > 1) lprintf(" (%d faces, using the biggest face with both eyes)", nFaces);#endif // write the neural net face shape into Shape #define _X(x) (double)((x) - Img.width / 2) // convert NN coords to our internal shape coords #define _Y(y) (double)(Img.height / 2 - (y)) Shape(NN_BottomLeft, VX) = _X(pLocations[iBest].x1); Shape(NN_BottomLeft, VY) = _Y(pLocations[iBest].y1); Shape(NN_TopRight, VX) = _X(pLocations[iBest].x2); Shape(NN_TopRight, VY) = _Y(pLocations[iBest].y2); Shape(NN_LEye, VX) = _X(pLocations[iBest].xRightEye); Shape(NN_LEye, VY) = _Y(pLocations[iBest].yRightEye); Shape(NN_REye, VX) = _X(pLocations[iBest].xLeftEye); Shape(NN_REye, VY) = _Y(pLocations[iBest].yLeftEye); }if (fWriteImage && nFaces > 0) WriteNnImage(Img, sImageFile, nFaces, pLocations, Shape);free(pLocations);//Shape.print("\nNnShape\n");return fSuccess;}//-----------------------------------------------------------------------------// Convert a neural net stored in NnShape to a Location.void GlobalDetShapeToLocation (tFaceEyeLocation &Location, const SHAPE &NnShape, const RgbImage &Img){Location.x1 = NnShape(NN_BottomLeft, VX) + Img.width/2; // add width to convert to image coordsLocation.y1 = -NnShape(NN_BottomLeft, VY) + Img.height/2;Location.x2 = NnShape(NN_TopRight, VX) + Img.width/2;Location.y2 = -NnShape(NN_TopRight, VY) + Img.height/2;if (NnShape.nrows() == NBR_ROWLEY_POINTS) // eye positions available? { Location.xLeftEye = NnShape(NN_LEye, VX) + Img.width/2; Location.yLeftEye = -NnShape(NN_LEye, VY) + Img.height/2; Location.xRightEye = NnShape(NN_REye, VX) + Img.width/2; Location.yRightEye = -NnShape(NN_REye, VY) + Img.height/2; }}//-----------------------------------------------------------------------------void DrawNnResult (RgbImage &RgbImg, const tFaceEyeLocation &Location){DrawRectInBmp(RgbImg, Location.x1, Location.y1, Location.x2 - Location.x1, Location.y2 - Location.y1, C_YELLOW);if (Location.xLeftEye > 0) { int w = (Location.x2 - Location.x1) / 10; // left eye size DrawRectInBmp(RgbImg, Location.xLeftEye-w, Location.yLeftEye-w, 2*w+1, 2*w+1, C_YELLOW); }if (Location.xRightEye > 0) { int w = (Location.x2 - Location.x1) / 10; // right eye size DrawRectInBmp(RgbImg, Location.xRightEye-w, Location.yRightEye-w, 2*w+1, 2*w+1, C_YELLOW); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -