Ejemplo n.º 1
0
def train():
    videofile_path = ['videos/ostrich.mp4'
                      ]  #Enter a folder OR a list of videos to analyze.
    if not os.path.isfile(videofile_path[0]):
        sys.exit("File {} not found!!".format(videofile_path[0]))

    path_config_file = '/home/braincreator/Pose_estimation/DeepLabCut/examples/ostrich/Testing-Shan-2019-08-07/config.yaml'

    print("\n" + "=" * 80 + "\n\t\t\t\tTRAINING\n" + "=" * 80)
    print("\nCreating Training dataset from labeled frames...\n")
    deeplabcut.create_training_dataset(path_config_file)
    print("\nDataset created, Next stop Training!\n\n")
    deeplabcut.train_network(path_config_file,
                             shuffle=1,
                             saveiters=2000,
                             displayiters=500,
                             gputouse=None,
                             maxiters=50000)

    print("\nEvauating network:\n" + "=" * 50)
    deeplabcut.evaluate_network(path_config_file, plotting=True)

    print("\n" + "=" * 80 + "\n\t\t\t\tANALYZE\n" + "=" * 80)
    deeplabcut.analyze_videos(path_config_file,
                              videofile_path,
                              gputouse=None,
                              videotype='.mp4')

    print("\n" + "=" * 80 + "\n\t\t\t\tCreating video\n" + "=" * 80)
    deeplabcut.create_labeled_video(path_config_file,
                                    videofile_path,
                                    outputframerate=5,
                                    draw_skeleton=True)
Ejemplo n.º 2
0
def analyze_videos_CLARA(config_path, v, crp):
    import deeplabcut
    deeplabcut.analyze_videos(config_path,
                              v,
                              videotype='.mp4',
                              save_as_csv=True,
                              cropping=crp)
def train_dlc(path):
    os.chdir(path)
    folders = [name for name in os.listdir(path) if os.path.isdir(name) and name[0].isdigit()]
    num_folder = len(folders)
    for i in range(len(folders)):
        start = time()

        if os.path.exists(r"%s\%s\labeled-data"%(path, folders[i])) and os.path.exists(r"%s\%s\config.yaml"%(path, folders[i])) \
        and os.path.exists(r"%s\%s\dlc-models\iteration-0"%(path, folders[i])) == False and get_size(r"%s\%s\labeled-data"%(path, folders[i])) > 0:
            path_config_file = r"%s\%s\config.yaml" %(path, folders[i])
            deeplabcut.check_labels(path_config_file)
            deeplabcut.create_training_dataset(path_config_file)
            deeplabcut.train_network(path_config_file, shuffle = 1, autotune = True, displayiters = 5000, saveiters = 5000, maxiters = 200000)
            deeplabcut.analyze_videos(path_config_file, r"%s\%s\videos"%(path, folders[i]), videotype = '.mp4', save_as_csv = True)
            print("%s training has been done, %s left"%(folders[i], num_folder - i))
        elif os.path.exists(r"%s\%s\dlc-models\iteration-0"%(path, folders[i])):
            path_config_file = r"%s\%s\config.yaml" %(path, folders[i])
            print("%s model has been trained, do you want to retrain it? y/n"%(folders[i]))
            feedback = input()
            if feedback == "y":
                deeplabcut.train_network(path_config_file, shuffle = 1, autotune = True, displayiters = 5000, saveiters = 5000, maxiters = 200000)
            # If model was previously trained, read config.yaml to retrieve
            deeplabcut.analyze_videos(path_config_file, r"%s\%s\videos"%(path, folders[i]), videotype='.mp4', save_as_csv=True)
            print("%s training has been done, %s left"%(folders[i], num_folder - i))

        else:
            print("labeled-data folder not found OR empty OR config not found")
        print("Running time for %s is %s sec" %(folders[i], time() - start))
Ejemplo n.º 4
0
def task(gpuNo):
    deeplabcut.analyze_videos(config_path,
                              videolists,
                              shuffle=1,
                              save_as_csv=True,
                              videotype=".mp4",
                              gputouse=gpuNo)
