def create_labeled_videos(folders_to_analyze, marked_vids_parent, view_config_paths, scorernames,
                          cropped_vid_type='.avi',
                          skipdirect=False,
                          skipmirror=False,
                          view_list=('direct', 'leftmirror', 'rightmirror')
):
    '''
    
    :param folders_to_analyze: 
    :param view_config_paths: 
    :param scorernames: dictionary with keys 'direct' and 'mirror'
    :param cropped_vid_type:
    :param move_to_new_folder: if True, create a new folder in which the marked videos and analysis files are stored
        to make it easier to move them to another computer without taking the original videos with them
    :return: 
    '''
    # in case there are some previously cropped videos that need to be analyzed
    folders_to_analyze = navigation_utilities.find_folders_to_analyze(cropped_videos_parent, view_list=view_list)
    # view_list = folders_to_analyze.keys()

    for view in view_list:
        if 'direct' in view:
            if skipdirect:
                continue
            dlc_network = 'direct'
        elif 'mirror' in view:
            if skipmirror:
                continue
            dlc_network = 'mirror'
        else:
            print(view + ' does not contain the keyword "direct" or "mirror"')
            continue
        config_path = view_config_paths[dlc_network]
        scorername = scorernames[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)
            deeplabcut.create_video_with_all_detections(config_path, cropped_video_list, scorername)

            # current_basename = os.path.basename(current_folder)
            new_dir =  navigation_utilities.create_marked_vids_folder(current_folder, cropped_videos_parent, marked_vids_parent)
            #    os.path.join(marked_vids_parent, current_basename + '_marked')

            test_name = os.path.join(current_folder, '*' + scorername + '*.mp4')
            marked_vid_list = glob.glob(test_name)
            pickle_list = glob.glob(os.path.join(current_folder, '*.pickle'))

            for marked_vid in marked_vid_list:
                 # if the file already exists in the marked_vid directory, don't move it
                 _, marked_vid_name = os.path.split(marked_vid)
                 if not os.path.isfile(os.path.join(new_dir, marked_vid_name)):
                      shutil.move(marked_vid, new_dir)
                 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.move(pickle_file, new_dir)
