Look for files in different paths

Models and other supporting files can now be in different directories
instead of the working directory.
This commit is contained in:
Epicalert 2021-01-17 21:36:38 +08:00
parent ef534eb51f
commit f72c81b79b
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
6 changed files with 78 additions and 7 deletions

View file

@ -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 )

View file

@ -1,7 +1,10 @@
#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include <iostream>
#include <graphics.hpp>
#include <paths.hpp>
cv::Ptr<cv::face::Facemark> 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);
}

View file

@ -12,6 +12,7 @@
#include <graphics.hpp>
#include <modelpart.hpp>
#include <paths.hpp>
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);

View file

@ -1,9 +1,16 @@
#include <graphics.hpp>
#include <cv.hpp>
#include <paths.hpp>
#include <iostream>
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();

47
src/paths.cpp Normal file
View file

@ -0,0 +1,47 @@
#include <unistd.h>
#include <iostream>
#include <cstring>
#include <paths.hpp>
#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;
}

13
src/paths.hpp Normal file
View file

@ -0,0 +1,13 @@
#ifndef PATHS_HPP
#define PATHS_HPP
#include <string>
extern std::string prefixCustom;
extern std::string prefixDefault;
void initPrefixes();
std::string resolvePath(const char* path);
#endif