prepare_chalearn.py
def unzip_sort_videos(sVideoDir, sZipFile, sListFile):
    """ Unzip videos use Label information defined in sListFile 
    to move videos into folders=labels
    """

    print("Unzipping and sorting ChaLearn videos from %s into %s" % (sZipFile, sVideoDir))
    # save current directory
    sCurrentDir = os.getcwd()

    # unzip videos to tmp directory 
    sTmpDir = sVideoDir + "/tmp"
    print("Unzipping videos to {} ...".format(sTmpDir))
    if os.path.exists(sTmpDir): raise ValueError("Folder {} alredy exists".format(sTmpDir)) 

    zip_ref = zipfile.ZipFile(sZipFile, "r")
    zip_ref.extractall(sTmpDir)
    zip_ref.close()
    print("{} files unzipped".format(len(glob.glob(sTmpDir + "/*/*/*.*"))))

    # read list of videos with labels    
    dfFiles = pd.read_csv(sListFile, 
        sep=" ", header=None, 
        names=["sVideoPath", "s2", "nLabel"])

    # assume sVideoPath = "train/001/M_00023.avi" and extract folders
    se_li_sVideoPath = dfFiles.sVideoPath.apply(lambda s: s.split("/"))
    dfFiles["sTrainVal"] = se_li_sVideoPath.apply(lambda s: s[0])
    dfFiles["sFileName"] = se_li_sVideoPath.apply(lambda s: s[2])

    # check target folder not yet existing
    arTrainVal = dfFiles.sTrainVal.unique()
    if len(arTrainVal) != 1: raise ValueError("sListFile supposed to contain only one train or val folder")
    sTrainValDir = arTrainVal[0]
    if os.path.exists(sTrainValDir): raise ValueError("Folder {} alredy exists".format(sVideoDir + "/" + sTrainValDir)) 

    # create new folders=classes
    seClasses = dfFiles.groupby("nLabel").size().sort_values(ascending=True)
    print("%d videos, with %d classes, occuring between %d-%d times" % \
        (len(dfFiles), len(seClasses), min(seClasses), max(seClasses)))

    for nClass, _  in seClasses.items():
        sClassDir = sVideoDir + "/" + sTrainValDir + "/" + "c{:03d}".format(nClass)
        print("Create directory", sClassDir)
        os.makedirs(sClassDir)

    # move video files (leave depth files in place)
    nCount = 0
    for pos, seVideo in dfFiles.iterrows():
        sPathNew = sTrainValDir + "/c{:03d}".format(seVideo.nLabel) + "/" + seVideo.sFileName

        if nCount % 1000 == 0:
            print ("{:5d} Move video {}".format(nCount, sPathNew))
        os.rename(sTmpDir + "/" + seVideo.sVideoPath, sVideoDir + "/" + sPathNew)  

        nCount += 1

    # cleanup
    print("{:d} videos moved".format(nCount))
    shutil.rmtree(sTmpDir)
    print("Removed tmp folder")
    return
 Unzip videos use Label information defined in sListFile 
    to move videos into folders=labels
    
Unzip videos use Label information defined in sListFile
to move videos into folders=labels
Unzip videos use Label information defined in sListFile to move videos into folders = labels
def unzip_sort_videos(sVideoDir, sZipFile, sListFile):
    print("Unzipping and sorting ChaLearn videos from %s into %s" % (sZipFile, sVideoDir))
    sCurrentDir = os.getcwd()
    sTmpDir = sVideoDir + "/tmp"
    print("Unzipping videos to {} ...".format(sTmpDir))
    if os.path.exists(sTmpDir): raise ValueError("Folder {} alredy exists".format(sTmpDir)) 
    zip_ref = zipfile.ZipFile(sZipFile, "r")
    zip_ref.extractall(sTmpDir)
    zip_ref.close()
    print("{} files unzipped".format(len(glob.glob(sTmpDir + "/*/*/*.*"))))
    dfFiles = pd.read_csv(sListFile, 
        sep=" ", header=None, 
        names=["sVideoPath", "s2", "nLabel"])
    se_li_sVideoPath = dfFiles.sVideoPath.apply(lambda s: s.split("/"))
    dfFiles["sTrainVal"] = se_li_sVideoPath.apply(lambda s: s[0])
    dfFiles["sFileName"] = se_li_sVideoPath.apply(lambda s: s[2])
    arTrainVal = dfFiles.sTrainVal.unique()
    if len(arTrainVal) != 1: raise ValueError("sListFile supposed to contain only one train or val folder")
    sTrainValDir = arTrainVal[0]
    if os.path.exists(sTrainValDir): raise ValueError("Folder {} alredy exists".format(sVideoDir + "/" + sTrainValDir)) 
    seClasses = dfFiles.groupby("nLabel").size().sort_values(ascending=True)
    print("%d videos, with %d classes, occuring between %d-%d times" % \
        (len(dfFiles), len(seClasses), min(seClasses), max(seClasses)))
    for nClass, _  in seClasses.items():
        sClassDir = sVideoDir + "/" + sTrainValDir + "/" + "c{:03d}".format(nClass)
        print("Create directory", sClassDir)
        os.makedirs(sClassDir)
    nCount = 0
    for pos, seVideo in dfFiles.iterrows():
        sPathNew = sTrainValDir + "/c{:03d}".format(seVideo.nLabel) + "/" + seVideo.sFileName
        if nCount % 1000 == 0:
            print ("{:5d} Move video {}".format(nCount, sPathNew))
        os.rename(sTmpDir + "/" + seVideo.sVideoPath, sVideoDir + "/" + sPathNew)  
        nCount += 1
    print("{:d} videos moved".format(nCount))
    shutil.rmtree(sTmpDir)
    print("Removed tmp folder")
    return
