Example #1
0
    def __data_generation(self, seVideo: pd.Series) -> (np.array(float), int):
        "Returns frames for 1 video, including normalizing & preprocessing"

        # Get the frames from disc
        ar_nFrames = files2frames(seVideo.sFrameDir)
        #print(ar_nFrames.shape)
        #scipy.misc.imsave('outfile.jpg', ar_nFrames[0])
        #scipy.misc.imsave('outfile_flip.jpg', np.fliplr(ar_nFrames[0]))
        ar_nFrames_flip = np.array(
            [np.fliplr(ar_nFrames[i]) for i in range(ar_nFrames.shape[0])])

        # only use the first nChannels (typically 3, but maybe 2 for optical flow)
        ar_nFrames = ar_nFrames[..., 0:self.nChannels]
        ar_nFrames_flip = ar_nFrames_flip[..., 0:self.nChannels]

        ar_nFrames = images_normalize(ar_nFrames,
                                      self.nFrames,
                                      self.nHeight,
                                      self.nWidth,
                                      bRescale=True)
        ar_nFrames_flip = images_normalize(ar_nFrames_flip,
                                           self.nFrames,
                                           self.nHeight,
                                           self.nWidth,
                                           bRescale=True)
        self.prv_frame = ar_nFrames
        self.prv_frame_flip = ar_nFrames_flip

        return ar_nFrames, seVideo.nLabel, ar_nFrames_flip, seVideo.nLabel
Example #2
0
    def __data_generation(self, seVideo:pd.Series) -> (np.array(float), int):
        "Returns frames for 1 video, including normalizing & preprocessing"
       
        # Get the frames from disc
        ar_nFrames = files2frames(seVideo.sFrameDir)

        # only use the first nChannels (typically 3, but maybe 2 for optical flow)
        ar_nFrames = ar_nFrames[..., 0:self.nChannels]
        
        ar_fFrames = images_normalize(ar_nFrames, self.nFrames, self.nHeight, self.nWidth, bRescale = True)
        
        return ar_fFrames, seVideo.nLabel
def framesDir2flowsDir(sFrameBaseDir:str, sFlowBaseDir:str, nFramesNorm:int = None, sAlgorithm:str = "tvl1-fast"):
    """ Calculate optical flow from frames (extracted from videos) 
    
    Input videoframe structure:
    ... sFrameDir / train / class001 / videoname / frames.jpg

    Output:
    ... sFlowDir / train / class001 / videoname / flow.jpg
    """

    # do not (partially) overwrite existing directory
    #if os.path.exists(sFlowBaseDir): 
    #    warnings.warn("\nOptical flow folder " + sFlowBaseDir + " alredy exists: flow calculation stopped")
    #    return

    # get list of directories with frames: ... / sFrameDir/train/class/videodir/frames.jpg
    sCurrentDir = os.getcwd()
    os.chdir(sFrameBaseDir)
    liVideos = sorted(glob.glob("*/*/*"))
    os.chdir(sCurrentDir)
    print("Found %d directories=videos with frames in %s" % (len(liVideos), sFrameBaseDir))

    # loop over all videos-directories
    nCounter = 0
    for sFrameDir in liVideos:

        # generate target directory
        sFlowDir = sFlowBaseDir + "/" + sFrameDir

        if nFramesNorm != None and os.path.exists(sFlowDir):
            nFlows = len(glob.glob(sFlowDir + "/*.*"))
            if nFlows == nFramesNorm: 
                print("Video %5d: optical flow already extracted to %s" % (nCounter, sFlowDir))
                nCounter += 1
                continue
            else: 
                print("Video %5d: Directory with %d instead of %d flows detected" % (nCounter, nFlows, nFramesNorm))

        # retrieve frame files - in ascending order
        arFrames = files2frames(sFrameBaseDir + "/" + sFrameDir)

        # downsample
        if nFramesNorm != None: 
            arFrames = frames_downsample(arFrames, nFramesNorm)

        # calculate and save optical flow
        print("Video %5d: Calc optical flow with %s from %s frames to %s" % (nCounter, sAlgorithm, str(arFrames.shape), sFlowDir))
        arFlows = frames2flows(arFrames, sAlgorithm = sAlgorithm)
        flows2file(arFlows, sFlowDir)

        nCounter += 1      

    return