Exemplo n.º 1
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)
Exemplo n.º 2
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])
Exemplo n.º 3
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)
 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(dlc_params['roi_detect'],
                                     [str(tfile['mp4_sub'])])
     tfile['h5_sub'] = next(tpath.glob(f'*{out}*.h5'), None)
     return pd.read_hdf(tfile['h5_sub'])
Exemplo n.º 5
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)
Exemplo n.º 7
0
    def create_videos(self, event):

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

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

        if self.video_slow.GetStringSelection() == "Yes":
            self.slow = True
        else:
            self.slow = False

        if self.filter.GetStringSelection() == "Yes":
            if len(self.bodyparts) == 0:
                self.bodyparts = 'all'

                deeplabcut.create_labeled_video(
                    self.config,
                    self.filelist,
                    self.videotype.GetValue(),
                    shuffle=shuffle,
                    trainingsetindex=trainingsetindex,
                    save_frames=self.slow,
                    draw_skeleton=self.draw,
                    displayedbodyparts=self.bodyparts,
                    trailpoints=self.trail_points.GetValue(),
                    filtered=True)

        if len(self.bodyparts) == 0:
            self.bodyparts = 'all'
        deeplabcut.create_labeled_video(
            self.config,
            self.filelist,
            self.videotype.GetValue(),
            shuffle=shuffle,
            trainingsetindex=trainingsetindex,
            save_frames=self.slow,
            draw_skeleton=self.draw,
            displayedbodyparts=self.bodyparts,
            trailpoints=self.trail_points.GetValue(),
            filtered=False)
Exemplo n.º 8
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)
Exemplo n.º 9
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
Exemplo n.º 10
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)
Exemplo n.º 11
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
Exemplo n.º 12
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)
        # Make super short video (so the analysis is quick!)
        newvideo = deeplabcut.ShortenVideo(video[0],
                                           start='00:00:00',
                                           stop='00:00:00.4',
                                           outsuffix='short',
                                           outpath=os.path.join(
                                               cfg['project_path'], 'videos'))
        vname = Path(newvideo).stem
    deeplabcut.analyze_videos(path_config_file, [newvideo],
                              shuffle=shuffle,
                              save_as_csv=True,
                              destfolder=dfolder)

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

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

    print("EXTRACT OUTLIERS")
    deeplabcut.extract_outlier_frames(path_config_file, [newvideo],
                                      shuffle=shuffle,
                                      outlieralgorithm='jump',
                                      epsilon=0,
                                      automatic=True,
                                      destfolder=dfolder)
    file = os.path.join(cfg['project_path'], 'labeled-data', vname,
Exemplo n.º 14
0
path_config_file = os.path.join(os.getcwd(), "openfield-Pranav-2018-10-30/config.yaml")
deeplabcut.load_demo_data(path_config_file)
shuffle = 13

deeplabcut.create_training_dataset(path_config_file, Shuffles=[shuffle])
cfg = deeplabcut.auxiliaryfunctions.read_config(path_config_file)

# example how to set pose config variables:
posefile, _, _ = deeplabcut.return_train_network_path(path_config_file, shuffle=shuffle)
edits = {"save_iters": 15000, "display_iters": 1000, "multi_step": [[0.005, 15001]]}
DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits)

print("TRAIN NETWORK")
deeplabcut.train_network(path_config_file, shuffle=shuffle, max_snapshots_to_keep=3)

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
)  # ,videotype='.mp4')

print("Create Labeled Video")
deeplabcut.create_labeled_video(
    path_config_file, [videofile_path], save_frames=False, shuffle=shuffle
)  # ,videotype='.mp4')
Exemplo n.º 15
0
 def analyze_videos(self):
     test_videos = self.test_video
     deeplabcut.analyze_videos(self.full_config_path(), [test_videos])
     deeplabcut.create_labeled_video(self.full_config_path(), [test_videos])
    )

    print("Plotting trajectories...")
    deeplabcut.plot_trajectories(
        config_path,
        [new_video_path],
        "mp4",
        track_method=TESTTRACKER
    )
    print("Trajectory plotted.")

    print("Creating labeled video...")
    deeplabcut.create_labeled_video(
        config_path,
        [new_video_path],
        "mp4",
        save_frames=False,
        color_by="individual",
        track_method=TESTTRACKER
    )
    print("Labeled video created.")

    print("Filtering predictions...")
    deeplabcut.filterpredictions(
        config_path,
        [new_video_path],
        "mp4",
        track_method=TESTTRACKER
    )
    print("Predictions filtered.")

    print("Extracting outlier frames...")
