コード例 #1
0
def test_detectors_with_stats(test_video_file):
    """ Test all detectors functionality with a StatsManager. """
    for detector in [ContentDetector, ThresholdDetector, AdaptiveDetector]:
        vm = VideoManager([test_video_file])
        stats = StatsManager()
        sm = SceneManager(stats_manager=stats)
        sm.add_detector(create_detector(detector, vm))

        try:
            end_time = FrameTimecode('00:00:15', vm.get_framerate())

            vm.set_duration(end_time=end_time)
            vm.set_downscale_factor()

            vm.start()
            sm.detect_scenes(frame_source=vm)
            initial_scene_len = len(sm.get_scene_list())
            assert initial_scene_len > 0  # test case must have at least one scene!
            # Re-analyze using existing stats manager.
            sm = SceneManager(stats_manager=stats)
            sm.add_detector(create_detector(detector, vm))

            vm.release()
            vm.reset()
            vm.set_duration(end_time=end_time)
            vm.set_downscale_factor()
            vm.start()

            sm.detect_scenes(frame_source=vm)
            scene_list = sm.get_scene_list()
            assert len(scene_list) == initial_scene_len

        finally:
            vm.release()
コード例 #2
0
def test_adaptive_detector(test_movie_clip):
    """ Test SceneManager with VideoManager and AdaptiveDetector. """
    # We use the ground truth of ContentDetector with threshold=27.
    start_frames = TEST_MOVIE_CLIP_GROUND_TRUTH_CONTENT[1][1]
    vm = VideoManager([test_movie_clip])
    sm = SceneManager()
    assert sm._stats_manager is None
    # The SceneManager should implicitly create a StatsManager since this
    # detector requires it.
    sm.add_detector(AdaptiveDetector(video_manager=vm))
    assert sm._stats_manager is not None

    try:
        video_fps = vm.get_framerate()
        start_time = FrameTimecode('00:00:50', video_fps)
        end_time = FrameTimecode('00:01:19', video_fps)

        vm.set_duration(start_time=start_time, end_time=end_time)
        vm.set_downscale_factor()

        vm.start()
        sm.detect_scenes(frame_source=vm)
        scene_list = sm.get_scene_list()
        assert len(scene_list) == len(start_frames)
        detected_start_frames = [
            timecode.get_frames() for timecode, _ in scene_list
        ]
        assert all(x == y
                   for (x, y) in zip(start_frames, detected_start_frames))

    finally:
        vm.release()
コード例 #3
0
def extract_shots_with_pyscenedetect(src_video,
                                     threshold=0,
                                     min_scene_length=15):
    video_manager = VideoManager([src_video])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(ContentDetector(threshold, min_scene_length))
    base_timecode = video_manager.get_base_timecode()
    scene_list = []

    try:
        start_time = base_timecode
        video_manager.set_duration(start_time=start_time)
        video_manager.set_downscale_factor(downscale_factor=1)
        video_manager.start()
        scene_manager.detect_scenes(frame_source=video_manager, frame_skip=0)
        scene_list = scene_manager.get_scene_list(base_timecode)
        # print('List of scenes obtained:')
        # for i, scene in enumerate(scene_list):
        # 	print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
        # 		i + 1,
        # 		scene[0].get_timecode(), scene[0].get_frames(),
        # 		scene[1].get_timecode(), scene[1].get_frames(),))
        write_csv(src_video, scene_list)
    except:
        print("error")
    finally:
        video_manager.release()
    return scene_list
コード例 #4
0
def main():

    #import csv files as pandas
    movieRuntimes = pd.read_csv(
        'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Numerical Data\\movie_runtimes.csv'
    )
    movieList = list(movieRuntimes['movie'])

    #detect number of scenes within a 30 second period
    for movie in movieList:
        aslList = list()
        effectiveRuntime = movieRuntimes['effective runtime'][movieList.index(
            movie)]
        for j in range(0, effectiveRuntime):
            moviePath = 'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Movie Intervals\\' + movie + '\\' + movie + '_' + str(
                j) + '.mp4'
            video_manager = VideoManager([moviePath])
            stats_manager = StatsManager()
            scene_manager = SceneManager(stats_manager)
            scene_manager.add_detector(ContentDetector(40))
            #Set downscale factor to improve processing speed
            video_manager.set_downscale_factor()
            #Start video_manager.
            video_manager.start()
            #Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)
            scene_list = scene_manager.get_scene_list(
                video_manager.get_base_timecode())
            aslList.append(len(scene_list))
            video_manager.release()
        #save the ASL List
        savePath = 'C:\\Users\\hasna\\Desktop\\Smell of Fear\\Pickle Objects\\ASL\\' + movie + '.p'
        pickle.dump(aslList, open(savePath, "wb"))
