Ejemplo n.º 1
0
def DLCLabel(config_path):
    """
    Utility to facilitate labelling DeepLabCut frames from the MesoNet GUI.

    :param config_path: (required) The path to a DeepLabCut configuration file (.yaml).
    """
    deeplabcut.extract_frames(config_path, crop=False)
    deeplabcut.label_frames(config_path)
    deeplabcut.check_labels(config_path)
Ejemplo n.º 2
0
def create_dlc_project(name, author, files, props):
    config_path = deeplabcut.create_new_project(name, author, files)
    update_dlc_config(config_path, props)
    deeplabcut.extract_frames(config_path,
                              'automatic',
                              'kmeans',
                              userfeedback=False)
    deeplabcut.label_frames(config_path)
    deeplabcut.check_labels(config_path)

    return config_path.replace('/config.yaml', '')
Ejemplo n.º 3
0
 def label_frames(self, event):
     deeplabcut.label_frames(self.config)
)

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)
Ejemplo n.º 5
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")
Ejemplo n.º 6
0
@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)
        configuration file, use the scroll bar to navigate across the video, and select ‘Grab a Frame’ to extract
        the frame. The user can also look at the extracted frames and, e.g., delete frames (from the directory)
        that are too similar before manually annotating them. The methods can be used in a mix-and-match
        way to create a diverse set of frames. The user can also choose to select frames for only specific videos.
        
        CRITICAL STEP It is advisable to keep the frame size small, as large frames increase the training
        and inference times.
        """

    # ------------------------------------------------------------------
    # Stage IV: labeling of the frames
    # Steps 5, 6
    # ------------------------------------------------------------------
    if "step_5" in stages_to_run or "labeling_frames" in stages_to_run:
        # The following command invokes the labeling toolbox
        deeplabcut.label_frames(config_path)

        """
        Next, use the ‘Load Frames’ button to select the directory that stores the extracted frames from one
        of the videos. A right click places the first body part, and, subsequently, you can either select one of
        the radio buttons (top right) to select a body part to label, or use the built-in mechanism that
        automatically advances to the next body part. If a body part is not visible, simply do not label the
        part and select the next body part you want to label. Each label will be plotted as a dot in a unique
        color (see Fig. 4 for more details).
        
        You can also move the label around by left-clicking and dragging. Once the position is
        satisfactory, you can select another radio button (in the top right) to switch to another label (it also
        auto-advances, but you can manually skip labels if needed). Once all the visible body parts are
        labeled, then you can click ‘Next’ to load the following frame, or ‘Previous’ to look at and/or adjust
        the labels on previous frames. You need to save the labels after all the frames from one of the videos
        are labeled by clicking the ‘Save’ button. You can save at intermediate points, and then relaunch the
Ejemplo n.º 8
0
learningVideo = ["add video name for learning"]

config_path = dlc.create_new_project('PJ name',
                                     'my name',
                                     learningVideo,
                                     working_directory='add your directory',
                                     copy_videos=False)

# if the project was already created, run this
config_path = 'path to yaml file'

# extract frame
dlc.extract_frames(config_path, 'automatic', 'kmeans')

# label the data
dlc.label_frames(config_path)

# check labels
#dlc.check_labels(config_path)

# creating training dataset
dlc.create_training_dataset(config_path, num_shuffles=1)

# start training
dlc.train_network(config_path)

# evaluate the trained network
#dlc.evaluate_network(config_path, plotting=True)

full_path_to_videos = []
root = 'add your path'
 def clicked_label():
     deeplabcut.label_frames(self.path_config_file)
Ejemplo n.º 10
0
def new_project():
    deeplabcut.label_frames(config_path)

    return "Hello World!"
def cli_create_project(project_path,
                       video_paths,
                       name='eye_video_demo',
                       experimenter='experimenter',
                       recursive=False,
                       format_='mp4',
                       exclude=[],
                       num_frames=20,
                       train=False,
                       analyze=False,
                       create_videos=False):
    """run all steps to create a DLC project"""

    if len(video_paths) == 0:
        # use provided example
        video_files = [op.join(op.split(op.realpath(__file__))[0], 'data', 'example_eye_camera_video.mp4')]
    else:
        # get video files
        video_files = []
        for vp in list(video_paths):
            video_files.extend(get_video_files(vp, recursive, exclude,
                                               file_format=format_.lower()))

    # list all video files (and convert to mp4 if required)
    for i, vf in enumerate(video_files):
        print("found video file: %s" % vf)

        if op.splitext(vf)[1] == '.h264':
            vide_files[i] = convert_h264_to_mp4(vf)

    # create a new project
    config_path = deeplabcut.create_new_project(name, experimenter, video_files,
                                                working_directory=project_path,
                                                copy_videos=False)

    config = deeplabcut.utils.read_config(config_path)
    config['bodyparts'] = ['pupil_center', 'nasal_corner', 'temporal_corner']
    config['numframes2pick'] = num_frames
    deeplabcut.utils.write_config(config_path, config)

    # extract and label frames
    deeplabcut.extract_frames(config_path,
                              mode='automatic',
                              algo='kmeans',
                              crop=True)
    deeplabcut.label_frames(config_path)
    deeplabcut.check_labels(config_path)

    # create training dataset
    deeplabcut.create_training_dataset(config_path)

    if train:
        # train and evaluate the network
        deeplabcut.train_network(config_path)
        deeplabcut.evaluate_network(config_path)

        if analyze:
            deeplabcut.analyze_videos(config_path, video_files)

            if create_videos:
                # create a video
                deeplabcut.create_labeled_video(config_path, video_files)
