import cv2 import numpy as np from IDataStreamModule import IDataStreamModule class DataStreamModule(IDataStreamModule): # Method to find available cameras and return their IDs def findCameras(self): # List to store the IDs of the found cameras cameras = [] # Iterating through all possible camera IDs i = 0 while True: cap = cv2.VideoCapture(i) if not cap.isOpened(): # No further cameras found, break the loop break # Camera found, store ID in the list cameras.append(i) cap.release() i += 1 # Return the IDs of the found cameras return cameras # Method to get the camera stream from a given camera def get_camera_stream(self, camera_name): cap = cv2.VideoCapture(camera_name) total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) current_frame = 0 while cap.isOpened(): ret, frame = cap.read() # Yield each frame as a numpy array yield np.array(frame) current_frame += 1 if current_frame == total_frames - 1: # If the current frame is the second-to-last frame, reset to the beginning of the file cap.set(cv2.CAP_PROP_POS_FRAMES, 0) current_frame = 0 cap.release()