Skip to content

Commit

Permalink
add dlib test
Browse files Browse the repository at this point in the history
  • Loading branch information
kekxv committed May 21, 2020
1 parent 57c5fb5 commit 99c990b
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ else ()
endif ()
option(ENABLE_OPENCV "option for OpenCV" OFF)
if (ENABLE_OPENCV)
add_definitions(-DCV__ENABLE_C_API_CTORS)
else ()
list(FILTER srcSourceHeader EXCLUDE REGEX ".*CameraTool")
list(FILTER srcSourceHeader EXCLUDE REGEX ".*freetype_tool")
Expand Down Expand Up @@ -172,7 +173,7 @@ endif ()
target_link_libraries(${libTools_LIBRARIES}
${OpenCV_LIBS} ${FREETYPE_LIBRARIES} ${LIBUSB_1_LIBRARIES}
${dl_LIBRARIES} ${OPENSSL_LIBRARIES} ${X11_LIBRARIES} ${GTK3_LIBRARIES} ${NCURSES_LIBRARIES}
${OpenJPEG_LIBRARIES}
${OpenJPEG_LIBRARIES} ${dlib_LIBRARIES}
)
target_link_libraries(${libTools_LIBRARIES} ${Iconv_LIBRARIES})

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
41 changes: 41 additions & 0 deletions Example/opencv_faceDetect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Created by caesar kekxv on 2020/5/20.
//

#include <opencv_tool.h>
#include <logger.h>

using namespace std;
using namespace cv;

int main(int argc, char *argv[]) {
logger::instance()->init_default();
logger::instance()->d(__FILENAME__, __LINE__, "running go");

opencv_tool opencvTool(logger::get_local_path() + "/data/haarcascades/haarcascade_frontalface_default.xml");
logger::instance()->d(__FILENAME__, __LINE__, "opencvTool load %s", opencvTool.isLoad() ? "True" : "False");

cv::VideoCapture cap;
cap.open(0);
int fps = (int) (1000 / cap.get(CAP_PROP_FPS));
while (cap.isOpened()) {
Mat frame;
cap >> frame;
opencv_tool::rotate180(frame);

vector<Rect> faces;
opencvTool.findMaxFace(frame,faces);
for(const auto& item : faces){
cv::rectangle(frame, item, Scalar(255, 255, 255),2, LINE_8,0);
}

imshow("frame", frame);
if (27 == waitKey(fps)) {
break;
}
}

logger::instance()->d(__FILENAME__, __LINE__, "running finish");
return 0;
}

