Add proper command line argument parsing with argp

This commit is contained in:
Epicalert 2021-01-26 23:36:22 +08:00
parent 625e9680d3
commit 82edd492c0
No known key found for this signature in database
GPG key ID: CAA46F858D0979BD
6 changed files with 71 additions and 10 deletions

View file

@ -30,5 +30,12 @@ if (APPLE)
DESTINATION
$ {PROJECT_BINARY_DIR} )
endif (APPLE)
add_executable( fc2d src/main.cpp src/graphics.cpp src/modelpart.cpp src/cv.cpp src/paths.cpp )
add_executable( fc2d
src/main.cpp
src/graphics.cpp
src/modelpart.cpp
src/cv.cpp
src/paths.cpp
src/args.cpp
)
target_link_libraries( fc2d ${OpenCV_LIBS} ${OPENGL_LIBRARIES} FreeGLUT::freeglut GLEW::glew )

37
src/args.cpp Normal file
View file

@ -0,0 +1,37 @@
#include <args.hpp>
const char* argp_program_version =
"Facecam2D 0.1.0\n"
"License: GPLv3 <https://www.gnu.org/licenses/gpl-3.0.html>\n"
"If you did not receive a copy of the source code, it is\n"
"available at <https://gitlab.com/epicalert/facecam2d.git>.";
const struct argp_option options[] = {
//name, key, arg, flags, doc, group
{"haar-cascade", 0x00, 0, 0, "Use Haar Cascades for faster (but less accurate) face detection.", 0},
{0}
};
struct argp argp = {
options,
parseOptions,
0,
0
};
struct optData optData = {
false
};
error_t parseOptions(int key, char* arg, struct argp_state* state) {
switch (key) {
case 0x00: //--haar-cascade
optData.useHaar = true;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}

17
src/args.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef ARGS_HPP
#define ARGS_HPP
#include <argp.h>
error_t parseOptions(int key, char* arg, struct argp_state* state);
struct optData {
bool useHaar; //use haar cascades (0x00)
};
extern const char* argp_program_version;
extern const struct argp_option options[];
extern struct argp argp;
extern struct optData optData;
#endif

View file

@ -5,17 +5,15 @@
#include <graphics.hpp>
#include <paths.hpp>
#include <args.hpp>
cv::Ptr<cv::face::Facemark> facemark;
cv::CascadeClassifier haarFaceDetector;
cv::dnn::Net dnnFaceDetector;
cv::VideoCapture vid;
cv::Mat frame, gray, small;
bool useHaar;
void initCV(bool haar) {
useHaar = haar;
void initCV() {
haarFaceDetector = cv::CascadeClassifier (resolvePath("cvdata/haarcascade_frontalface_alt2.xml"));
dnnFaceDetector = cv::dnn::readNetFromCaffe(
resolvePath("cvdata/deploy.prototxt"),
@ -59,7 +57,7 @@ void cvFrame() {
std::vector<cv::Rect> faces;
if (useHaar) {
if (optData.useHaar) {
//downsample image for face detection, works too slow on full res
cv::pyrDown (gray, small);
cv::pyrDown (small, small);
@ -74,7 +72,7 @@ void cvFrame() {
int biggestArea = 0;
for (int i = 0; i < faces.size(); i++) {
//convert face region to full res, because we perform facemark on full res
if (useHaar) {
if (optData.useHaar) {
faces[i] = cv::Rect (faces[i].x * 4, faces[i].y * 4, faces[i].width * 4, faces[i].height * 4);
}

View file

@ -1,7 +1,7 @@
#ifndef CV_HPP
#define CV_HPP
void initCV(bool haar);
void initCV();
void cvFrame();

View file

@ -1,11 +1,14 @@
#include <graphics.hpp>
#include <cv.hpp>
#include <paths.hpp>
#include <args.hpp>
#include <iostream>
#include <cstring>
int main (int argc, char** argv) {
argp_parse(&argp, argc, argv, 0, 0, 0);
std::cout << "Facecam2D is starting..." << std::endl;
initPrefixes();
@ -13,8 +16,7 @@ int main (int argc, char** argv) {
std::cout << "Default asset prefix: " << prefixDefault << std::endl;
initGraphics();
//TODO: real argument parsing
initCV(argc > 1 && strcmp(argv[1], "--haar-cascade") == 0);
initCV();
while (true) {
cvFrame();