Ejemplo n.º 12
0
import deeplabcut
import tensorflow as tf

'''deeplabcut.create_new_project('1_2_4_chamber','MC',['C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3477.2b\\05_08_2019\\BRAC34772b top_left 05_08_2019 12_40_54 1_trimmed.mp4'],
working_directory='C:\\Users\\analysis\\Desktop')'''

videopath =['C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3477.2b\\13_08_2019\\BRAC34772b top_right 13_08_2019 14_39_52 2_trimmed.mp4',
'C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3583.3e\\23_07_2019\\BRAC35833e 23_07_2019 13_35_06 4_trimmed.mp4',
'C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3616.3c\\23_07_2019\\BRAC36163c 23_07_2019 12_41_11 3_trimmed.mp4']
config = 'C:\\Users\\analysis\\Desktop\\1_2_4_chamber-MC-2019-08-23\\config.yaml'


'''deeplabcut.add_new_videos(config,videopath)
deeplabcut.extract_frames(config,'automatic','kmeans')'''
deeplabcut.label_frames(config)

deeplabcut.check_labels(config)
deeplabcut.create_training_dataset(config)
deeplabcut.train_network(config)


deeplabcut.extract_outlier_frames(config,videopath)
deeplabcut.refine_labels(config)
deeplabcut.merge_datasets(config)
deeplabcut.create_training_dataset(config)
deeplabcut.train_network(config)
Ejemplo n.º 13
0
# In[3]:


path_config = '/home/amr/Trial_DeepLabCut/Mouse-Amr-2018-12-03/config.yaml'


# In[4]:


deeplabcut.extract_frames(path_config, 'automatic', 'kmeans')


# In[9]:


deeplabcut.label_frames(path_config, Screens=2)


# Now, a GUI will open for you to label the body part you want

# In[10]:


deeplabcut.check_labels(path_config)


# In[11]:


deeplabcut.create_training_dataset(path_config)
Ejemplo n.º 14
0
def label(path_config_file):
    dlc.label_frames(path_config_file)
    print("Checking labels")
    dlc.check_labels(path_config_file)
Ejemplo n.º 15
0
def get_video(exp_path):
    for file in os.listdir(exp_path):
        if "eye.mp4" in file:
            return os.path.join(exp_path, file)
    raise ValueError(f"No eye.mp4 found in {exp_path}")


def get_ls_of_all_videos(drive):
    ls = []
    add2ls = lambda path: ls.append(get_video(path))
    apply_to_all_one_plane_recordings(drive, add2ls)
    return ls


if __name__ == "__main__":
    project_name = "mousepupils"
    creator = "viviani"
    vids = get_ls_of_all_videos("H:\\")
    dlc.create_new_project(project_name, creator, vids, copy_vids=True)
    root = "C:/Users/viviani/Desktop/micepupils-viviani-2020-07-09/"
    config_path = root + "config.yaml"
    vid_dir = root + "videos/"
    vids = [vid_dir + file for file in os.listdir(vid_dir) if ".mp4" in file]
    with open(config_path, "w") as file:
        with open("config.yaml", "r") as source:
            for line in source:
                file.write(line)
    dlc.label_frames(config_path, vids)
    #Once frames are labelled, call dlc.create_training_dataset
    #and then dlc.train_network
Ejemplo n.º 16
0
 def label_frames(self):
     # There probably is a more elegant way to do this but it preseves the ability to run in an interactive session
     # try:  # if is iPython
     # %gui wx
     # finally:
     deeplabcut.label_frames(self.full_config_path())
Ejemplo n.º 17
0
import matplotlib

task='whisk_only' # Enter the name of your experiment Task
experimenter='Guy' # Enter the name of the experimenter
video=['videos/animal_3_video_2_150fps_correct.mp4', 'videos/crush_19_01_07_animal_3.mp4'] # Enter the paths of your videos you want to grab frames from.

deeplabcut.create_new_project(task,experimenter,video, working_directory='dlc-blinking/whisk',copy_videos=True) #change the working directory to where you want the folders created.

%matplotlib inline
path_config_file = '/dlc-blinking/whisk/whisk_only-Guy-2019-02-01/config.yaml' # Enter the path of the config file that was just created from the above step (check the folder)
deeplabcut.extract_frames(path_config_file,'automatic','uniform',crop=True, checkcropping=True, opencv=False) #there are other ways to grab frames, such as by clustering 'kmeans'; please see the paper. 