Ejemplo n.º 5
0
def step4B_analyze(result, session, **kwargs):
    networks = listdirs(kwargs['networks'])
    network=None
    indicator = 'L'
    if kwargs['hand']==1:
        indicator = 'R'

    for onenetwork in networks:
        if onenetwork[0] == indicator:
            network = onenetwork

    if network == None:
        print("Can't find network for {}, {}".format(result, session))
        sys.exit(1)

    networkconfig = os.path.join(kwargs['networks'], network,'config.yaml')
    
    analyze_folder = os.path.abspath(os.path.join(kwargs['videos_clipped'], result))
    video_dlcout_2D_folder = os.path.abspath(os.path.join(kwargs['video_dlcout_2D'],result))
    os.makedirs(video_dlcout_2D_folder, exist_ok=True)
    deeplabcut.analyze_videos(networkconfig,
                              analyze_folder,
                              videotype='mp4',
                              shuffle=1,
                              gputouse=kwargs['gpu'],
                              save_as_csv=True,
                              destfolder=video_dlcout_2D_folder)
Ejemplo n.º 6
0
    def process_motion_tracking(self, config, create_labelled_video=False):
        """
        Run DeepLabCut motion tracking on behavioural videos.
        """
        # bloated so imported when needed
        import deeplabcut  # pylint: disable=import-error

        self.extract_videos()

        config = Path(config).expanduser()
        if not config.exists():
            raise PixelsError(f"Config at {config} not found.")

        for recording in self.files:
            if 'camera_data' in recording:
                video = self.interim / recording['camera_data'].with_suffix(
                    '.avi')
                if not video.exists():
                    raise PixelsError(
                        f"Path {video} should exist but doesn't... discuss.")

                deeplabcut.analyze_videos(config, [video])
                deeplabcut.plot_trajectories(config, [video])
                if create_labelled_video:
                    deeplabcut.create_labeled_video(config, [video])
Ejemplo n.º 7
0
def run_analysis_videos(cam_video_paths,filtertype='arima', windowlength=20, p_bound=0.9, ARdegree=0,
                        MAdegree=2,alpha=0.1):
    """
    Function to analyze an experimental video using pre-trained deeplabcut neural networks to predict specified
    joint locations from raw video files obtained during an experiment.
    Additionally, DeepLabCut can filter data using (S)ARIMAX models. For more information, please see
    https://github.com/DeepLabCut/DeepLabCut
    Information is saved inside the video file to a .csv file necessary for 3-D reconstruction.
    Parameters
    ----------
    cam_video_paths : list of file paths for each individual camera

    Returns
    -------

    """
    # set up network and get it ready
    config_path = '/clusterfs/bebb/users/bnelson/DLC/CatScan-Brett-2020-07-27/config.yaml'
    shuffle = 5 # shuffle of the network to use
    print("Starting to extract files..")
    # analyze each video path
    deeplabcut.analyze_videos(config_path, cam_video_paths, videotype='mp4', shuffle=shuffle, 
                              save_as_csv=True)
    deeplabcut.filterpredictions(config_path, cam_video_paths, videotype='.mp4',shuffle=shuffle,
                                 filtertype=filtertype, windowlength=windowlength,
                                 p_bound=p_bound, ARdegree=ARdegree, MAdegree=MAdegree,alpha=alpha, save_as_csv=True)
    return
Ejemplo n.º 8
0
def process_session(config, session_path):
    pipeline_videos_raw = config['pipeline']['videos_raw']
    pipeline_pose = config['pipeline']['pose_2d']
    video_ext = config['video_extension']

    config_name = os.path.join(config['model_folder'], 'config.yaml')

    source_folder = os.path.join(session_path, pipeline_videos_raw)
    outdir = os.path.join(session_path, pipeline_pose)

    videos = glob(os.path.join(source_folder, '*.' + video_ext))
    videos = sorted(videos, key=natural_keys)

    if len(videos) > 0:
        os.makedirs(outdir, exist_ok=True)

    for video in videos:
        basename = os.path.basename(video)
        basename, ext = os.path.splitext(basename)

        dataname = os.path.join(outdir, basename + '.h5')
        print(dataname)
        if os.path.exists(dataname):
            continue
        else:
            import deeplabcut
            trap = io.StringIO()
            with redirect_stdout(trap):
                deeplabcut.analyze_videos(config_name, [video],
                                          videotype=ext,
                                          save_as_csv=False,
                                          destfolder=outdir)
            rename_dlc_files(outdir, basename)
Ejemplo n.º 9
0
def generate_track_h5(config_path,untracked_videos,suffix=".mp4"):    
    os.environ["DLClight"]='True' # all the child process have this env but not its father process
    if (platform.system()=="Linux"):
        import deeplabcut
        deeplabcut.analyze_videos(config_path,untracked_videos,shuffle=1,save_as_csv=True,videotype=suffix)
        deeplabcut.plot_trajectories(config_path,untracked_videos)
        deeplabcut.create_labeled_video(config_path,untracked_videos)
