我如何解决IndexError:列表索引超出范围?

问题描述 投票:-1回答:1

我正在尝试复制此存储库:https://github.com/sujiongming/UCF-101_video_classification。运行2_extract_files.py文件时出现以下错误。

Traceback (most recent call last):
  File "2_extract_files.py", line 99, in <module>
    main()
  File "2_extract_files.py", line 96, in main
    extract_files()
  File "2_extract_files.py", line 38, in extract_files
    video_parts = get_video_parts(video_path)
  File "2_extract_files.py", line 76, in get_video_parts
    filename = parts[3]
IndexError: list index out of range

代码如下:

def extract_files():
    data_file = []
    folders = ['./train/', './test/']

    for folder in folders:
        class_folders = glob.glob(folder + '*')

        for vid_class in class_folders:
            class_files = glob.glob(vid_class + '/*.avi')

            for video_path in class_files:
                video_parts = get_video_parts(video_path)

                train_or_test, classname, filename_no_ext, filename = video_parts
                if not check_already_extracted(video_parts):

                    src = train_or_test + '/' + classname + '/' + \
                        filename
                    dest = train_or_test + '/' + classname + '/' + \
                        filename_no_ext + '-%04d.jpg'
                    call(["ffmpeg", "-i", src, dest])

                nb_frames = get_nb_frames_for_video(video_parts)

                data_file.append([train_or_test, classname, filename_no_ext, nb_frames])

                print("Generated %d frames for %s" % (nb_frames, filename_no_ext))

    with open('data_file.csv', 'w') as fout:
        writer = csv.writer(fout)
        writer.writerows(data_file)

    print("Extracted and wrote %d video files." % (len(data_file)))

def get_nb_frames_for_video(video_parts):
    train_or_test, classname, filename_no_ext, _ = video_parts
    generated_files = glob.glob(train_or_test + '/' + classname + '/' +
                                filename_no_ext + '*.jpg')
    return len(generated_files)

def get_video_parts(video_path):
    parts = video_path.split('/')
    filename = parts[3]
    filename_no_ext = filename.split('.')[0]
    classname = parts[2]
    train_or_test = parts[1]

    return train_or_test, classname, filename_no_ext, filename

谁能告诉我我在做什么错,并指导我如何正确设置列表索引。预先感谢。

Window 10
Python 3.7.6
python deep-learning data-extraction os.path
1个回答
0
投票

[建议仅使用os.path.split(video_path)os.path.splitext()并逐步解决,它更安全,也更便于携带:

def get_video_parts(video_path):
    head, filename = os.path.split(video_path)
    filename_no_ext, ext = os.path.splitext(filename)
    head, classname = os.path.split(head)
    head, train_or_test = os.path.split(head)

    return train_or_test, classname, filename_no_ext, filename

https://docs.python.org/2/library/os.path.html#os.path.split

© www.soinside.com 2019 - 2024. All rights reserved.