From f72c81b79b093e22c91e6663f3346a43376b4f99 Mon Sep 17 00:00:00 2001 From: Epicalert Date: Sun, 17 Jan 2021 21:36:38 +0800 Subject: [PATCH] Look for files in different paths Models and other supporting files can now be in different directories instead of the working directory. --- CMakeLists.txt | 2 +- src/cv.cpp | 7 +++++-- src/graphics.cpp | 9 +++++---- src/main.cpp | 7 +++++++ src/paths.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/paths.hpp | 13 +++++++++++++ 6 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 src/paths.cpp create mode 100644 src/paths.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fe666f1..9dc2f35 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,5 +30,5 @@ if (APPLE) DESTINATION $ {PROJECT_BINARY_DIR} ) endif (APPLE) -add_executable( fc2d src/main.cpp src/graphics.cpp src/modelpart.cpp src/cv.cpp ) +add_executable( fc2d src/main.cpp src/graphics.cpp src/modelpart.cpp src/cv.cpp src/paths.cpp ) target_link_libraries( fc2d ${OpenCV_LIBS} ${OPENGL_LIBRARIES} FreeGLUT::freeglut GLEW::glew ) diff --git a/src/cv.cpp b/src/cv.cpp index 18cbfea..a2e08df 100644 --- a/src/cv.cpp +++ b/src/cv.cpp @@ -1,7 +1,10 @@ #include #include +#include + #include +#include cv::Ptr facemark; cv::CascadeClassifier faceDetector; @@ -10,10 +13,10 @@ cv::Mat frame, gray, small; void initCV() { //TODO: switch to DNN face detection - faceDetector = cv::CascadeClassifier ("cvdata/haarcascade_frontalface_alt2.xml"); + faceDetector = cv::CascadeClassifier (resolvePath("cvdata/haarcascade_frontalface_alt2.xml")); facemark = cv::face::FacemarkLBF::create(); - facemark->loadModel ("cvdata/lbfmodel.yaml"); + facemark->loadModel (resolvePath("cvdata/lbfmodel.yaml")); vid = cv::VideoCapture (0); } diff --git a/src/graphics.cpp b/src/graphics.cpp index cc61613..722be8d 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -12,6 +12,7 @@ #include #include +#include GLuint shader; //standard shader program used for all elements GLuint transUniform; //location of the "transMatrix" transformation matrix uniform in the shader @@ -80,10 +81,10 @@ void initGraphics () { initShader(); - parts[0] = ModelPart("models/test/head-base.png", transUniform); - parts[1] = ModelPart("models/test/face-eyes.png", transUniform); - parts[2] = ModelPart("models/test/face-mouth-closed.png", transUniform); - parts[2].addTexture("models/test/face-mouth-open.png", 1); + parts[0] = ModelPart(resolvePath("models/test/head-base.png").c_str(), transUniform); + parts[1] = ModelPart(resolvePath("models/test/face-eyes.png").c_str(), transUniform); + parts[2] = ModelPart(resolvePath("models/test/face-mouth-closed.png").c_str(), transUniform); + parts[2].addTexture(resolvePath("models/test/face-mouth-open.png").c_str(), 1); //enable blending for alpha textures glEnable(GL_BLEND); diff --git a/src/main.cpp b/src/main.cpp index 84047f0..614f7f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,16 @@ #include #include +#include #include int main () { + std::cout << "Facecam2D is starting..." << std::endl; + + initPrefixes(); + std::cout << "Custom asset prefix: " << prefixCustom << std::endl; + std::cout << "Default asset prefix: " << prefixDefault << std::endl; + initGraphics(); initCV(); diff --git a/src/paths.cpp b/src/paths.cpp new file mode 100644 index 0000000..9b46c2f --- /dev/null +++ b/src/paths.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +#ifdef _WIN32 +#define READABLE(p) _access(p.c_str(),R_OK)==0 +#else +#define READABLE(p) access(p.c_str(),R_OK)==0 +#endif + +std::string prefixCustom; +std::string prefixDefault; + +void initPrefixes() { +#if defined (__gnu_linux__) + prefixCustom = getenv("HOME") + std::string("/.local/facecam2d/"); + prefixDefault = "/usr/share/facecam2d/"; +#elif defined (__APPLE__) + prefixCustom = getenv("HOME") + std::string("/Library/Facecam2D/"); + prefixDefault = "/Applications/Facecam2D.app/"; +#elif defined (_WIN32) + prefixCustom = getenv("AppData") + std::string("\\Facecam2D\\"); + prefixDefault = getenv("ProgramFiles") + std::string("\\Facecam2D\\"); +#endif +} + +std::string resolvePath(const char* path) { + std::string customString = prefixCustom + path; + std::string defaultString = prefixDefault + path; + + std::string result; + + if (READABLE(customString)) { + result = customString; + } else if (READABLE(defaultString)) { + result = defaultString; + } else { + result = path; + + if(!(READABLE(std::string(path)))) { + std::cerr << path << " not found!" << std::endl; + } + } + + return result; +} diff --git a/src/paths.hpp b/src/paths.hpp new file mode 100644 index 0000000..31ba82f --- /dev/null +++ b/src/paths.hpp @@ -0,0 +1,13 @@ +#ifndef PATHS_HPP +#define PATHS_HPP + +#include + +extern std::string prefixCustom; +extern std::string prefixDefault; + +void initPrefixes(); + +std::string resolvePath(const char* path); + +#endif