Saturday, March 14, 2015

OpenBR + OpenCV + Qt with CMake

In this post I'll provide minimum required code to compile OpenBR, OpenCV, Qt with Cmake.

CMake is cross-platform free and open-source software for managing the build process of software using a compiler-independent method. It is designed to support directory hierarchies and applications that depend on multiple libraries.

OpenCV, OpenBR is popular computer vision libraries. First mainly aimed at real-time computer vision(general purposes), second mainly aimed at face detection and recognition.

OpenBR is highly dependent on Qt.

Example will consists from face detection problem. Main.cpp, CMakeLists and bash script to run all this stuff.

${PROJECT_DIR}/src/main.cpp:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <QString>
#include <iostream>
#include <openbr/openbr_plugin.h>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>

int main( int argc, char* argv[] ) {

    br::Context::initialize(argc, argv);
    br::Globals->quiet=true; //don't print output
    br::Globals->enrollAll = true; //enable to detect multiple faces
    QSharedPointer<br::Transform> detectionTransform = 
    br::Transform::fromAlgorithm(
        "Open+Cascade(FrontalFace)+Expand"
        "+ASEFEyes+Draw[distribute=false]"
        );
    cv::VideoCapture capture;
    capture.open(0);
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

    if (!capture.isOpened()) {
            std::cerr << "---(!)Error opening video capture\n";
            exit(1);
    }

    int counter = 0;
    time_t start, end;
    double fps;
    time(&start);
    cv::Mat frame, final;


    while(capture.read(frame)) {
        time(&end);
        fps = round(++counter / difftime(end,start));
        std::stringstream ss;
        ss << fps;

        // Openbr magic
        br::Template query(frame);
        br::Globals->enrollAll = true;    
        query >> *detectionTransform;
        cv::Mat detectionResult = query.m().clone();
   
        if (detectionResult.rows != 0) {
            final = detectionResult;
        } else {
            final = frame;
        }

        //add Fps to frame
        putText(final, "FPS: " +  ss.str(), cv::Point(10, 30),
                        cv::FONT_HERSHEY_PLAIN, 2,
                        cv::Scalar::all(255),
                        2, 3);
        
        imshow("Test", final);

        int c = cv::waitKey(5);
        if ((c == 'c') || (c == 'q') || (c == 27)) {
            break;
        }
    }
    
    br::Context::finalize();

}


${PROJECT_DIR}/src/CMakeLists.txt:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
cmake_minimum_required(VERSION 2.8.11)
project(MainTest)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(OpenCV REQUIRED)
find_package(OpenBR REQUIRED)

link_directories(/usr/lib/)

find_package(Qt5 5.4.0 REQUIRED Core Gui Xml )

include_directories(${Qt5Widgets_INCLUDE_DIRS} 
                    ${Qt5Core_INCLUDE_DIRS} 
                    ${Qt5Gui_INCLUDE_DIRS})

add_executable(MainTest main.cpp)

target_link_libraries(MainTest ${OpenCV_LIBS} ${OPENBR_LIBS} Qt5::Core) 


${PROJECT_DIR}/start.sh:

1
2
3
4
5
6
rm -rf build
mkdir build
cd build
cmake -DCMAKE_PREFIX_PATH=/usr/local/Cellar/qt5/5.4.0 ../src
make
./MainTest


Few Notes:
1. You should change DCMAKE_PREFIX_PATH to your Qt path.
2. Change Qt Version in CMakeLists.txt if you have 5.1.1 as example.

1 comment:

  1. Nice example. I don't know what I did wrong but I''m running your code and it's only detecting one face.

    ReplyDelete