def get_DLC_prediction(file_path = None, dlc_config_file = r"C:\Users\asree\Downloads\Tool_tracking_main\Tool_Tracking-Ambareesh-2019-07-31\config.yaml", threshold=0.0, display = False, marker_size = 10, save_path = None, save_format='png'): """ filepath: Complete file.path of the image to be tested threshold: The probability value for the prediction points. If a point is below threshold it won't be marked display: If True display the test and final images #TO DO: Remove boundary Preserve size as is """ import pathlib import matplotlib.pyplot as plt import pandas as pd import deeplabcut path_config_file = dlc_config_file directory_path = str(pathlib.Path(file_path).parent) file_suffix = pathlib.Path(file_path).suffix deeplabcut.analyze_time_lapse_frames(path_config_file, directory=directory_path, frametype=file_suffix, save_as_csv=True) #looks promising im = plt.imread(file_path)[:,:,:3] #.PNG can have four channels! for file_name in pathlib.Path(file_path).parent.iterdir(): if '.csv' in str(file_name): pred_csv = pd.read_csv(file_name, skiprows=[0,1], usecols=lambda x: x not in 'coords') X, Y = [], [] for x, y, prob in zip(*[iter(pred_csv.columns)]*3): if pred_csv.loc[0,prob] > threshold: X.append(pred_csv.loc[0,x]) Y.append(pred_csv.loc[0,y]) plt.figure(figsize=(im.shape[0]/96, im.shape[1]/96)) # CHANGE TO INCORPORATE CORRECT SIZE implot = plt.imshow(im.copy()) plt.scatter(x= X, y= Y, c= 'b', s=marker_size) if not save_path: plt.savefig('dlc_prediction.'+save_format, bbox_inches='tight', dpi = 'figure') else: plt.savefig(pathlib.Path(save_path).joinpath('dlc_prediction.'+save_format), bbox_inches='tight', dpi = 'figure') if not display: plt.close() print(f"DLC prediction done, file saved as dlc_prediction.{save_format}") break
Shuffles=[2], net_type=net_type, augmenter_type=augmenter_type3) 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 DLC_config['display_iters'] = 2 DLC_config['multi_step'] = [[0.001, 10]] print("CHANGING training parameters to end quickly!") deeplabcut.auxiliaryfunctions.write_plainconfig(posefile, DLC_config) print("TRAINING shuffle 2, with smaller allocated memory") deeplabcut.train_network(path_config_file, shuffle=2, allow_growth=True) print("ANALYZING some individual frames") deeplabcut.analyze_time_lapse_frames( path_config_file, os.path.join(cfg['project_path'], 'labeled-data/reachingvideo1/')) print("ALL DONE!!! - default cases are functional.") print("Re-import DLC with env. variable set to test DLC light mode.") os.environ['DLClight'] = 'True' subprocess.call(['python3', "-c", "import deeplabcut"])
def moving_window(x_1, y_1, x_2, y_2, stp, path_img_big, path_image_tree, config_path): textFile_filter_results = path_image_tree + 'results_image_filter.txt' ## filePath = String(date) + '_' + String(time) + '_datalog.txt' ## File filePath = SD.open (filePath , FILE_WRITE) ## filePath.println('date, hour, min, sec, T1, R1, T2, R2, R, G, B, L') ## textFile_filter_results = open(textFile_filter_results, 'w') textFile_filter_results.write( 'y_pixel,n_trees,n,n_caught_by_dlc_prob,n_caught_by_slope,n_caught_by_array_error,n_caught_by_tree_pixels\n' ) x_rows = range(x_1, x_2, stp) y_rows = range(y_1, y_2, stp) for ypixel in y_rows: if 'img' in locals(): img.close() print('closed img') Image.MAX_IMAGE_PIXELS = 265568257 img = PIL.Image.open(path_img_big) img.load() path_im_crop_yrow = path_image_tree + str(ypixel) + '/' if os.path.exists(path_im_crop_yrow): print(path_im_crop_yrow + ' exists') else: print(path_im_crop_yrow + ' made') os.mkdir(path_im_crop_yrow) textFile = path_im_crop_yrow + 'predicted_tree_coordinates.txt' textFile = open(textFile, 'w') textFile.write( 'raster,x_1,y_1,x_2,y_2,slope,intercept,tree_px,delta_tree,image_px\n' ) cropped_images = [] for xpixel in x_rows: left = xpixel upper = ypixel - 50 right = xpixel + 50 lower = ypixel path_im_crop = path_im_crop_yrow + str(left) + '_' + str( upper) + '_' + str(right) + '_' + str(lower) + '.png' cropped_images.append(path_im_crop) if os.path.exists(path_im_crop): print(path_im_crop + ' exists') else: im_crop = img.crop((left, upper, right, lower)) im_crop.save(path_im_crop) im_crop.close() img.close() deeplabcut.analyze_time_lapse_frames(config_path, path_im_crop_yrow, frametype='.png', shuffle=2, save_as_csv=True) path_dlc_csv = glob.glob(path_im_crop_yrow + '*.csv') path_dlc_h5 = glob.glob(path_im_crop_yrow + '*.h5') ## costs a lot of storage space path_dlc_pickle = glob.glob( path_im_crop_yrow + '*.pickle') ## costs a lot of storage space ## we want to get rid of both the .pickle and .h5 files since we will not be using them for h5 in path_dlc_h5: os.remove(h5) for pickle in path_dlc_pickle: os.remove(pickle) dat = pandas.read_csv( path_dlc_csv[0]) ## opens up the .csv file created by dlc os.remove(path_dlc_csv[0]) dat = dat.iloc[ 2:] # gets rid of the first two rows of dat, which are strings and will cause erros n_caught_by_dlc_prob = 0 n_caught_by_slope = 0 n_caught_by_array_error = 0 n_caught_by_tree_pixels = 0 n_trees = 0 n = 0 for row in dat.iterrows(): n = n + 1 crap, data = row image_dat = data[0] x_b = float(data[1]) y_b = float(data[2]) p1l = float(data[3]) x_t = float(data[4]) y_t = float(data[5]) p2l = float(data[6]) if (x_t - x_b) != 0: slope = (y_t - y_b) / (x_t - x_b) else: slope = 0 intercept = y_b - slope * x_b path_img = path_im_crop_yrow + image_dat if p1l >= 0.9 and p2l >= 0.9: if slope > -2 and slope < -0.5: img = Image.open(path_img).convert('L') m = np.array(img) x_min = round(x_b, 0) x_max = round(x_t, 0) m_mean = np.mean(m) pixel_values = [] if x_min < 50 and x_max < 50: for i in range(int(x_min), int(x_max)): pixel_i = slope * i + intercept pixel_i = round(pixel_i, 0) if pixel_i < 50: pixel_values.append(m[int(pixel_i), i]) if len(pixel_values) > 0: tree_px = float( sum(pixel_values) / len(pixel_values)) delta_tree = m_mean - tree_px if tree_px is not None and tree_px > 45 and tree_px < 114 and delta_tree > 42 and delta_tree < 137: n_trees = n_trees + 1 img.close() os.remove(path_img) ''' img = Image.open(path_img) draw = ImageDraw.Draw(img) shape_line = [(x_b,y_b),(x_t,y_t)] draw.line(shape_line, fill = 'blue', width = 0) draw.point((x_b, y_b), fill = 'red') draw.point((x_t, y_t), fill = 'red') img.save(path_img) img.close() ''' textFile.write( os.path.basename(path_img).split('.')[0] + ',') textFile.write( str(x_b) + ',' + str(y_b) + ',' + str(x_t) + ',' + str(y_t) + ',' + str(slope) + ',') textFile.write( str(intercept) + ',' + str(tree_px) + ',') textFile.write( str(delta_tree) + ',' + str(m_mean) + '\n') else: n_caught_by_tree_pixels = n_caught_by_tree_pixels + 1 img.close() os.remove(path_img) ''' path_img = path_im_crop_yrow + image_dat img = Image.open(path_img) draw = ImageDraw.Draw(img) shape_line = [(x_b,y_b),(x_t,y_t)] draw.line(shape_line, fill = 'green', width = 0) draw.point((x_b, y_b), fill = 'cyan') draw.point((x_t, y_t), fill = 'cyan') img.save(path_img) img.close() ''' else: n_caught_by_array_error = n_caught_by_array_error + 1 img.close() os.remove(path_img) else: n_caught_by_slope = n_caught_by_slope + 1 img.close() os.remove(path_img) ''' path_img = path_im_crop_yrow + image_dat img = Image.open(path_img) draw = ImageDraw.Draw(img) shape_line = [(x_b,y_b),(x_t,y_t)] draw.line(shape_line, fill = 'orange', width = 0) draw.point((x_b, y_b), fill = 'yellow') draw.point((x_t, y_t), fill = 'yellow') img.save(path_img) img.close() ''' else: n_caught_by_dlc_prob = n_caught_by_dlc_prob + 1 os.remove(path_img) ''' path_img = path_im_crop_yrow + image_dat img = Image.open(path_img) draw = ImageDraw.Draw(img) shape_line = [(x_b,y_b),(x_t,y_t)] draw.line(shape_line, fill = 'red', width = 0) draw.point((x_b, y_b), fill = 'cyan') draw.point((x_t, y_t), fill = 'cyan') img.save(path_img) img.close() ''' textFile.close() textFile_filter_results.write(str(ypixel) + ',') textFile_filter_results.write(str(n_trees) + ',') textFile_filter_results.write(str(n) + ',') textFile_filter_results.write(str(n_caught_by_dlc_prob) + ',') textFile_filter_results.write(str(n_caught_by_slope) + ',') textFile_filter_results.write(str(n_caught_by_array_error) + ',') textFile_filter_results.write(str(n_caught_by_tree_pixels) + '\n') textFile_filter_results.close()
) path_config = '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/Arimina_model_1/' if os.path.exists(path_config) != True: os.mkdir(path_config) path_config = deeplabcut.create_new_project(project, experimenter, videos, copy_videos=True, working_directory=path_config) path_config = '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/Arimina_model_1/Armina_model_1-Theresa-2020-07-01/config.yaml' ''' deeplabcut.extract_frames(path_config, mode = 'automatic', algo = 'uniform', userfeedback = False, crop = False) ''' deeplabcut.label_frames(path_config) deeplabcut.create_training_dataset(path_config) deeplabcut.train_network(path_config, maxiters=200000, displayiters=10) deeplabcut.evaluate_network(path_config) deeplabcut.analyze_time_lapse_frames( path_config, '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/left_test_images/', save_as_csv=True)
# saveDirMovie = '/home/felix/biteData/FFDEET/test/' 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)
@author: viviani """ import deeplabcut as dlc from accdatatools.DeepLabCutPipeline.cmdline_utils import config_path as machine_learning_config_path #Deeplabcut doesn't have inbuilt tools for making seperate testing #datasets...so we're going to have to hack something together. #The authors have provided some instruction for how to make this happen #see here: https://forum.image.sc/t/how-to-move-partial-labeled-frame-to-a-new-project-how-to-quickly-evaluate-a-trained-dataset-on-analyzing-new-frames-without-retraining/29793 #and here: https://forum.image.sc/t/is-there-a-way-to-evaluate-a-deeplabcut-network-on-data-not-present-in-the-test-training-set/32222 video_list = ["C:/Users/viviani/Desktop/2017-02-08_01_CFEB040_eye.mp4"] testing_config = "C:\\Users\\viviani\\Desktop\\testing_dataset_factory-viviani-2020-07-27\\config.yaml" if __name__=="__main__": # dlc.create_new_project("testing_dataset_factory", # "viviani", # videos = video_list, # working_directory = "C:/Users/viviani/Desktop", # copy_videos=True) # dlc.extract_frames(testing_config, mode='automatic', algo='kmeans', crop=False) dlc.label_frames(testing_config) dlc.analyze_time_lapse_frames(machine_learning_config_path, "C:/Users/viviani/Desktop/testing_dataset_factory-viviani-2020-07-27/labeled-data/2017-02-08_01_CFEB040_eye", save_as_csv=True)
def moving_window(x_1, y_1, x_2, y_2, stp, path_img_big, big_img_size, path_image_tree, config_path): textFile_filter_results = path_image_tree + 'results_image_filter.txt' ## filePath = String(date) + '_' + String(time) + '_datalog.txt' ## File filePath = SD.open (filePath , FILE_WRITE) ## filePath.println('date, hour, min, sec, T1, R1, T2, R2, R, G, B, L') ## textFile_filter_results = open(textFile_filter_results, 'w') textFile_filter_results.write( 'y_pixel,n_trees,n,n_caught_by_dlc_prob,n_caught_by_slope\n') x_rows = range(x_1, x_2, stp) y_rows = range(y_1, y_2, stp) for xpixel in x_rows: if 'img' in locals(): img.close() print('closed img') Image.MAX_IMAGE_PIXELS = big_img_size img = PIL.Image.open(path_img_big) img.load() path_im_crop_xrow = path_image_tree + str(xpixel) + '/' if os.path.exists(path_im_crop_xrow): print(path_im_crop_xrow + ' exists') else: print(path_im_crop_xrow + ' made') os.mkdir(path_im_crop_xrow) textFile = path_im_crop_xrow + 'predicted_tree_coordinates.txt' textFile = open(textFile, 'w') textFile.write( 'raster,x_1,y_1,x_2,y_2,slope,intercept,tree_px,delta_tree,image_px\n' ) cropped_images = [] for ypixel in y_rows: left = xpixel upper = ypixel - 50 right = xpixel + 50 lower = ypixel path_im_crop = path_im_crop_xrow + str(left) + '_' + str( upper) + '_' + str(right) + '_' + str(lower) + '.png' cropped_images.append(path_im_crop) if os.path.exists(path_im_crop): print(path_im_crop + ' exists') else: im_crop = img.crop((left, upper, right, lower)) im_crop.save(path_im_crop) im_crop.close() img.close() deeplabcut.analyze_time_lapse_frames(config_path, path_im_crop_xrow, frametype='.png', shuffle=2, save_as_csv=True) path_dlc_csv = glob.glob(path_im_crop_xrow + '*.csv') path_dlc_h5 = glob.glob(path_im_crop_xrow + '*.h5') ## costs a lot of storage space path_dlc_pickle = glob.glob( path_im_crop_xrow + '*.pickle') ## costs a lot of storage space ## we want to get rid of both the .pickle and .h5 files since we will not be using them for h5 in path_dlc_h5: os.remove(h5) for pickle in path_dlc_pickle: os.remove(pickle) dat = pandas.read_csv( path_dlc_csv[0]) ## opens up the .csv file created by dlc dat = dat.iloc[ 2:] # gets rid of the first two rows of dat, which are strings and will cause erros n_caught_by_dlc_prob = 0 n_caught_by_slope = 0 n_trees = 0 n = 0 for row in dat.iterrows(): n = n + 1 crap, data = row image_dat = data[0] x_b = float(data[1]) y_b = float(data[2]) p1l = float(data[3]) x_t = float(data[4]) y_t = float(data[5]) p2l = float(data[6]) if (x_t - x_b) != 0: slope = (y_t - y_b) / (x_t - x_b) else: slope = 0 intercept = y_b - slope * x_b path_img = path_im_crop_xrow + image_dat if p1l >= 0.9 and p2l >= 0.9: if slope > -2 and slope < -0.5: n_trees = n_trees + 1 textFile.write( os.path.basename(path_img).split('.')[0] + ',') textFile.write( str(x_b) + ',' + str(y_b) + ',' + str(x_t) + ',' + str(y_t) + ',' + str(slope) + ',') textFile.write(str(intercept) + '\n') else: n_caught_by_slope = n_caught_by_slope + 1 #os.remove(path_img) else: n_caught_by_dlc_prob = n_caught_by_dlc_prob + 1 os.remove(path_img) img.close() textFile.close() textFile_filter_results.write(str(ypixel) + ',') textFile_filter_results.write(str(n_trees) + ',') textFile_filter_results.write(str(n) + ',') textFile_filter_results.write(str(n_caught_by_dlc_prob) + ',') textFile_filter_results.write(str(n_caught_by_slope) + '\n') textFile_filter_results.close()
) DLC_config = deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config["save_iters"] = 10 DLC_config["display_iters"] = 2 DLC_config["multi_step"] = [[0.001, 10]] print("CHANGING training parameters to end quickly!") deeplabcut.auxiliaryfunctions.write_plainconfig(posefile, DLC_config) print("TRAINING shuffle 2, with smaller allocated memory") deeplabcut.train_network(path_config_file, shuffle=2, allow_growth=True) print("ANALYZING some individual frames") deeplabcut.analyze_time_lapse_frames( path_config_file, os.path.join(cfg["project_path"], "labeled-data/reachingvideo1/"), ) print("Export model...") deeplabcut.export_model(path_config_file, shuffle=2, make_tar=False) print("Merging datasets...") trainIndices, testIndices = deeplabcut.mergeandsplit( path_config_file, trainindex=0, uniform=True ) print("Creating two identical splits...") deeplabcut.create_training_dataset( path_config_file, Shuffles=[4, 5], trainIndices=[trainIndices, trainIndices],