[Python] Issue with Reading Frames from CCTV Camera Using OpenCV via RTSP

Discussão em 'Python' iniciado por Stack, Agosto 18, 2025.

  1. Stack

    Stack Membro Participativo

    import sys
    import time
    import cv2
    import numpy as np


    class CameraManager:
    def __init__(self, camera_uris):
    self.camera_uris = camera_uris
    self.cameras = self._initialize_cameras()

    def _initialize_cameras(self):
    cameras = []
    for uri in self.camera_uris:
    cap = cv2.VideoCapture(uri)
    if not cap.isOpened():
    print("Cannot open camera")
    sys.exit()
    cameras.append(cap)
    return cameras

    def read_frames(self,skip_frames=3):
    frames = []
    for index, cam in enumerate(self.cameras):
    for _ in range(skip_frames):
    cam.read()
    ret, frame = cam.read()
    # print(int(cam.get(cv2.CAP_PROP_POS_FRAMES)))
    if not ret:
    print("Can't receive frame (stream end?). Exiting ...")
    self.cameras[index] = cv2.VideoCapture(self.camera_uris[index])
    frame = np.zeros((1080, 1920, 3), dtype=np.uint8)
    frames.append(frame)
    return frames

    def resize_images(images, new_width=640, new_height=480):
    """
    Resize all images in the list to the specified dimensions.
    """
    resized_images = []
    for img in images:
    resized = cv2.resize(img, (new_width, new_height))
    resized_images.append(resized)
    return resized_images

    def calculate_grid_size(num_images):
    """
    Calculate the grid size based on the number of images.
    """
    if num_images == 0:
    return 0, 0
    rows = ((num_images - 1) // 3) + 1
    cols = 3
    return rows, cols

    def merge_images_in_grid(images):
    """
    Merge images into a grid layout, with the grid size dynamically calculated.
    """
    if not images:
    raise ValueError("No images to merge")

    # Resize images
    images = resize_images(images, 640, 480)

    # Calculate grid size
    grid_rows, grid_cols = calculate_grid_size(len(images))

    # Get dimensions of the resized images
    img_height, img_width, _ = images[0].shape

    # Grid dimensions
    grid_width = img_width * grid_cols
    grid_height = img_height * grid_rows

    # Create an empty black image for the grid
    merged_image = np.zeros((grid_height, grid_width, 3), dtype=np.uint8)

    # Place each image in the grid
    for i, img in enumerate(images):
    row = i // grid_cols
    col = i % grid_cols
    merged_image[row * img_height:(row + 1) * img_height, col * img_width:(col + 1) * img_width, :] = img

    return merged_image

    class MainApplication:
    def __init__(self):
    self.camera_uris = ["rtsp://admin:srivas123@192.168.1.23","rtsp://admin:srivas123@192.168.1.22","rtsp://admin:srivas123@192.168.1.21","rtsp://admin:srivas123@192.168.1.20"]
    self.camera_manager = CameraManager(camera_uris=self.camera_uris)

    def run(self):
    frame_number = 0
    while True:
    frames = self.camera_manager.read_frames()
    time.sleep(0.5)
    merge_image = merge_images_in_grid(images=frames)
    cv2.namedWindow('output', cv2.WINDOW_NORMAL)
    cv2.imshow('output', merge_image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
    exit()


    if __name__ == "__main__":
    try:
    app = MainApplication()
    app.run()
    except KeyboardInterrupt:
    print("Exiting ..")


    In this code, I manage multiple CCTV cameras connected via RTSP (Real-Time Streaming Protocol). The program continuously captures frames from these cameras, selectively skipping a set number of frames. 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

    Can't receive frame (stream end?). Exiting ...
    [h264 @ 0x1c6bbc0] error while decoding MB 114 18, bytestream -35
    Can't receive frame (stream end?). Exiting ...
    Can't receive frame (stream end?). Exiting ...
    [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
    Can't receive frame (stream end?). Exiting ...
    Can't receive frame (stream end?). Exiting ...
    [hevc @ 0x1c42900] Could not find ref with POC 0
    [rtsp @ 0x1ade880] RTP: PT=60: bad cseq 1d84 expected=0ba8
    [hevc @ 0x1c27880] Could not find ref with POC 36


    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.

    Continue reading...

Compartilhe esta Página