dlib with QT MinGW slow even in Release mode - qt

I am compiling dlib 18.18 on Windows 10 for QT 5.6 with MinGW 4.9.2.
I have read all recommendations (http://dlib.net/faq.html#Whyisdlibslow): AVX or SSE4 instructions, Release mode.
The example "webcam_face_pose_ex.exe" works like a charm and really fast.
The problem:
But when I use the frontal_face_detector under QT the framerate is really low.
640x480 ~170 ms
1920x1080 ~1100 ms
System configuration: Windows 10 (x64), Intel Core i5-3550, QT 5.6, OpenCV 3.1.0, MinGW 4.9.2
Tried all variations of using AVX/SSE4 instructions in dlib and opencv.
But I think that it's something different - since the example is fast.
Code used in QT is copy-pasted from the example as well, even without the GUI.
In Debug mode 640x480 frames take 6-7 seconds to process.
.pro file dump:
DIR_DLIB = "$${LIBSDIR}dlib/dlib18.18/"
DEFINES += DLIB_ENABLE_ASSERTS
LIBS += -luser32 -lws2_32 -lgdi32 -lcomctl32 -limm32 -lwinmm
INCLUDEPATH += "$${DIR_DLIB}include"
DEPENDPATH += "$${DIR_DLIB}include"
LIBS += -L"$${DIR_DLIB}lib"
LIBS += -ldlib

Dlib face detector is header-based and does not depend on the compiler flags used to compile dlib.lib itself.
Dlib has a documented option "-DUSE_AVX_INSTRUCTIONS=ON", but it will not work if you are not building with CMAKE and including dlib/cmake file into your CMakeLists.txt
You should add AVX enabling flags for compiler into your project to make it work fast
The solution is to add an explicit parameter to your .pro file:
QMAKE_CXXFLAGS_RELEASE += -mavx
When you are compiling project in Qt Creator, you can see how does it call compiler and what flags are used. Ensure you have -mavx flag. -msse2 will work about 20% slower

Related

Qt requires C++11 support

I used Qt 5.7 and gcc 4.9.2. Qt Core module throw Qt requires C++11 support error.
This page say that
gcc 4.9.2 fails to compile Qt.
So I installed gcc 4.8. I check using below command on terminal :
$ g++ --version
g++ (Ubuntu 4.8.4-1ubuntu15) 4.8.4
My kit uses cmake not qmake. I add TARGET_LINK_LIBRARIES ( xxxx yyyy /usr/bin/c++ -std=c++11 to CMakeLists-txt.
I restart my pc and run my application again. Same error is throwed.
/opt/Qt/5.7/gcc_64/include/QtCore/qbasicatomic.h:61: error: #error "Qt requires C++11 support"
# error "Qt requires C++11 support"
^
How can I solve it?
If using QtCreator, you can add this to your .pro file:
CONFIG += c++11
https://wiki.qt.io/How_to_use_C%2B%2B11_in_your_Qt_Projects
Its has been a while.
How I finally solve it is indicating in CMakeLists.txt the following line just after project(MyProject):
add_compile_options(-std=c++11)
That says to cmake, to create a Makefile that will use c++11 solving issues.
solution for me was (in your .pro file):
QMAKE_CXXFLAGS += -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -mmacosx-version-min=10.7
QMAKE_LFLAGS += -mmacosx-version-min=10.7
Turn c++11 on explicitly:
set(CMAKE_CXX_FLAGS "-std=c++11" CACHE STRING "compile flags" FORCE) after project(...) declaration.
add_library(MyLib SHARED ${PROJECT_HEADERS} ${PROJECT_SOURCES})
...
set_property(TARGET MyLib PROPERTY CXX_STANDARD 11)
set_property(TARGET MyLib PROPERTY CXX_STANDARD_REQUIRED ON)
SOMETIMES, this will not be a configuration issue as mentioned in other answers. In my case, the problem was one file that happened to have been saved with a .CPP extension rather than .cpp. QMake (Qt5) was misidentifying the file and trying to compile it with the C compiler rather than the C++ compiler. The QMake from Qt4 was not exhibiting this issue. Renaming the file fixed the issue.
My comment at the time was "Could this really be that f&%%& simple!!"

Qt linker errors: cannot find -lQtCored

A problem with a Qt *.pro file:
TARGET = ProgName
TEMPLATE = app
CONFIG += console
QT += core
QT += gui
LIBS += -LC:\\Qt\\4.8.5\\bin
LIBS += -LC:\\Qt\\4.8.5\\lib
LIBS += -LS:\\lib
# LIBS += -lQtCored4 # not necessary
# LIBS += -lQtCore4
# LIBS += -lQtGuid4
# LIBS += -lQtGui4
SOURCES += ...
HEADERS += ...
I get these linker errors:
:-1: error: cannot find -lQtGuid
:-1: error: cannot find -lQtCored
collect2.exe:-1: error: error: ld returned 1 exit status
All the DLLs exist in the specified directories.
The documentation did not help much.
This pro file worked a few days ago, and it seems to have issues since I installed Qt 5.1 (this is not used, yet; I am still working with Qt4).
Platform: Windows 7, MinGW, Qt 4.8.4
You only need the following content:
TARGET = ProgName
TEMPLATE = app
CONFIG += console
QT -= gui
SOURCES += ...
HEADERS += ...
That is because core and gui are added to the QT variable automatically. In this particular case however, you can remove the gui default if you wish to build a console application as you seem. Although "CONFIG += console" could eventually do that for you. Besides this, everything will work automatically for you, so you do not need to pass the library path to the qt libraries and so forth. You would only need to use QT += widgets and similar lines if you used further Qt modules there are not there by default. Even then, you would not need to set the Qt library path as you did in your question.
Besides, if you wanna target cross-platform later, you may wish to add this:
CONFIG -= app_bundle
to avoid creating Mac bundle for console based applications.
If you have multiple versions of Qt installed as it seems, you need to use the qmake from the required version, and it will be fine. Just to give an example: I use qmake for Qt 5 on my Archlinux system, and qmake-qt4 for Qt4.
On windows, you should either have the desired qt installation bin in the path before the undesired, or you need to call desired qmake explicitly, something like C:\path\to\my\desired\qmake.
There's no need to link to any Qt libraries afaik.
If you're using Qt Creator you need to make sure Qt 4.8.4 is properly recognized in Tools - Options - Build & Run - Kits. Since you installed 5.1 this may no longer be true. Then set the 4.8.4 kit for your project and compile it (cleaning the build directory also helps sometimes).

Linking opencv ".a" libraries with mingw and Qt 5.0

This one is tough ! Please don't mistake this for a newbie question ;) I have far too many miles in this OpenCV + MinGW static stuff for the simple mistakes....
Environment:
a. Qt 5.0.1 built with MinGW (the standard opensource download from qt-project) - so everything is built with the same MinGW environment.
b. Using CMake I setup minimal build (using the same g++/gcc as Qt) and turned OFF SHARED_LIBRARIES.
c. generated the cmake + mingw32-make + mingw32-make install works fine.
up till here everything looks pretty smooth ...
Now when I added:
win32-g++:INCLUDEPATH += D:/Dev/opencv/build/install/include
win32-g++:LIBS += -LD:/Dev/opencv/build/install/lib
win32-g++:LIBS += -static
win32-g++:LIBS += -llibopencv_core243d
win32-g++:LIBS += -llibopencv_imgproc243d
win32-g++:LIBS += -llibopencv_highgui243d
(and also without the "win32-g++" addition..)
I get d:/dev/qt/qt5.0.1/tools/mingw/bin/../lib/gcc/i686-w64-mingw32/4.7.2/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -llibopencv_highgui243d
...
...
now.. I do have libopencv_highgui243d.a in the right directory, but the ld.exe linker is ignoring anything else than a lib ! which supposed to be a MS library (which I wanted to avoid in the first place!!!) - I even tried to rename the .a files to .lib which caused the linker to regard the files, but show many "unreferenced" errors...
So who's to blame? the linker? CMake? mingw? Qt? ....
I tried many command line / flag options but without success ..
You should be referring to the libraries as:
LIBS += -lopencv_core243d
LIBS += -lopencv_imgproc243d
LIBS += -lopencv_highgui243d
Since -l already inserts lib at the beginning of the name.

How to add specific flags to moc in a qmake project?

I compile a Qt executable using qmake && make on the following project.pro file:
INCLUDEPATH *= ../../dependencies/boost
QT *= opengl xml
CONFIG *= qt opengl static
TARGET = myexe
HEADERS = Viewer.hpp MainWindow.hpp Inspector.hpp
SOURCES = main.cpp Viewer.cpp MainWindow.cpp Inspector.cpp
However, when compiling, moc chokes on a boost macro which it cannot parse. To work around this bug, I need to pass the flag -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED to moc, but I cannot manage to do so.
How to I edit my .pro file to pass a given flag to moc? (but not to g++, as QMAKE_CXXFLAGS does)
A bit hackish, but what about if you overwrite the moc compiler so that it includes the flag. In the .pro:
QMAKE_MOC = $$QMAKE_MOC -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED
In my code in header files I just have:
#ifndef Q_MOC_RUN
//Here we include Boost or Ogre headers with Macro
#endif
It works perfectly in Qt5 x64 and need not recompiling anything.
There is an update to this issue for boost 1.53, see the last post here:
We need to add -DBOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION also.
For add specific flags to moc in a qmake project(qt5.2) need:
load(moc)
QMAKE_MOC += -DBOOST_INCLUDE_GUARD_GOES_HERE
The best way I found to do this is based on a comment on the accepted solution from David Faure:
QMAKE_MOC_OPTIONS += -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED
The other proposed methods that involve load(moc) and manipulating the QMAKE_MOC variable itself have an unfortunate side effect: they prevent qmake from automatically adding INCLUDEPATH variables (and maybe others) that would normally be on moc's command line, if INCLUDEPATH is set up after the call to load(moc).
This approach composes easier if you have your qmake configuration split up to multiple files; you don't need to ensure that the change to the moc command line comes after all INCLUDEPATH directories are set.
See for example the Qmake docs for variables which details
ten different variables in the QMAKE_CFLAGS_* pattern, and
ten different variables in the QMAKE_CXXFLAGS* pattern
and hence I would start with QMAKE_CXXFLAGS which is documented as:
QMAKE_CXXFLAGS
This variable contains the C++ compiler flags that are
used when building a project. The value of this variable is typically
handled by qmake or qmake.conf and rarely needs to be modified. The
flags specific to debug and release modes can be adjusted by modifying
the QMAKE_CXXFLAGS_DEBUG and QMAKE_CXXFLAGS_RELEASE variables,
respectively. Note: On the Symbian platform, this variable can be used
to pass architecture specific options to each compiler in the Symbian
build system. For example:
QMAKE_CXXFLAGS.CW += -O2
QMAKE_CXXFLAGS.ARMCC += -O0
For more information, see qmake Platform Notes.
So I'd start with
QMAKE_CXXFLAGS += -DBOOST_TT_HAS_OPERATOR_HPP_INCLUDED
which you can just add to your .pro file.
In case you are doing Ogre3D (OGRE 1.8.1 SDK for Visual C++ .Net 2010 (32-bit) ) then use QT5 32bit instead of the QT5 64bit version, it will pass.
Also consider this :
#ifndef BOOST_SYSTEM_NO_DEPRECATED
#define BOOST_SYSTEM_NO_DEPRECATED 1
#endif
I think it has been compiled (boost included) with 32bit thus i suppose i have to take src and recompile (using my 64bit) but thats another story where i wont go for now since OGRE 1.9 RC 1 SDK for Visual C++ .Net 2012 (64-bit) will be soon release on stable version.
Regards
EDIT 1 :
Downloaded Qt libraries 4.8.4 for Windows and configured my QT creator (the one coming with full QT5 and guess what …. Ogre3D is working like a charm when compiling on 4.8.4.
Qt 5.0.2 for Windows 32-bit (VS 2010, 485 MB)
Qt libraries 4.8.4 for Windows (VS 2010, 234 MB)
OGRE 1.8.1 SDK for Visual C++ .Net 2010 (32-bit)
I also mentionned it for who is interested here : http://qt-project.org/forums/viewreply/128660/

Linking Matlab shared library into Qt (Windows)

I want to use Matlab's C API within QT (http://www.mathworks.com/help/techdoc/matlab_external/f39876.html#bsfvqhp-1) under Windows for opening a .mat file. In my .pro file I have included
INCLUDEPATH += "C:\Program Files\MATLAB\R2010b\extern\include"
which works fine (the code compiles). But when trying to link the libmat.lib file (I have read the .dll files cannot be linked directly) using
LIBS += -L"C:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft" -llibmat
the application crashes on execution. The error given says [file].exe exited with code -1073741515
I'm neither a QT nor a Windows expert but for this project I am forced to use both (I guess it would be easier to fix this in GNU/Linux) so any help would be appreciated. Using Windows XP, QT version 4.7.0 with Qt Creator 2.0.1, and Matlab R2010b.
The last output from the compiler just in case it is useful:
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug/MainUI.exe debug/main.o debug/maingui.o debug/matparser.o debug/matutils.o debug/moc_maingui.o -L'c:/Qt/2010.05/qt/lib' -lmingw32 -lqtmaind "-LC:\Program Files\MATLAB\R2010b\extern\lib\win32\microsoft" -llibmat -lQtGuid4 -lQtCored4
I just tested building a simple C program that uses the MAT-File Interface Library with no problems. The example file is located in: matlabroot/examples/eng_mat/matcreat.c. I am compiling using MinGW on a Windows XP 32-bit machine. Here is the Makefile I used:
# root directory of MATLAB installation
MATLABROOT="/c/Program Files/MATLAB/R2010b"
.PHONY : all clean run
all: matcreat
matcreat:
gcc ${MATLABROOT}/extern/examples/eng_mat/matcreat.c -o matcreat \
-I${MATLABROOT}/extern/include \
-L${MATLABROOT}/extern/lib/win32/microsoft -llibmat -llibmx
clean:
rm -rf matcreat *.exe *.mat
run:
# UNIX uses LD_LIBRARY_PATH to find libs at runtime, Windows/MinGW uses PATH
#PATH=${MATLABROOT}/bin/win32:"${PATH}" ./matcreat
I suspect that the Matlab library will have been compiled with MSVC and since you say you are compiling your own code with MingW I would imagine the two are incompatible.
Have a look at the MingW page on mixing compilers for more information.

Resources