Using Caffe in Qt Console Application - c++

I am trying to use caffe in Qt IDE,
I have the following code:
#include <QCoreApplication>
#include "caffe/caffe.hpp"
using namespace caffe;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Caffe::set_mode(Caffe::CPU);
return a.exec();
}
I just wanted to set Caffe in CPU Mode.
I have the following .pro file
QT += core
QT -= gui
CONFIG += c++11
TARGET = CaffeTest_v0
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
INCLUDEPATH += /home/ahmet/caffe/include \
/home/ahmet/caffe/distribute/include \
/home/ahmet/caffe/build/lib
LIBS += -lcaffe
SOURCES += main.cpp
I have the following problem :
My problem is I want to set caffe in CPU mode, I installed caffe in CPU mode.
How can I set Caffe in CPU mode in main?
P.S. I believe I installed caffe corectly, because I tested by the following command in gnome terminal
caffe train -solver lenet_train_solver.prototxt
I also searched the following websites:
http://tzutalin.blogspot.co.uk/2015/05/caffe-on-ubuntu-eclipse-cc.html
Error while including Caffe in C++ Project using cmake
https://github.com/BVLC/caffe/issues/3317
https://groups.google.com/forum/#!topic/caffe-users/DAv5EcsvbIU

In .pro file, I should add
DEFINES += CPU_ONLY
solves the problem.
Final .pro file is below:
QT += core
QT -= gui
CONFIG += c++11
TARGET = CaffeTest_v0
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
DEFINES += CPU_ONLY
INCLUDEPATH += /home/ahmet/caffe/include \
/home/ahmet/src/caffe/proto \
/usr/include/boost
LIBS += -L/home/ahmet/caffe/build/lib \
-lcaffe \
-L/usr/lib/x86_64-linux-gnu \
-lboost_system
SOURCES += main.cpp

Related

Library Version Mismatch (C++ HDF5 Windows)