コード例 #5
0
def detect_scene(path, fps, min_sec):
    video_manager = VideoManager([path], framerate=fps)
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(
        ContentDetector(threshold=30, min_scene_len=fps * min_sec))
    base_timecode = video_manager.get_base_timecode()

    video_manager.set_downscale_factor()
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager,
                                show_progress=False)
    scene_list = scene_manager.get_scene_list(base_timecode)

    # Like FrameTimecodes, each scene in the scene_list can be sorted if the
    # list of scenes becomes unsorted.

    # print('List of scenes obtained:')
    # for i, scene in enumerate(scene_list):
    #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
    #         i + 1,
    #         scene[0].get_timecode(), scene[0].get_frames(),
    #         scene[1].get_timecode(), scene[1].get_frames(),))
    scene_list = [(round(s[0].get_seconds()), round(s[1].get_seconds()))
                  for s in scene_list]
    video_manager.release()
    return scene_list
コード例 #6
0
def scene_detect(opt):

    video_manager = VideoManager(
        [os.path.join(opt.avi_dir, opt.reference, 'video.avi')])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    video_manager.set_downscale_factor()

    video_manager.start()

    scene_manager.detect_scenes(frame_source=video_manager)

    scene_list = scene_manager.get_scene_list(base_timecode)

    savepath = os.path.join(opt.work_dir, opt.reference, 'scene.pckl')

    if scene_list == []:
        scene_list = [(video_manager.get_base_timecode(),
                       video_manager.get_current_timecode())]

    with open(savepath, 'wb') as fil:
        pickle.dump(scene_list, fil)

    print('%s - scenes detected %d' % (os.path.join(
        opt.avi_dir, opt.reference, 'video.avi'), len(scene_list)))

    return scene_list
コード例 #7
0
def process_youtube_video(youtube_url):
    #url = "https://www.youtube.com/watch?v=BGLTLitLUAo"

    videoPafy = pafy.new(youtube_url)
    best = videoPafy.getbest(preftype="mp4")

    cap = cv2.VideoCapture(best.url)

    sm = SceneManager()

    sm.add_detector(ContentDetector())

    try:
        video_fps = cap.get(cv2.CAP_PROP_FPS)
        frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        duration = frame_count / video_fps
        dur = FrameTimecode(duration, video_fps)

        num_frames = sm.detect_scenes(frame_source=cap, end_time=dur)

        #base_timecode = FrameTimecode('00:00:05', fps=video_fps)
        scene_list = sm.get_scene_list(dur)
        print("Scene List Count: " + str(len(scene_list)))

        result_urls = generate_images(cap, scene_list, 1, "testvid")

    finally:
        cap.release()

    #return urls
    return result_urls
コード例 #8
0
def find_scenes(video_path):
    # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

    finally:
        video_manager.release()

    scene_list_new = []
    for cur_scene in scene_list:
        start_frame = cur_scene[0].get_frames()
        end_frame = cur_scene[1].get_frames()
        scene_list_new.append((start_frame, end_frame))

    return scene_list_new
コード例 #9
0
def test_scene_list(test_video_file):
    """ Test SceneManager get_scene_list method with VideoManager/ContentDetector. """
    vm = VideoManager([test_video_file])
    sm = SceneManager()
    sm.add_detector(ContentDetector())

    try:
        base_timecode = vm.get_base_timecode()
        video_fps = vm.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        end_time = FrameTimecode('00:00:10', video_fps)

        vm.set_duration(start_time=start_time, end_time=end_time)
        vm.set_downscale_factor()

        vm.start()
        num_frames = sm.detect_scenes(frame_source=vm)

        assert num_frames == end_time.get_frames() + 1

        scene_list = sm.get_scene_list(base_timecode)

        for i, _ in enumerate(scene_list):
            if i > 0:
                # Ensure frame list is sorted (i.e. end time frame of
                # one scene is equal to the start time of the next).
                assert scene_list[i - 1][1] == scene_list[i][0]

    finally:
        vm.release()
コード例 #10
0
def frameExtraction(video, method, treshold):

    video_manager = VideoManager([video])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    if method == 'ContentDetector':
        scene_manager.add_detector(ContentDetector(treshold))
    if method == 'threshold_detector':
        scene_manager.add_detector(ThresholdDetector(treshold))

    base_timecode = video_manager.get_base_timecode()

    # Set downscale factor to improve processing speed (no args means default).
    video_manager.set_downscale_factor()

    # Start video_manager.
    video_manager.start()

    # Perform scene detection on video_manager.
    scene_manager.detect_scenes(frame_source=video_manager)

    # Obtain list of detected scenes.
    scene_list = scene_manager.get_scene_list(base_timecode)

    cap = cv2.VideoCapture(video)

    for i, scene in enumerate(scene_list):
        i = i + 1
        cut_frame = scene[0].get_frames()
        cap.set(1, cut_frame)
        ret, frame = cap.read()
        frame_name = "shot " + str(i) + ".jpg"
        cv2.imwrite(frame_name, frame)
