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
def filter_after_refinement(self, event): # why is video type needed? shuffle = self.shuffle.GetValue() trainingsetindex = self.trainingset.GetValue() method = os.path.splitext(self.datafile)[0] if method.endswith("sk"): tracker = "skeleton" elif method.endswith("bx"): tracker = "box" else: tracker = "ellipse" window_length = self.filterlength_track.GetValue() if window_length % 2 != 1: raise ValueError("Window length should be odd.") deeplabcut.filterpredictions( self.config, [self.video], videotype=self.videotype.GetValue(), shuffle=shuffle, trainingsetindex=trainingsetindex, filtertype=self.filter_track.GetValue(), track_method=tracker, windowlength=self.filterlength_track.GetValue(), save_as_csv=True, )
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')
def filter_after_refinement(self, event): # why is video type needed? shuffle = self.shuffle.GetValue() trainingsetindex = self.trainingset.GetValue() tracker = ("skeleton" if os.path.splitext( self.datafile)[0].endswith("sk") else "box") deeplabcut.filterpredictions( self.config, [self.video], videotype=self.videotype.GetValue(), shuffle=shuffle, trainingsetindex=trainingsetindex, filtertype=self.filter_track.GetValue(), track_method=tracker, windowlength=self.filterlength_track.GetValue(), save_as_csv=False, )
def filter_after_refinement(self, event): # why is video type needed? shuffle = self.shuffle.GetValue() #trainingsetindex = self.trainingset.GetValue() window_length = self.filterlength_track.GetValue() if window_length % 2 != 1: raise ValueError("Window length should be odd.") deeplabcut.filterpredictions( self.config, [self.video], videotype=self.videotype.GetValue(), shuffle=shuffle, #trainingsetindex=trainingsetindex, filtertype=self.filter_track.GetValue(), windowlength=self.filterlength_track.GetValue(), save_as_csv=True, )
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)
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)
# evaluate the trained network #dlc.evaluate_network(config_path, plotting=True) full_path_to_videos = [] root = 'add your path' for path in videos: full_path_to_videos.append(root + '/' + path) # video analysis and plotting results dlc.analyze_videos(config_path, full_path_to_videos, shuffle=1, save_as_csv=False, videotype='.mp4') dlc.filterpredictions(config_path, full_path_to_videos, shuffle=1, save_as_csv=True, videotype='.mp4') videoCreate = ["add your videos"] dlc.create_labeled_video(config_path, videoCreate, filtered=True) # refine videos refineVideos = ["add your videos"] dlc.extract_outlier_frames(config_path, refineVideos, outlieralgorithm='jump') dlc.refine_labels(config_path) dlc.merge_datasets(config_path) # then, back to "create_training_dataset()"
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...") deeplabcut.extract_outlier_frames( config_path, [new_video_path], "mp4", automatic=True, track_method=TESTTRACKER ) print("Outlier frames extracted.") vname = Path(new_video_path).stem
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 process_videos(subject, date, gputouse=None): base_video_path = os.path.join(cfg['preprocessed_data_dir'], subject, date, 'video') with open(os.path.join(base_video_path,'config.json')) as json_file: video_cfg=json.load(json_file) if not os.path.exists(os.path.join(base_video_path,'combined')): os.mkdir(os.path.join(base_video_path,'combined')) trial_info = pd.read_csv('%s/%s/%s/trial_info.csv' % (cfg['preprocessed_data_dir'], subject, date)) origin_checked={ 'front': False, 'side': False, 'top': False, } table_corners_checked={ 'front': False, 'side': False, 'top': False, } tocchini_checked={ 'front': False, 'side': False, 'top': False, } all_videos={} for t_idx in range(len(trial_info.index)): if len(trial_info.video[t_idx]): fname=trial_info.video[t_idx] for view in cfg['camera_views']: view_path=os.path.join(base_video_path, view) if not view in all_videos: all_videos[view]=[] all_videos[view].append(os.path.join(view_path,fname)) for t_idx in range(len(trial_info.index)): if len(trial_info.video[t_idx]): fname=trial_info.video[t_idx] (base, ext) = os.path.splitext(fname) block = trial_info.block[t_idx] task=trial_info.task[t_idx] condition=trial_info.condition[t_idx] trial_num = trial_info.trial[t_idx] video_fnames = {} for view in cfg['camera_views']: view_path=os.path.join(base_video_path, view) if task in dlc_projects and condition in dlc_projects[task] and view in dlc_projects[task][condition]: dlc_cfg=os.path.join('/home/bonaiuto/Projects/tool_learning/preprocessed_data/dlc_projects', dlc_projects[task][condition][view],'config.yaml') deeplabcut.analyze_videos(dlc_cfg, [os.path.join(view_path, fname)], shuffle=1, save_as_csv=True, gputouse=gputouse) deeplabcut.filterpredictions(dlc_cfg, os.path.join(view_path, fname), shuffle=1) deeplabcut.create_labeled_video(dlc_cfg, [os.path.join(view_path, fname)], shuffle=1, filtered=True, draw_skeleton=True) dlc_files = glob.glob(os.path.join(base_video_path, view, '%s*DeepCut*.mp4' % base)) if len(dlc_files): video_fnames[view] = dlc_files[0] if len(video_fnames.keys()) == len(cfg['camera_views']): out_path = os.path.join(base_video_path, 'combined') if task in dlc_3d_projects and condition in dlc_3d_projects[task]: dlc_cfg = os.path.join('/home/bonaiuto/Projects/tool_learning/preprocessed_data/dlc_projects', dlc_3d_projects[task][condition], 'config.yaml') fnames={} for view in cfg['camera_views']: view_path = os.path.join(base_video_path, view) fnames[view]=os.path.join(view_path, fname) if not origin_checked[view]: init_origin=None if view in video_cfg['origins'] and video_cfg['origins'][view] is not None: init_origin = np.array(video_cfg['origins'][view]) video_cfg['origins'][view]=select_coordinate.show(all_videos[view], 'Select origin', init_coords=[init_origin])[0] origin_checked[view]=True if not table_corners_checked[view]: init_table_corners=[] if view in video_cfg['table_corners'] and video_cfg['table_corners'][view] is not None: init_table_corners=video_cfg['table_corners'][view] selected_corners=select_coordinate.show(all_videos[view], 'Select table corners', init_coords=init_table_corners) video_cfg['table_corners'][view]=[] for corner in selected_corners: video_cfg['table_corners'][view].append(corner) table_corners_checked[view]=True if not tocchini_checked[view]: init_tocchini=[] if view in video_cfg['tocchini'] and video_cfg['tocchini'][view] is not None: init_tocchini = video_cfg['tocchini'][view] selected_tocchini = select_coordinate.show(all_videos[view], 'Select tocchini', init_coords=init_tocchini) video_cfg['tocchini'][view] = [] for tocchino in selected_tocchini: video_cfg['tocchini'][view].append(tocchino) tocchini_checked[view] = True (fname, table_corners_3d, tocchini_3d)=triangulate(dlc_cfg, copy.copy(video_cfg), copy.copy(video_cfg['origins']), copy.copy(video_cfg['table_corners']), copy.copy(video_cfg['tocchini']), fnames, filterpredictions=True, destfolder=base_video_path, save_as_csv=True) vid_fname=create_labeled_video_3d(dlc_cfg, fname, table_corners_3d, tocchini_3d, fps=100, xlim=[-20, 20], ylim=[-30, 10],zlim=[-2,13]) video_fnames['3d']=os.path.split(vid_fname)[1] combine_video(base_video_path, video_fnames, out_path, '%d-%d_%s-%s_labeled.mp4' % (block, trial_num, task, condition)) with open(os.path.join(base_video_path,'config.json'),'w') as outfile: json.dump(video_cfg, outfile)
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)
"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(main_video_folder_path) #Now create the filtered videos and CSVs for file in video_files: if file.endswith(".mp4") and single_vid in file: deeplabcut.filterpredictions(config_path, [main_video_folder_path + file], videotype='mp4', shuffle=1, track_method='box') deeplabcut.create_labeled_video(config_path, [main_video_folder_path + file], videotype='.mp4', shuffle=1, draw_skeleton=True, track_method='box', save_frames=False, filtered=True) video_files = os.listdir(main_video_folder_path) #Now finally get the percent of "good" points files = [] good_percents = []
[new_video_path], "mp4", save_frames=False, color_by="individual", track_method="box", ) deeplabcut.create_labeled_video( config_path, [new_video_path], "mp4", save_frames=False, color_by="bodypart", track_method="skeleton", ) print("Labeled video created.") print("Filtering predictions...") deeplabcut.filterpredictions(config_path, [new_video_path], "mp4", track_method="box") print("Predictions filtered.") print("Extracting outlier frames...") deeplabcut.extract_outlier_frames(config_path, [new_video_path], "mp4", automatic=True, track_method="box") print("Outlier frames extracted.") print("ALL DONE!!! - default multianimal cases are functional.")
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, )
/February/February1 /February/February2 etc. ''' ''' subfolders=getsubfolders(basepath) for subfolder in subfolders: #this would be January, February etc. in the upper example print("Starting analyze data in:", subfolder) subsubfolders=getsubfolders(subfolder) for subsubfolder in subsubfolders: #this would be Febuary1, etc. in the upper example...''' subfolders = getsubfolders(basepath) '''for subfolder in subfolders: print("Starting analyze data in:", subfolder) subsubfolders=getsubfolders(subfolder) for subsubfolder in subsubfolders: print("Starting analyze data in:", subsubfolder)''' for path in videopath: for vtype in ['.mp4']: deeplabcut.analyze_videos(config, [path], shuffle=shuffle, videotype=vtype, save_as_csv=True) deeplabcut.filterpredictions(config, [path], videotype=vtype, shuffle=shuffle) deeplabcut.plot_trajectories(config, [path], videotype=vtype) deeplabcut.create_labeled_video(config, [path], videotype=vtype, filtered=True)
DLC_config['save_iters']=5 DLC_config['display_iters']=1 DLC_config['multi_step']=[[0.001,5]] print("CHANGING training parameters to end quickly!") deeplabcut.auxiliaryfunctions.write_config(posefile,DLC_config) print("TRAIN") deeplabcut.train_network(path_config_file) print("Inference with new direct cropping") deeplabcut.analyze_videos(path_config_file,[newvideo],destfolder=dfolder,cropping=[0,50,0,50],save_as_csv=True) print("Extracting skeleton distances, filter and plot filtered output") deeplabcut.analyzeskeleton(path_config_file, [newvideo], save_as_csv=True, destfolder=dfolder) deeplabcut.filterpredictions(path_config_file,[newvideo]) #deeplabcut.create_labeled_video(path_config_file,[newvideo], destfolder=dfolder,filtered=True) deeplabcut.create_labeled_video(path_config_file,[newvideo], destfolder=dfolder,displaycropped=True,filtered=True) deeplabcut.plot_trajectories(path_config_file,[newvideo], destfolder=dfolder,filtered=True) print("CREATING TRAININGSET for shuffle 2") print("will be used for 3D testscript...") deeplabcut.create_training_dataset(path_config_file,Shuffles=[2]) 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(2),'train/pose_cfg.yaml') DLC_config=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['save_iters']=10
def create_pretrained_project( project, experimenter, videos, model="full_human", working_directory=None, copy_videos=False, videotype=None, analyzevideo=True, filtered=True, createlabeledvideo=True, trainFraction=None, ): """ Creates a new project directory, sub-directories and a basic configuration file. Change its parameters to your projects need. The project will also be initialized with a pre-trained model from the DeepLabCut model zoo! http://www.mousemotorlab.org/dlc-modelzoo Parameters ---------- project : string String containing the name of the project. experimenter : string String containing the name of the experimenter. model: string, options see http://www.mousemotorlab.org/dlc-modelzoo Current option and default: 'full_human' 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! 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 ON WINDOWS: TRUE is often necessary! 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 filtered: bool, default false Boolean variable indicating if filtered pose data output should be plotted rather than frame-by-frame predictions. Filtered version can be calculated with deeplabcut.filterpredictions trainFraction: By default value from *new* projects. (0.95) Fraction that will be used in dlc-model/trainingset folder name. Example -------- Linux/MacOs loading full_human model and analzying video /homosapiens1.avi >>> deeplabcut.create_pretrained_project('humanstrokestudy','Linus',['/data/videos/homosapiens1.avi'], copy_videos=False) Loading full_cat model and analzying video "felixfeliscatus3.avi" >>> deeplabcut.create_pretrained_project('humanstrokestudy','Linus',['/data/videos/felixfeliscatus3.avi'], model='full_cat') Windows: >>> deeplabcut.create_pretrained_project('humanstrokestudy','Bill',[r'C:\yourusername\rig-95\Videos\reachingvideo1.avi'],r'C:\yourusername\analysis\project' copy_videos=True) Users must format paths with either: r'C:\ OR 'C:\\ <- i.e. a double backslash \ \ ) """ if model in globals()["Modeloptions"]: cwd = os.getcwd() cfg = deeplabcut.create_new_project(project, experimenter, videos, working_directory, copy_videos, videotype) if trainFraction is not None: auxiliaryfunctions.edit_config( cfg, {"TrainingFraction": [tranFraction]}) config = auxiliaryfunctions.read_config(cfg) if model == "full_human": 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" else: # just make a case and put the stuff you want. # TBD: 'partaffinityfield_graph' >> use to set skeleton! pass 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 print("Dowloading weights...") auxfun_models.DownloadModel(model, train_dir) pose_cfg = deeplabcut.auxiliaryfunctions.read_plainconfig( path_train_config) print(path_train_config) # Updating config file: dict = { "default_net_type": pose_cfg["net_type"], "default_augmenter": pose_cfg["dataset_type"], "bodyparts": pose_cfg["all_joints_names"], "skeleton": [], # TODO: update with paf_graph "dotsize": 6, } auxiliaryfunctions.edit_config(cfg, dict) # 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, ) # downloading base encoder / not required unless on re-trains (but when a training set is created this happens anyway) # model_path, num_shuffles=auxfun_models.Check4weights(pose_cfg['net_type'], parent_path, num_shuffles= 1) # Updating training and test pose_cfg: snapshotname = [fn for fn in os.listdir(train_dir) if ".meta" in fn][0].split(".meta")[0] dict2change = { "init_weights": str(os.path.join(train_dir, snapshotname)), "project_path": str(config["project_path"]), } UpdateTrain_pose_yaml(pose_cfg, dict2change, path_train_config) keys2save = [ "dataset", "dataset_type", "num_joints", "all_joints", "all_joints_names", "net_type", "init_weights", "global_scale", "location_refinement", "locref_stdev", ] MakeTest_pose_yaml(pose_cfg, keys2save, path_test_config) video_dir = os.path.join(config["project_path"], "videos") if analyzevideo == True: print("Analyzing video...") deeplabcut.analyze_videos(cfg, [video_dir], videotype, save_as_csv=True) if createlabeledvideo == True: if filtered: deeplabcut.filterpredictions(cfg, [video_dir], videotype) print("Plotting results...") deeplabcut.create_labeled_video(cfg, [video_dir], videotype, draw_skeleton=True, filtered=filtered) deeplabcut.plot_trajectories(cfg, [video_dir], videotype, filtered=filtered) os.chdir(cwd) return cfg, path_train_config else: return "N/A", "N/A"
def filterpredictions(projectId): config_path = projectRepository.find_one({'_id': ObjectId(projectId) })['config_path'] deeplabcut.filterpredictions(config_path, []) return "Not Implemented", 501
fullVideo = 'Unlabeled_Video.avi' #Name for Unlabeled Combined Video cap1 = cv2.VideoWriter(fullVideo,cv2.VideoWriter_fourcc('M','J','P','G'), 20,(width,height)) for infile in sorted(filelist, key=lambda x: int("".join([i for i in x if i.isdigit()]))): print ('Combining:'+directory_video + '/' +str(infile)) cap = cv2.VideoCapture(directory_video + '/' +str(infile)) while(cap.isOpened()): ret,frame = cap.read() if(ret == True): cap1.write(frame) else: break cap.release() cap1.release() shutil.move(fullVideo, videofile_path) #Moves the unlabeled video to the deeplabdirectory print('Moved File to DeepLabCuts Directory') ##This will analyze the videos from the project's DeepLearning Model and use DeepLabCut's Median Filter. deeplabcut.analyze_videos(path_config_file,videofile_path1, videotype=VideoType) deeplabcut.filterpredictions(path_config_file,videofile_path1, videotype=VideoType) print('Analyzed and Filtered using DeepLabCuts Models') #Gets the Raw-Filtered Data After applying a Low-Pass Butterworth n1,r1,t1,n2,r2,t2 = getData('Unlabeled_Video'+'DLC_resnet101_SampledMar14shuffle1_200000filtered.csv',thr) print('Applied LowPass Filtering From DeepLabCut Data') videoandmatlabfunction(fullVideo,n1,r1,t1,n2,r2,t2) ##Should Spitout the Labeled Video and Matlab Files print('Labeled Video, RawPosition MatlabFile and Annonatation Matlabfile are ready for download!') cv2.destroyAllWindows()
""" """ The labels for each body part across the video (‘trajectories’) can also be filtered and plotted after analyze_videos is run (which has many additional options (seedeeplabcut.filterpredictions?)). We also provide a function to plot the data overtime and pixels in frames. The provided plotting function in this toolbox utilizes matplotlib 44 ; therefore, these plots can easily be customized. To call this function, type the following: The ouput files can also be easily imported into many programs for further behavioral analysis (see Stage XI and ‘Anticipated results’). """ if "filterpredictions" in stages_to_run: deeplabcut.filterpredictions(config_path, videos_to_filter_predictions, videotype='avi', shuffle=1, trainingsetindex=0, filtertype='median', windowlength=5, p_bound=.001, ARdegree=3, MAdegree=1, alpha=.01, save_as_csv=True, destfolder=None) if "step_12" in stages_to_run or "plot_trajectories" in stages_to_run: deeplabcut.plot_trajectories(config_path, videos_to_plot_trajectories, videotype='.avi', shuffle=1, trainingsetindex=0, filtered=False, showfigures=False, destfolder=None) if "step_12_bis" in stages_to_run or "analyzeskeleton" in stages_to_run: deeplabcut.analyzeskeleton(config_path, videos_to_plot_trajectories, videotype='avi', shuffle=1, trainingsetindex=0, save_as_csv=True, destfolder=None) """ In addition, the toolbox provides a function to create labeled videos based on the extracted poses by plotting the labels on top of the frame and creating a video. To use it to create multiple labeled
#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 = []