Ejemplo n.º 10
0
def track(config_file, video, dest_fld):
    analyze_videos(config_file, [video],
                   gputouse=0,
                   destfolder=dest_fld,
                   videotype='.mp4',
                   save_as_csv=False,
                   dynamic=(False, .5, 10))

    filterpredictions(config_file, [video], filtertype='median')
Ejemplo n.º 11
0
def repeatAnalyzeVideos(path_config_file, path):
    for curDir, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(str_videotype):
                path_videos = os.path.join(curDir, file)
                print(path_videos)
                deeplabcut.analyze_videos(path_config_file, [path_videos],
                                          shuffle=1,
                                          videotype=str_videotype,
                                          save_as_csv=True,
                                          gputouse=Ngputouse)
Ejemplo n.º 12
0
def deeplabcut_detection(config_dir, video_dir):
    """Detect snout and tailbase coordinated with Deeplabcut model
    config_dir: path to config file
    video_dir: path to video input
    return:
        features_mouse1: coordinates of snout and tailbase of mouse 1
        features_mouse2: coordinates of snout and tailbase of mouse 2
    """
    deeplabcut.analyze_videos(config_dir, video_dir, videotype='.avi')

    dlc_output = [
        f for f in os.listdir(os.path.dirname(video_dir[0]))
        if f.endswith('.h5')
    ]

    # mouse1
    mouse1_dlc = pd.read_hdf(
        os.path.join(os.path.dirname(video_dir[0]), dlc_output[0]))
    features_mouse1 = mouse1_dlc.values[:, [0, 1, 9, 10]]
    features_mouse1 = np.round(features_mouse1, 2)
    features_mouse1_df = pd.DataFrame({
        'snout_x':
        np.round(mouse1_dlc.values[:, 0], 2),
        'snout_y':
        np.round(mouse1_dlc.values[:, 1], 2),
        'tailbase_x':
        np.round(mouse1_dlc.values[:, 9], 2),
        'tailbase_y':
        np.round(mouse1_dlc.values[:, 10], 2)
    })
    features_mouse1_df.to_csv(os.path.join(os.path.dirname(video_dir[0]),
                                           'features_mouse1_dlc.csv'),
                              index=False)

    mouse2_dlc = pd.read_hdf(
        os.path.join(os.path.dirname(video_dir[0]), dlc_output[1]))
    features_mouse2 = mouse2_dlc.values[:, [0, 1, 9, 10]]
    features_mouse2 = np.round(features_mouse2, 2)
    features_mouse2_df = pd.DataFrame({
        'snout_x':
        np.round(mouse2_dlc.values[:, 0], 2),
        'snout_y':
        np.round(mouse2_dlc.values[:, 1], 2),
        'tailbase_x':
        np.round(mouse2_dlc.values[:, 9], 2),
        'tailbase_y':
        np.round(mouse2_dlc.values[:, 10], 2)
    })
    features_mouse2_df.to_csv(os.path.join(os.path.dirname(video_dir[0]),
                                           'features_mouse2_dlc.csv'),
                              index=False)

    return features_mouse1, features_mouse2
Ejemplo n.º 13
0
def process_session(config, session_path):
    pipeline_videos_raw = config['pipeline']['videos_raw']
    pipeline_pose = config['pipeline']['pose_2d']
    video_ext = config['video_extension']

    config_name = os.path.join(config['model_folder'], 'config.yaml')

    source_folder = os.path.join(session_path, pipeline_videos_raw)
    outdir = os.path.join(session_path, pipeline_pose)

    videos = glob(os.path.join(source_folder, '*.' + video_ext))
    videos = sorted(videos, key=natural_keys)

    if len(videos) > 0:
        os.makedirs(outdir, exist_ok=True)

    videos_to_process = []
    for video in videos:
        basename = os.path.basename(video)
        basename, ext = os.path.splitext(basename)

        dataname = os.path.join(outdir, basename + '.h5')
        if os.path.exists(dataname):
            continue
        else:
            rename_dlc_files(
                outdir, basename)  # try renaming in case it processed before
            if os.path.exists(dataname):
                print(video)
                continue
            else:
                videos_to_process.append(video)

    if len(videos_to_process) > 0:
        import deeplabcut
        trap = io.StringIO()
        for i in range(0, len(videos_to_process), 5):
            batch = videos_to_process[i:i + 5]
            for video in batch:
                print(video)
            with redirect_stdout(trap):
                os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
                deeplabcut.analyze_videos(config_name,
                                          batch,
                                          videotype=video_ext,
                                          save_as_csv=False,
                                          destfolder=outdir,
                                          TFGPUinference=False)
            for video in batch:
                basename = os.path.basename(video)
                basename, ext = os.path.splitext(basename)
                rename_dlc_files(outdir, basename)
