首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NameError:未定义名称“videos_root”

NameError:未定义名称“videos_root”
EN

Stack Overflow用户
提问于 2019-09-19 14:59:22
回答 1查看 713关注 0票数 0

我正在尝试为ucf101数据集生成denseflow,但我一直收到以下错误:

‘'ApplyEyeMakeup','ApplyLipstick','Archery','BabyCrawling','BalanceBeam','BandMarching','BaseballPitch',’篮球‘,’Basketball Diving‘,'BenchPress','Biking','Billiards','BlowDryHair','BlowingCandles','BodyWeightSquats','Bowling','BoxingPunchingBag','BoxingSpeedBag','BreastStroke','BrushingTeeth','CleanAndJerk','CliffDiving','CricketBowling','CricketShot','CuttingInKitchen','Diving’“‘Drumming”,“Fencing”,“FieldHockeyPenalty”,“FloorGymnastics”,“FrisbeeCatch”,“FrontCrawl”,“GolfSwing”,“Haircut”,“Hammering”,“HammerThrow”,“HandstandPushups”,“HandstandWalking”,“HeadMassage”,“HighJump”,“HorseRace”,“HorseRiding”,“HulaHoop”,“IceDancing”,“JavelinThrow”,“JugglingBalls”,“JumpingJack”,“JumpingJack”“‘MilitaryParade”、“Mixing”、“MoppingFloor”、“Nunchucks”、“ParallelBars”、“PizzaTossing”、“PlayingCello”、“PlayingDaf”、“PlayingDhol”、“PlayingFlute”、“PlayingGuitar”、“PlayingPiano”、“PlayingSitar”、“PlayingTabla”、“PlayingViolin”、“PoleVault”、“PommelHorse”、“PullUps”、“Punch”、“PushUps”、“漂流”、“RockClimbingIndoor”、“Rlaying气候”、“划船”、“SalsaSpin”、“ShavingBeard”、“ShavingBeard”。‘射击’,‘滑板’,‘滑雪’,‘斯基耶’,‘跳伞’,‘足球比赛’,'StillRings',‘相扑’,‘冲浪’,‘摇摆’,'TableTennisShot','TaiChi','TennisSwing','ThrowDiscus','Typing',‘打字’,'UnevenBars',‘排球扣球’,'WalkingWithDog','WallPushups','WritingOnBoard','YoYo‘查找13320个视频。完成视频列表!回溯(最近一次调用):

文件"",第1行,克隆wdir=‘D:/ runfile('D:/Clones/py-denseflow-master/denseflow.py',/py-denseflow-runfile(’D:/Clones/py-denseflow-master/denseflow.py‘,’)

运行文件execfile(文件名,命名空间)中的文件"C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",行704

文件名第108行,在execfile exec( "C:\Users\sancy\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",(f.read(),filename,'exec'),命名空间)中)

Pool.map中的文件"D:/Clones/py-denseflow-master/denseflow.py",第186行(dense_flow,zip(video_list,flows_dirs,step*len(video_list),bound*len(Video_list)

文件返回行290,在map return self._map_async( "C:\Users\sancy\Anaconda3\lib\multiprocessing\pool.py",,iterable,mapstar,chunksize).get()

get raise self._value中的文件"C:\Users\sancy\Anaconda3\lib\multiprocessing\pool.py",行683

NameError:未定义名称“videos_root”

我该如何解决这个问题?

`

代码语言:javascript
复制
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<-bound]=-bound
        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
        '''
        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():
        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()
        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(video_list,flows_dirs,[step]*len(video_list),[bound]*len(video_list)))
        else: #mode=='debug
            dense_flow((video_list[0],flows_dirs[0],step,bound))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 15:06:21

dense_flowget_videos_list函数无法访问变量videos_root,您需要在函数调用期间将其作为参数传递

像这样

代码语言:javascript
复制
video_list,len_videos=get_video_list(videos_root)

还要对函数定义进行更改,以反映此更改,例如

代码语言:javascript
复制
def get_video_list(videos_root):
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58005396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档