2 changes: 1 addition & 1 deletion Example/opencv_textDetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace std;
int main(int argc, char *argv[]) {
logger::instance()->init_default();

string faceXml = logger::get_local_path() + "/../data/haarcascades/haarcascade_frontalface_alt2.xml";
string faceXml = logger::get_local_path() + "/data/haarcascades/haarcascade_frontalface_alt2.xml";
opencv_tool::instance()->init(faceXml);
logger::instance()->d(__FILENAME__, __LINE__, "CascadeClassifier load : %s",
(opencv_tool::instance()->isLoad() ? "true" : "false"));
Expand Down
103 changes: 103 additions & 0 deletions Example/test_dlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*
This example program shows how to find frontal human faces in an image and
estimate their pose. The pose takes the form of 68 landmarks. These are
points on the face such as the corners of the mouth, along the eyebrows, on
the eyes, and so forth.
This example is essentially just a version of the face_landmark_detection_ex.cpp
example modified to use OpenCV's VideoCapture object to read from a camera instead
of files.
Finally, note that the face detector is fastest when compiled with at least
SSE2 instructions enabled. So if you are using a PC with an Intel or AMD
chip then you should enable at least SSE2 instructions. If you are using
cmake to compile this program you can enable them by using one of the
following commands when you create the build project:
cmake path_to_dlib_root/examples -DUSE_SSE2_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_SSE4_INSTRUCTIONS=ON
cmake path_to_dlib_root/examples -DUSE_AVX_INSTRUCTIONS=ON
This will set the appropriate compiler options for GCC, clang, Visual
Studio, or the Intel compiler. If you are using another compiler then you
need to consult your compiler's manual to determine how to enable these
instructions. Note that AVX is the fastest but requires a CPU from at least
2011. SSE4 is the next fastest and is supported by most current machines.
*/

#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
#include <src/logger/logger.h>
#include <opencv_tool.h>

using namespace dlib;
using namespace std;

int main() {
try {
cv::VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Unable to connect to camera" << endl;
return 1;
}

image_window win;
logger::instance()->init_default();

// Load face detection and pose estimation models.
frontal_face_detector detector = get_frontal_face_detector();
shape_predictor pose_model;
// deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
deserialize("shape_predictor_5_face_landmarks.dat") >> pose_model;

// Grab and process frames until the main window is closed by the user.
while (!win.is_closed()) {
// Grab a frame
cv::Mat temp;
if (!cap.read(temp)) {
break;
}
auto start = logger::get_time_tick();
resize(temp, temp, cv::Size(320, 180));
// opencv_tool::rotate180(temp);
// Turn OpenCV's Mat into something dlib can deal with. Note that this just
// wraps the Mat object, it doesn't copy anything. So cimg is only valid as
// long as temp is valid. Also don't do anything to temp that would cause it
// to reallocate the memory which stores the image as that will make cimg
// contain dangling pointers. This basically means you shouldn't modify temp
// while using cimg.
cv_image<bgr_pixel> cimg(temp);
logger::instance()->i(__FILENAME__, __LINE__, "%lld", logger::get_time_tick() - start);
// Detect faces
std::vector<rectangle> faces = detector(cimg);
logger::instance()->d(__FILENAME__, __LINE__, "%lld : faces.size() : %lu", logger::get_time_tick() - start,
faces.size());
// Find the pose of each face.
std::vector<full_object_detection> shapes;
for (unsigned long i = 0; i < faces.size(); ++i)
shapes.push_back(pose_model(cimg, faces[i]));
logger::instance()->d(__FILENAME__, __LINE__, "%lld", logger::get_time_tick() - start);

// Display it all on the screen
win.clear_overlay();
win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
}
}
catch (serialization_error &e) {
cout << "You need dlib's default face landmarking model file to run this example." << endl;
cout << "You can get it from the following URL: " << endl;
cout << " http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
cout << endl << e.what() << endl;
}
catch (exception &e) {
cout << e.what() << endl;
}
}

6 changes: 6 additions & 0 deletions cmake/example.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ if (clangToolsExample OR "${Tools_Other_Project}" STREQUAL "ON")
list(FILTER ExampleSourceSrc EXCLUDE REGEX ".*i2c\\.cpp")
endif ()

option(ENABLE_DLIB "option for dlib" OFF)
if (ENABLE_DLIB)
else ()
list(FILTER ExampleSourceSrc EXCLUDE REGEX ".*dlib\\.cpp")
endif ()

option(ENABLE_OpenJPEG "option for OpenJPEG" OFF)
if (ENABLE_OpenJPEG)
else ()
Expand Down
114 changes: 114 additions & 0 deletions cmake/find_libs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,117 @@ if (ENABLE_OpenJPEG)
endif ()
ENDIF (ENABLE_OpenJPEG)

option(ENABLE_DLIB "option for dlib" OFF)
if (ENABLE_DLIB)
find_package(dlib QUIET)
IF (dlib_FOUND)
INCLUDE_DIRECTORIES(${dlib_INCLUDE_DIR})
ELSE (dlib_FOUND)
include(ExternalProject)