Exemplo n.º 17
0
def create_pretrained_human_project(project,
                                    experimenter,
                                    videos,
                                    working_directory=None,
                                    copy_videos=False,
                                    videotype='.avi',
                                    createlabeledvideo=True,
                                    analyzevideo=True):
    """
    Creates a demo human project and analyzes a video with ResNet 101 weights pretrained on MPII Human Pose. This is from the DeeperCut paper by Insafutdinov et al. https://arxiv.org/abs/1605.03170 Please make sure to cite it too if you use this code!

    Parameters
    ----------
    project : string
        String containing the name of the project.

    experimenter : string
        String containing the name of the experimenter.

    videos : list
        A list of string containing the full paths of the videos to include in the project.

    working_directory : string, optional
        The directory where the project will be created. The default is the ``current working directory``; if provided, it must be a string.

    copy_videos : bool, optional
        If this is set to True, the videos are copied to the ``videos`` directory. If it is False,symlink of the videos are copied to the project/videos directory. The default is ``False``; if provided it must be either
        ``True`` or ``False``.
    analyzevideo " bool, optional
        If true, then the video is analzyed and a labeled video is created. If false, then only the project will be created and the weights downloaded. You can then access them

    Example
    --------
    Linux/MacOs
    >>> deeplabcut.create_pretrained_human_project('human','Linus',['/data/videos/mouse1.avi'],'/analysis/project/',copy_videos=False)

    Windows:
    >>> deeplabcut.create_pretrained_human_project('human','Bill',[r'C:\yourusername\rig-95\Videos\reachingvideo1.avi'],r'C:\yourusername\analysis\project' copy_videos=False)
    Users must format paths with either:  r'C:\ OR 'C:\\ <- i.e. a double backslash \ \ )
    --------
    """

    cfg = deeplabcut.create_new_project(project, experimenter, videos,
                                        working_directory, copy_videos,
                                        videotype)

    config = auxiliaryfunctions.read_config(cfg)
    config['bodyparts'] = [
        'ankle1', 'knee1', 'hip1', 'hip2', 'knee2', 'ankle2', 'wrist1',
        'elbow1', 'shoulder1', 'shoulder2', 'elbow2', 'wrist2', 'chin',
        'forehead'
    ]
    config['skeleton'] = [['ankle1', 'knee1'], ['ankle2', 'knee2'],
                          ['knee1', 'hip1'], ['knee2', 'hip2'],
                          ['hip1', 'hip2'], ['shoulder1', 'shoulder2'],
                          ['shoulder1', 'hip1'], ['shoulder2', 'hip2'],
                          ['shoulder1', 'elbow1'], ['shoulder2', 'elbow2'],
                          ['chin', 'forehead'], ['elbow1', 'wrist1'],
                          ['elbow2', 'wrist2']]
    config['default_net_type'] = 'resnet_101'
    auxiliaryfunctions.write_config(cfg, config)
    config = auxiliaryfunctions.read_config(cfg)

    train_dir = Path(
        os.path.join(
            config['project_path'],
            str(
                auxiliaryfunctions.GetModelFolder(
                    trainFraction=config['TrainingFraction'][0],
                    shuffle=1,
                    cfg=config)), 'train'))
    test_dir = Path(
        os.path.join(
            config['project_path'],
            str(
                auxiliaryfunctions.GetModelFolder(
                    trainFraction=config['TrainingFraction'][0],
                    shuffle=1,
                    cfg=config)), 'test'))

    # Create the model directory
    train_dir.mkdir(parents=True, exist_ok=True)
    test_dir.mkdir(parents=True, exist_ok=True)

    modelfoldername = auxiliaryfunctions.GetModelFolder(
        trainFraction=config['TrainingFraction'][0], shuffle=1, cfg=config)

    path_train_config = str(
        os.path.join(config['project_path'], Path(modelfoldername), 'train',
                     'pose_cfg.yaml'))
    path_test_config = str(
        os.path.join(config['project_path'], Path(modelfoldername), 'test',
                     'pose_cfg.yaml'))

    # Download the weights and put then in appropriate directory
    cwd = os.getcwd()
    os.chdir(train_dir)
    print(
        "Checking if the weights are already available, otherwise I will download them!"
    )
    weightfilename = auxfun_models.download_mpii_weigths(train_dir)
    os.chdir(cwd)

    # Create the pose_config.yaml files
    parent_path = Path(os.path.dirname(deeplabcut.__file__))
    defaultconfigfile = str(parent_path / 'pose_cfg.yaml')
    trainingsetfolder = auxiliaryfunctions.GetTrainingSetFolder(config)
    datafilename, metadatafilename = auxiliaryfunctions.GetDataandMetaDataFilenames(
        trainingsetfolder,
        trainFraction=config['TrainingFraction'][0],
        shuffle=1,
        cfg=config)
    bodyparts = config['bodyparts']
    net_type = 'resnet_101'
    num_shuffles = 1
    model_path, num_shuffles = auxfun_models.Check4weights(
        net_type, parent_path, num_shuffles)
    items2change = {
        "dataset": 'dataset-test.mat',  #datafilename,
        "metadataset": metadatafilename,
        "num_joints": len(bodyparts),
        "all_joints": [[i] for i in range(len(bodyparts))],
        "all_joints_names": [str(bpt) for bpt in bodyparts],
        "init_weights":
        weightfilename.split('.index')[0],  #'models/mpii/snapshot-1030000',
        "project_path": str(config['project_path']),
        "net_type": net_type,
        "dataset_type": "default"
    }
    trainingdata = MakeTrain_pose_yaml(items2change, path_train_config,
                                       defaultconfigfile)

    keys2save = [
        "dataset", "dataset_type", "num_joints", "all_joints",
        "all_joints_names", "net_type", 'init_weights', 'global_scale',
        'location_refinement', 'locref_stdev'
    ]
    MakeTest_pose_yaml(trainingdata, keys2save, path_test_config)

    video_dir = os.path.join(config['project_path'], 'videos')

    if analyzevideo == True:
        # Analyze the videos
        deeplabcut.analyze_videos(cfg, [video_dir],
                                  videotype,
                                  save_as_csv=True)
    if createlabeledvideo == True:
        deeplabcut.create_labeled_video(cfg, [video_dir],
                                        videotype,
                                        draw_skeleton=True)
        deeplabcut.plot_trajectories(cfg, [video_dir], videotype)
    return cfg, path_train_config
