Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

High cpu usage in OpenCV java

OpenCV v3.10

I have written a multithreaded application that fetches rtsp streams from several cameras. Quality of cameras varies from 640x480 cameras to 4k hd cameras.

I am struggling with very high cpu usage. Opening 4 cameras simultaneously has my CPU loaded at 40-50-60% (depending on test bench), which is way too high. Especially the 4k cameras introduce a massive CPU increase. The increased CPU is caused by the following code alone, before I do any parsing and/or drawing of the fetched mats.

Here is code for fetching from the streams:

Thread grabber = new Thread(new Runnable() {
            @Override
            public void run() {
                int error_count = 0;
                while(keep_running) {
                    while(camera.grab()) {
                        error_count = 0; // Reset error count - we're OK
                        if(!keep_running)
                            break; // Someone stopped the camera!
                        // Spawn a new thread that can retrieve the frame!
                        Thread t = new Thread(() -> {
                            mutex = true;
                            Mat tmp = new Mat();
                            if(camera.retrieve(tmp)) {
                                tmp.copyTo(grabbed_frame);
                            }
                            tmp.release();
                            if(write_video) {
                                video_writer.write(grabbed_frame);
                            }
                            mutex = false;
                        });t.start();
                    }
                    error_count++;
                    if(error_count > 50) {
                        System.out.println("Error count too high on camera " + name + "("+ip+")");
                        keep_running = false;
                        break;
                    }
                }

Is there any way I can reduce the cpu usage dramatically?