# changed the cropping dimensions in the config.yaml file
%gui wx
deeplabcut.label_frames(path_config_file)

# Lables have now been created

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)
class tutorial():
    def __init__(self, algo, crop, userfeedback, shuffle, saveiters, displayiters, angle, center, scale, filenames):
        self.algo = "automatic"
        self.crop = False
        self.userfeedback = False
        self.shuffle = 1
        self.saveiters = 200
        self.displayiters = 100
        self.angle = -4.5
        self.center = None
        self.scale = 1.0
        #Generate all rotating videos
        self.filenames = glob.glob('*.mp4') #Return the file name with .mp4 extention

    def rotate(self, image, angle, center=None, scale=1):
        #scale = 1: original size
        rows,cols,ch = image.shape
        if center == None:
            center = (cols / 2, rows / 2)
        M = cv2.getRotationMatrix2D(center, angle, scale)
        #Matrix: Rotate with center by angles
        dst = cv2.warpAffine(image,M,(cols,rows))
        #After rotation
    return dst


    def videorotate(self, filenames, output_name, display_video = False):
        # capture video
        cap = cv2.VideoCapture(filename)

        #read video frame by frame
        #extract original video frame features
        sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
                int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))

        fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))

        fps = int(cap.get(cv2.CAP_PROP_FPS))

        #Make a directory to store the rotated videos
        path = "./rotated"
        try:
            os.mkdir(path)
        except OSError:
            pass
        else:
            print ("Successfully created the directory %s " % path)

        #Automatically name the rotated videos
        file = "./rotated/" + output_name
        out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, sz)
        #Integrate all frames to video


        #Read videos and rotate by certain degrees
        while(cap.isOpened()):
            #flip for truning(fliping) frames of video
            ret,img = cap.read()
            try:
                img2 = rotate(img, -4.5)
                #Flipped Vertically
                out.write(img2)
                if display_video == True:
                    cv2.imshow('rotated video',img2)

                k=cv2.waitKey(30) & 0xff
                #once you inter Esc capturing will stop
                if k==27:
                    break
            except:
                print (filename, 'successfully rotated!!!' )
                break
        cap.release()
        out.release()
        cv2.destroyAllWindows()

    for i in filenames:
        output_name = os.path.splitext(i)[0] + " rotated.mp4"


    cwd = os.chdir("./rotated")
    #we are using rotated videos
    cwd = os.getcwd()
    mp4files = [f for f in listdir(cwd) if isfile(join(cwd, f)) and os.path.splitext(f)[1] == ".mp4"]
    #Get all mp4 files

    task='Reaching' # Enter the name of your experiment Task
    experimenter='Donghan' # Enter the name of the experimenter
    video=mp4files # Enter the paths of your videos you want to grab frames from.
    now = datetime.datetime.now()

    try:
        path_config_file=deeplabcut.create_new_project(task,experimenter,video, working_directory='/home/donghan/DeepLabCut/data/rotated',copy_videos=True)
    #change the working directory to where you want the folders created.
    except:
        overwrite = input("Do you want to overwrite the folder since it already exists? y/n:")
        if overwrite == 'y':
            os.rmdir(task + '-' + experimenter + '-' + str(now.year) + '-' + str(now.month) + '-' + str(now.day))
            path_config_file=deeplabcut.create_new_project(task,experimenter,video, working_directory='/home/donghan/DeepLabCut/data/rotated',copy_videos=True)
        else:
            continue

# The function returns the path, where your project is.
# You could also enter this manually (e.g. if the project is already created and you want to pick up, where you stopped...)
#path_config_file = '/home/Mackenzie/Reaching/config.yaml' # Enter the path of the config file that was just created from the above step (check the folder)

    %matplotlib inline
    deeplabcut.extract_frames(path_config_file,'automatic',crop=False, userfeedback=False) #there are other ways to grab frames, such as by clustering 'kmeans'; please see the paper.
    #You can change the cropping to false, then delete the checkcropping part!
    #userfeedback: ask if users would like to continue or stop

    %gui wx
    deeplabcut.label_frames(path_config_file)

    deeplabcut.check_labels(path_config_file) #this creates a subdirectory with the frames + your labels

    deeplabcut.create_training_dataset(path_config_file)

    deeplabcut.train_network(path_config_file, shuffle=1, saveiters=200, displayiters=10)
#Other parameters include trainingsetindex=0,gputouse=None,max_snapshots_to_keep=5,autotune=False,maxiters=None
#Detailed function explanation can be found here https://github.com/AlexEMG/DeepLabCut/blob/efa95129061b1ba1535f7361fe76e9267568a156/deeplabcut/pose_estimation_tensorflow/training.py

    deeplabcut.evaluate_network(path_config_file)

    videofile_path = ['1035 SI_A, Aug 15, 13 17 7 rotated.mp4'] #Enter the list of videos to analyze.
    deeplabcut.analyze_videos(path_config_file,videofile_path)

    deeplabcut.create_labeled_video(path_config_file,videofile_path)