Using GPU with Python OpenCV on XU4

Post Reply
AnnaVT
Posts: 3
Joined: Tue May 19, 2020 11:24 pm
languages_spoken: english
ODROIDs: XU4
Has thanked: 0
Been thanked: 1 time
Contact:

Using GPU with Python OpenCV on XU4

Post by AnnaVT » Wed May 20, 2020 1:39 am

Hi, I am using an ODROID XU4 to read video from multiple cameras, do some image processing in OpenCV and send the processed video as an MJPEG stream to a webpage. The system is running Ubuntu 18.04 headless with OpenCV 4.1.2 and Python 3.6. Currently the CPU usage is very high and I was wondering if it is possible to use the GPU for tasks like image scaling and JPEG encoding. If so, how would I go about setting this up?

User avatar
mad_ady
Posts: 8151
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 567 times
Been thanked: 404 times
Contact:

Re: Using GPU with Python OpenCV on XU4

Post by mad_ady » Wed May 20, 2020 3:19 am

Not sure how much you can gain with hardware jpeg encoding. I may be wrong, but I remember crashoverride once played with the jpeg encoder (or was it on the c2?) and it was slower than turbojpeg software encoder.


AnnaVT
Posts: 3
Joined: Tue May 19, 2020 11:24 pm
languages_spoken: english
ODROIDs: XU4
Has thanked: 0
Been thanked: 1 time
Contact:

Re: Using GPU with Python OpenCV on XU4

Post by AnnaVT » Wed May 20, 2020 9:36 pm

mad_ady wrote:
Wed May 20, 2020 3:19 am
Not sure how much you can gain with hardware jpeg encoding. I may be wrong, but I remember crashoverride once played with the jpeg encoder (or was it on the c2?) and it was slower than turbojpeg software encoder.
Hmm, noted. I also built OpenCV with OpenCL and so from what I have read it *might* be using the GPU behind the scenes for certain functions anyway?? How do I tell?

Edit: Based on your method from this thread (viewtopic.php?f=99&t=25903), is isn't:
mad_ady wrote:
Sat Jan 05, 2019 2:06 am
There is no directly equivalent command on kernel 4.14, but if you set gpu governor to simple_ondemand, you should be able to view the current gpu frequency and infer wether it's used or not:

Code: Select all

mali=/sys/bus/platform/devices/11800000.mali/devfreq/devfreq0/governor
if [ -f "$mali" ]; then
    logger -s -t $0 "Setting Mali governor"
    echo 'simple_ondemand' > $mali
fi
cat /sys/bus/platform/devices/11800000.mali/devfreq/devfreq0/cur_freq
When I set the GPU to ondemand, it stays at 177MHz whether my code is running or not.

AnnaVT
Posts: 3
Joined: Tue May 19, 2020 11:24 pm
languages_spoken: english
ODROIDs: XU4
Has thanked: 0
Been thanked: 1 time
Contact:

Re: Using GPU with Python OpenCV on XU4

Post by AnnaVT » Sat May 23, 2020 12:17 am

Update: I finally stumbled across this page: https://wiki.odroid.com/etc/opencv/opencl and followed its instructions to install the GPU libraries and rebuild OpenCV. I am able to compile and run the two examples and indeed the GPU is faster on larger matrices. Hurray!

For my project I am using OpenCV with Python, so I tested the Python examples from here: https://www.learnopencv.com/opencv-transparent-api/ It does work but it's a bit finicky. I had to reduce the kernel size on the Gaussian functions from what was shown in the example, or else it throws this kind of error:

Code: Select all

OpenCL error CL_OUT_OF_RESOURCES (-5) during call: clEnqueueNDRangeKernel('row_filter_C1_D0', dims=2, globalsize=960x2176x1, localsize=16x16x1) sync=false
The Canny edge detect runs out of resources no matter what. I tested OpenCV's Laplacian and Sobel functions as well and those worked on the GPU provided the kernel size was small.

Running the examples, even in a loop, doesn't increase the GPU frequency when the governor is set to simple_ondemand, but they do run faster when GPU is set to performance. Regardless, the GPU was slower than the CPU for these Python examples (working on 1280x720 images); however it did significantly reduce the CPU load so this might still be helpful for my application where the CPU is pretty much railed.
These users thanked the author AnnaVT for the post:
mad_ady (Sat May 23, 2020 3:59 am)

User avatar
mad_ady
Posts: 8151
Joined: Wed Jul 15, 2015 5:00 pm
languages_spoken: english
ODROIDs: XU4, C1+, C2, C4, N1, N2, H2, Go, Go Advance
Location: Bucharest, Romania
Has thanked: 567 times
Been thanked: 404 times
Contact:

Re: Using GPU with Python OpenCV on XU4

Post by mad_ady » Sat May 23, 2020 4:18 am

Unfortunately the GPU is no speed daemon, but can help offload some tasks. You could have some process instance use the gpu while another use the cpu and do more work overall.

Post Reply

Return to “Ubuntu”

Who is online

Users browsing this forum: No registered users and 2 guests