Python Multithreaded camera and visualization fast

Viewed 33

Challenge: I want to run three USB cameras 1600x1300@ 60 fps on a jetson Xavier NX using python. Now there are some ways of doing this but my approach has been:

Main -> Camera1 Thread -> Memory 1 -> Visualization thread 1.

The main starts up three Camera threads and three visualizations. The problem is the latency. I store the images from camera 1 in Memory 1 which is shared with the visualization thread. There are thread-lock on both the memory and cv2.imshow in the visualization thread.

  • Is there a way of speeding up the camera visualization. I get about 16fps. Is it better to have 1 visualization thread showing all three images in one view or as I have now, three separate.

The input capture is:

cv2.VideoCapture(Gstreamer_string, cv2.CAP_GSTREAMER)

The output to disc with the Gstreamer string is by branching the stream to a multifilesink and an appsink. The file-sink writes all three at 60FPS. Its just the visualization on screen that takes for-ever.

I have tried also to visualize directly after the capture in the camera thread, without the memory, not much difference. I have a tendency to think that the imshow thread-lock I need in order not to crash/freeze the GUI is the reason. Perhaps combining all three into one is faster.

1 Answers

It is hard to guess without code, but possible bottlenecks may be:

  1. cv imshow is not so efficient on Jetsons. You may use opencv VideoWriters with gstreamer backend to some display sinks such as nveglglessink.

  2. Your disk storage may not be able to store 3 streams at that resolution at 60 fps. Are you using a NVME SSD ? SD Card may be slow depending on model. Does lowering the framerate help ? Are you encoding or trying to save RAW video ?

  3. Opencv may also add some overhead. If opencv is not required for processing, a pure gstreamer pipeline may be able to display and record (if point 2 is not the issue).

Related