Skip to content

Commit

Permalink
Made same file for single images and multiple images in a .txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
voletiv committed Nov 9, 2017
1 parent fef52c5 commit 24bee91
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 190 deletions.
7 changes: 2 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ if(WITH_TOOLS)

find_package(Boost COMPONENTS program_options REQUIRED)

add_executable(gazr_benchmark_head_pose_single_frame tools/benchmark_head_pose_estimation_single_frame.cpp)
target_link_libraries(gazr_benchmark_head_pose_single_frame gazr ${OpenCV_LIBRARIES})

add_executable(gazr_benchmark_head_pose_multiple_frames tools/benchmark_head_pose_estimation_multiple_frames.cpp)
target_link_libraries(gazr_benchmark_head_pose_multiple_frames gazr ${OpenCV_LIBRARIES})
add_executable(gazr_estimate_head_pose tools/estimate_head_pose_from_image_or_file.cpp)
target_link_libraries(gazr_estimate_head_pose gazr ${OpenCV_LIBRARIES})

add_executable(gazr_estimate_head_direction tools/estimate_head_direction.cpp)
target_link_libraries(gazr_estimate_head_direction gazr ${OpenCV_LIBRARIES} ${Boost_LIBRARIES})
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,13 @@ $ make
Run ``./gazr_show_head_pose ../share/shape_predictor_68_face_landmarks.dat`` to test
the library. You should get something very similar to the picture above.

#### Example - single frame
#### Example - estimate head pose on image/images

Run ``./gazr_benchmark_head_pose_single_frame ../share/shape_predictor_68_face_landmarks.dat frame.jpg``
Run ``./gazr_estimate_head_pose ../share/shape_predictor_68_face_landmarks.dat frame.jpg``
to print the head pose detected in _frame.jpg_.

#### Example - multiple frames

Run ``./gazr_benchmark_head_pose_multiple_frames ../share/shape_predictor_68_face_landmarks.dat filenames.txt``
to print the head pose detected in each file listed in _filenames.txt_.
Run ``./gazr_estimate_head_pose ../share/shape_predictor_68_face_landmarks.dat ex_images.txt``
to print the head pose detected in each image file listed in _filenames.txt_ (image file names written in new lines).


### Installation - 2/2
Expand Down
97 changes: 0 additions & 97 deletions tools/benchmark_head_pose_estimation_multiple_frames.cpp

This file was deleted.

82 changes: 0 additions & 82 deletions tools/benchmark_head_pose_estimation_single_frame.cpp

This file was deleted.

153 changes: 153 additions & 0 deletions tools/estimate_head_pose_from_image_or_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#define STR_EXPAND(tok) #tok
#define STR(tok) STR_EXPAND(tok)

#ifdef OPENCV3
#include <opencv2/imgcodecs.hpp>
#else
#include <opencv2/highgui/highgui.hpp>
#endif

#include <iostream>

#include "../src/head_pose_estimation.hpp"

using namespace std;
using namespace cv;

const static size_t NB_TESTS = 100; // number of time the detection is run, to get better average detection duration

std::vector<std::string> readFileToVector(const std::string& filename)
{
std::ifstream source;
source.open(filename);
std::vector<std::string> lines;
std::string line;
while (std::getline(source, line))
{
lines.push_back(line);
}
return lines;
}


