def __init__(self, frames=[], fps=20):
     self.__callbacks = {}
     self.fps = fps
     self.playFps = 0
     self.calculation = 0
     if isinstance(frames, list): self.frames = [Image(f) for f in frames]
     elif isinstance(frames, tuple) or isinstance(frames, list):
         self.frames = [Image(frame) for frame in frames]
     elif isinstance(frames, str):
         self.load(frames)
     else:
         self.frames = []
 def load(self, path):
     cap = cv2.VideoCapture(path)
     self.fps = cap.get(cv2.CAP_PROP_FPS)
     self.frames = []
     try:
         while (cap.isOpened()):
             ret, frame = cap.read()
             if ret and not frame is None: self.frames.append(Image(frame))
             else: break
     finally:
         cap.release()
     return self
    def play(self, loop=True, name='Video Frame'):
        dataLoop = [True]

        def __exit__():
            dataLoop[0] = False

        def __mouse__(e, x, y, f, p):
            self.__call('mouse', x, y, __exit__)

        cv2.namedWindow(name)
        cv2.setMouseCallback(name, __mouse__)
        lastException = None
        while dataLoop[0]:
            i = 0
            while i < self.length:
                frame = self.frames[i]
                now = time.clock()
                key = cv2.waitKey(1)
                if key == 27: dataLoop[0] = False
                elif key != -1:
                    self.__call('key#' + str(key), key, __exit__)
                    self.__call('keys', key, __exit__)
                if not dataLoop[0]: break
                rendered = frame.copy()
                if 'elaborate' in self.__callbacks:
                    try:
                        tmp = self.__call('elaborate', rendered, i, __exit__)
                        rendered = Image(tmp) if tmp is not None else rendered
                        printedException = False
                    except Exception as ex:
                        exMessage = str(ex)
                        if exMessage != lastException:
                            print('video callback:', exMessage)
                        lastException = exMessage
                        rentered = frame
                cv2.imshow(name, rendered.asByte().data)
                self.calculation = time.clock() - now
                sleepAmmount = (1.0 / self.fps -
                                self.calculation) if self.fps > 0 else 0
                if self.fps < 240 and sleepAmmount > 0:
                    time.sleep(sleepAmmount)
                self.playFps = 1.0 / (time.clock() - now + 0.0001)
                if 'index' in self.__callbacks:
                    i = self.__call('index', i, __exit__)
                else:
                    i += 1
                if i < 0: i = self.length + i
            dataLoop[0] = dataLoop[0] and loop
        cv2.destroyWindow(name)
        return self
 def mode(self):
     mode = np.stack(self.frames, axis=0)
     mode = stats.mode(mode, axis=0)[0][0]
     return Image(mode)
 def min(self):
     min = np.stack(self.frames, axis=0)
     min = np.min(min, axis=0).astype(np.uint8)
     return Image(min)
 def max(self):
     max = np.stack(self.frames, axis=0)
     max = np.max(max, axis=0).astype(np.uint8)
     return Image(max)
 def mean(self):
     mean = np.stack(self.frames, axis=0)
     mean = np.mean(mean, axis=0).astype(np.uint8)
     return Image(mean)