Performance of OpenCV3.4 vs OpenCV4 on the XAVIER (Solved)

I was curious about the performance difference of OpenCV3 vs OpenCV4 on the Jetson XAVIER. So I compiled OpenCV4 from source and was hoping for a little performance boost. Unfortunately quite the opposite happened.
When I captured the video from my webcam and ran some Gaussian Blur and Canny-Edge, the output was extremely laggy (OpenCV3.4 rab super smooth in comparison)

The code I used (EXAMPLE/cannyDetection.py):

Someone experience the same issue with OpenCV4?

I haven’t tried, but one thing to think about: Have you tried compiling OpenCV3.4 from source and comparing it to the supplied version? In your build script, there do not appear to be any compiler flags. My (possibly naive) guess is that the supplied version of 3.4 has architecture specific flags for optimization which produce some incremental gains.

NVIDIA has optimized the OpenCV that ships with JetPack more than just using compiler flags – it’s configured and patched to take advantage of available hardware in many cases. Building from source will not achieve the same benefits. We had the same “problem” on previous Jetsons, so we just have to accept that we should use the version NVIDIA ships.

I am the first time to build 4.0.0-pre, but I did not feel particularly late.

In Xavier, the speed was quite different depending on TBB ON/OFF(-D WITH_TBB=ON) at 3.4 build.
So it is a difference in build options.

opencv-3.3.1(JetPack4.1): cv2_Canny(): 0.26661168300779536
opencv-3.4.1(build from source): cv2_Canny(): 0.2553883320069872
opencv-4.4.0-pre(build from source): cv2_Canny(): 0.3225515089870896
opencv-4.4.0-pre(build from source with your option): cv2_Canny(): 2.381617232997087

Test code:

import timeit
import sys
import cv2
print("opencv - {}".format(cv2.__version__))
print("Python - {}".format(sys.version))

N = 100
print("N = {}".format(N))

cap = cv2.VideoCapture(0)
width=1280
height=720
edgeThreshold = 40
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
if not cap.isOpened():
    # camera failed
    raise IOError(("Couldn't open video file or webcam."))
ret_val, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(hsv, (7, 7), 1.5 )

def cv2_read():
    ret_val, frame = cap.read()
def cv2_resize():
    f = cv2.resize(frame, (640, 360))
def cv2_toGray():
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
def cv2_GaussianBlur():
    blur = cv2.GaussianBlur(hsv, (7, 7), 1.5 )
def cv2_Canny():
    edges = cv2.Canny(blur, 0, edgeThreshold)

if __name__ == "__main__":
    t = timeit.timeit(setup="from __main__ import cv2_read", stmt='cv2_read()', number=N)
    print("cv2_read(): {}".format(t))
    t = timeit.timeit(setup="from __main__ import cv2_resize", stmt='cv2_resize()', number=N)
    print("cv2_resize(): {}".format(t))
    t = timeit.timeit(setup="from __main__ import cv2_toGray", stmt='cv2_toGray()', number=N)
    print("cv2_toGray(): {}".format(t))
    t = timeit.timeit(setup="from __main__ import cv2_GaussianBlur", stmt='cv2_GaussianBlur()', number=N)
    print("cv2_GaussianBlur(): {}".format(t))
    t = timeit.timeit(setup="from __main__ import cv2_Canny", stmt='cv2_Canny()', number=N)
    print("cv2_Canny(): {}".format(t))

Result: opnecv-3.3.1 (JetPack4.1)

opencv - 3.3.1
Python - 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
N = 100
cv2_read(): 13.39863662699645
cv2_resize(): 0.038163264995091595
cv2_toGray(): 0.025768125997274183
cv2_GaussianBlur(): 0.5404331860045204
cv2_Canny(): 0.26661168300779536

Result: opencv-3.4.1 (build from source)

opencv - 3.4.1
Python - 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
N = 100
nvbuf_utils: Could not get EGL display connection
nvbuf_utils: Could not get EGL display connection
nvbuf_utils: Could not get EGL display connection
nvbuf_utils: Could not get EGL display connection
cv2_read(): 3.3356981789984275
cv2_resize(): 0.03994057800446171
cv2_toGray(): 0.02382903700345196
cv2_GaussianBlur(): 0.14375564000511076
cv2_Canny(): 0.2553883320069872

Result: opencv-4.0.0-pre (build from source)

opencv - 4.0.0-pre
Python - 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
N = 100
cv2_read(): 14.22163662899402
cv2_resize(): 0.043780744992545806
cv2_toGray(): 0.02563618701242376
cv2_GaussianBlur(): 0.11342920899915043
cv2_Canny(): 0.3225515089870896

Result: opencv-4.0.0-pre (build from source with your option)

opencv - 4.0.0-pre
Python - 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
N = 1000
cv2_resize(): 0.36299609699926805
cv2_toGray(): 0.16359981399727985
cv2_GaussianBlur(): 0.6509453210019274
cv2_Canny(): 2.381617232997087

Sorry, I missed loop times.

opencv-3.3.1(JetPack4.1): cv2_Canny(): 0.26661168300779536
opencv-3.4.1(build from source): cv2_Canny(): 0.2553883320069872
opencv-4.4.0-pre(build from source): cv2_Canny(): 0.3225515089870896
opencv-4.4.0-pre(build from source with your option): cv2_Canny(): 0.30854691300191917

Result: opencv-4.0.0-pre (build from source with your option)

opencv - 4.0.0-pre
Python - 3.6.6 (default, Sep 12 2018, 18:26:19) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]]
N = 100
cv2_read(): 13.420250533003127
cv2_resize(): 0.036654338997323066
cv2_toGray(): 0.019496705004712567
cv2_GaussianBlur(): 0.1020277320058085
cv2_Canny(): 0.30854691300191917

It’s not slow for me.

@naisy

Thanks, I am about to try those options too.

I also figured out that I compiled it for the PASCAL architecture and not for VOLTA.

I will play around and give an update later on

So I did some more troubleshooting and checked the frame rate of the video capturing device via

video_capture.get(cv2.CAP_PROP_FPS)

This gave me a value of 3 back?! After setting the fps explicitly to 30 via:

video_capture.set(cv2.CAP_PROP_FPS,30)

… I get an absolutely smooth performance. Weirdly after setting the FPS to 30, OpenCV is still reporting 3 FPS back when I do the get-Command?

@Markstein you may have to perform a read so that cap props are updated.

In my environment 3.4.1/3.4.2 are 30 FPS, 3.3.1/3.4.3/4.0.0-pre are 7.5 FPS.
If you expect similar behavior in different opencv versions, it seems necessary to set up FPS.

I tried that but for whatever reason I always get 3.0 back.
Though setting the fps works.