Exemplo n.º 18
0
def write_config(configname,cfg):
    with open(str(configname), 'w') as ymlfile:
                yaml.dump(cfg, ymlfile,default_flow_style=False)

# Loading example data set 
path_config_file = os.path.join(os.getcwd(),'openfield-Pranav-2018-10-30/config.yaml')
deeplabcut.load_demo_data(path_config_file)

cfg=read_config(path_config_file)
posefile=os.path.join(cfg['project_path'],'dlc-models/iteration-'+str(cfg['iteration'])+'/'+ cfg['Task'] + cfg['date'] + '-trainset' + str(int(cfg['TrainingFraction'][0] * 100)) + 'shuffle' + str(1),'train/pose_cfg.yaml')
DLC_config=read_config(posefile)
DLC_config['save_iters']=10
DLC_config['display_iters']=2
DLC_config['multi_step']=[[0.005,15001]]
write_config(posefile,DLC_config)

print("TRAIN NETWORK")

deeplabcut.train_network(path_config_file, shuffle=1,saveiters=15000,displayiters=100)

print("EVALUATE")
deeplabcut.evaluate_network(path_config_file,plotting=False)


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])

print("Create Labeled Video")
deeplabcut.create_labeled_video(path_config_file,[videofile_path],save_frames=True)
Exemplo n.º 19
0
dlc.auxiliaryfunctions.edit_config(config_path, edits)