Ejemplo n.º 14
0
def DLCPredictBehavior(config, input_file, output):
    """
    Takes a video of animal behaviour and predicts body movements based on a supplied DeepLabCut body movement model.

    :param config: The path to the DeepLabCut configuration file.
    :param input_file: The folder containing the behavioural images to be analyzed.
    :param output: The folder to which we save the predicted body movements.
    """
    video_array = []
    img_array = []
    print(input_file)
    for filename in glob.glob(os.path.join(input_file, "*.mp4")):
        video_array.append(os.path.join(input_file, filename))

    for _ in glob.glob(os.path.join(input_file, "*.png")):
        _nsre = re.compile("([0-9]+)")
        return [
            int(text) if text.isdigit() else text.lower()
            for text in re.split(_nsre, s)
        ]
        img = cv2.imread(filename)
        height, width, layers = img.shape
        size = (width, height)
        img_array.append(img)

    if len(img_array) > 0:
        video_output_path = os.path.join(output, "dlc_output", "behavior")
        video_name = os.path.join(video_output_path, "behavior_video.mp4")
        video_output_path = [video_output_path]
        video_name = [video_name]

        if not os.path.isdir(video_output_path[0]):
            os.mkdir(video_output_path[0])
        out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*"MP4V"), 30,
                              size)
        for i in range(len(img_array)):
            out.write(img_array[i])
        out.release()
    elif len(video_array) > 0:
        video_output_path = video_array
        video_name = video_array

    deeplabcut.analyze_videos(config,
                              video_output_path,
                              videotype=".mp4",
                              save_as_csv=True,
                              destfolder=output)
    deeplabcut.create_labeled_video(config,
                                    video_name,
                                    filtered=True,
                                    destfolder=output)
    cv2.destroyAllWindows()
        def anaslyse_deeplabcut(my_download_flag, my_download_list):

            time.sleep(2)
            print(my_download_flag)
            while my_download_flag or len(my_download_list) > 0:
                if len(my_download_list) > 0:
                    directory = my_download_list[0]
                    # print(directory)
                    if os.path.exists(directory):
                        deeplabcut.analyze_videos(self.path_config_file, [directory], save_as_csv=True)
                        if self.generate_labeled_video:
                            deeplabcut.create_labeled_video(self.path_config_file, [directory])
                        os.remove(directory)
                        my_download_list.remove(directory)
Ejemplo n.º 16
0
 def s02_detect_rois():
     """
     step 2 run DLC to detect ROIS
     returns: df_crop, dataframe used to crop video
     """
     out = deeplabcut.analyze_videos(dlc_params['roi_detect'], [str(tfile['mp4_sub'])])
     # deeplabcut.create_labeled_video(config_path_roi_detection, files_mp4)
     tfile['h5_sub'] = next(tpath.glob(f'*{out}*.h5'), None)
     return pd.read_hdf(tfile['h5_sub'])
Ejemplo n.º 17
0
def train():
    videofile_path = ['videos/ostrich.mp4'
                      ]  #Enter a folder OR a list of videos to analyze.
    if not os.path.isfile(videofile_path[0]):
        sys.exit("File {} not found!!".format(videofile_path[0]))

    path_config_file = '/home/braincreator/Pose_estimation/DeepLabCut/examples/ostrich/Testing-Shan-2019-08-07/config.yaml'

    print("\n" + "=" * 80 + "\n\t\t\t\tANALYZE\n" + "=" * 80)
    deeplabcut.analyze_videos(path_config_file,
                              videofile_path,
                              gputouse=None,
                              videotype='.mp4')

    print("\n" + "=" * 80 + "\n\t\t\t\tCreating video\n" + "=" * 80)
    deeplabcut.create_labeled_video(path_config_file,
                                    videofile_path,
                                    outputframerate=5,
                                    draw_skeleton=True)
