I’m working on a Python script that reads multiple RTSP streams using OpenCV, detects motion, and saves frames when motion is detected. Initially, I faced issues with ash-colored frames due to H.265 codec, which OpenCV doesn’t support by default. After switching the camera codecs to H.264, the ash-colored frames issue was resolved. However, I’m now encountering decoding errors and glitching frames.
System Specifications:
Processor: Intel Core i3-6100 CPU @ 3.70GHz
RAM:8GB
Resource Usage:
CPU: 35-45%
RAM: 1GB max
Network Speed: 7.4 Mb/s
Disk Usage: 2.3 MB/s
Here’s the Python script I’m using:
import cv2
import os
import datetime
import threading
import argparse
from cryptography.fernet import Fernet
def encrypt_text(text):
return text
class MotionDetector:
def __init__(self, base_dir="motion_frames"):
self.base_dir = base_dir
self.output_dirs = [os.path.join(self.base_dir, str(i)) for i in range(1, 4)]
for dir_path in self.output_dirs:
os.makedirs(dir_path, exist_ok=True)
self.fgbg_dict = {}
def initialize_fgbg(self, camera_name):
if camera_name not in self.fgbg_dict:
self.fgbg_dict[camera_name] = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=35, detectShadows=True)
def detect_motion(self, frame, camera_name):
self.initialize_fgbg(camera_name)
fgmask = self.fgbg_dict[camera_name].apply(frame)
thresh = cv2.threshold(fgmask, 200, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
motion_detected = any(cv2.contourArea(contour) > 500 for contour in contours)
return motion_detected
def save_frame(self, frame, camera_name, count):
folder_index = (count - 1) % 3 # This will rotate between 0, 1, and 2
output_dir = self.output_dirs[folder_index]
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"{camera_name}_Entireframe_object_{count}_{timestamp}.jpg"
name = encrypt_text(filename)
pathname = os.path.join(output_dir, f'{name}.jpg')
cv2.imwrite(pathname, frame)
# print(f"Motion detected: {pathname}")
def process_camera_stream(rtsp_url, camera_name, detector, stop_event):
cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
print("Started Camera : ",camera_name)
count = 0
while not stop_event.is_set():
ret, frame = cap.read()
if not ret:
print(f"Connection lost: {camera_name}. Reconnecting...")
cap.release()
cap = cv2.VideoCapture(rtsp_url)
continue
if detector.detect_motion(frame, camera_name):
count += 1
detector.save_frame(frame, camera_name, count)
cap.release()
def main():
parser = argparse.ArgumentParser(description='RTSP Motion Detection')
parser.add_argument('--output', type=str, default="motion_frames", help='Output directory')
args = parser.parse_args()
rtsp_urls = {
"Camera1": "rtsp://admin:J**@884@192.168.1.103:554/cam/realmonitor?channel=1&subtype=1&protocol=TCP",
"Camera2": "rtsp://admin:J***@884@192.168.1.105:554/cam/realmonitor?channel=1&subtype=1&protocol=TCP",
"Camera3": "rtsp://admin:J***@884@192.168.1.104:554/cam/realmonitor?channel=1&subtype=1&protocol=TCP",
"Camera4": "rtsp://admin:J@884@192.168.1.101:554/cam/realmonitor?channel=1&subtype=1&protocol=TCP",
"Camera5": "rtsp://admin:admin123@192.168.1.33:554/Streaming/Channels/301&protocol=TCP",
"Camera6": "rtsp://admin:admin123@192.168.1.33:554/Streaming/Channels/401&protocol=TCP",
"Camera7": "rtsp://admin:admin123@192.168.1.33:554/Streaming/Channels/701&protocol=TCP",
}
detector = MotionDetector(base_dir=args.output)
stop_event = threading.Event()
threads = []
try:
for camera_name, url in rtsp_urls.items():
thread = threading.Thread(target=process_camera_stream, args=(url, camera_name, detector, stop_event))
thread.start()
threads.append(thread)
while True:
pass
except KeyboardInterrupt:
print("Stopping...")
stop_event.set()
for thread in threads:
thread.join()
if __name__ == "__main__":
main()
i am getting glitched some images are having glitches, i am attaching some image examples. When running the script, I’m getting the following decoding errors in the terminal:
[h264 @ 00000185da541a80] error while decoding MB 26 1, bytestream -29
[h264 @ 00000185d8fefb00] error while decoding MB 23 31, bytestream -5
[h264 @ 00000185cedcc140] error while decoding MB 36 35, bytestream -7
[h264 @ 00000185d8ae73c0] cabac decode of qscale diff failed at 40 35
[h264 @ 00000185d8ae73c0] error while decoding MB 40 35, bytestream -5
[h264 @ 00000185da541a80] error while decoding MB 32 30, bytestream -11
[h264 @ 00000185e15f8500] error while decoding MB 16 34, bytestream -11
[h264 @ 00000185e15f9700] error while decoding MB 9 33, bytestream -9
[h264 @ 00000185e15fb680] error while decoding MB 6 32, bytestream -5
[h264 @ 00000185e15f8500] error while decoding MB 23 23, bytestream -7
[h264 @ 00000185e15fb680] error while decoding MB 28 23, bytestream -5
[h264 @ 00000185e15fa000] error while decoding MB 27 19, bytestream -37
[h264 @ 00000185e15fa900] error while decoding MB 6 27, bytestream -7
[h264 @ 00000185e15f8500] error while decoding MB 14 12, bytestream -5
[h264 @ 00000185e15f9280] error while decoding MB 22 35, bytestream -7
[h264 @ 00000185d8fefb00] error while decoding MB 31 32, bytestream -7
[h264 @ 00000185e15fb680] error while decoding MB 5 24, bytestream -5
[h264 @ 00000185d8ae7b00] error while decoding MB 29 26, bytestream -7
i have ip cameras & analog cameras, the error from ip
camera are too frequent compared to analog camera.
What I’ve Tried:
- Switched camera codecs from H.265 to H.264 (resolved the ash-colored frames issue).
- Tested the script with a single camera using a different object detection script, but the same errors occurred.
Questions:
- What could be causing these decoding errors and glitching frames?
- Are there any specific settings or configurations I need to adjust in OpenCV or FFMPEG to handle H.264 streams more reliably?
- Could this be related to network latency, hardware limitations, or OpenCV’s handling of RTSP streams?
- Are there any alternative approaches or libraries I can use to improve the stability of RTSP stream processing?
the glitches are more on person or moving objects & i have no idea if the error and glitches are rtelated