print('\nediting the config file... ')
for item in edits.items():
    print(item)

print('edit completed!')

dlc.create_labeled_video(config=config_path,
                         videos=videos_path_list[listindex1:listindex2],
                         videotype='.mp4',
                         shuffle=shuffleindex,
                         trainingsetindex=0,
                         filtered=False,
                         save_frames=True,
                         Frames2plot=None,
                         delete=True,
                         displayedbodyparts='all',
                         codec='mp4v',
                         outputframerate=None,
                         destfolder=None,
                         draw_skeleton=True,
                         trailpoints=0,
                         displaycropped=False)

print("dlc_create_labeled_video.py with the call", str(sys.argv), "is done!")

print("returning snapshotindex back to 'all'...!")
edits = {'snapshotindex': 'all'}
dlc.auxiliaryfunctions.edit_config(config_path, edits)
print("snapshotindex is set back to 'all'")
def Get_Percent_Good(coords):
    bbs, it, mh = coords[0], coords[1], coords[2]

    current_video_path = test_video_folder_path.format(bbs=bbs, it=it, mh=mh)

    #Writes a new file here for the future labeling and things to use
    i_cfg_file = open(inference_cfg_path, "w")
    i_cfg_file.write(inference_cfg_text.format(bbs=bbs, it=it, mh=mh))
    i_cfg_file.close()

    #Creates the folder and videos
    if not os.path.isdir(current_video_path):
        os.mkdir(current_video_path)

        for v in og_video_files:
            shutil.copy2(main_video_folder_path + v, current_video_path + v)

        for p in og_pickle_files:
            shutil.copy2(main_video_folder_path + p, current_video_path + p)

    #analyze video
    #No longer needed since they are the same each time
    #deeplabcut.analyze_videos(config_path,[current_video_path], videotype='.mp4', save_as_csv = True)

    #convert to tracklets
    deeplabcut.convert_detections2tracklets(config_path, [current_video_path],
                                            videotype='mp4',
                                            shuffle=1,
                                            trainingsetindex=0,
                                            track_method='box')

    video_files = os.listdir(current_video_path)

    #Convert to h5s
    for file in video_files:
        if file.endswith("_bx.pickle"):
            h5_name = file.replace("pickle", "h5")
            video_name = file.replace

            if h5_name in video_files:
                print("h5 for {} already exists!".format(file))
            else:
                print("Converting {} to h5!".format(file))
                deeplabcut.convert_raw_tracks_to_h5(config_path,
                                                    current_video_path + file)

    #Need to do this each time to update with new videos
    video_files = os.listdir(current_video_path)
    #fix the messy bits in the files so all points exist
    for file in video_files:
        if file.endswith("_bx.h5"):

            print(file)

            if file.startswith("2020_6"):
                x_len = 1920
                y_len = 800
            else:
                x_len = 2368
                y_len = 1152

            rep_values = [x_len - x_len / 10, y_len / 2, 0]

            hf = h5py.File(current_video_path + file, 'r+')
            table = hf['df_with_missing/table']

            n_table = np.array(table)
            new_table = n_table

            for i in range(n_table.shape[0]):
                dlc_data = n_table[i][1]
                dlc_data = np.nan_to_num(dlc_data)

                for j in range(len(dlc_data)):
                    # j%3 gives 0 = x, 1 = y, 2 = prob
                    pos = j % 3

                    #if there is no data then replace it with default
                    if dlc_data[j] == 0:
                        new_table[i][1][j] = rep_values[pos]

                        if pos != 2:
                            print(
                                "No Points. Set to {} in position {}.".format(
                                    rep_values[pos], pos))

                    #if x is out of range replace with default
                    if pos == 0 and (dlc_data[j] > x_len or dlc_data[j] < 0):
                        new_table[i][1][j] = rep_values[pos]
                        print(
                            "Out of X Bounds: Replaced {} with {} in position {}."
                            .format(dlc_data[j], rep_values[pos], pos))

                    #if y is out of range replace with default
                    if pos == 1 and (dlc_data[j] > y_len or dlc_data[j] < 0):
                        new_table[i][1][j] = rep_values[pos]
                        print(
                            "Out of Y Bounds: Replaced {} with {} in position {}."
                            .format(dlc_data[j], rep_values[pos], pos))

                    #This is all to allow me to just correct the first n frames and only extract those

            table[...] = new_table
            hf.close()

    video_files = os.listdir(current_video_path)
    #Now create the filtered videos and CSVs
    for file in video_files:
        if file.endswith(".mp4"):

            print(file)

            deeplabcut.filterpredictions(config_path,
                                         [current_video_path + file],
                                         videotype='mp4',
                                         shuffle=1,
                                         track_method='box')
            deeplabcut.create_labeled_video(config_path,
                                            [current_video_path + file],
                                            videotype='.mp4',
                                            shuffle=1,
                                            draw_skeleton=True,
                                            track_method='box',
                                            save_frames=False,
                                            filtered=True)

    video_files = os.listdir(current_video_path)
    #Now finally get the percent of "good" points
    files = []
    good_percents = []

    for file_name in video_files:
        if file_name.endswith(".csv"):

            in_csv = open(current_video_path + file_name, "r")

            #Skip the first 4
            skip_lines = 0

            good_points = 0
            total_points = 0

            for line in in_csv:
                if skip_lines >= 4:
                    lis = line.split(",")[:-24]

                    #Off by 1 for the first row of frame counts
                    probs = np.asarray(lis[3::3]).astype(np.float)

                    #If there are no zeros in probs
                    good_points += np.sum(probs > prob_cutoff)
                    total_points += len(probs)

                else:
                    skip_lines += 1

                    lis = line.split(",")[:-24]

            files.append(file_name)
            good_percents.append(round(good_points / total_points * 100, 2))

            in_csv.close()
            #out_csv.close()

    files = np.asarray(files)
    good_percents = np.asarray(good_percents)

    sort_percent = np.flip(good_percents[np.argsort(good_percents)])
    sorted_files = np.flip(files[np.argsort(good_percents)])

    out_file = open(
        current_video_path +
        "PGV_{bbs}_{it}_{mh}.txt".format(bbs=bbs, it=it, mh=mh), "w")

    for i in range(len(sorted_files)):
        out_file.write("{} : {}% good points\n".format(sorted_files[i][:22],
                                                       sort_percent[i]))
        print("{} : {}% good points".format(sorted_files[i][:22],
                                            sort_percent[i]))

    out_file.close()

    score = np.sum((100 - good_percents)**2)

    main_csv = open(
        main_video_folder_path + "PGV_all.csv".format(bbs=bbs, it=it, mh=mh),
        "a")
    main_csv.write(
        "{bbs}_{it}_{mh},{bbs},{it},{mh},{v0},{v1},{v2},{v3},{v4},{m},{score}\n"
        .format(bbs=bbs,
                it=it,
                mh=mh,
                v0=good_percents[0],
                v1=good_percents[1],
                v2=good_percents[2],
                v3=good_percents[3],
                v4=good_percents[4],
                m=np.mean(good_percents),
                score=score))
    main_csv.close()

    shutil.rmtree(current_video_path)

    return (score)