When trying to run a simple program using the hdf5-file format i get these messages and the program crashes:
I found posts to other problems like this, but most of them refer to linux and python. I use C++ (Qt Creator) and Windows (Windows 7 64bit) with the MSVC17 64bit Compiler, so these solutions dont work for me.
I don't understand why this occurs, because i simply donwloaded the HDF5 version 1.10.2 and installed it, i don't know why it says something about the version 1.8.15. Where could this come from?
I guess "library version" (1.8.15) is the version of my hdf5.lib file, right?
What does "header version" (1.10.2) mean? I simply #include <hdf5.h> and #include <H5Cpp.h> in the main.cpp and added paths in the hdf5_test_2.pro (my project) file.
Here is my code:
hdf5_test.pro:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
win32: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5 -lhdf5_cpp -lhdf5_hl_cpp
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
win32:!win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/libhdf5.lib'
else:win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/liblibhdf5.a'
main.cpp:
#include <iostream>
#include <hdf5.h>
#include <H5Cpp.h>
using namespace std;
using namespace H5;
int main()
{
hid_t file_id;
herr_t status;
file_id = H5Fcreate("file.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
status = H5Fclose(file_id);
cout << "Hello World!" << endl;
return 0;
}
The code example i tried to use comes from High Level Introduction to HDF5 (Pages 16/17).
Solved (meaning next error)
I think i solved the problem: There was another Version of HDF5 installed (1.8.15) on the system (the one who used this PC before me installed it).
I told my programm to link my lib (1.10.2) and passed the path, but somehow he found the 1.8.15 version previously installed. I now told my program to link the old version and it worked.
My hdf5_test_2.pro (my project) looks now like this:
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
win32: LIBS += -LC:/Anaconda2/Library/lib/ -lhdf5 -lhdf5_cpp
INCLUDEPATH += C:/Anaconda2/Library/include
DEPENDPATH += C:/Anaconda2/Library/include
Nice so far, but i get these two warnings now:
Any ideas, what that means?

undefined reference in Qt creator for DCMTK libraries

I'm trying to run a simple application in Qt creator using DCMTK libraries and it doesn't work as it should. I receive the following message:
error: undefined reference to `DicomImage::DicomImage(char const*, unsigned long, unsigned long, unsigned long)'
main.cpp:
#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "iostream"
int main(int argc, char *argv[])
{
DicomImage *image = new DicomImage("test.dcm");
if (image != NULL){
std::cout << "hell yeah!";
}
return 0;
}
.pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2016-02-18T19:16:51
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
#------------------
CONFIG += c++11
INCLUDEPATH += "C:\Program Files (x86)\DCMTK_1\include"
LIBS += -L"C:\Program Files (x86)\DCMTK_1\lib"
LIBS +=-ldcmimgle -ldcmdata -loflog -lofstd
win32:LIBS += -lAdvapi32 -lofstd -loflog -ldcmdata -ldcmnet -ldcmimage -ldcmimgle -lws2_32 -lnetapi32 -lwsock32
I think something is wrong with .pro file but I cannot figure out. I have included the necessary libraries and still no use.. I read that for DCMTK the order of libraries is important but I think it is already in order and cannot figure out what could be. Can you provide a solution to this problem?

OpenCV3.0 With Qt creator 3.2 & Qt 5.4 build error "mingw32-make: *** [Makefile] Error 3"

I have compiled OpenCV 3.0 with Qt5.4 & Qt Creator 3.2 64 bits in a Windows 7 machine.
I have been trying to execute the most basic OpenCV functionalaty in loading a picture. Unfortunately it gives the following error:
C:\Qt\qt-5.4.0-x64-mingw492r0-sjlj\qt-5.4.0-x64-mingw492r0-sjlj\bin\qmake.exe
-spec win32-g++ CONFIG+=release -o Makefile ..\opencv_xpto\opencv_xpto.pro C:/Users/Nelson
Faria/Documents/Programacao/opencv_xpto/opencv_xpto.pro:14: Extra
characters after test expression. Error processing project file:
..\opencv_xpto\opencv_xpto.pro makefile:175: recipe for target
'Makefile' failed mingw32-make: *** [Makefile] Error 3 20:53:17: The
process
"C:\Qt\qt-5.4.0-x64-mingw492r0-sjlj\mingw64\bin\mingw32-make.exe"
exited with code 2. Error while building/deploying project opencv_xpto
(kit: Qt 5.4) When executing step "Make" 20:53:17: Elapsed time:
00:01.
The code I'm trying to execute is the following:
'#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
cv::Mat mat;
mat = cv::imread("img.JPG");
cv::namedWindow("hello");
cv::imshow("hello",mat);
cv::waitKey(0);
return 0;
}
And finally the .pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += C:\opencv-mingw\install\include
LIBS += -LC:\\opencv-mingw\\install\\x64\mingw\\lib
-lopencv_core300.dll \
-lopencv_highgui300.dll \
-lopencv_imgproc300.dll \
-lopencv_features2d300.dll \
-lopencv_calib3d300.dll
include(deployment.pri)
qtcAddDeployment()'
Can someone inlight me where I gone wrong?
Correct pro file like that:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH += C:/opencv-mingw/install/include
LIBS += -LC:/opencv-mingw/install/x64/mingw/lib
-lopencv_core300 \
-lopencv_highgui300 \
-lopencv_imgproc300 \
-lopencv_features2d300 \
-lopencv_calib3d300
include(deployment.pri)
qtcAddDeployment()
If you have a crash it is most probable that application is unable to load openCV dlls. There are two ways to fix this:
copy those dlls to directory with your executable (most probaly more or less something like that: <your roject location>\..\debug-windows-64x-qt5-XXX
add to the path variable location of those dlls

link error- Qt Creator OpenCV Ubuntu

I am currently having a link error with Qt.
I cant compile my code since i got the error message:
error: undefined reference to `cv::imread(cv::String const&, int)'
Heres the content of my .pro
QT += core
QT -= gui
TARGET = edge_detection
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv
INCLUDEPATH += /usr/local/include/opencv2
LIBS += -L/usr/local/lib
LIBS += -lopencv_core
LIBS += -lopencv_imgproc
LIBS += -lopencv_highgui
Heres my simple code:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(){
cv::Mat img;
img=cv::imread("/home/cgross/Downloads/2.jpg");
return 0;
}
I am not sure of where is the problem
includepath must show opencv and opencv2.just like that:
INCLUDEPATH += C:\\opencv-2.4.6.1\\build-msvc2012\\install\\include
LIBS+=-LC:\\opencv-2.4.6.1\\build-msvc2012\\install\\lib \
-lopencv_calib3d246d \
-lopencv_contrib246d \
-lopencv_core246d \
-lopencv_features2d246d \
-lopencv_flann246d \
lıbs must show .lib file
Your code builds and runs just fine for me on a somewhat antique stock ubuntu 12.10 with opencv 2.3 from the distribution packages.
It picks up a cv::imread(std::string const &, int) from libopencv_highgui.

OpenCV with Qt: The program has unexpectedly finished

I am trying to configure OpenCV with Qt Creator 2.7.0 (Qt 5.0.2) on windows 8 64bit.
While executing my program, I get the following error:
The program has unexpectedly finished.
This is my main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
IplImage *image = cvLoadImage("E:\\lena.jpg"); //If this is removed, the program runs OK
return a.exec();
}
My .pro file is
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
unix:!mac {
message("* Using settings for Unix/Linux.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Mac OS X
macx {
message("* Using settings for Mac OS X.")
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib/ \
-lopencv_core \
-lopencv_highgui \
-lopencv_imgproc
}
## OpenCV settings for Windows and OpenCV
win32 {
message("* Using settings for Windows.")
INCLUDEPATH += "C:\\OpenCV\\opencv\\build\\include" \
"C:\\OpenCV\\opencv\\build\\include\\opencv" \
"C:\\OpenCV\\opencv\\build\\include\\opencv2"
LIBS += -L"C:\\OpenCV\\opencv\\build\\x64\\mingw\\lib" \
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
}
Environment Variables are:
OPENCV_DIR:C:\OpenCV\opencv\build\x64\mingw Path:
G:\5.0.2\Tools\MinGW\bin;G:\Qt\5.0.2\mingw47_32\bin
What could be the problem ?
I suggest you use the OpenCV 2.x API if possible. The error handling is better.
This would be cv::Mat image = cv::imread("E:\lena.jpg");
If the image is empty, it means you have the wrong path.
Also, make sure the opencv dll are in the path of your executable (core, highgui and imgproc).
You might need to change
-lopencv_core244 \
-lopencv_highgui244 \
-lopencv_imgproc244
to
-lopencv_core244d \
-lopencv_highgui244d \
-lopencv_imgproc244d
note 'd' at the end of each lib (if you are to build in debug)
Have you solved the problem?
Have you tried adding system("PAUSE"); after the return a.exec(); ?
Just some extra notes:
Also try
try
{
...
}
catch (Exception e)
{
...
}
to see if you can find the error
thirdly, check the file permissions for lena.jpg
Lastly, see if you can use the path E:\lena.jpg