コード例 #11
0
def get_scene_startFrames(filepath):
    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager([filepath])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        scene_startFrames = []
        for i, scene in enumerate(scene_list):
            scene_startFrames.append(scene[0].get_frames())

    finally:
        video_manager.release()

    return scene_startFrames
コード例 #12
0
def test_scene_list(test_video_file):
    """ Test SceneManager get_scene_list method with VideoManager/ContentDetector. """
    vm = VideoManager([test_video_file])
    sm = SceneManager()
    sm.add_detector(ContentDetector())

    try:
        base_timecode = vm.get_base_timecode()
        video_fps = vm.get_framerate()
        start_time = FrameTimecode('00:00:00', video_fps)
        end_time = FrameTimecode('00:00:10', video_fps)

        vm.set_duration(start_time=start_time, end_time=end_time)
        vm.set_downscale_factor()

        vm.start()
        num_frames = sm.detect_scenes(frame_source=vm)

        assert num_frames == end_time.get_frames() + 1

        scene_list = sm.get_scene_list(base_timecode)

        for i, _ in enumerate(scene_list):
            if i > 0:
                # Ensure frame list is sorted (i.e. end time frame of
                # one scene is equal to the start time of the next).
                assert scene_list[i-1][1] == scene_list[i][0]

    finally:
        vm.release()
コード例 #13
0
def test_content_detector(test_movie_clip):
    """ Test SceneManager with VideoManager and ContentDetector. """
    for threshold, start_frames in TEST_MOVIE_CLIP_GROUND_TRUTH_CONTENT:
        vm = VideoManager([test_movie_clip])
        sm = SceneManager()
        sm.add_detector(ContentDetector(threshold=threshold))

        try:
            video_fps = vm.get_framerate()
            start_time = FrameTimecode('00:00:50', video_fps)
            end_time = FrameTimecode('00:01:19', video_fps)

            vm.set_duration(start_time=start_time, end_time=end_time)
            vm.set_downscale_factor()

            vm.start()
            sm.detect_scenes(frame_source=vm)
            scene_list = sm.get_scene_list()
            assert len(scene_list) == len(start_frames)
            detected_start_frames = [
                timecode.get_frames() for timecode, _ in scene_list
            ]
            assert all(x == y
                       for (x, y) in zip(start_frames, detected_start_frames))

        finally:
            vm.release()
