python列表索引超出范围 等于啥_python如何解决IndexError:列表索引超出范围?-问答-阿里云开发者社区-阿里云...
我正在尝试为ucf101数据集生成密集流,但我不断收到以下错误:
我尝试在第68行中将video_name.split('')[1]更改为video_name.split('')[0],已编译代码,但出现读取错误(请参见第70行)
这是我尝试运行的python代码,但我一直在获取IndexError:列表索引超出范围:
import os,sys import numpy as np import cv2 from PIL import Image from multiprocessing import Pool import argparse from IPython import embed #to debug import skvideo.io import scipy.misc
def ToImg(raw_flow,bound): ''' this function scale the input pixels to 0-255 with bi-bound
:param raw_flow: input raw pixel value (not in 0-255)
:param bound: upper and lower bound (-bound, bound)
:return: pixel value scale from 0 to 255
'''
flow=raw_flow
flow[flow>bound]=bound
flow[flow
flow-=-bound
flow*=(255/float(2*bound))
return flow
def save_flows(flows,image,save_dir,num,bound): ''' To save the optical flow images and raw images :param flows: contains flow_x and flow_y :param image: raw image :param save_dir: save_dir name (always equal to the video id) :param num: the save id, which belongs one of the extracted frames :param bound: set the bi-bound to flow images :return: return 0 ''' #rescale to 0~255 with the bound setting flow_x=ToImg(flows[...,0],bound) flow_y=ToImg(flows[...,1],bound) if not os.path.exists(os.path.join(data_root,new_dir,save_dir)): os.makedirs(os.path.join(data_root,new_dir,save_dir))
#save the image
save_img=os.path.join(data_root,new_dir,save_dir,'img_{:05d}.jpg'.format(num))
scipy.misc.imsave(save_img,image)
#save the flows
save_x=os.path.join(data_root,new_dir,save_dir,'flow_x_{:05d}.jpg'.format(num))
save_y=os.path.join(data_root,new_dir,save_dir,'flow_y_{:05d}.jpg'.format(num))
flow_x_img=Image.fromarray(flow_x)
flow_y_img=Image.fromarray(flow_y)
scipy.misc.imsave(save_x,flow_x_img)
scipy.misc.imsave(save_y,flow_y_img)
return 0
def dense_flow(augs): ''' To extract dense_flow images :param augs:the detailed augments: video_name: the video name which is like: 'v_xxxxxxx',if different ,please have a modify. save_dir: the destination path's final direction name. step: num of frames between each two extracted frames bound: bi-bound parameter :return: no returns ''' videos_root,video_name,save_dir,step,bound=augs #video_name,save_dir,step,bound=augs video_path=os.path.join(videos_root,video_name.split('_')[1],video_name)
# provide two video-read methods: cv2.VideoCapture() and skvideo.io.vread(), both of which need ffmpeg support
# videocapture=cv2.VideoCapture(video_path)
# if not videocapture.isOpened():
# print 'Could not initialize capturing! ', video_name
# exit()
try:
videocapture=skvideo.io.vread(video_path)
except:
print('read error!'.format(video_name))
return 0
print (video_name)
# if extract nothing, exit!
if videocapture.sum()==0:
print ('Could not initialize capturing',video_name)
exit()
len_frame=len(videocapture)
frame_num=0
image,prev_image,gray,prev_gray=None,None,None,None
num0=0
while True:
#frame=videocapture.read()
if num0>=len_frame:
break
frame=videocapture[num0]
num0+=1
if frame_num==0:
image=np.zeros_like(frame)
gray=np.zeros_like(frame)
prev_gray=np.zeros_like(frame)
prev_image=frame
prev_gray=cv2.cvtColor(prev_image,cv2.COLOR_RGB2GRAY)
frame_num+=1
# to pass the out of stepped frames
step_t=step
while step_t>1:
#frame=videocapture.read()
num0+=1
step_t-=1
continue
image=frame
gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
frame_0=prev_gray
frame_1=gray
##default choose the tvl1 algorithm
dtvl1=cv2.createOptFlow_DualTVL1()
flowDTVL1=dtvl1.calc(frame_0,frame_1,None)
save_flows(flowDTVL1,image,save_dir,frame_num,bound) #this is to save flows and img.
prev_gray=gray
prev_image=image
frame_num+=1
# to pass the out of stepped frames
step_t=step
while step_t>1:
#frame=videocapture.read()
num0+=1
step_t-=1
def get_video_list(videos_root): video_list=[] for cls_names in os.listdir(videos_root): cls_path=os.path.join(videos_root,cls_names) for video_ in os.listdir(cls_path): video_list.append(video_) video_list.sort() return video_list,len(video_list)
def parse_args(): parser = argparse.ArgumentParser(description="densely extract the video frames and optical flows") parser.add_argument('--dataset',default='ucf101',type=str,help='set the dataset name, to find the data path') parser.add_argument('--data_root',default='D:/Clones/py-denseflow-master/video_classification/data',type=str) parser.add_argument('--new_dir',default='flows',type=str) parser.add_argument('--num_workers',default=4,type=int,help='num of workers to act multi-process') parser.add_argument('--step',default=1,type=int,help='gap frames') parser.add_argument('--bound',default=15,type=int,help='set the maximum of optical flow') parser.add_argument('--s_',default=0,type=int,help='start id') parser.add_argument('--e_',default=13320,type=int,help='end id') parser.add_argument('--mode',default='run',type=str,help='set 'run' if debug done, otherwise, set debug') args = parser.parse_args() return args
if name =='main':
# example: if the data path not setted from args,just manually set them as belows.
#dataset='ucf101'
#data_root='/S2/MI/zqj/video_classification/data'
#data_root=os.path.join(data_root,dataset)
args=parse_args()
data_root=os.path.join(args.data_root,args.dataset)
videos_root=os.path.join(data_root,'videos')
#print(videos_root)
print (os.listdir(videos_root))
#specify the augments
num_workers=args.num_workers
step=args.step
bound=args.bound
s_=args.s_
e_=args.e_
new_dir=args.new_dir
mode=args.mode
#get video list
video_list,len_videos=get_video_list(videos_root)
video_list=video_list[s_:e_]
len_videos=min(e_-s_,13320-s_) # if we choose the ucf101
print ('find {} videos.'.format(len_videos))
flows_dirs=[video.split('.')[0] for video in video_list]
print ('get videos list done! ')
pool=Pool(num_workers)
if mode=='run':
pool.map(dense_flow,zip(videos_root , video_list,flows_dirs,[step]*len(video_list),[bound]*len(video_list)))
#pool.map(dense_flow(zip(video_list,flows_dirs,[step]*len(video_list),[bound]*len(video_list)),videos_root))
else: #mode=='debug
dense_flow((videos_root, video_list[0],flows_dirs[0],step,bound))
我得到以下结果
runfile('D:/Clones/py-denseflow-master/denseflow.py', wdir='D:/Clones/py-denseflow-master') ['ApplyEyeMakeup', 'ApplyLipstick1', 'Archery', 'BabyCrawling', 'BalanceBeam', 'BandMarching', 'BaseballPitch', 'Basketball', 'BasketballDunk'] find 13320 videos. get videos list done! Traceback (most recent call last):
File "
", line 1, in
runfile('D:/Clones/py-denseflow-master/denseflow.py', wdir='D:/Clones/py-denseflow-master')
File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)
File "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/Clones/py-denseflow-master/denseflow.py", line 187, in
pool.map(dense_flow,zip(videos_root , video_list,flows_dirs,[step]*len(video_list),[bound]*len(video_list)))
File "C:\Users\sancy\Anaconda3\lib\multiprocessing\pool.py", line 290, in map return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:\Users\sancy\Anaconda3\lib\multiprocessing\pool.py", line 683, in get raise self._value
IndexError: list index out of range
python列表索引超出范围 等于啥_python如何解决IndexError:列表索引超出范围?-问答-阿里云开发者社区-阿里云...相关推荐
- python分行打印list_python怎么打印list-问答-阿里云开发者社区-阿里云
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- python列表做参数传值_python不定参数传值怎么做-问答-阿里云开发者社区-阿里云...
使用arg_name定义的位置参数,表示任意多个位置参数:Python标准库中习惯使用args来命名不定长位置参数,当然我们可以自定义 这个名称:不定长位置参数的类型为元组: Python 允许在形参 ...
- python场景建立_Python创建一个街道地址表-问答-阿里云开发者社区-阿里云
首先,你应该转换givenNumber成int()因为input()总是返回字符串.此外,您可以将整体strAddress转换为自身并反转自身版本,以便更轻松地访问它.splitAddress这里不需 ...
- python中str用法_python中的str()不能直接用吗
-问答-阿里云开发者社区-阿里云
str函数是Python的内置函数,它将参数转换成字符串类型,即人适合阅读的形式. 其语法格式为 1 str(object) 返回值: 返回object的字符串形式 使用示例 无参调用 当str()函 ...
- python路径分隔符_Python:当读取一个没有默认分隔符的文件(包含数百万条记录)并将其放入dataframe (pa-问答-阿里云开发者社区-阿里云...
Python:在没有默认分隔符(包含数百万条记录)的情况下读取文件并将其放入"数据框架(panda)"中,最有效的方法是什么? 文件是:"file_sd.txt" ...
- python中如何输出中文_python中怎么输出中文-问答-阿里云开发者社区-阿里云
方法一: 用encode和decode 如: ? 1 2 3 4 5 6 7 8 9 10 11 import os.path import xlrd,sys Filename='/home/tom/ ...
- python集合可以修改吗_修改包含Python3中的集合的集合列表-问答-阿里云开发者社区-阿里云...
我试图创建一个以元组为元素的列表.每个元组都有4个整数.前两个整数是对2个range进行压缩的结果,而其他2个则是对2个不同的整数进行压缩的结果. 我正在使用此代码创建元组和最终列表,这些列表是从笛卡 ...
- python安装gz文件_python tar.gz怎么安装-问答-阿里云开发者社区-阿里云
Windows环境: 安装whl包:pip install wheel -> pip install **.whl 下载whl文件 MySQL_python-1.2.5-cp27-none-wi ...
- 如何阅读python文档_python 文档怎么看-问答-阿里云开发者社区-阿里云
调用help函数,可以看到一个函数或者方法的字符串文档. In [1]: import requests In [2]: help(requests.get) Help on function get ...
最新文章
- java iris_利用K-Means聚类算法实现对iris.data.ulab
- oracle表空间 unifor,Oracle 表空间的监控
- 为什么现在的人越来越不幸福
- linux 启动流程图
- 函数用计算机代码,用main函数传参做简单的计算器的代码
- xml-rpc 以及 xml-rpc 在asp.net中的实现
- sql的case when用法
- python在线投票系统讲解_在线投票系统功能分析
- ESI大学最新排名出炉:中国内地342所大学上榜!中国科学院大学排榜首!(附内地榜单)...
- Netty的ChannelFuture和ChannelPromise
- Android APK反编译步骤
- 弹层,iframe页面
- PHP中splice,php中array_splice()函数有什么功能呢?
- Photoshop CS5的序列号
- t3网络计算机浏览卡死,凭证一点打印 系统就卡死了 重启后还是这样
- 前景检测算法(十三)--KDE2000
- MPLS virtual private network Spoken-Hub网络实验(华为设备)
- jQuery具体实例介绍什么时候用ajax,ajax应该在什么地方使用
- 2018年湘潭大学程序设计竞赛 F	maze
- 【IP分析】合并信号concat,拆分总线slice
热门文章
- Scrum中的角色:你是鸡还是猪?
- OpenCV最全函数用法
- iview表单验证写法
- 【数据异常校验】狄克逊准则(Dixon Criterion)处理异常数据
- html5 录制mp3音频,支持采样率和比特率设置
- 小程序源码:喝酒神器新UI版本带特效和音效,缩减版本微信小程序
- 立体匹配---左右一致性检测/遮挡区填充
- mysql gettimestamp_使用mysql数据库,存储日期字段使用timestamp类型的时候,取出来的数据最后面会多一个.0 【解决方案】...
- 大气等离子表面处理机 金铂利莱
- 市场发展快速,IDC如何应对?