TX2 OpenCV tracking Segfault

I know this is a very specific problem, but i cannot get any OpenCV configuration, on which trackerKCF would work since moving from TX1 to TX2. With JetPack3.1 and OpenCV 3.3, it would fail with a Segmentation fault. With OpenCV 3.2 it could not compile opencv_tracking because in the words of Alalek “BTW, glog/gflags autodetection doesn’t work on Ubuntu 16.04 at this moment, but it works on Ubuntu 14.04.” https://github.com/opencv/opencv/issues/8536 and none of the linking and giving paths helped cMake find these dependencies.
Is there any hope that JetPack 3.0 will be different? Has anyone successfully launched OCV trackerKCF? Any other suggestions?

Hi ArvidsP,

I have no experience with tracker, but some experience in opencv configurations.
If you can share some ready code trigerring the fault, I would be curious to try to investigate this.
I have opencv-3.3.0 for a R28.1 (JetPack3.1) TX1 and for a TX2 R27.0.1.

Best regards,
HP

Hi, Honey_Patouceul!

I have never made test examples, but i think this should work.

trackinfo.h:

#include <opencv2/opencv.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>

using namespace std;
using namespace cv;

class TrackInfo {
private:
    string method;
    Ptr<Tracker> tracker;

public:
    TrackInfo(const string tracking_method);
    ~TrackInfo();
};

trackinfo.cpp:

#include <opencv2/opencv.hpp>
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>

#include "trackInfo.h"

using namespace std;
using namespace cv;

TrackInfo::TrackInfo(const string tracking_method)
{
    TrackerKCF::Params param; // THIS is where the segfault occurs
    param.desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
    param.desc_npca = 0;
    param.compress_feature = true;
    param.compressed_size = 2;
    param.resize = true;
    tracker = TrackerKCF::create(param);
}

Probably most of this is unneccesary and you can trim it much more, but i hope you know what you are doing. You will need to link OpenCV libs to the compiler. Feel free to ask for more info, as i’m sure i missed something.

Best regards,
Arvids

Hi Arvids,

So far, I can see no problem neither with TX2 nor TX1.
Do you have more information for triggering the fault ?
I have just downloaded last zip archive of opencv_contrib from github and reconfigured my opencv-3.3.0 build with the path of its modules directory (using cmake-gui is convenient to check and adjust configurations, but I use to disable any opencl* it may enable in ‘WITH’ options), rebuilt and installed opencv-3.3.0.
Compiling and running an application with one instance of this class works fine (just added a default destructor, but probably unrelated).
Please check your PMs for details and let us know.

This example works fine on my TX2. I use the KCF tracker with the onboard camera video :

#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>

using namespace std;
using namespace cv;

int main( int argc, char** argv ){
  // Gstreamer pipeline for accessing on board camera, rotate image 180° writing to CPU mem and convert to BGR for opencv input  
  const char* gst =    "nvcamerasrc             ! video/x-raw(memory:NVMM), format=(string)I420, width=640, height=480, framerate=30/1 ! \
						nvvidconv flip-method=2 ! video/x-raw,              format=(string)BGRx ! \
						videoconvert            ! video/x-raw,              format=(string)BGR  ! \
						appsink";

// declares all required variables
  Rect2d roi;
  Mat frame;

  // create a tracker object
  Ptr<Tracker> tracker = TrackerKCF::create();

  // set input video
  VideoCapture cap(gst);

  // Skip the first second to be sure it has auto adjusted exposure (first frames are dark)
  for (int i=0; i < 30; i++)
  	cap >> frame;

  cap >> frame;

  roi=selectROI("tracker",frame);
  //quit if ROI was not selected
  if(roi.width==0 || roi.height==0)
    return 0;
  // initialize the tracker
  tracker->init(frame,roi);
  // perform the tracking process
  printf("Start the tracking process, press ESC to quit.\n");
  for ( ;; ){
    // get frame from the video
    cap >> frame;
    // stop the program if no more images
    if(frame.rows==0 || frame.cols==0)
      break;
    // update the tracking result
    tracker->update(frame,roi);
    // draw the tracked object
    rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
    // show image with the tracked object
    imshow("tracker",frame);
    //quit on ESC button
    if(waitKey(1)==27)break;
  }
  return 0;
}

My opencv-3.3.0 with opencv_contrib modules being installed in /usr/local/opencv-3.3.0, I just build the application and run it with:

export OPENCV_VERSION=3.3.0
export OPENCV_DIR=/usr/local/opencv-$OPENCV_VERSION
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$OPENCV_DIR/lib

gcc -O3 \
    -I$OPENCV_DIR/include \
    example_tracking_kcf.cpp \
    -L$OPENCV_DIR/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio -lopencv_tracking -lstdc++ -lm \
    -o testTracker

./testTracker

[EDIT: Seems that opencv_contrib (cnn/dnn) could also cause problem on python caffe.]

I found a not-so-good solution. Cloned and installed OpenCV and opencv-contrib from TX1, which had an older release of OpenCV 3.3.

opencv
commit 9313978f618dbe39ad737ccd8bffa23b52d59aaa
Merge: ce6b06e 2360291
Author: Alexander Alekhin alexander.a.alekhin@gmail.com
Date: Thu Jul 20 15:32:47 2017 +0000

opnecv_contrib:
commit ffdec47924cc680518f7d0c56064fff393b6e996
Merge: 07fb53c 02ea1b6
Author: Alexander Alekhin alexander.a.alekhin@gmail.com
Date: Fri Jul 21 04:57:38 2017 +0000

Built protobuf 3.4 and now it works on TX2 as well.

Just to confirm the above example also works fine on my TX2 upgraded to L4T 28.1.
I’ve used released zip archive for opencv-3.3.0 and opencv_contrib, rather than git TOT.

[EDIT: Note that from L4T R28.1, the flip-method=2 of nvvidconv is no longer required, nvcamerasrc now sends the image upside up. You may change to flip-method=0 (do nothing) or no flip-method option (default is 0).]

Hi,

I have tried to install opencv 3.3 with opencv_contrib 3.3 many times and with many configurations, but still receive the following error:

tracker = cv2.Tracker_create(tracker_type)

Traceback (most recent call last):
File “”, line 1, in
AttributeError: module ‘cv2’ has no attribute ‘Tracker_create’

Please advise.

Hi,

Did you get the exact commit of opencv and opencv_contrib? Also when launching tracker, some functions have changed names, compared to older versions of openCV. For me it was TrackerKCF::createTracker -> TrackerKCF::create, or the other way around.
In your case it seems to be a problem with calling the right function.

Oh, sorry, you are not at that step yet, if i understand correctly. Also build protobuf 3.4 manually.

Arvids

Hi,

I think you’re right.

What do you mean by: " build protobuf 3.4 manuall " ?

Thank you

Go to download site https://github.com/google/protobuf/releases and download protobuf-cpp-3.4.0.tar.gz. Follow these instructions https://github.com/google/protobuf/blob/master/src/README.md to install. Then use this installation when building opencv.

Arvids

Great, thanks.

Hi
I’ve tried this with opencv 4.1.1
but there’s no tracking.hpp.
Is this only possible for opencv 3.3?

The include path is slightly different with opencv4:

g++ -std=c++11 -Wall -I$OPENCV_PATH/include/opencv4  testTracker.cpp -L$OPENCV_PATH/lib -lopencv_core -lopencv_imgproc -lopencv_video -lopencv_videoio -lopencv_highgui -lopencv_tracking  -o testTracker

Your opencv library needs to have been configured and built with opencv_contrib where the trackers are defined.

You would also have to change the pipeline with recent L4T releases to:

const char* gst = "nvarguscamerasrc ! video/x-raw(memory:NVMM), format=NV12, width=640, height=480, framerate=30/1 ! "
		 		  "nvvidconv        ! video/x-raw,              format=(string)BGRx ! "
		 		  "videoconvert     ! video/x-raw,              format=(string)BGR  ! "
		 		  "appsink";

This works fine with opencv-4.2.2 on L4T R32.3.1 with a OV5693 camera module, you would adjust for your camera.

I changed my opencv version to 3.4.0
opencv-3.4.0 is in /home/nvidia/opencv/opencv-3.4.0

export OPENCV_VERSION=3.4.0
export OPENCV_DIR=/usr/local/opencv-$OPENCV_VERSION
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$OPENCV_DIR/lib

gcc -O3
-I$OPENCV_DIR/include
example_tracking_kcf.cpp
-L$OPENCV_DIR/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_videoio -lopencv_tracking -lstdc++ -lm
-o testTracker

./testTracker

is this correct?

It depends…What did you set as CMAKE_INSTALL_PREFIX ? And did you run ?

sudo make install

Thank you. It worked fine:)