コード例 #14
0
def find_scenes(video_path):
    """
    based on changes between frames in the HSV color space
    """
    # three main calsses: VideoManager, SceneManager, StatsManager
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())

    base_timecode = video_manager.get_base_timecode()

    # We save our stats file to {VIDEO_PATH}.stats.csv.
    stats_file_path = '%s.stats.csv' % video_path

    scene_list = []

    try:
        # If stats file exists, load it.
        if os.path.exists(stats_file_path):
            # Read stats from CSV file opened in read mode:
            with open(stats_file_path, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Each scene is a tuple of (start, end) FrameTimecodes.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(stats_file_path, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()

    return scene_list
def videodataset_scenedetect(dirpath):
    from scenedetect.video_manager import VideoManager
    from scenedetect.scene_manager import SceneManager
    from scenedetect.stats_manager import StatsManager
    from scenedetect.detectors import ContentDetector
    from scenedetect.detectors import ThresholdDetector
    result_list = []
    for video_name in sorted(os.listdir(dirpath)):
        video_path = os.path.join(dirpath, video_name)
        print(video_path)
        video_info = get_video_info(video_path)
        # print (video_info)
        total_frames = int(video_info['duration_ts'])
        print('总帧数:' + str(total_frames))
        # 创建一个video_manager指向视频文件
        video_manager = VideoManager([video_path])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        # #添加ContentDetector算法(构造函数采用阈值等检测器选项)
        scene_manager.add_detector(ContentDetector(threshold=20))
        base_timecode = video_manager.get_base_timecode()

        try:
            frames_num = total_frames
            # 设置缩减系数以提高处理速度。
            video_manager.set_downscale_factor()
            # 启动 video_manager.
            video_manager.start()
            # 在video_manager上执行场景检测。
            scene_manager.detect_scenes(frame_source=video_manager)

            # 获取检测到的场景列表。
            scene_list = scene_manager.get_scene_list(base_timecode)
            # 与FrameTimecodes一样,如果是,则可以对scene_list中的每个场景进行排序
            # 场景列表变为未排序。
            # print('List of scenes obtained:')
            # print(scene_list)
            # 如果scene_list不为空,整理结果列表,否则,视频为单场景
            re_scene_list = []
            if scene_list:
                for i, scene in enumerate(scene_list):
                    # print('%d , %d' % (scene[0].get_frames(), scene[1].get_frames()))
                    re_scene = (scene[0].get_frames(),
                                scene[1].get_frames() - 1)
                    re_scene_list.append(re_scene)
            else:
                re_scene = (0, frames_num - 1)
                re_scene_list.append(re_scene)
            # 输出切分场景的列表结果
            print("\n")
            print(re_scene_list)
            print("\n")
            # for item in re_scene_list:
            #     assert item[1] - item[0] + 1 >= 3
            result_list.append(re_scene_list)
        finally:
            video_manager.release()
    with open(os.path.join(dirpath, 'scene.pickle'), 'wb') as f:
        pickle.dump(result_list, f)
コード例 #16
0
def main():

    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager(['00Lty3r6JLE.mp4'])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    # scene_manager.add_detector(ThresholdDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # If stats file exists, load it.
        if os.path.exists(STATS_FILE_PATH):
            # Read stats from CSV file opened in read mode:
            with open(STATS_FILE_PATH, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        start_time = base_timecode + 20  # 00:00:00.667
        end_time = base_timecode + 20.0  # 00:00:20.000

        # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        # video_manager.set_duration(start_time=start_time, end_time=end_time)
        video_manager.set_duration()

        # Set downscale factor to improve processing speed (no args means default).
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager,
                                    show_progress=False)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print(
                '    Scene %2d: Start %s / Frame %d, Second %f, End %s / Frame %d, Second %f'
                % (i + 1, scene[0].get_timecode(), scene[0].get_frames(),
                   scene[0].get_seconds(), scene[1].get_timecode(),
                   scene[1].get_frames(), scene[1].get_seconds()))

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()
コード例 #17
0
class Shot_Detector(object):
    '''
    This is for detect shot in videos. In other words, the frames in each shot are very similar.
    '''
    def __init__(self):
        '''
        Init the detection manager.
        '''

        print("Init the shot detector")
        print("PySceneDetect version being used: %s" %
              str(scenedetect.__version__))
        self.stats_manager = StatsManager()
        self.scene_manager = SceneManager(self.stats_manager)
        self.scene_manager.add_detector(ContentDetector())

    def detect(self, video_path):
        '''

        :param video_path:

        :return: the boundary of each shot:
        '''
        if not os.path.exists(video_path):
            print('The video does not exist!')

        self.video_manager = VideoManager([video_path])
        self.base_timecode = self.video_manager.get_base_timecode()
        self.video_manager.set_downscale_factor()
        self.video_manager.start()
        self.scene_manager.detect_scenes(frame_source=self.video_manager)

        scene_list = self.scene_manager.get_scene_list(self.base_timecode)

        split_frames = [0]
        split_time = [0]
        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))
            split_frames.append(scene[1].get_frames())
            split_time.append(self.__time_trans(scene[1].get_timecode()))
        self.video_manager.release()

        return split_frames, split_time

    def __time_trans(self, second_time):
        # TODO better time transform
        temp = datetime.strptime(second_time, '%H:%M:%S.%f')
        milliseconds = (temp -
                        datetime(1900, 1, 1)) // timedelta(milliseconds=1)
        return milliseconds
コード例 #18
0
def test_api():

    print("Running PySceneDetect API test...")

    print("PySceneDetect version being used: %s" % str(scenedetect.__version__))

    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager(['testvideo.mp4'])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        # If stats file exists, load it.
        if os.path.exists(STATS_FILE_PATH):
            # Read stats from CSV file opened in read mode:
            with open(STATS_FILE_PATH, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        start_time = base_timecode + 20     # 00:00:00.667
        end_time = base_timecode + 20.0     # 00:00:20.000
        # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        video_manager.set_duration(start_time=start_time, end_time=end_time)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i+1,
                scene[0].get_timecode(), scene[0].get_frames(),
                scene[1].get_timecode(), scene[1].get_frames(),))

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

    finally:
        video_manager.release()
