def doProcessing(self,
                     video_worker,
                     limit=0,
                     verbose=False,
                     force_no_seek=False):
        #initially....
        width = None
        height = None

        offset_frame = -1
        absolute_frame = 0
        absolute_time = 0.0

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " has begun")

        #for timer...
        timer = TimeHelper()
        timer.startTimer()

        #open video...
        for video_idx, video_file in enumerate(self.file_list):
            try:
                capture = cv2.VideoCapture(video_file)
            except Exception as e:
                # error loading
                raise Exception("The file <" + video_file +
                                "> could not be opened")

            capture_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
            capture_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

            # ...check size ...
            forced_resizing = False
            if width is None:
                # first video....
                # initialize local parameters....
                if self.forced_width is not None:
                    # ...size...
                    width = self.forced_width
                    height = self.forced_height

                    if capture_width != self.forced_width or capture_height != self.forced_height:
                        forced_resizing = True
                else:
                    width = capture_width
                    height = capture_height

                # on the worker class...
                video_worker.initialize(width, height)
            else:
                if self.forced_width is not None:
                    forced_resizing = (capture_width != self.forced_width
                                       or capture_height != self.forced_height)
                else:
                    if (width != capture_width) or (height != capture_height):
                        # invalid, all main video files must be the same resolution...
                        raise Exception(
                            "All video files on the list must have the same resolution"
                        )

            # get current FPS
            video_fps = capture.get(cv2.CAP_PROP_FPS)
            # will use some frames per second
            jump_frames = int(video_fps / self.frames_per_second)

            # Read video until the end or until limit has been reached
            selection_step = 4 if force_no_seek else 1
            timer_1 = TimeHelper()
            timer_2 = TimeHelper()

            while limit == 0 or offset_frame < limit:

                if selection_step == 2 or selection_step == 5:
                    # jump to frame in single step
                    timer_2.startTimer()

                    target_frame = capture.get(
                        cv2.CAP_PROP_POS_FRAMES) + jump_frames - 1
                    valid_grab = capture.set(cv2.CAP_PROP_POS_FRAMES,
                                             target_frame)

                    timer_2.endTimer()

                    if selection_step == 2:
                        selection_step = 3

                if selection_step == 1 or selection_step == 4:
                    timer_1.startTimer()

                    # jump to frame by grabbing frames ...
                    valid_grab = True
                    for x in range(jump_frames - 1):
                        valid_grab = capture.grab()
                        if not valid_grab:
                            break

                    timer_1.endTimer()

                    if selection_step == 1:
                        selection_step = 2

                if selection_step == 3:
                    # decide which sampling grabbing method is faster
                    if timer_1.totalElapsedTime() < timer_2.totalElapsedTime():
                        print("Grabbing frames to jump")
                        selection_step = 4
                    else:
                        print("Jumping to frames directly")
                        selection_step = 5

                # get frame..
                if valid_grab:
                    flag, frame = capture.read()
                else:
                    flag, frame = False, None

                #print("Grab time: " + str(capture.get(cv2.CAP_PROP_POS_FRAMES)))
                #print((valid_grab, flag, type(frame), selection_step, jump_frames))
                if not flag:
                    # end of video reached...
                    break
                else:
                    offset_frame += 1
                    current_time = capture.get(cv2.CAP_PROP_POS_MSEC)
                    current_frame = capture.get(cv2.CAP_PROP_POS_FRAMES)

                if forced_resizing:
                    frame = cv2.resize(frame,
                                       (self.forced_width, self.forced_height))

                if offset_frame > 0:
                    frame_time = absolute_time + current_time
                    frame_idx = int(absolute_frame + current_frame)
                    video_worker.handleFrame(frame, last_frame, video_idx,
                                             frame_time, current_time,
                                             frame_idx)

                    if verbose and offset_frame % 50 == 0:
                        print( "Frames Processed = " + str(offset_frame) + \
                               ", Video Time = " + TimeHelper.stampToStr( frame_time ) )

                last_frame = frame
                last_time = current_time

            #at the end of the processing of current video
            capture.set(cv2.CAP_PROP_POS_AVI_RATIO, 1.0)
            video_length = capture.get(cv2.CAP_PROP_POS_MSEC)
            video_frames = capture.get(cv2.CAP_PROP_POS_FRAMES)

            absolute_time += video_length
            absolute_frame += video_frames

        #processing finished...
        video_worker.finalize()

        #end time counter...
        timer.endTimer()

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " completed: " +
                  TimeHelper.stampToStr(timer.lastElapsedTime() * 1000.0))