Exemplo n.º 21
0
def usingDeep(path_config, project):
    exit = False
    while exit == False:
        video_path = path_config.split("/")
        video_path = '/' + video_path[1] + '/' + video_path[
            2] + '/' + video_path[3] + '/' + video_path[4] + '/videos/'
        print_usage("project")
        action = input()
        while action not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
            try:
                action = int(action)
                if action not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
                    print("please type number between 0 and 12")
                    action = input()
            except ValueError:
                print("Please enter number")
                action = input()
            print("been here")
        if action == 0:
            return False
        elif action == 1:
            print("do you want to crop the video? yes/no")
            if input() == "yes" or "y":
                print(
                    "how many videos you you want to crop? (use number: 1,2,3 etc)"
                )
                crops = int(input())
                print("only crop all the video's and than exit")
                for loop in range(0, crops):
                    deeplabcut.extract_frames(path_config,
                                              'manual',
                                              'kmeans',
                                              crop=True)
            deeplabcut.extract_frames(path_config,
                                      'automatic',
                                      'kmeans',
                                      crop=True)
        elif action == 2:
            deeplabcut.label_frames(path_config)
        elif action == 3:
            deeplabcut.check_labels(path_config)
        elif action == 4:
            deeplabcut.create_training_dataset(path_config)
        elif action == 5:
            with open("training_network.py") as fp:
                lines = fp.readlines()
                lines[3] = lines[3].split("=")
                lines[3] = lines[3][0] + "= '" + path_config + "'\n"

            with open("training_network.py", "w") as fp:
                for line in lines:
                    fp.writelines(line)

            print("run: sbatch slurm.sh")
            return True
        elif action == 6:
            try:
                deeplabcut.evaluate_network(path_config,
                                            Shuffles=[1],
                                            trainingsetindex=0,
                                            plotting=None,
                                            show_errors=True,
                                            comparisonbodyparts='all',
                                            gputouse=None,
                                            rescale=False)
            except OSError as e:
                print("file does not exist")
        elif action == 7:
            print("\nType video name in project/videos you want to analyze")
            video_path = video_path + create_dict_list(
                path_config[:-11] + "videos/", 1)[0]
            with open("training_network.py") as fp:
                lines = fp.readlines()
                lines[3] = lines[3].split("=")
                lines[3] = lines[3][0] + "= '" + path_config + "'\n"
                lines[4] = lines[4].split("=")
                lines[4] = lines[4][0] + "= '" + video_path + "'\n"

            with open("training_network.py", "w") as fp:
                for line in lines:
                    fp.writelines(line)
            print(
                "run: sbatch slurm.sh after changing the command in training_network.py"
            )
            return True
        elif action == 8:
            print(
                "\nChoose the video in project/videos you want to plot trajectories from"
            )
            video_path = video_path + create_dict_list(
                path_config[:-11] + "videos/", 1)[0]
            print(video_path)
            deeplabcut.plot_trajectories(path_config, [video_path],
                                         filtered=True)
        elif action == 9:
            print(
                "\nChoose the video in project/videos you want to make a labeled video from"
            )
            video_path = video_path + create_dict_list(
                path_config[:-11] + "videos/", 1)[0]
            deeplabcut.create_labeled_video(path_config, [video_path],
                                            videotype='.mp4',
                                            draw_skeleton=True)
        elif action == 10:
            print("\nChoose where to upload the video from")
            video_path = '/data/11012579/videos/' + create_dict_list(
                '/data/11012579/videos/', 0)[0]
            print("\nChoose which video to upload")
            video_path_list = [
                video_path + "/" + create_dict_list(video_path, 1)[0]
            ]
            while True:
                print("\nDo you want to add more videos?\nType yes or no")
                if input() == 'yes':
                    video_path_list.append(video_path + "/" +
                                           create_dict_list(video_path, 1)[0])
                else:
                    deeplabcut.add_new_videos(path_config,
                                              video_path_list,
                                              copy_videos=False)
                    break
        elif action == 11:
            print("also here")
            Dlc_results2 = pd.read_hdf(
                '/data/11012579/videos/vidDLC_resnet50_demo_project_grab2Feb7shuffle1_11500.h5'
            )
            Dlc_results2.plot()
        else:
            print_usage("error")

        print("klaar")
