Ask Your Question
0

Help updating 10 lines of deprecated OpenCV code in C++ [closed]

asked 2020-07-26 17:37:11 -0500

Hari Seldon gravatar image

I need help updating 10 deprecated (OpenCV 3.2.0) lines of code found in https://github.com/yirgagithub/Cat-and-Dog-SVM-classifier (their last commit was on Mar 1, 2017) to work with OpenCV 4 on Ubuntu 20.04 LTS, because this is one of the few GitHub projects that uses C++ (NOT Python) to do image classification (in this case distinguising pictures of dogs and cats). I am taking a Udacity C++ online nanodegree courseand for their capstone project they suggest either making a computer game or creating an AI. I have decided to create an image classifier AI starting from https://github.com/yirgagithub/Cat-an... because that is the closest to what I might do when I get a job.

I understand that there are OpenCV courses (https://opencv.org/courses/) that if I spent weeks or months on I might figure this out on my own, but I have already taken several R Studio and Python (Spyder) AI online courses (that did not use OpenCV they used TensorFlow, etc.) so I do not want to spend time on more. I looked for C++ TensorFlow GitHub image classifier projects and that is not promising.

Below is my Fork of https://github.com/yirgagithub/Cat-and-Dog-SVM-classifier. The fork has the settings.json and launch.json in the .vscode directory and I updated some deprecated #include to their new locations in OpenCV 4. I ran cmake (below) and got stuck (errors) on the 12 deprecated elements:

cmake ..

cmake --build . --config Release

My Fork: https://github.com/ProfHariSeldon/CppND-Capstone-Hello-World

You can skip the Install Instructions and just look at DEPRICIATED: to see the code I need help with. I have confirmed that Visual Studio Code recognizes the OpenCV #include (I added that path "/home/tlroot/installation/OpenCV-master/include/opencv4/" to settings.json) and that cmake finds the OpenCV files too (I added that path "/home/tlroot/installation/OpenCV-master/" to CMakeLists.txt). I also don't really understand my CMakeLists.txt this is my first time building one from scratch. I got it working but if you have advice please let me know.

Install instructions

How to upgrade to Ubuntu 20.04 LTS sudo do-release-upgrade -d -f DistUpgradeViewGtk3

How to Install OpenCV 4 on Ubuntu Instructions here: https://www.learnopencv.com/install-opencv-4-on-ubuntu-18-04/

Download: https://github.com/spmallick/learnopencv/blob/master/InstallScripts/installOpenCV-4-on-Ubuntu-18-04.sh

building from Source in my home directory I guess that's OK

$ cd /home/tlroot

$ sudo chmod +x ./installOpenCV-4-on-Ubuntu-18-04.sh

$ sudo bash installOpenCV-4-on-Ubuntu-18-04.sh

$ cd /home/tlroot/installation

$ sudo ldconfig

$ cd /home/tlroot/C++/Capstone

$ git clone https://github.com/yirgagithub/Cat-an...

The CMakeLists.txt file to use

# see CMake Lists.txt file in /home/tlroot/Documents/C++/OOP/Project/CppND-System-Monitor

# https://www.learnopencv.com/install-o...

cmake_minimum_required(VERSION 3.1)

# Enable C++11

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

SET(OpenCV_DIR /home/tlroot/installation/OpenCV-master/include/opencv4/)

# https://stackoverflow.com/questions/5...

find_package( OpenCV REQUIRED PATHS "/home/tlroot/installation/OpenCV-master/")

project(classifier)

file(GLOB SOURCES "dictionary/*.cpp")

add_executable(classifier ${SOURCES})

# https ... (more)

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Hari Seldon
close date 2020-07-29 14:47:03.522746

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-07-27 09:11:03 -0500

berak gravatar image

updated 2020-07-27 09:25:05 -0500

the code you're trying with, is unfortunately from opencv's deprecated 2.x api (not even 3.2 !), a lot of things changed since then:

SIFT was moved back to the main repository (patent expired) with 4.3, you would use that like:

#include "opencv2/features2d.hpp"
Ptr<Feature2D> sift = SIFT::create()
sift->detect(...)

also the ml api changed significantly:

#include "opencv2/ml.hpp"
Ptr<ml::SVM> svm = ml::SVM::create(); //no more params struct & ml namespace
svm->setKernel(ml::SVM::LINEAR); // setters for the params
svm->train(data, ml::ROW_SAMPLE, labels);

then, you need more / better data ! trying BoW with 40 images is ridiculous.

you'll probably also get better results using a cnn and transfer learning

edit flag offensive delete link more

Comments

I switched to the CNN you suggested and I have one little question about the CMakeLists.txt. How do I add the OpenCV library? I tried:

target_link_libraries( classifier ${OpenCV_LIBS} )

And that compiles fine but when I run ./classifier I get an error:

./classifier: error while loading shared libraries: libopencv_core.so.4.4: cannot open shared object file: No such file or directory

So then I direct linked:

target_link_libraries( classifier "/home/tlroot/installation/OpenCV-master/lib/" )

But then I got the "cmake .." error: WARNING: Target "classifier" requests linking to directory "/home/tlroot/installation/OpenCV-master/lib/". Targets may link only to libraries. CMake is dropping the item.

And cmake --build . --config Release couldn't find the OpenCV files in #include

Hari Seldon gravatar imageHari Seldon ( 2020-07-27 13:52:57 -0500 )edit

error while loading shared libraries: libopencv_core.so.4.4: cannot open shared object file: No such file or directory

you installed the opencv so's to a non standard directory, now they cannot be found at runtime

lookup how to use ldconfig to add the path

berak gravatar imageberak ( 2020-07-28 02:15:48 -0500 )edit

I can't find an ldconfig help forum so I have a quick question. I tried sudo ldconfig -n /home/tlroot/installation/OpenCV-master/lib and sudo ldconfig -n /home/tlroot/installation/OpenCV-master/include/opencv4 based on advice from https://linux.101hacks.com/unix/ldconfig/ and neither fixed the two errors. I could try sudo ldconfig -l I looked at the man page but I don't know which files to add. Just libopencv_core.so.4.4 or more? I tried to add include /home/tlroot/installation/OpenCV-master/lib/ and include /home/tlroot/installation/OpenCV-master/include/opencv4/ to ld.so.conf and do ldconfig and that didn't work

Hari Seldon gravatar imageHari Seldon ( 2020-07-28 13:15:09 -0500 )edit

OK I figured out ldconfig. The MAN page advice about adding to ld.so.conf did not work, but creating a file called opencv.conf in /etc/ld.so.conf.d/ and adding /home/tlroot/installation/OpenCV-master/lib/ to it got everything working. Thank you so much berak for telling me about cats.vs.dogs.cpp that was much easier to adapt than the code I found.

Hari Seldon gravatar imageHari Seldon ( 2020-07-29 14:46:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2020-07-26 17:37:11 -0500

Seen: 60 times

Last updated: Jul 27