Issue with Reading Frames from CCTV Camera Using OpenCV via RTSP

I manage multiple CCTV cameras connected via RTSP (Real-Time Streaming Protocol). The program continuously captures frames from these cameras. After reading a frame, it’s added to a list which is then returned. Instead of implementing YOLO object detection, I’ve introduced a sleep interval of 0.5 seconds. Following this pause, the captured frames are displayed using OpenCV’s imshow function

The program runs without any errors when time.sleep(0.5) is not included, but including this line causes an error.Below the error messages
1.HEVC

[hevc @ 0x13d3840] Could not find ref with POC 6
[h264 @ 0x1c2e9c0] error while decoding MB 102 13, bytestream -5
[h264 @ 0x244bfc0] error while decoding MB 12 31, bytestream -7

2.RTSP:

[rtsp @ 0x1ade880] RTP: PT=60: bad cseq 1d84 expected=0ba8

When I replace the time.sleep(0.5) with YOLO object detection, the program also encounters an error.

why the program throws an error when time.sleep(0.5) is included or when YOLO object detection is implemented. Identifying the root cause of these errors will help in finding an appropriate solution.

Whilst it could be anything the most obvious reason for this is that you are not consuming the frames as fast as they are produced. In that case your OS will have to discard data when its buffer is full. Because you are using h264 where each frame is “not” encoded seperately you encounter decoding errors when this happens which affect multiple frames.

The best solution is to decode in a seperate thread and drop the decoded frames which you cannot process.

Thank you for your response. I understand the value of multithreading for reading and processing video frames, but my scenario involves managing four IP cameras, each assigned with distinct tasks like human detection, attendance tracking, crowd detection, and intrusion detection. To address this, I have implemented a multiprocessing approach, creating individual processes for each camera, resulting in four separate processes.

My primary concern is the potential increase in CPU usage if multithreading is integrated within each of these processes. Considering the distinct and resource-intensive tasks each camera performs, implementing multithreading might lead to significant demands on system resources. I am exploring efficient solutions for this multi-camera system that can balance effective processing with optimal resource utilization, and I welcome any insights or alternative strategies that could align with these requirements.

If you use a video format which uses spatial as well as temporal redundancy and don’t decode in another thread then the best solution would be to reduce the GOP size to 1 if possible. This would mean you “should” only loose a single frame when your buffers overflow.

Otherwise I would try MJPEG.

Both solutions will probably increase the decoding load on your CPU which may negate the advantage of using them over using a separate thread. Additionaly if you use a separate thread you get to choose which frames you drop.

Thanks for your answer, but I’m new to OpenCV and I’m finding it hard to understand your explanation. Could you please simplify it for me?

Here, the my cctv camera configuration:

Resolution : 1920*1080(1080P)
Bitrate Type : Variable
Video Quality : Medium
Frame Rate : 25 fps
Max. Bitrate : 2048Kbps
Video Encoding : H.265

Video streaming via RTSP

This has nothing to do with OpenCV.

If you have

Video Encoding : H.265

on your camera then you really need to do as I suggested and decode the stream in a separate thread. If you can’t or don’t want to then try to change the GOP to 1 so that each frame can be decoded independantly. This means that every time the OS buffer overflows (because you are trying to process the frames in the same thread that you decode them in and cannot do this as fast as the source produces them) you “should” only get a single corrupted frame.

If your CCTV camera allows you to use MJPEG instead this will have the same affect.

In both cases you will most likely increase the decoding load on your CPU and definitely increase the bitrate.

How to change GOP to 1 & Where to change

If there is an option to do this it will be in the web interface of your CCTV camera.

Camera : [D1] Entrance_1
Stream Type : Main Stream(Normal)
Video Type : Video Stream
Resolution : 1920*1080(1080P)
Bitrate Type : Variable
Video Quality : Medium
Frame Rate : 25 fps
Max. Bitrate : 2048 Kbps
Video Encoding : H.265
H.265+ : Off

This is my Cctv camera configuration here only way change GOP mean i can change frame rate 25 to 20 or 15. there is only way