Ejemplo n.º 18
0
def runDeepLabCut(Inputfilepath,OutputFilepath):
    '''Function inputs are filepath to videos to be tracked by DLC and the folder to save the output to
    Videos are copied to output folder, than processed in DLC based on the dlc config path 
    DLC output is saved in outputfilepath and the output is also converted to npy and saved as well
    '''
    
    #####################Copy Videos to DLC Folder############
    for dir in [Inputfilepath]:#Iterates through input folder
        for video in os.listdir(dir):#Iterates through each video in folder
            #ffmpeg call to copy videos to dlc folder
            subprocess.call(['ffmpeg', '-i', Inputfilepath+'/'+video,  OutputFilepath+'/'+video])


    #################### DeepLabCut ############################
    for dir in [OutputFilepath]:# Loop through dlc folder
        for video in os.listdir(dir):
            #Analyze the videos through deeplabcut
            deeplabcut.analyze_videos(baseProjectPath+'/'+DLCconfigPath, [OutputFilepath +'/'+ video], save_as_csv=True)
            deeplabcut.plot_trajectories(baseProjectPath+'/'+DLCconfigPath,[OutputFilepath +'/'+ video])

    for dir in [OutputFilepath]:#Loop through dlc folder
        for video in dir:# for each video in folder
            #Create a DLC video   
            deeplabcut.create_labeled_video(baseProjectPath+'/'+DLCconfigPath, glob.glob(os.path.join(OutputFilepath ,'*mp4')))

    #If there is not a folder for dlc npy output, create one
    if not os.path.exists(OutputFilepath + 'DLCnpy'):
        os.mkdir(OutputFilepath+ 'DLCnpy')
    
    #Load all dlc csv output files  
    csvfiles = glob.glob(OutputFilepath+'/*csv')
    #For loop gets csv data from all cameras
    j=0
    for data in csvfiles:     
        datapoints = pd.read_csv(data) # read in the csv data 
        print(datapoints)            

        parsedDlcData = datapoints.iloc[3:,7:10].values#the last element in the array is the P value
        #print(parsedDlcData)
    
        print(parsedDlcData)
        np.save(OutputFilepath+'/DLCnpy/dlc_'+cam_names[j]+'.npy',parsedDlcData)#Save data
        j+=1
Ejemplo n.º 19
0
    def analyze_videos(self,event):

        shuffle = self.shuffle.GetValue()
        trainingsetindex = self.trainingset.GetValue()

        if self.csv.GetStringSelection() == "Yes":
            save_as_csv = True
        else:
            save_as_csv = False

        if self.cfg['cropping']:
            crop = self.cfg['x1'], self.cfg['x2'], self.cfg['y1'], self.cfg['y2']
        else:
            crop = None

        if self.dynamic.GetStringSelection() == "No":
            dynamic = (False, .5, 10)
        else:
            dynamic = (True, .5, 10)

        if self.filter.GetStringSelection() == "No":
            filter = None
        else:
            filter = True

        # if self.draw_skeleton.GetStringSelection() == "Yes":
        #     self.draw = True
        # else:
        #     self.draw = True

#        print(self.config,self.filelist,self.videotype.GetValue(),shuffle,trainingsetindex,gputouse=None,save_as_csv=save_as_csv,destfolder=self.destfolder,cropping=cropping)
        deeplabcut.analyze_videos(self.config, self.filelist, videotype=self.videotype.GetValue(), shuffle=shuffle,
                                  trainingsetindex=trainingsetindex, gputouse=None, save_as_csv=save_as_csv,
                                  destfolder=self.destfolder, crop=crop, dynamic=dynamic)
        if self.filter.GetStringSelection() == "Yes":
            deeplabcut.filterpredictions(self.config, self.filelist, videotype=self.videotype.GetValue(), shuffle=shuffle, trainingsetindex=trainingsetindex, filtertype='median', windowlength=5, p_bound=0.001, ARdegree=3, MAdegree=1, alpha=0.01, save_as_csv=True, destfolder=self.destfolder)

        if self.create_labeled_videos.GetStringSelection() == "Yes":
            deeplabcut.create_labeled_video(self.config,self.filelist,self.videotype.GetValue(),shuffle=shuffle, trainingsetindex=trainingsetindex, draw_skeleton= self.draw,trailpoints = self.trail_points.GetValue(), filtered=True)

        if self.trajectory.GetStringSelection() == "Yes":
            deeplabcut.plot_trajectories(self.config, self.filelist, displayedbodyparts=self.bodyparts,
                                         videotype=self.videotype.GetValue(), shuffle=shuffle, trainingsetindex=trainingsetindex, filtered=True, showfigures=False, destfolder=self.destfolder)
Ejemplo n.º 20
0
def predict_images(images, dlccfg, dbfile):

    viddir = mkdtemp()
    try:
        print('Writing temp video files in dir ' + viddir)
        images2avidir(images, viddir)
        print('Finished video writing')
    except:
        shutil.rmtree(viddir)
        print('Failed video writing')
        raise

    destdir = mkdtemp()
    try:
        print('Running DLC. Results will be stored in ' + destdir)
        dlc.analyze_videos(dlccfg, [viddir], destfolder=destdir)
        print('Finished DLC run')
    except:
        shutil.rmtree(viddir)
        shutil.rmtree(destdir)
        print('DLC failed')
        raise

    print('Storing prediction in ' + dbfile)

    try:
        for tracklet in images.keys():
            h5file = sorted(glob(join(destdir, tracklet) + '*.h5'),
                            key=os.path.getmtime)[-1]
            df = pd.read_hdf(h5file)
            df.to_hdf(dbfile, key=tracklet, mode='a')
    except:
        shutil.rmtree(viddir)
        shutil.rmtree(destdir)
        print('Failed storing results')
        raise

    # clean
    print('Cleaning up')
    shutil.rmtree(destdir)
    shutil.rmtree(viddir)