if (libTools_LIBRARIES_CMAKE)
else ()
if (WIN32)
if (${CMAKE_GENERATOR} STREQUAL "Visual Studio 16 2019")
if (${CMAKE_GENERATOR_PLATFORM} STREQUAL "")
set(CMAKE_GENERATOR_PLATFORM WIN64)
endif ()
set(G_CMAKE_GENERATOR_PLATFORM
-G "${CMAKE_GENERATOR}" -A "${CMAKE_GENERATOR_PLATFORM}")
elseif ("${CMAKE_GENERATOR_PLATFORM}" STREQUAL "")
set(G_CMAKE_GENERATOR_PLATFORM
-G "${CMAKE_GENERATOR}")
else ()
set(G_CMAKE_GENERATOR_PLATFORM
-G "${CMAKE_GENERATOR} ${CMAKE_GENERATOR_PLATFORM}")
endif ()
ExternalProject_Add(dlib
URL https://github.com/davisking/dlib/archive/v19.19.tar.gz
URL_MD5 "94ec18c4f31c6c3b9af15306af1867ee"
CONFIGURE_COMMAND cmake -DCMAKE_BUILD_TYPE=RELEASE ${G_CMAKE_GENERATOR_PLATFORM} -DCMAKE_USER_MAKE_RULES_OVERRIDE=${ToolsCmakePath}/MSVC.cmake -DDLIB_USE_BLAS=OFF -DDLIB_USE_LAPACK=OFF -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dependencies -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} <SOURCE_DIR>
PREFIX ${CMAKE_BINARY_DIR}/dependencies
INSTALL_DIR ${INSTALL_DIR}
BUILD_COMMAND cmake --build "${CMAKE_BINARY_DIR}/dependencies/src/dlib-build"
INSTALL_COMMAND cmake --build "${CMAKE_BINARY_DIR}/dependencies/src/dlib-build" --target install
)
else ()
ExternalProject_Add(dlib
URL https://github.com/davisking/dlib/archive/v19.19.tar.gz
URL_MD5 "94ec18c4f31c6c3b9af15306af1867ee"
CONFIGURE_COMMAND CC=${CMAKE_C_COMPILER} CXX=${CMAKE_CXX_COMPILER} cmake -DCMAKE_BUILD_TYPE=RELEASE -DDLIB_USE_BLAS=OFF -DDLIB_USE_LAPACK=OFF -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/dependencies -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} <SOURCE_DIR>
PREFIX ${CMAKE_BINARY_DIR}/dependencies
INSTALL_DIR ${INSTALL_DIR}
BUILD_COMMAND ${MAKE}
)
endif ()
endif ()

set(dlib_FOUND ON)
set(dlib_LIB_DIR "${CMAKE_BINARY_DIR}/dependencies/lib")
set(prefix "lib")
if (WIN32)
# set(suffix "-d.lib")
set(suffix ".lib")
set(dlib_LIBRARIES
"${dlib_LIB_DIR}/${prefix}dlib${suffix}")
else ()
set(suffix ".a")
set(dlib_LIBRARIES "${dlib_LIB_DIR}/${prefix}dlib${suffix}")





