11.29.2015

Video Streaming with Flask + pygame.camera

This code is based on Video Streaming with Flask
    @classmethod
    def _thread(cls):
        pygame.init()
        pygame.camera.init()

        SIZE = (640, 480)
        camlist = pygame.camera.list_cameras()
        if not camlist:
            print('No camera device existed!')

        cam = pygame.camera.Camera(camlist[0], SIZE)        
        cam.start()
        capture = True
        while capture:
            if not cam.query_image():
                continue

            screen = cam.get_image()
            srcdata = pygame.image.tostring(screen, 'RGB')

            img = Image.fromstring('RGB', SIZE, srcdata)

            stream = io.BytesIO()
            img.save(stream, 'JPEG')

            # store frame
            stream.seek(0)
            cls.frame = stream.read()

            # reset stream for next frame
            stream.seek(0)
            stream.truncate()

            # if there hasn't been any clients asking for frames in
            # the last 10 seconds stop the thread
            if time() - cls.last_access > 10:
               break

        cls.thread = None
        cam.stop()
        pygame.quit()

沒有留言:

張貼留言