コード例 #19
0
ファイル: aiohttp-scenedetect.py プロジェクト: lwangbm/Metis
async def hello(request):
    entry_time = time.time()
    # file_path = request.app['file_path']
    file_key, cache_conn = app['cache_params']
    video_file = tempfile.NamedTemporaryFile(delete=False)
    try:
        file_byte = cache_conn.get(file_key)
        if file_byte is None:
            file_path = app['file_path']
            with open(file_path, 'rb') as f:
                file_byte = f.read()
            cache_conn.set(file_key, file_byte)
        video_file.write(file_byte)
        # print(video_file.name)
        video_file.close()
        video_file_name = video_file.name
        video_manager = VideoManager([video_file_name])
        print("load time: {}".format(time.time()-entry_time))
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        scene_manager.add_detector(ContentDetector())
        base_timecode = video_manager.get_base_timecode()
        
        try:
            start_time = base_timecode # 00:00:00.667
            # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
            end_time = start_time + 60
            video_manager.set_duration(start_time=start_time, end_time=end_time)

            # Set downscale factor to improve processing speed (no args means default).
            video_manager.set_downscale_factor()
    
            # Start video_manager.
            video_manager.start()
    
            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)
    
            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.
    
            output_string = 'List of scenes obtained:\n'
            for i, scene in enumerate(scene_list):
                output_string += ('     Scene %2d: Start %s / Frame %d, End %s / Frame %d\n' % (
                    i+1,
                    scene[0].get_timecode(), scene[0].get_frames(),
                    scene[1].get_timecode(), scene[1].get_frames(),))
        finally:
            video_manager.release()        
    finally:
        os.unlink(video_file.name)
        video_file.close()
    print("overall latency:{}".format(time.time()-entry_time)) 
    return web.Response(text="%s" % output_string)
コード例 #20
0
def getSceneList(name="testvideo.mp4", start_frame=0):

    print("Running PySceneDetect API test...")

    print("PySceneDetect version being used: %s" %
          str(scenedetect.__version__))

    # Create a video_manager point to video file testvideo.mp4. Note that multiple
    # videos can be appended by simply specifying more file paths in the list
    # passed to the VideoManager constructor. Note that appending multiple videos
    # requires that they all have the same frame size, and optionally, framerate.
    video_manager = VideoManager([name])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:

        start_time = base_timecode + start_frame  # + 20     # 00:00:00.667
        #end_time = base_timecode + 20.0     # 00:00:20.000
        # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
        video_manager.set_duration(start_time=start_time)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor(16)

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))
        #import code
        #code.interact(local=locals())
        a = [(scene_start.get_frames(), scene_end.get_frames())
             for scene_start, scene_end in scene_list]
        a[-1] = (a[-1][0], a[-1][-1] + start_frame)
        return a
    finally:
        video_manager.release()