endif ()
find_package(GIF QUIET)
if (GIF_FOUND)
set (dlib_needed_includes ${dlib_needed_includes} ${GIF_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${GIF_LIBRARY})
endif()
find_package(PNG QUIET)
if(PNG_FOUND)
set (dlib_needed_includes ${dlib_needed_includes} ${PNG_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARY})
endif()
find_package(JPEG QUIET)
if(JPEG_FOUND)
set (dlib_needed_includes ${dlib_needed_includes} ${JPEG_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${JPEG_LIBRARY})
endif()
find_package(OpenMP)
if (OPENMP_FOUND)
set(openmp_libraries ${OpenMP_CXX_FLAGS})
set (dlib_needed_libraries ${dlib_needed_libraries} ${openmp_libraries})
endif()


set(dlib_LIBS ${dlib_LIBRARIES} ${dlib_needed_libraries})


link_directories(${dlib_LIB_DIR})
set(dlib_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/dependencies/include/)
include_directories(${dlib_INCLUDE_DIRS})
add_definitions(-DBUILDING_LIBDLIB)

add_dependencies(${libTools_LIBRARIES} dlib)
if (libTools_LIBRARIES_CMAKE)
else ()
target_link_libraries(${libTools_LIBRARIES} ${dlib_LIBRARIES})
endif ()
# ENDIF (dlib_FOUND)
if (dlib_FOUND)
if ("${Tools_Other_Project}" STREQUAL "ON")
message(STATUS "dlib library status:")
message(STATUS " version: ${dlib_VERSION}")
message(STATUS " libraries: ${dlib_LIBS}")
message(STATUS " libraries: ${dlib_LIBRARIES}")
message(STATUS " lib_dir: ${dlib_LIB_DIR}")
message(STATUS " include path: ${dlib_INCLUDE_DIRS}")
endif ()
add_definitions(-DENABLE_DLIB=ON)
else ()
set(${dlib_LIBRARIES} "")
endif ()
endif ()
ENDIF (ENABLE_DLIB)

50 changes: 49 additions & 1 deletion cmake/resource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,52 @@ if (ENABLE_HZK)
install(FILES
${CMAKE_BINARY_DIR}/gov.json
DESTINATION bin)
endif (ENABLE_HZK)
endif (ENABLE_HZK)

option(ENABLE_DLIB "option for dlib" OFF)
if (ENABLE_DLIB)
option(ENABLE_DLIB_5_FACE_LANDMARKS "option for dlib Shape_predictor_5_face_landmarks" ON)
if (ENABLE_DLIB_5_FACE_LANDMARKS)
file(DOWNLOAD
https://github.com/ClangTools/clangTools/releases/download/shape_predictor_5_face_landmarks/shape_predictor_5_face_landmarks.dat.tar.gz
${CMAKE_BINARY_DIR}/Shape_predictor_5_face_landmarks.dat.tar.gz
TIMEOUT 120 INACTIVITY_TIMEOUT 120 SHOW_PROGRESS EXPECTED_MD5 "d41d8cd98f00b204e9800998ecf8427e"
)
add_custom_target(UnpackingShape_predictor_5_face_landmarks ALL)
add_custom_command(TARGET UnpackingShape_predictor_5_face_landmarks PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/Shape_predictor_5_face_landmarks.dat
COMMAND ${CMAKE_COMMAND} -E tar xjf ${CMAKE_BINARY_DIR}/Shape_predictor_5_face_landmarks.dat.tar.gz
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${CMAKE_BINARY_DIR}/Shape_predictor_5_face_landmarks.dat.tar.gz
COMMENT "Unpacking Shape_predictor_5_face_landmarks.dat"
VERBATIM)

install(FILES
${CMAKE_BINARY_DIR}/Shape_predictor_5_face_landmarks.dat
DESTINATION bin
)
else ()
file(DOWNLOAD
https://github.com/ClangTools/clangTools/releases/download/shape_predictor_68_face_landmarks/shape_predictor_68_face_landmarks.dat.tar.gz
${CMAKE_BINARY_DIR}/shape_predictor_68_face_landmarks.dat.tar.gz
TIMEOUT 120 INACTIVITY_TIMEOUT 120 SHOW_PROGRESS EXPECTED_MD5 "4ea78ffbb6b0c4d2e5fba9be71d93e9a"
)
add_custom_target(UnpackingShape_predictor_68_face_landmarks ALL)
add_custom_command(TARGET UnpackingShape_predictor_68_face_landmarks PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/shape_predictor_68_face_landmarks.dat
COMMAND ${CMAKE_COMMAND} -E tar xjf ${CMAKE_BINARY_DIR}/shape_predictor_68_face_landmarks.dat.tar.gz
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS ${CMAKE_BINARY_DIR}/shape_predictor_68_face_landmarks.dat.tar.gz
COMMENT "Unpacking shape_predictor_68_face_landmarks.dat"
VERBATIM)

install(FILES
${CMAKE_BINARY_DIR}/shape_predictor_68_face_landmarks.dat
DESTINATION bin
)

endif ()
endif (ENABLE_DLIB)



0 comments on commit 99c990b

Please sign in to comment.