Exemplo n.º 22
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)
Exemplo n.º 23
0
    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")
DF=pd.read_hdf(file,'df_with_missing')
DLCscorer=np.unique(DF.columns.get_level_values(0))[0]
DF.columns.set_levels([scorer.replace(DLCscorer,scorer)],level=0,inplace=True)
Exemplo n.º 24
0
img_array = []

for filename in files:
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

videoBaseName = os.path.basename(os.path.normpath(dataDir))

if '.' in videoBaseName:
    videoBaseName = videoBaseName[:-2]

videoName = dataDir + videoBaseName + '.avi'
out = cv2.VideoWriter(videoName, cv2.VideoWriter_fourcc(*'DIVX'), 25, size)

for i in range(len(img_array)):
    out.write(img_array[i])
out.release()

videoPath = videoName
imageDir = dataDir

deeplabcut.analyze_time_lapse_frames(config,
                                     imageDir,
                                     save_as_csv=True,
                                     rgb=False)

deeplabcut.create_labeled_video(config, [videoPath], filtered=False)
Exemplo n.º 25
0
    deeplabcut.analyze_videos(
        path_config_file,
        [newvideo],
        save_as_csv=True,
        destfolder=dfolder,
        dynamic=(True, 0.1, 5),
    )

    print("analyze again...")
    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,
                                    save_frames=True)

    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,
    )