コード例 #21
0
def find_scenes(video_path):
    # type: (str) -> List[Tuple[FrameTimecode, FrameTimecode]]

    url = "https://www.youtube.com/watch?v=BGLTLitLUAo"
    videoPafy = pafy.new(url)
    best = videoPafy.getbest(preftype="webm")

    video = cv2.VideoCapture(best.url)

    video_manager = VideoManager([video])

    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager()

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    scene_list = []

    try:

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        #scene_manager.detect_scenes(frame_source=video_manager)
        scene_manager.detect_scenes(frame_source=video_manager)
        #vcap =

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        timecodes = []
        # Each scene is a tuple of (start, end) FrameTimecodes.

        print('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            timecodes.append(scene[0].get_timecode())
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

    finally:
        video_manager.release()

    return timecodes
コード例 #22
0
 def get_plans(video_path, threshold):
     video_manager = VideoManager([video_path])
     base_timecode = video_manager.get_base_timecode()
     video_manager.set_downscale_factor()
     video_manager.start()
     stats_manager = StatsManager()
     plan_manager = SceneManager(stats_manager)
     plan_manager.add_detector(ContentDetector(threshold=threshold))
     plan_manager.detect_scenes(frame_source=video_manager)
     plan_list = plan_manager.get_scene_list(base_timecode)
     return plan_list
コード例 #23
0
    def find_scenes(self, video_folder, threshold=50.0):
        video_paths = os.listdir(video_folder)
        # self.data.set_index("id", inplace=True)
        ids = self.data["id"]
        sep_times = {}
        for i, vp in enumerate(video_paths):
            if i % 100 == 0:
                self.logger.critical("Extracting scene frames, at %d/%d" %
                                     (i, len(video_paths)))
            id = vp.split('.')[0]
            # row = self.data.loc[int(id)]
            sep_times[id] = []
            video_path = os.path.join(video_folder, vp)

            # Create our videos & scene managers, then add the detector.
            video_manager = VideoManager([video_path], logger=self.logger)
            scene_manager = SceneManager()
            scene_manager.add_detector(ContentDetector(threshold=threshold))

            # Improve processing speed by downscaling before processing.
            # video_manager.set_downscale_factor(2)

            # Start the videos manager and perform the scene detection.
            video_manager.start()
            scene_manager.detect_scenes(frame_source=video_manager)

            scene_list = scene_manager.get_scene_list()
            # Each returned scene is a tuple of the (start, end) timecode.
            save_images(scene_list,
                        video_manager,
                        image_name_template='%s-Scene-$SCENE_NUMBER' % id,
                        num_images=1,
                        output_dir=os.path.join(config.data_folder,
                                                'scene_cover'))

            for scene in scene_list:
                sta_sec = scene[0].get_seconds()
                end_sec = scene[1].get_seconds()
                sep_times[id].append((sta_sec, end_sec))

            # print(id)
            # for i, scene in enumerate(scene_list):
            #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
            #         i + 1,
            #         1000 * scene[0].get_seconds(), scene[0].get_frames(),
            #         1000 * scene[1].get_seconds(), scene[1].get_frames()))
            # separate_texts = json.loads(row["separated_text"])
            # separate_timestamps = json.loads(row["separate_timestamps"])
            # for text, timestamp in zip(separate_texts, separate_timestamps):
            #     print(text, timestamp)
            # print('-' * 30)
            video_manager.release()
        sep_times_list = [json.dumps(sep_times[str(id)]) for id in ids]
        self.data["scene_times"] = sep_times_list
コード例 #24
0
ファイル: av1an.py プロジェクト: IIIBlueberry/Av1an
    def scenedetect(self, video: Path):
        # Skip scene detection if the user choosed to
        if self.skip_scenes:
            return ''

        try:

            # PySceneDetect used split video by scenes and pass it to encoder
            # Optimal threshold settings 15-50

            video_manager = VideoManager([str(video)])
            scene_manager = SceneManager()
            scene_manager.add_detector(
                ContentDetector(threshold=self.threshold))
            base_timecode = video_manager.get_base_timecode()

            # If stats file exists, load it.
            if self.scenes and self.scenes.exists():
                # Read stats from CSV file opened in read mode:
                with self.scenes.open() as stats_file:
                    stats = stats_file.read()
                    return stats

            # Work on whole video
            video_manager.set_duration()

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager,
                                        show_progress=True)

            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.

            scenes = [scene[0].get_timecode() for scene in scene_list]
            scenes = ','.join(scenes[1:])

            # We only write to the stats file if a save is required:
            if self.scenes:
                self.scenes.write_text(scenes)
            return scenes

        except Exception:

            print('Error in PySceneDetect')
            sys.exit()
コード例 #25
0
def scenes(video):
    """
    convert video file into list of scenes based on frame range and fps
    :param: video: video filename
    """
    video_manager = VideoManager([video])
    scene_manager = SceneManager()
    scene_manager.add_detector(ContentDetector())
    video_manager.set_downscale_factor()
    video_manager.start()
    scene_manager.detect_scenes(frame_source=video_manager)
    scene_list = scene_manager.get_scene_list(video_manager.get_base_timecode())
    return scene_list
コード例 #26
0
def find_shots(video_path, stats_file, threshold):
    video_manager = VideoManager([video_path])
    stats_manager = StatsManager()
    # Construct our SceneManager and pass it our StatsManager.
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (each detector's constructor
    # takes detector options, e.g. threshold).
    scene_manager.add_detector(ContentDetector(threshold=threshold))
    base_timecode = video_manager.get_base_timecode()

    scene_list = []

    try:
        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)

        # Each scene is a tuple of (start, end) FrameTimecodes.
        print('List of shots obtained:')
        for i, scene in enumerate(scene_list):
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))

        # Save a list of stats to a csv
        if stats_manager.is_save_required():
            with open(stats_file, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)
    except Exception as err:
        print(
            "Failed to find shots for: video: " + video_path + ", stats: " +
            stats_file + ", threshold: " + threshold, err)
        traceback.print_exc()
    finally:
        video_manager.release()

    return scene_list
コード例 #27
0
    def scenedetect(self, video):
        # PySceneDetect used split video by scenes and pass it to encoder
        # Optimal threshold settings 15-50
        video_manager = VideoManager([video])
        scene_manager = SceneManager()
        scene_manager.add_detector(ContentDetector(threshold=self.threshold))
        base_timecode = video_manager.get_base_timecode()

        try:
            # If stats file exists, load it.
            if self.scenes and os.path.exists(join(self.here, self.scenes)):
                # Read stats from CSV file opened in read mode:
                with open(join(self.here, self.scenes), 'r') as stats_file:
                    stats = stats_file.read()
                    return stats

            # Set video_manager duration to read frames from 00:00:00 to 00:00:20.
            video_manager.set_duration()

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager)

            # Obtain list of detected scenes.
            scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.

            scenes = []
            for i, scene in enumerate(scene_list):
                scenes.append(scene[0].get_timecode())

            scenes = ','.join(scenes[1:])

            # We only write to the stats file if a save is required:
            if self.scenes:
                with open(join(self.here, self.scenes), 'w') as stats_file:
                    stats_file.write(scenes)
                    return scenes
            else:
                return scenes
        except:
            print('Error in PySceneDetect')
            sys.exit()