void estimate_head_pose_on_frameFileName(const std::string& frameFileName, HeadPoseEstimation estimator, std::vector<head_pose>& prev_poses, bool print_prev_poses)
{
cout << "Estimating head pose on " << frameFileName << endl;
#ifdef OPENCV3
Mat img = imread(frameFileName, IMREAD_COLOR);
#else
Mat img = imread(frameFileName, CV_LOAD_IMAGE_COLOR);
#endif

auto nbfaces = 0;

for(size_t i = 0; i < NB_TESTS; i++) {
estimator.update(img);
}

// auto t_detection = getTickCount();

for(size_t i = 0; i < NB_TESTS; i++) {
auto poses = estimator.poses();
nbfaces += poses.size(); // this is completly artifical: the only purpose is to make sure the compiler does not optimize away estimator.poses()
}

cout << "Found " << nbfaces/NB_TESTS << " face(s)" << endl;

auto poses = estimator.poses();
if (poses.size() > 0) {
for(auto pose : poses) {
cout << "Head pose: (" << pose(0,3) << ", " << pose(1,3) << ", " << pose(2,3) << ")" << endl;
}
prev_poses = poses;
}
else if (print_prev_poses) {
for(auto pose : prev_poses) {
cout << "Head pose: (" << pose(0,3) << ", " << pose(1,3) << ", " << pose(2,3) << ")" << endl;
}
}
else {
cout << "Head pose not calculated!" << endl;
}

}


int main(int argc, char **argv)
{
Mat frame;

if(argc < 3) {
cerr << argv[0] << " " << STR(GAZR_VERSION) << "\n\nUsage: "
<< endl << argv[0] << " model.dat frame.{jpg|png}\n\nOR\n\n"
<< endl << argv[0] << " model.dat filenames.txt" << endl;
#ifdef HEAD_POSE_ESTIMATION_DEBUG
cerr << "Output: a new frame 'head_pose_<frame>.png'" << endl;
#endif
return 1;
}

std::string fileName = argv[2];

if (fileName.find(".jpg") == std::string::npos and
fileName.find(".png") == std::string::npos and
fileName.find(".txt") == std::string::npos) {
cerr << "Please input a .jpg file, or .png file, or .txt file containing list of images in individual lines.";
return 1;
}

auto estimator = HeadPoseEstimation(argv[1]);
estimator.focalLength = 500;

cout << "Running " << NB_TESTS << " loops to get a good performance estimate..." << endl;
#ifdef HEAD_POSE_ESTIMATION_DEBUG
cerr << "ATTENTION! The benchmark is compiled in DEBUG mode: the performance is no going to be good!!" << endl;
#endif

// Prev pose (default)
head_pose prev_pose = {
-1, -1, -1, -1.,
-1, -1, -1, -1.,
-1, -1, -1, -1.,
0, 0, 0, 1
};
std::vector<head_pose> prev_poses;
prev_poses.push_back(prev_pose);

auto t_start = getTickCount();

// Single image file
if (fileName.find("jpg") != std::string::npos or fileName.find("png") != std::string::npos) {
cout << ".jpg or .png file" << endl;
estimate_head_pose_on_frameFileName(fileName, estimator, prev_poses, false);
}

// Multiple lines
else {
cout << ".txt file" << endl;
// Read file
std::string frameFilesTxt(fileName);
std::vector<std::string> frameFileNames = readFileToVector(frameFilesTxt);

if (frameFileNames.size() == 0) {
cout << fileName << "does not exist, or has no image names" << endl;
}

for(auto frameFileName: frameFileNames) {
if (frameFileName.find("jpg") != std::string::npos or frameFileName.find("png") != std::string::npos) {
estimate_head_pose_on_frameFileName(frameFileName, estimator, prev_poses, false);
}
}
}

auto t_end = getTickCount();

// cout << "Face feature detection: " <<((t_detection-t_start) / NB_TESTS) /getTickFrequency() * 1000. << "ms;";
// cout << "Pose estimation: " <<((t_end-t_detection) / NB_TESTS) /getTickFrequency() * 1000. << "ms;";
cout << "Total time: " << (t_end-t_start) / getTickFrequency() * 1000. << "ms" << endl;

#ifdef HEAD_POSE_ESTIMATION_DEBUG
imwrite("head_pose.png", estimator._debug);
#endif

}
3 changes: 3 additions & 0 deletions tools/ex_images.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/home/voletiv/AMOUNT_00745_19.jpg
/home/voletiv/AMOUNT_00745_20.jpg
/home/voletiv/AMOUNT_00745_21.jpg

0 comments on commit 24bee91

Please sign in to comment.