Beispiel #1
0
    def createVideoStream(system, source, fps, resx, resy, begin):

        try:  # Camera as source
            source = int(source)

            if system == "Windows":
                vs = cv2.VideoCapture(source)
                vs.set(3,
                       resx)  # Set resolution x (3 = CV_CAP_PROP_FRAME_WIDTH)
                vs.set(4,
                       resy)  # Set resolution y (4 = CV_CAP_PROP_FRAME_HEIGHT)
                vs.set(5, fps)  # Set frame rate (5 = CV_CAP_PROP_FPS )

                cv2.ocl.setUseOpenCL(
                    False)  # Necessary to avoid background subtraction errors

            elif system == "Raspi":
                vs = PiVideoStream((resx, resy), fps).start()

        except ValueError:  # Video as source
            vs = cv2.VideoCapture(source)
            vs.set(
                0,
                begin)  # Set start of video in ms (0 = CV_CAP_PROP_POS_MSEC)

            if system == "Windows":
                cv2.ocl.setUseOpenCL(
                    False)  # Necessary to avoid background subtraction errors

        return vs
class Camera(object):
    def __init__(self):
        if cv2.__version__.startswith('2'):
            PROP_FRAME_WIDTH = cv2.cv.CV_CAP_PROP_FRAME_WIDTH
            PROP_FRAME_HEIGHT = cv2.cv.CV_CAP_PROP_FRAME_HEIGHT
        elif cv2.__version__.startswith('3'):
            PROP_FRAME_WIDTH = cv2.CAP_PROP_FRAME_WIDTH
            PROP_FRAME_HEIGHT = cv2.CAP_PROP_FRAME_HEIGHT

        self.video = PiVideoStream().start()
        time.sleep(2.0)
        #self.video = cv2.VideoCapture(1)
        #self.video.set(PROP_FRAME_WIDTH, 640)
        #self.video.set(PROP_FRAME_HEIGHT, 480)
        self.video.set(PROP_FRAME_WIDTH, 320)
        self.video.set(PROP_FRAME_HEIGHT, 240)

    def __del__(self):
        self.video.release()

    def get_frame(self):
        success, image = self.video.read()
        ret, jpeg = cv2.imencode('.jpg', image)
        return jpeg.tostring()