コード例 #28
0
def video_scene_detect(video_name, stats_file_name, file_name, video_path):
    """
    returns a list of scenes
    https://pyscenedetect.readthedocs.io/en/latest/examples/usage-python/
    https://github.com/Breakthrough/PySceneDetect/blob/master/scenedetect/video_manager.py
    """
    os.chdir(video_path)
    os.system(f"scenedetect --input \
    '{video_name}' --stats {stats_file_name} \
    detect-content --threshold 27")
    video_manager = VideoManager([video_name])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)
    scene_manager.add_detector(ContentDetector())
    base_timecode = video_manager.get_base_timecode()

    try:
        if os.path.exists(stats_file_name):
            with open(stats_file_name, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)
        start_time = base_timecode + 10  # set start time
        end_time = base_timecode + 200000.0  #set end_time
        video_manager.set_duration(start_time=start_time, end_time=end_time)
        video_manager.set_downscale_factor()
        video_manager.start()
        scene_manager.detect_scenes(frame_source=video_manager)
        scene_list = scene_manager.get_scene_list(base_timecode)
        final_scene_list = []
        for i, scene in enumerate(scene_list):
            print('Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
                i + 1,
                scene[0].get_timecode(),
                scene[0].get_frames(),
                scene[1].get_timecode(),
                scene[1].get_frames(),
            ))
            final_scene_list.append({
                "scene_num": i + 1,
                "start": scene[0].get_timecode(),
                "end": scene[0].get_frames(),
                "start_frame": scene[0].get_frames(),
                "end_frame": scene[1].get_frames()
            })
        if stats_manager.is_save_required():
            with open(file_name, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)
    finally:
        video_manager.release()
    return final_scene_list
コード例 #29
0
    def detect_scenes(self, video_id):
        if video_id in self.cache:
            return self.cache[video_id]
        scenes_file_path = self._get_scenes_path(video_id)
        if exists(scenes_file_path):
            print('loading scenes from ', scenes_file_path)
            with open(scenes_file_path, 'rb') as f:
                scenes = pickle.load(f)
            self.cache[video_id] = scenes
            return scenes

        print('Detecting scenes for {}'.format(video_id))

        stats_file_path = self._get_stats_path(video_id)

        video_manager = VideoManager([get_video_path(video_id)])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        scene_manager.add_detector(self._create_detector())
        base_timecode = video_manager.get_base_timecode()

        try:
            if exists(stats_file_path):
                with open(stats_file_path, 'r') as stats_file:
                    stats_manager.load_from_csv(stats_file, base_timecode)

            # Set downscale factor to improve processing speed (no args means default).
            video_manager.set_downscale_factor()

            video_manager.start()
            scene_manager.detect_scenes(frame_source=video_manager)
            scenes_list = scene_manager.get_scene_list(base_timecode)
            scenes = [(scene[0].get_seconds(), scene[1].get_seconds())
                      for scene in scenes_list]
            self.cache[video_id] = scenes
            if self.save_scenes:
                scenes_file_path = self._get_scenes_path(video_id)
                print('saving scenes to ', scenes_file_path)
                with open(scenes_file_path, 'wb') as f:
                    pickle.dump(scenes, f)

            # We only write to the stats file if a save is required:
            if stats_manager.is_save_required():
                with open(stats_file_path, 'w') as stats_file:
                    stats_manager.save_to_csv(stats_file, base_timecode)

            return self.cache[video_id]
        finally:
            video_manager.release()