Beispiel #2
0
def create_labeled_optitrack_videos(folders_to_analyze, parent_directories, config_path, scorername,
                                  cropped_vid_type='.avi'):
    '''
    :param folders_to_analyze:
    :param view_config_paths:
    :param scorernames: dictionary with keys 'direct' and 'mirror'
    :param cropped_vid_type:
    :param move_to_new_folder: if True, create a new folder in which the marked videos and analysis files are stored
        to make it easier to move them to another computer without taking the original videos with them
    :return:
    '''

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

    # in case there are some previously cropped videos that need to be analyzed
    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:
            _, folder_name = os.path.split(current_folder)
            if current_folder[:8] == 'dLight36':
                continue
            cropped_video_list = glob.glob(current_folder + '/*' + cropped_vid_type)
            try:
                deeplabcut.create_video_with_all_detections(config_path, cropped_video_list, scorername)
            except:
                pass

            # current_basename = os.path.basename(current_folder)
            new_dir = navigation_utilities.create_marked_vids_folder(current_folder, cropped_vids_parent,
                                                                     marked_vids_parent)
            #    os.path.join(marked_vids_parent, current_basename + '_marked')

            test_name = os.path.join(current_folder, '*' + scorername + '*.mp4')
            marked_vid_list = glob.glob(test_name)
            pickle_list = glob.glob(os.path.join(current_folder, '*.pickle'))

            for marked_vid in marked_vid_list:
                # if the file already exists in the marked_vid directory, don't move it
                _, marked_vid_name = os.path.split(marked_vid)
                if not os.path.isfile(os.path.join(new_dir, marked_vid_name)):
                    shutil.move(marked_vid, new_dir)
                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.move(pickle_file, new_dir)
    deeplabcut.analyze_videos(
        config_path,
        [new_video_path],
        "mp4",
        robust_nframes=True,
        allow_growth=True,
        use_shelve=USE_SHELVE,
    )

    print("Video analyzed.")

    print("Create video with all detections...")
    scorer, _ = auxiliaryfunctions.GetScorerName(cfg, 1, TRAIN_SIZE)

    deeplabcut.create_video_with_all_detections(
        config_path, [new_video_path], shuffle=1, displayedbodyparts=["bodypart1"]
    )

    print("Video created.")

    print("Convert detections to tracklets...")
    deeplabcut.convert_detections2tracklets(
        config_path,
        [new_video_path],
        "mp4",
        track_method=TESTTRACKER
    )
    print("Tracklets created...")

    deeplabcut.stitch_tracklets(
        config_path,
    def analyze_videos(self, event):

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

        if self.cfg.get("multianimalproject", False):
            print("Analyzing ... ")
        else:
            if self.csv.GetStringSelection() == "Yes":
                save_as_csv = True
            else:
                save_as_csv = False
            if self.dynamic.GetStringSelection() == "No":
                dynamic = (False, 0.5, 10)
            else:
                dynamic = (True, 0.5, 10)
            if self.filter.GetStringSelection() == "No":
                filter = None
            else:
                filter = True

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

        if self.cfg.get("multianimalproject", False):
            if self.robust.GetStringSelection() == "No":
                robust = False
            else:
                robust = True
            scorername = deeplabcut.analyze_videos(
                self.config,
                self.filelist,
                videotype=self.videotype.GetValue(),
                shuffle=shuffle,
                trainingsetindex=trainingsetindex,
                gputouse=None,
                cropping=crop,
                robust_nframes=robust,
            )
            if self.create_video_with_all_detections.GetStringSelection(
            ) == "Yes":
                trainFrac = self.cfg["TrainingFraction"][trainingsetindex]
                scorername, DLCscorerlegacy = auxiliaryfunctions.GetScorerName(
                    self.cfg, shuffle, trainFraction=trainFrac)
                print(scorername)
                deeplabcut.create_video_with_all_detections(
                    self.config, self.filelist, DLCscorername=scorername)

        else:
            scorername = deeplabcut.analyze_videos(
                self.config,
                self.filelist,
                videotype=self.videotype.GetValue(),
                shuffle=shuffle,
                trainingsetindex=trainingsetindex,
                gputouse=None,
                save_as_csv=save_as_csv,
                cropping=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,
                    save_as_csv=True,
                )

            if self.trajectory.GetStringSelection() == "Yes":
                if self.showfigs.GetStringSelection() == "No":
                    showfig = False
                else:
                    showfig = True
                deeplabcut.plot_trajectories(
                    self.config,
                    self.filelist,
                    displayedbodyparts=self.bodyparts,
                    videotype=self.videotype.GetValue(),
                    shuffle=shuffle,
                    trainingsetindex=trainingsetindex,
                    filtered=True,
                    showfigures=showfig,
                )
    def analyze_videos(self, event):

        shuffle = self.shuffle.GetValue()
        videotype = self.videotype.GetValue(),

        if self.cfg.get("multianimalproject", False):
            print("DLC network loading and video analysis starting ... ")
            auto_track = True
        else:
            if self.csv.GetStringSelection() == "Yes":
                save_as_csv = True
            else:
                save_as_csv = False
            if self.dynamic.GetStringSelection() == "No":
                dynamic = (False, 0.5, 10)
            else:
                dynamic = (True, 0.5, 10)
            if self.filter.GetStringSelection() == "No":
                _filter = False
            else:
                _filter = True

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

        if self.cfg.get("multianimalproject", False):
            if self.robust.GetStringSelection() == "No":
                robust = False
            else:
                robust = True
            scorername = deeplabcut.analyze_videos(
                self.config,
                self.filelist,
                self.videotype.GetValue(),
                shuffle=shuffle,
                gputouse=None,
                cropping=crop,
                robust_nframes=robust,
                auto_track=True,
                n_tracks=self.ntracks.GetValue(),
                calibrate=self.calibrate.GetStringSelection() == "Yes",
                identity_only=self.identity_toggle.GetStringSelection() ==
                "Yes",
            )

            if self.create_video_with_all_detections.GetStringSelection(
            ) == "Yes":
                deeplabcut.create_video_with_all_detections(
                    self.config,
                    self.filelist,
                    self.videotype.GetValue(),
                    shuffle=shuffle,
                )
            if self.filter.GetStringSelection() == "Yes":
                deeplabcut.filterpredictions(
                    self.config,
                    self.filelist,
                    self.videotype.GetValue(),
                )

            if self.csv.GetStringSelection() == "Yes":
                deeplabcut.analyze_videos_converth5_to_csv(self.filelist,
                                                           listofvideos=True)

            if self.nwb.GetStringSelection() == "Yes":
                deeplabcut.analyze_videos_converth5_to_nwb(
                    self.config,
                    self.filelist,
                    listofvideos=True,
                )

            if self.trajectory.GetStringSelection() == "Yes":
                if self.showfigs.GetStringSelection() == "No":
                    showfig = False
                else:
                    showfig = True
                deeplabcut.plot_trajectories(self.config,
                                             self.filelist,
                                             self.videotype.GetValue(),
                                             showfigures=showfig)
        else:
            scorername = deeplabcut.analyze_videos(
                self.config,
                self.filelist,
                self.videotype.GetValue(),
                shuffle=shuffle,
                gputouse=None,
                save_as_csv=save_as_csv,
                cropping=crop,
                dynamic=dynamic,
            )
            if _filter:
                deeplabcut.filterpredictions(
                    self.config,
                    self.filelist,
                    self.videotype.GetValue(),
                    shuffle=shuffle,
                    filtertype="median",
                    windowlength=5,
                    save_as_csv=save_as_csv,
                )

            if self.trajectory.GetStringSelection() == "Yes":
                if self.showfigs.GetStringSelection() == "No":
                    showfig = False
                else:
                    showfig = True
                deeplabcut.plot_trajectories(
                    self.config,
                    self.filelist,
                    self.videotype.GetValue(),
                    displayedbodyparts=self.bodyparts,
                    shuffle=shuffle,
                    filtered=_filter,
                    showfigures=showfig,
                )
Beispiel #6
0
new_video_path = deeplabcut.ShortenVideo(
    video_path,
    start="00:00:00",
    stop="00:00:01",
    outsuffix="short",
    outpath=os.path.join(cfg["project_path"], "videos"),
)

print("Analyzing video...")
deeplabcut.analyze_videos(config_path, [new_video_path], "mov", robust_nframes=True)
print("Video analyzed.")

print("Create video with all detections...")
scorer, _ = auxiliaryfunctions.GetScorerName(cfg, 1, TRAIN_SIZE)
deeplabcut.create_video_with_all_detections(config_path, [new_video_path], scorer)
print("Video created.")

edgewisecondition = True
print("Convert detections...")
deeplabcut.convert_detections2tracklets(
    config_path,
    [new_video_path],
    "mov",
    track_method="box",
    edgewisecondition=edgewisecondition,
)
deeplabcut.convert_detections2tracklets(
    config_path, [new_video_path], "mov", track_method="skeleton"
)
import os
os.environ["DLClight"] = "True"
import sys
sys.path.append('/home/jma819/DeepLabCut/')
import deeplabcut

#path_config_file = '/projects/p30771/dlc_analysis/GRIN_rotarod_rearview-JJM-2020-04-02/config.yaml'
path_config_file = sys.argv[1]

behav_cam_directory = sys.argv[2]

avi_list = [
    os.path.join(dp, f)
    for dp, dn, fn in os.walk(os.path.expanduser(behav_cam_directory))
    for f in fn if f.endswith('.avi')
]

scorername = 'DLC_resnet50_rotarod_side_view_multi_animalAug25shuffle1_200000'

deeplabcut.create_video_with_all_detections(path_config_file,
                                            avi_list,
                                            DLCscorername=scorername)

print('finished creating labeled videos!')
# os.environ["DLClight"]="True"

import deeplabcut

config_path = '/home/felix/biteOscope/aedes-bbb-2020-11-16_05/config.yaml'

videos = glob.glob('/home/felix/test/WT*cropTrim.mp4')

# # #
print('analyzing ' + str(len(videos)) + ' videos:')
print(*videos, sep = "\n")
# # #
# # #
deeplabcut.analyze_videos(config_path, videos, shuffle=1, batchsize=8, dynamic=(True, 0.5, 100))
# # # # #
deeplabcut.create_video_with_all_detections(config_path, videos, 'DLC_resnet50_aedesNov16shuffle1_80000')
# # #
# deeplabcut.convert_detections2tracklets(config_path, videos, shuffle=1, videotype='mp4', trainingsetindex=0, track_method='box')
# # #
# # # #
# trackletPickles = glob.glob('/home/felix/Dropbox/lisaBaik/*_bx.pickle')
# #
# print('Converting ' + str(len(trackletPickles)) + ' tracklets:')
# print(trackletPickles)
# #
# for tracklet in trackletPickles:
#     deeplabcut.convert_raw_tracks_to_h5(config_path, tracklet)
#
# deeplabcut.create_labeled_video(config_path, videos, videotype='.mp4', track_method='box', filtered=False, color_by='individual')
Beispiel #9
0
print('ZzzZzz.. Sleeping for', sleeptime, 'seconds.. zzzZZzzZz')
sleep(sleeptime)

edits = {'snapshotindex': snapshotindex}

dlc.auxiliaryfunctions.edit_config(config_path, edits)

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

print('edit completed!')

dlc.create_video_with_all_detections(
    config=config_path,
    videos=videos_path_list[SLURM_ARRAY_TASK_ID:SLURM_ARRAY_TASK_ID + 1],
    shuffle=shuffleindex,
    trainingsetindex=0,
    displayedbodyparts='all',
    destfolder=None,
    modelprefix='',
)

print("dlc_create_video_with_all_detections.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'")