class VStream: vsource = None def __init__(self, source='file', path=None, queueSize=128, src=0, resolution=(640, 480), framerate=30): # initialize the video stream along with the boolean:w # used to indicate if the thread should be stopped or not self.vsource = source if self.vsource == 'file': self.stream = FileVideoStream(path, queueSize=queueSize).start() elif self.vsource == 'usbcamera': self.stream = VideoStream(src=src, usePiCamera=False, resolution=resolution, framerate=framerate).start() elif self.vsource == 'picamera': self.stream = VideoStream(src=src, usePiCamera=True, resolution=resolution, framerate=framerate).start() def start(self): # start a thread to read frames from the file video stream return self.stream.start() def update(self): # keep looping infinitely until the thread indicator variable is set, # then stop the thread return self.stream.update() def read(self): # return next frame in the queue return self.stream.read() def more(self): # return True if there are still frames in the queue if self.vsource == 'file': return self.stream.more() else: return True def stop(self): # indicate that the thread should be stopped self.stream.stop() def isopen(self): # check if the camera or file is already open if self.vsource == 'picamera': #return self.stream.stream._check_camera_open() return True else: return self.stream.stream.isOpened() def get(self, obj): # acess cv2.VideoCapture.get() within the FileVideoStream class if self.vsource == 'picamera': if obj == cv2.CAP_PROP_FRAME_WIDTH: # Width of the frames in the video stream return 640 elif obj == cv2.CAP_PROP_FRAME_HEIGHT: # Height of the frames in the video stream return 480 elif obj == cv2.CAP_PROP_FPS: # Frame rate return 30 elif obj == cv2.CAP_PROP_FRAME_COUNT: # Number of frames in the video file return 1 else: return self.stream.stream.get(obj)
class VStream: vsource = None def __init__(self, source='file', path=None, qs=128, src=0, resolution=(640, 480), fr=30): """ Only the PiCamera will allow you to set its resolution at creation time. In other cases (i.e. usb camera or file), the function VideoCapture.set() needs to be used post-creation to set resolution. But this will not work uniformly for all types of cameras. As a result, the frame must be resized manually to your desired resolution. """ self.vsource = source self.target_width = resolution[0] self.target_height = resolution[1] if self.vsource == 'file': self.stream = FileVideoStream(path, queueSize=qs).start() elif self.vsource == 'usbcamera': self.stream = VideoStream(src=src, usePiCamera=False).start() elif self.vsource == 'picamera': self.stream = VideoStream(usePiCamera=True, resolution=resolution, framerate=fr).start() if self.vsource == 'picamera': # read one frame to determine the resolution of the camera #frame = self.read() #self.native_width = frame.shape[0] #self.native_height = frame.shape[1] # this isn't right but can't figure out the picarmera self.native_width = resolution[0] self.native_height = resolution[1] else: self.native_width = self.get(cv2.CAP_PROP_FRAME_WIDTH) self.native_height = self.get(cv2.CAP_PROP_FRAME_HEIGHT) def native_res(self): return (self.native_width, self.native_height) def target_res(self): return (self.target_width, self.target_height) def resize(self, frame, resolution): return cv2.resize(frame, resolution) def start(self): """ This start a thread to read frames from the file or video stream """ return self.stream.start() def update(self): """This will keep looping infinitely until the thread indicator variable is set, which then stops the thread.""" return self.stream.update() def read(self): """This returns the next frame in the queue.""" return self.stream.read() def more(self): """This returns True if there are still frames in the queue.""" if self.vsource == 'file': return self.stream.more() else: return True def stop(self): """This request that the video stream be stopped.""" self.stream.stop() def isopen(self): """Check if the camera or file is already open and retrun True if it is and False otherwise.""" if self.vsource == 'picamera': #return self.stream.stream._check_camera_open() return True else: return self.stream.stream.isOpened() def version(self): """Return the version number of the camera being used. Only works for the Pi Camera.""" from pkg_resources import require if self.vsource == 'picamera': return require('picamera')[0].version else: return None def get(self, obj): """Access cv2.VideoCapture.get() within the FileVideoStream class""" if self.vsource == 'picamera': if obj == cv2.CAP_PROP_FRAME_WIDTH: # width of the frames return self.native_width elif obj == cv2.CAP_PROP_FRAME_HEIGHT: # height of the frames return self.native_height else: print("Value of " + str(obj) + " not supported in VStream.get() for PiCamera") return None else: return self.stream.stream.get(obj)