def unzip_sort_videos ( sVideoDir , sZipFile , sListFile ) : print ( "Unzipping and sorting ChaLearn videos from %s into %s" % ( sZipFile , sVideoDir ) ) sCurrentDir = os . getcwd ( ) sTmpDir = sVideoDir + "/tmp" print ( "Unzipping videos to {} ..." . format ( sTmpDir ) ) if os . path . exists ( sTmpDir ) : raise ValueError ( "Folder {} alredy exists" . format ( sTmpDir ) ) zip_ref = zipfile . ZipFile ( sZipFile , "r" ) zip_ref . extractall ( sTmpDir ) zip_ref . close ( ) print ( "{} files unzipped" . format ( len ( glob . glob ( sTmpDir + "/*/*/*.*" ) ) ) ) dfFiles = pd . read_csv ( sListFile , sep = " " , header = None , names = [ "sVideoPath" , "s2" , "nLabel" ] ) se_li_sVideoPath = dfFiles . sVideoPath . apply ( lambda s : s . split ( "/" ) ) dfFiles [ "sTrainVal" ] = se_li_sVideoPath . apply ( lambda s : s [ 0 ] ) dfFiles [ "sFileName" ] = se_li_sVideoPath . apply ( lambda s : s [ 2 ] ) arTrainVal = dfFiles . sTrainVal . unique ( ) if len ( arTrainVal ) != 1 : raise ValueError ( "sListFile supposed to contain only one train or val folder" ) sTrainValDir = arTrainVal [ 0 ] if os . path . exists ( sTrainValDir ) : raise ValueError ( "Folder {} alredy exists" . format ( sVideoDir + "/" + sTrainValDir ) ) seClasses = dfFiles . groupby ( "nLabel" ) . size ( ) . sort_values ( ascending = True ) print ( "%d videos, with %d classes, occuring between %d-%d times" % ( len ( dfFiles ) , len ( seClasses ) , min ( seClasses ) , max ( seClasses ) ) ) for nClass , _ in seClasses . items ( ) : sClassDir = sVideoDir + "/" + sTrainValDir + "/" + "c{:03d}" . format ( nClass ) print ( "Create directory" , sClassDir ) os . makedirs ( sClassDir ) nCount = 0 for pos , seVideo in dfFiles . iterrows ( ) : sPathNew = sTrainValDir + "/c{:03d}" . format ( seVideo . nLabel ) + "/" + seVideo . sFileName if nCount % 1000 == 0 : print ( "{:5d} Move video {}" . format ( nCount , sPathNew ) ) os . rename ( sTmpDir + "/" + seVideo . sVideoPath , sVideoDir + "/" + sPathNew ) nCount += 1 print ( "{:d} videos moved" . format ( nCount ) ) shutil . rmtree ( sTmpDir ) print ( "Removed tmp folder" ) return
Unzip videos use Label information defined in sListFile
to move videos into folders=labels
Unzip videos use Label information defined in sListFile to move videos into folders = labels
""" Unzip videos use Label information defined in sListFile 
    to move videos into folders=labels
    """ # save current directory # unzip videos to tmp directory  # read list of videos with labels     # assume sVideoPath = "train/001/M_00023.avi" and extract folders # check target folder not yet existing # create new folders=classes # move video files (leave depth files in place) # cleanup
sVideoDir
sZipFile
sListFile