예제 #1
0
 def _init_cam(self):
     """
     Initialises the CvPiCamera object to provide the frames
     """
     self._cam = CvPiCamera()
     self._cam.resolution = FRAME_SIZE[::-1] # openCV flips dimensions
     if os.getuid() == 0:
         self._cam.led = False
     else:
         print("LED kept on. To turn off run as root")
예제 #2
0
class PiVideoStream(VideoStream):
    """
    A subclass of VideoStream for the raspberryPi camera
    which isn't supported by opencv
    """
    def __init__(self, savePath, bgStart, nBackgroundFrames):
        """
        :param str filePath: The destination file path to save the video to
        :param int bgStart: The frame to use as background frames range start
        :param int nBackgroundFrames: The number of frames to use for the background
        """
        VideoStream.__init__(self, savePath, bgStart, nBackgroundFrames)

    def _init_cam(self):
        """
        Initialises the CvPiCamera object to provide the frames
        """
        self._cam = CvPiCamera()
        self._cam.resolution = FRAME_SIZE[::-1] # openCV flips dimensions
        if os.getuid() == 0:
            self._cam.led = False
        else:
            print("LED kept on. To turn off run as root")
        
    def _startVideoCaptureSession(self, savePath):
        """
        Initiates a picamera.array.PiRGBArray object to store
        the frames from the picamera when reading 
        and a cv2.VideoWriter object to save a potential output
        
        :param str filePath: the destination file path
        
        :return: array and videoWriter object
        :type: (picamera.array.PiRGBArray, cv2.VideoWriter)
        """
        videoWriter = cv2.VideoWriter(savePath, CODEC, FPS, FRAME_SIZE)
        stream = picamera.array.PiRGBArray(self._cam)
        return stream, videoWriter
        
    def read(self):
        """
        Returns the next frame after updating the count
        
        :return: A video frame
        :rtype: video_frame.Frame
        """
        stream = self.stream
        self._cam.quickCapture(stream)
        # stream.array now contains the image data in BGR order
        frame = stream.array
        stream.truncate(0)
        self.currentFrameIdx +=1
        return Frame(frame.astype(np.float32))
        
    def restartRecording(self, reset):
        """
        Restarts the camera and potentially resets the output stream and frame index
        
        :param bool reset: Whether to reset the output stream (overwrite previous) and frame index
        """
        if self._cam.closed:
            self._init_cam()
        if reset:
            self.stream, self.videoWriter = self._startVideoCaptureSession(self.savePath)
            self.currentFrameIdx = -1
    
    def stopRecording(self, msg):
        """
        Stops recording and performs cleanup actions
        
        :param str msg: The message to print before closing.
        """
        VideoStream.stopRecording(self, msg)
        self._cam.closeEncoder()
        self._cam.close()