Ejemplo n.º 21
0
def deeplabcut(video):
    import deeplabcut
    import os
    import glob
    from shutil import copy2

    path_config = '/home/in/aeed/plus_maize/Plus_Maiz-Amr-2018-12-25/config.yaml'

    deeplabcut.analyze_videos(path_config, [video], save_as_csv=True)

    deeplabcut.create_labeled_video(path_config, [video], trailpoints=10)

    h5_file = os.path.abspath(glob.glob('plus_maze_*.h5')[0])
    labeled_video = os.path.abspath(glob.glob('plus_maze_*_labeled.mp4')[0])

    pickle = os.path.abspath(glob.glob('plus_maze_*.pickle')[0])
    csv_file = os.path.abspath(glob.glob('plus_maze_*.csv')[0])

    # I failed to force deeplabcut to put the output in the workflow directory, so I am copying them
    # like a caveman

    subj_no = os.getcwd()[-3:]
    experiment_dir = '/home/in/aeed/Work/October_Acquistion/'
    working_dir = 'Plus_Maze_workingdir'

    copy2(
        h5_file, '%s%s/Plus_Maze_workflow/_subject_id_%s/DeepLabCut/' %
        (experiment_dir, working_dir, subj_no))
    copy2(
        labeled_video, '%s%s/Plus_Maze_workflow/_subject_id_%s/DeepLabCut/' %
        (experiment_dir, working_dir, subj_no))
    copy2(
        pickle, '%s%s/Plus_Maze_workflow/_subject_id_%s/DeepLabCut/' %
        (experiment_dir, working_dir, subj_no))
    copy2(
        csv_file, '%s%s/Plus_Maze_workflow/_subject_id_%s/DeepLabCut/' %
        (experiment_dir, working_dir, subj_no))

    return h5_file, labeled_video
Ejemplo n.º 22
0
def process_gpu_batch(config_name, videos, outdir, gpu=None):

    for video in videos:
        basename = os.path.basename(video)
        basename, ext = os.path.splitext(basename)

        dataname = os.path.join(outdir, basename + ".h5")
        print(dataname)
        if os.path.exists(dataname):
            continue
        else:
            trap = io.StringIO()
            with redirect_stdout(trap):
                deeplabcut.analyze_videos(
                    config_name,
                    [video],
                    videotype=ext,
                    save_as_csv=True,
                    destfolder=outdir,
                    gputouse=gpu,
                )
        rename_dlc_files(outdir, basename)
Ejemplo n.º 23
0
def main():
    path_to_DLC_network = sys.argv[1]
    path_to_video = sys.argv[2]
    shuffle = 8

    config = os.path.join(path_to_DLC_network, 'config.yaml')

    # run (to be updated with new folder structures)
    if len(sys.argv) == 3:
        # only process specified video
        video_name = sys.argv[3]
        file = os.path.join(path_to_video, video_name)
        deeplabcut.analyze_videos(config, [file],
                                  shuffle=shuffle,
                                  videotype='mp4',
                                  save_as_csv=True)
        deeplabcut.filterpredictions(config, [file],
                                     shuffle=shuffle,
                                     videotype='mp4')
        deeplabcut.create_labeled_video(config, [file],
                                        shuffle=shuffle,
                                        videotype='mp4',
                                        filtered=True)

    else:
        files = getvideos(path_to_video)
        for file in files:
            deeplabcut.analyze_videos(config, [file],
                                      shuffle=shuffle,
                                      videotype='mp4',
                                      save_as_csv=True)
            deeplabcut.filterpredictions(config, [file],
                                         shuffle=shuffle,
                                         videotype='mp4')
            deeplabcut.create_labeled_video(config, [file],
                                            shuffle=shuffle,
                                            videotype='mp4',
                                            filtered=True)