コード例 #30
0
    def run(self):
        self.logger.info('run(): started')
        self.logger.info("Running PySceneDetect API test...")
        self.logger.info("PySceneDetect version being used: %s" %
                         str(scenedetect.__version__))

        # Create a video_manager point to video file testvideo.mp4
        video_manager = VideoManager([self.video_filename])
        stats_manager = StatsManager()
        scene_manager = SceneManager(stats_manager)
        # Add ContentDetector algorithm (constructor takes detector options like threshold).
        scene_manager.add_detector(ContentDetector())
        base_timecode = video_manager.get_base_timecode()
        print('base_timecode', base_timecode)

        try:
            # If stats file exists, load it.
            if os.path.exists(self.stats_filename):
                # Read stats from CSV file opened in read mode:
                with open(self.stats_filename, 'r') as stats_file:
                    stats_manager.load_from_csv(stats_file, base_timecode)

            # Set video_manager duration
            start_time = base_timecode

            # Set downscale factor to improve processing speed.
            video_manager.set_downscale_factor()

            # Start video_manager.
            video_manager.start()

            # Perform scene detection on video_manager.
            scene_manager.detect_scenes(frame_source=video_manager,
                                        start_time=start_time)

            # Obtain list of detected scenes.
            self.scene_list = scene_manager.get_scene_list(base_timecode)
            # Like FrameTimecodes, each scene in the scene_list can be sorted if the
            # list of scenes becomes unsorted.
            """
      # We only write to the stats file if a save is required:
      if stats_manager.is_save_required():
        with open(self.stats_filename, 'w') as stats_file:
          stats_manager.save_to_csv(stats_file, base_timecode)
      """

        finally:
            video_manager.release()
            self.logger.info('run(): finished')
コード例 #31
0
ファイル: main.py プロジェクト: cfculhane/LectureSplit
def detect_scenes(video_pth: Path,
                  scene_detection_threshold: int,
                  min_scene_length: int,
                  frame_skip: Optional[int],
                  show_progress: bool = False):
    """
    Detect scenes in a video
    :param video_pth: Path to video file to process
    :param scene_detection_threshold: Threshold for detection of change in scene
    :param min_scene_length: Minimum allowed length of scene, in frames
    :param frame_skip:  Number of frames to skip, peeding up the detection time at the expense of accuracy
    :param show_progress: If true, will show a tqdm progress bar of scene detection
    :return:
    """
    time_start = time.time()
    logger.info(
        f"Starting detect_scene with args: \n"
        f"{video_pth=}\n{scene_detection_threshold=}\n{min_scene_length=}\n{frame_skip=}"
    )
    video_manager = VideoManager([str(video_pth)])
    scene_manager = SceneManager()
    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(
        ContentDetector(threshold=scene_detection_threshold,
                        min_scene_len=min_scene_length))
    base_timecode = video_manager.get_base_timecode()

    try:
        video_manager.set_downscale_factor()  # no args means default
        video_manager.start()

        scene_manager.detect_scenes(frame_source=video_manager,
                                    frame_skip=frame_skip,
                                    show_progress=show_progress)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        time_taken = time.time() - time_start
        logger.info(
            f"Detection of scenes complete in {round(time_taken, 1)} seconds")
        logger.info('List of scenes obtained:')
        for i, scene in enumerate(scene_list):
            logger.info(
                f'    Scene {i + 1}: Start {scene[0].get_timecode()} / Frame {scene[0].get_frames()}, '
                f'End {scene[1].get_timecode()} / Frame {scene[1].get_frames()}'
            )
        return scene_list
    finally:
        video_manager.release()
コード例 #32
0
def detect_scenes(filepath):
    video_manager = VideoManager([filepath])
    stats_manager = StatsManager()
    scene_manager = SceneManager(stats_manager)

    # Add ContentDetector algorithm (constructor takes detector options like threshold).
    scene_manager.add_detector(ContentDetector(threshold=40, min_scene_len=30))
    base_timecode = video_manager.get_base_timecode()

    STATS_FILE_PATH = f"{filepath.split('/')[-1]}.stats.csv"

    try:
        # If stats file exists, load it.
        if os.path.exists(STATS_FILE_PATH):
            # Read stats from CSV file opened in read mode:
            with open(STATS_FILE_PATH, 'r') as stats_file:
                stats_manager.load_from_csv(stats_file, base_timecode)

        # Set downscale factor to improve processing speed.
        video_manager.set_downscale_factor()

        # Start video_manager.
        video_manager.start()

        # Perform scene detection on video_manager.
        scene_manager.detect_scenes(frame_source=video_manager)

        # Obtain list of detected scenes.
        scene_list = scene_manager.get_scene_list(base_timecode)
        # Like FrameTimecodes, each scene in the scene_list can be sorted if the
        # list of scenes becomes unsorted.

        # We only write to the stats file if a save is required:
        if stats_manager.is_save_required():
            with open(STATS_FILE_PATH, 'w') as stats_file:
                stats_manager.save_to_csv(stats_file, base_timecode)

        # print('List of scenes obtained:')
        # for i, scene in enumerate(scene_list):
        #     print('    Scene %2d: Start %s / Frame %d, End %s / Frame %d' % (
        #         i+1,
        #         scene[0].get_timecode(), scene[0].get_frames(),
        #         scene[1].get_timecode(), scene[1].get_frames(),))

        return scene_list

    finally:
        video_manager.release()