Exemplo n.º 26
0
        "mp4",
        track_method=TESTTRACKER,
        transformer_checkpoint=transformer_checkpoint,
    )

    print("Plotting trajectories...")
    deeplabcut.plot_trajectories(
        config_path, [new_video_path], "mp4", track_method=TESTTRACKER
    )
    print("Trajectory plotted.")

    print("Creating labeled video...")
    deeplabcut.create_labeled_video(
        config_path,
        [new_video_path],
        "mp4",
        save_frames=False,
        color_by="individual",
        track_method="transformer",
    )
    print("Labeled video created.")

    print("Filtering predictions...")
    deeplabcut.filterpredictions(
        config_path, [new_video_path], "mp4", track_method=TESTTRACKER
    )
    print("Predictions filtered.")

    print("Extracting outlier frames...")
    deeplabcut.extract_outlier_frames(
        config_path, [new_video_path], "mp4", automatic=True, track_method=TESTTRACKER
    )
Exemplo n.º 27
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!')
Exemplo n.º 28
0
    newvideo_path = os.path.join(path_prj, 'videostoanalyze', animal)
    newvideos = [os.path.join(newvideo_path, 'CPB15_120418.mp4')]

    # animal = 'Az'
    #    # Az/
    # newvideo_path = os.path.join(path_prj, 'videostoanalyze', animal)
    # newvideos = [os.path.join(newvideo_path, 'Az_Neural-190128_Azula-190128-141757_Cam1.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190313_Azula-190313-124618_Cam1.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190410_Azula-190410-120937_Cam1.avi')]

    # # Az/turningbehavior
    # newvideo_path = os.path.join(path_prj, 'videostoanalyze', animal,'turningbehavior')
    # newvideos = [os.path.join(newvideo_path, 'Az_Neural-190128_Azula-190128-141757_Cam2.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190228_Azula-190228-121940_Cam2.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190228_Azula-190228-123038_Cam2.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190410_Azula-190410-120937_Cam2.avi'), \
    # 			 os.path.join(newvideo_path, 'Az_Neural-190410_Azula-190410-122407_Cam2.avi')]
    if tag_analyze:
        # analyze new video
        deeplabcut.analyze_videos(path_config_file,
                                  newvideos,
                                  save_as_csv=True,
                                  videotype='.mp4')

    if tag_createlabel:
        # create labeled video in .mp4
        deeplabcut.create_labeled_video(path_config_file, newvideos)

# plot trajectories
#deeplabcut.plot_trajectories(path_config_file, newvideos)
Exemplo n.º 29
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
Exemplo n.º 30
0
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 17 14:19:41 2019

@author: gtsror
"""
import matplotlib
import deeplabcut
import tensorflow as tf
import os
from pathlib import Path

path_config_file = os.path.join(os.getcwd(),
                                'Reaching-Mackenzie-2018-08-30/config.yaml')
deeplabcut.load_demo_data(path_config_file)

deeplabcut.check_labels(path_config_file)
deeplabcut.create_training_dataset(path_config_file)
deeplabcut.train_network(path_config_file, shuffle=1)
deeplabcut.evaluate_network(path_config_file, plotting=True)

# creating the video path
videofile_path = '/Reaching-Mackenzie-2018-08-30/videos/MovieS2_Perturbation_noLaser_compressed.avi'
print("Start Analyzing the video!")
deeplabcut.analyze_videos(path_config_file, [videofile_path])
deeplabcut.create_labeled_video(path_config_file, [videofile_path])