how do I resolve IndexError: list index out of range?
how do I resolve IndexError: list index out of range?
我正在尝试复制此存储库: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
建议只使用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/3/library/os.path.html#os.path.split
我有点过时了 - 所以你可能想尝试 pathlib
对路径对象进行更高级的操作 - 在这种情况下,它可能是 path.stem()
到获取没有扩展名的最后一部分的名称,然后 path.parent()
上去。
https://docs.python.org/3/library/pathlib.html#module-pathlib
我正在尝试复制此存储库: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
建议只使用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/3/library/os.path.html#os.path.split
我有点过时了 - 所以你可能想尝试 pathlib
对路径对象进行更高级的操作 - 在这种情况下,它可能是 path.stem()
到获取没有扩展名的最后一部分的名称,然后 path.parent()
上去。
https://docs.python.org/3/library/pathlib.html#module-pathlib