Ejemplo n.º 24
0
def process_peter(videos,
                  model_folder,
                  out_folder,
                  video_type='avi'):
    os.makedirs(out_folder, exist_ok=True)
    config_name = os.path.join(model_folder, 'config.yaml')

    import deeplabcut
    trap = io.StringIO()
    for i in range(0, len(videos), 5):
        batch = videos[i:i + 5]
        with redirect_stdout(trap):
            os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
            deeplabcut.analyze_videos(config_name,
                                      batch,
                                      videotype=video_type,
                                      save_as_csv=True,
                                      destfolder=out_folder,
                                      TFGPUinference=True)
        for video in batch:
            basename = os.path.basename(video)
            basename, ext = os.path.splitext(basename)
            rename_dlc_files(out_folder, basename)
def analyze_cropped_videos(folders_to_analyze, view_config_paths, marked_vids_parent, cropped_vid_type='.avi', gputouse=0, save_as_csv=True):
    '''

    :param folders_to_analyze:
    :param view_config_paths:
    :param cropped_vid_type:
    :param gputouse:
    :return: scorernames - dictionary with keys 'direct' and 'mirror' containing the scorername strings returned by
        deeplabcut.analyze_videos
    '''

    view_list = folders_to_analyze.keys()
    scorernames = {'direct': '', 'mirror': ''}
    for view in view_list:
        if 'direct' in view:
            dlc_network = 'direct'
        elif 'mirror' in view:
            dlc_network = 'mirror'
        else:
            print(view + ' does not contain the keyword "direct" or "mirror"')
            continue

        config_path = view_config_paths[dlc_network]
        current_view_folders = folders_to_analyze[view]

        for current_folder in current_view_folders:
            cropped_video_list = glob.glob(current_folder + '/*' + cropped_vid_type)
            #todo: skip if analysis already done and stored in the _marked folder
            scorername = deeplabcut.analyze_videos(config_path,
                                      cropped_video_list,
                                      videotype=cropped_vid_type,
                                      gputouse=gputouse,
                                      save_as_csv=save_as_csv)
            scorernames[dlc_network] = scorername

            new_dir = navigation_utilities.create_marked_vids_folder(current_folder, cropped_videos_parent,
                                                                     marked_vids_parent)
            pickle_list = glob.glob(os.path.join(current_folder, '*.pickle'))
            for pickle_file in pickle_list:
                # if the file already exists in the marked_vid directory, don't move it
                _, pickle_name = os.path.split(pickle_file)
                if not os.path.isfile(os.path.join(new_dir, pickle_name)):
                    shutil.copy(pickle_file, new_dir)

    return scorernames
Ejemplo n.º 26
0
def analyze_cropped_optitrack_videos(folders_to_analyze, config_path, parent_directories, cropped_vid_type='.avi', gputouse=0, save_as_csv=True):
    '''

    :param folders_to_analyze:
    :param view_config_paths:
    :param cropped_vid_type:
    :param gputouse:
    :return: scorernames - dictionary with keys 'direct' and 'mirror' containing the scorername strings returned by
        deeplabcut.analyze_videos
    '''

    if cropped_vid_type[0]=='.':
        pass
    else:
        cropped_vid_type = '.' + cropped_vid_type

    marked_vids_parent = parent_directories['marked_vids_parent']

    cam_list = folders_to_analyze.keys()
    for cam_name in cam_list:

        current_cam_folders = folders_to_analyze[cam_name]

        for current_folder in current_cam_folders:
            cropped_video_list = glob.glob(current_folder + '/*' + cropped_vid_type)
            #todo: skip if analysis already done and stored in the _marked folder
            scorername = deeplabcut.analyze_videos(config_path,
                                      cropped_video_list,
                                      videotype=cropped_vid_type,
                                      gputouse=gputouse,
                                      save_as_csv=save_as_csv)
            # deeplabcut.convert_detections2tracklets(config_path, cropped_video_list, videotype='mp4', shuffle=1, trainingsetindex=0)
            # deeplabcut.stitch_tracklets(config_path, cropped_video_list, videotype='mp4', shuffle=1, trainingsetindex=0)

            new_dir = navigation_utilities.create_optitrack_marked_vids_folder(current_folder, cropped_vids_parent,
                                                                               marked_vids_parent)
            pickle_list = glob.glob(os.path.join(current_folder, '*.pickle'))
            for pickle_file in pickle_list:
                # if the file already exists in the marked_vid directory, don't move it
                _, pickle_name = os.path.split(pickle_file)
                if not os.path.isfile(os.path.join(new_dir, pickle_name)):
                    shutil.copy(pickle_file, new_dir)

    return scorername
Ejemplo n.º 27
0
deeplabcut.check_labels(path_config_file) #this creates a subdirectory with the frames + your labels
# Reviewed the labels, the seem to be ok