예제 #2
0
    def doProcessing(self, video_worker, limit=0, verbose=False):
        # initially....
        width = None
        height = None

        offset_frame = -1
        absolute_frame = 0
        absolute_time = 0.0
        last_frame = None

        next_sample_frame_idx = 0

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " has begun")

        # for timer...
        timer = TimeHelper()
        timer.startTimer()

        # open video...
        for video_idx, video_file in enumerate(self.file_list):
            try:
                capture = cv2.VideoCapture(video_file)
            except Exception as e:
                # error loading
                raise Exception("The file <" + video_file +
                                "> could not be opened")

            capture_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
            capture_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

            # ...check size ...
            forced_resizing = False
            if width is None:
                # first video....
                # initialize local parameters....
                if self.forced_width is not None:
                    # ...size...
                    width = self.forced_width
                    height = self.forced_height

                    if capture_width != self.forced_width or capture_height != self.forced_height:
                        forced_resizing = True
                else:
                    width = capture_width
                    height = capture_height

                # on the worker class...
                video_worker.initialize(width, height)
            else:
                if self.forced_width is not None:
                    forced_resizing = (capture_width != self.forced_width
                                       or capture_height != self.forced_height)
                else:
                    if (width != capture_width) or (height != capture_height):
                        # invalid, all main video files must be the same resolution...
                        raise Exception(
                            "All video files on the list must have the same resolution"
                        )

            # Read video until the end or until limit has been reached
            while (limit == 0 or offset_frame < limit
                   ) and not next_sample_frame_idx >= len(self.frame_list):

                if offset_frame == self.frame_list[next_sample_frame_idx]:
                    # grab and decode next frame since it is in the list
                    flag, frame = capture.read()
                else:
                    # just grab to move forward in the list
                    flag = capture.grab()

                # print("Grab time: " + str(capture.get(cv2.CAP_PROP_POS_FRAMES)))
                # print((valid_grab, flag, type(frame), selection_step, jump_frames))
                if not flag:
                    # end of video reached...
                    break

                if offset_frame == self.frame_list[next_sample_frame_idx]:
                    # continue ..
                    current_time = capture.get(cv2.CAP_PROP_POS_MSEC)
                    current_frame = capture.get(cv2.CAP_PROP_POS_FRAMES)

                    if forced_resizing:
                        frame = cv2.resize(
                            frame, (self.forced_width, self.forced_height))

                    frame_time = absolute_time + current_time
                    frame_idx = int(absolute_frame + current_frame)
                    video_worker.handleFrame(frame, last_frame, video_idx,
                                             frame_time, current_time,
                                             frame_idx)

                    if verbose:
                        print("Frames Processed = {0:d}, Video Time = {1:s}".
                              format(offset_frame,
                                     TimeHelper.stampToStr(frame_time)))

                    last_frame = frame

                    next_sample_frame_idx += 1
                    if next_sample_frame_idx >= len(self.frame_list):
                        # end of sample reached ...
                        break

                offset_frame += 1

            # at the end of the processing of current video
            capture.set(cv2.CAP_PROP_POS_AVI_RATIO, 1.0)
            video_length = capture.get(cv2.CAP_PROP_POS_MSEC)
            video_frames = capture.get(cv2.CAP_PROP_POS_FRAMES)

            absolute_time += video_length
            absolute_frame += video_frames

        # processing finished...
        video_worker.finalize()

        # end time counter...
        timer.endTimer()

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " completed: " +
                  TimeHelper.stampToStr(timer.lastElapsedTime() * 1000.0))
    def doProcessing(self, video_worker, limit=0, verbose=False):
        # initially....
        width = None
        height = None

        offset_frame = -1
        absolute_frame = 0
        absolute_time = 0.0

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " has begun")

        # for timer...
        timer = TimeHelper()
        timer.startTimer()

        # open video...
        try:
            print(self.src_dir)
            capture = ImageListGenerator(
                '{}/{}'.format(self.src_dir, 'JPEGImages'), self.img_extension)
        except Exception as e:
            # error loading
            print(e)
            raise Exception(
                "The directory <" + self.src_dir +
                "> is not in the correct export format, check index.json")

        last_frame = None
        capture_width = capture.width
        capture_height = capture.height
        # print(capture_width, capture_height)

        # ...check size ...
        forced_resizing = False
        if width is None:
            # first video....
            # initialize local parameters....
            if self.forced_width is not None:
                # ...size...
                width = self.forced_width
                height = self.forced_height

                if capture_width != self.forced_width or capture_height != self.forced_height:
                    forced_resizing = True
            else:
                width = capture_width
                height = capture_height

            # on the worker class...
            video_worker.initialize(width, height)
        else:
            if self.forced_width is not None:
                forced_resizing = (capture_width != self.forced_width
                                   or capture_height != self.forced_height)
            else:
                if (width != capture_width) or (height != capture_height):
                    # invalid, all main video files must be the same resolution...
                    raise Exception(
                        "All files on the list must have the same resolution")

        while limit == 0 or offset_frame < limit:
            # get frame..
            flag, frame = capture.read()
            if not flag:
                # end of video reached...
                print('end of video reached...')
                break
            else:
                offset_frame += 1
                current_time = capture.get('abs_time')
                current_frame = capture.index2frameID()
            if forced_resizing:
                frame = cv2.resize(frame,
                                   (self.forced_width, self.forced_height))

            if offset_frame >= 0:
                frame_time = absolute_time + current_time
                frame_idx = int(absolute_frame + current_frame)
                video_worker.handleFrame(frame, last_frame, 0, frame_time,
                                         current_time, frame_idx)

                if verbose and offset_frame % 50 == 0:
                    print("Frames Processed = " + str(offset_frame) +
                          ", Video Time = " +
                          TimeHelper.stampToStr(frame_time))

            last_frame = frame
            last_time = current_time

        # processing finished...
        video_worker.finalize()

        # end time counter...
        timer.endTimer()

        if verbose:
            print("Video processing for " + video_worker.getWorkName() +
                  " completed: " +
                  TimeHelper.stampToStr(timer.lastElapsedTime() * 1000.0))