Geben Sie mit Lesen von Frames aus der CCTV -Kamera mit OpenCV über RTSP ausPython

Python-Programme
Anonymous
 Geben Sie mit Lesen von Frames aus der CCTV -Kamera mit OpenCV über RTSP aus

Post by Anonymous »

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:[email protected]","rtsp://admin:[email protected]","rtsp://admin:[email protected]","rtsp://admin:[email protected]"]
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 ..")
< /code>
In diesem Code verwende ich mehrere CCTV-Kameras, die über RTSP (Echtzeit-Streaming-Protokoll) verbunden sind. Das Programm erfasst kontinuierlich Frames aus diesen Kameras und überspringt selektiv eine festgelegte Anzahl von Frames. Nach dem Lesen eines Frame wird eine Liste hinzugefügt, die dann zurückgegeben wird. Anstatt die YOLO -Objekterkennung zu implementieren, habe ich ein Schlafintervall von 0,5 Sekunden eingeführt. Nach dieser Pause werden die erfassten Frames mithilfe der IMSHOW -Funktion von OpenCV < /p>
angezeigt. Das Programm wird ohne Fehler ohne Fehler ausgeführt.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
< /code>
Wenn ich die Zeit ersetze. Das Identifizieren der Hauptursache dieser Fehler hilft bei der Suche nach einer geeigneten Lösung.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post