# Downloading the ResNets dataset:
deeplabcut.create_training_dataset(path_config_file)

# Training the dataset
deeplabcut.train_network(path_config_file)

# Evaluating the results
deeplabcut.evaluate_network(path_config_file)

# Analyzing video
videofile_path = ['dlc-blinking/an3_vid2_full/eyes_only-Guy-2019-01-25/videos/animal_3_video_2_150fps_correct.mp4',
                  'dlc-blinking/an3_vid2_full/eyes_only-Guy-2019-01-25/videos/march_8_animal_1_video_150fps_correct.mp4'] #Enter the list of videos to analyze.
videofile_path = ['dlc-blinking/whisk/whisk_only-Guy-2019-02-01/videos/crush_19_01_07_animal_3.mp4',
                  'dlc-blinking/whisk/whisk_only-Guy-2019-02-01/videos/animal_3_video_2_150fps_correct.mp4']
deeplabcut.analyze_videos(path_config_file,videofile_path,save_as_csv=True)


deeplabcut.create_labeled_video(path_config_file, ['D:\\dlc-blinking\\an3_vid2_full\\whisk_only-Guy-2019-02-01\\videos\\crush_19_01_07_animal_3.mp4'], save_frames=True)
deeplabcut.create_labeled_video(path_config_file, ['D:\\dlc-blinking\\whisk\\whisk_only-Guy-2019-02-01\\videos\\animal_3_video_2_150fps_correct.mp4'], save_frames=True)

%matplotlib notebook #for making interactive plots.
deeplabcut.plot_trajectories(path_config_file,videofile_path)


# TICTOCS:
# training - up to 72/96 hours
# analyzing - 45 minutes and 1.5 hours
# labeling - 25 minutes and 50 minutes
Ejemplo n.º 28
0
        DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits)

    print("TRAIN NETWORK", shuffle)
    deeplabcut.train_network(
        path_config_file,
        shuffle=shuffle,
        saveiters=10000,
        displayiters=200,
        maxiters=maxiters,
        max_snapshots_to_keep=11,
    )

    print("EVALUATE")
    deeplabcut.evaluate_network(path_config_file,
                                Shuffles=[shuffle],
                                plotting=True)

    print("Analyze Video")

    videofile_path = os.path.join(os.getcwd(), "openfield-Pranav-2018-10-30",
                                  "videos", "m3v1mp4.mp4")

    deeplabcut.analyze_videos(path_config_file, [videofile_path],
                              shuffle=shuffle)

    print("Create Labeled Video and plot")
    deeplabcut.create_labeled_video(path_config_file, [videofile_path],
                                    shuffle=shuffle)
    deeplabcut.plot_trajectories(path_config_file, [videofile_path],
                                 shuffle=shuffle)
Ejemplo n.º 29
0
import sys
import os
os.environ["DLClight"] = "True"
vid_list = sys.argv[1:]
import deeplabcut
config_path = '/dartfs-hpc/rc/home/7/f002qw7/thesis/ir_test-markt-2020-02-28/config.yaml'
os.system('echo analyzing')
deeplabcut.analyze_videos(config_path, vid_list)
deeplabcut.create_labeled_video(config_path, vid_list, save_frames=True)
os.system('echo finished vid!')
Ejemplo n.º 30
0
vname='brief'
newvideo=os.path.join(cfg['project_path'],'videos',vname+'.mp4')
try: #you need ffmpeg command line interface
    subprocess.call(['ffmpeg','-i',video[0],'-ss','00:00:00','-to','00:00:00.4','-c','copy',newvideo])
except:
    #for windows:
    from moviepy.editor import VideoFileClip,VideoClip
    clip = VideoFileClip(video[0])
    clip.reader.initialize()
    def make_frame(t):
        return clip.get_frame(1)

    newclip = VideoClip(make_frame, duration=1)
    newclip.write_videofile(newvideo,fps=30)

deeplabcut.analyze_videos(path_config_file,[newvideo],save_as_csv=True, destfolder=dfolder)

print("CREATE VIDEO")
deeplabcut.create_labeled_video(path_config_file,[newvideo], destfolder=dfolder)

print("Making plots")
deeplabcut.plot_trajectories(path_config_file,[newvideo], destfolder=dfolder)


print("EXTRACT OUTLIERS")
deeplabcut.extract_outlier_frames(path_config_file,[newvideo],outlieralgorithm='jump',epsilon=0,automatic=True, destfolder=dfolder)


file=os.path.join(cfg['project_path'],'labeled-data',vname,"machinelabels-iter"+ str(cfg['iteration']) + '.h5')

print("RELABELING")