コード例 #1
0
ファイル: plotting.py プロジェクト: chongchen20/Deeplabcut
def plot_trajectories(config,
                      videos,
                      videotype='.avi',
                      shuffle=1,
                      trainingsetindex=0,
                      filtered=False,
                      displayedbodyparts='all',
                      showfigures=False,
                      destfolder=None):
    """
    Plots the trajectories of various bodyparts across the video.

    Parameters
    ----------
     config : string
    Full path of the config.yaml file as a string.

    videos : list
        A list of strings containing the full paths to videos for analysis or a path to the directory, where all the videos with same extension are stored.

    videotype: string, optional
        Checks for the extension of the video in case the input to the video is a directory.\n Only videos with this extension are analyzed. The default is ``.avi``

    shuffle: list, optional
    List of integers specifying the shuffle indices of the training dataset. The default is [1]

    trainingsetindex: int, optional
    Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml).

    filtered: bool, default false
    Boolean variable indicating if filtered output should be plotted rather than frame-by-frame predictions. Filtered version can be calculated with deeplabcut.filterpredictions

    displayedbodyparts: list of strings, optional
        This select the body parts that are plotted in the video.
        Either ``all``, then all body parts from config.yaml are used,
        or a list of strings that are a subset of the full list.
        E.g. ['hand','Joystick'] for the demo Reaching-Mackenzie-2018-08-30/config.yaml to select only these two body parts.

    showfigures: bool, default false
    If true then plots are also displayed.

    destfolder: string, optional
        Specifies the destination folder that was used for storing analysis data (default is the path of the video).

    Example
    --------
    for labeling the frames
    >>> deeplabcut.plot_trajectories('home/alex/analysis/project/reaching-task/config.yaml',['/home/alex/analysis/project/videos/reachingvideo1.avi'])
    --------

    """
    cfg = auxiliaryfunctions.read_config(config)
    trainFraction = cfg['TrainingFraction'][trainingsetindex]
    DLCscorer, DLCscorerlegacy = auxiliaryfunctions.GetScorerName(
        cfg, shuffle, trainFraction
    )  #automatically loads corresponding model (even training iteration based on snapshot index)
    bodyparts = auxiliaryfunctions.IntersectionofBodyPartsandOnesGivenbyUser(
        cfg, displayedbodyparts)
    Videos = auxiliaryfunctions.Getlistofvideos(videos, videotype)
    for video in Videos:
        print(video)
        if destfolder is None:
            videofolder = str(Path(video).parents[0])
        else:
            videofolder = destfolder

        vname = str(Path(video).stem)
        print("Starting % ", videofolder, video)
        notanalyzed, dataname, DLCscorer = auxiliaryfunctions.CheckifNotAnalyzed(
            videofolder, vname, DLCscorer, DLCscorerlegacy, flag='checking')

        if notanalyzed:
            print("The video was not analyzed with this scorer:", DLCscorer)
        else:
            #LoadData
            print("Loading ", video, "and data.")
            datafound, metadata, Dataframe, DLCscorer, suffix = auxiliaryfunctions.LoadAnalyzedData(
                str(videofolder), vname, DLCscorer, filtered
            )  #returns boolean variable if data was found and metadata + pandas array
            if datafound:
                basefolder = videofolder
                auxiliaryfunctions.attempttomakefolder(basefolder)
                auxiliaryfunctions.attempttomakefolder(
                    os.path.join(basefolder, 'plot-poses'))
                tmpfolder = os.path.join(basefolder, 'plot-poses', vname)
                auxiliaryfunctions.attempttomakefolder(tmpfolder)
                PlottingResults(tmpfolder, Dataframe, DLCscorer, cfg,
                                bodyparts, showfigures, suffix + '.png')

    print(
        'Plots created! Please check the directory "plot-poses" within the video directory'
    )
コード例 #2
0
def extract_outlier_frames(config,
                           videos,
                           videotype='avi',
                           shuffle=1,
                           trainingsetindex=0,
                           outlieralgorithm='jump',
                           comparisonbodyparts='all',
                           epsilon=20,
                           p_bound=.01,
                           ARdegree=3,
                           MAdegree=1,
                           alpha=.01,
                           extractionalgorithm='kmeans',
                           automatic=False,
                           cluster_resizewidth=30,
                           cluster_color=False,
                           opencv=True,
                           savelabeled=True,
                           destfolder=None):
    """
    Extracts the outlier frames in case, the predictions are not correct for a certain video from the cropped video running from
    start to stop as defined in config.yaml.

    Another crucial parameter in config.yaml is how many frames to extract 'numframes2extract'.

    Parameter
    ----------
    config : string
        Full path of the config.yaml file as a string.

    videos : list
        A list of strings containing the full paths to videos for analysis or a path to the directory, where all the videos with same extension are stored.

    videotype: string, optional
        Checks for the extension of the video in case the input to the video is a directory.\n Only videos with this extension are analyzed. The default is ``.avi``

    shuffle : int, optional
        The shufle index of training dataset. The extracted frames will be stored in the labeled-dataset for
        the corresponding shuffle of training dataset. Default is set to 1

    trainingsetindex: int, optional
        Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml).

    outlieralgorithm: 'fitting', 'jump', 'uncertain', or 'manual'
        String specifying the algorithm used to detect the outliers. Currently, deeplabcut supports three methods + a manual GUI option. 'Fitting'
        fits a Auto Regressive Integrated Moving Average model to the data and computes the distance to the estimated data. Larger distances than
        epsilon are then potentially identified as outliers. The methods 'jump' identifies larger jumps than 'epsilon' in any body part; and 'uncertain'
        looks for frames with confidence below p_bound. The default is set to ``jump``.

    comparisonbodyparts: list of strings, optional
        This select the body parts for which the comparisons with the outliers are carried out. Either ``all``, then all body parts
        from config.yaml are used orr a list of strings that are a subset of the full list.
        E.g. ['hand','Joystick'] for the demo Reaching-Mackenzie-2018-08-30/config.yaml to select only these two body parts.

    p_bound: float between 0 and 1, optional
        For outlieralgorithm 'uncertain' this parameter defines the likelihood below, below which a body part will be flagged as a putative outlier.

    epsilon; float,optional
        Meaning depends on outlieralgoritm. The default is set to 20 pixels.
        For outlieralgorithm 'fitting': Float bound according to which frames are picked when the (average) body part estimate deviates from model fit
        For outlieralgorithm 'jump': Float bound specifying the distance by which body points jump from one frame to next (Euclidean distance)

    ARdegree: int, optional
        For outlieralgorithm 'fitting': Autoregressive degree of ARIMA model degree. (Note we use SARIMAX without exogeneous and seasonal part)
        see https://www.statsmodels.org/dev/generated/statsmodels.tsa.statespace.sarimax.SARIMAX.html

    MAdegree: int
        For outlieralgorithm 'fitting': MovingAvarage degree of ARIMA model degree. (Note we use SARIMAX without exogeneous and seasonal part)
        See https://www.statsmodels.org/dev/generated/statsmodels.tsa.statespace.sarimax.SARIMAX.html

    alpha: float
        Significance level for detecting outliers based on confidence interval of fitted ARIMA model. Only the distance is used however.

    extractionalgorithm : string, optional
        String specifying the algorithm to use for selecting the frames from the identified putatative outlier frames. Currently, deeplabcut
        supports either ``kmeans`` or ``uniform`` based selection (same logic as for extract_frames).
        The default is set to``uniform``, if provided it must be either ``uniform`` or ``kmeans``.

    automatic : bool, optional
        Set it to True, if you want to extract outliers without being asked for user feedback.

    cluster_resizewidth: number, default: 30
        For k-means one can change the width to which the images are downsampled (aspect ratio is fixed).

    cluster_color: bool, default: False
        If false then each downsampled image is treated as a grayscale vector (discarding color information). If true, then the color channels are considered. This increases
        the computational complexity.

    opencv: bool, default: True
        Uses openCV for loading & extractiong (otherwise moviepy (legacy))

    savelabeled: bool, default: True
        If true also saves frame with predicted labels in each folder.

    destfolder: string, optional
        Specifies the destination folder that was used for storing analysis data (default is the path of the video).

    Examples

    Windows example for extracting the frames with default settings
    >>> deeplabcut.extract_outlier_frames('C:\\myproject\\reaching-task\\config.yaml',['C:\\yourusername\\rig-95\\Videos\\reachingvideo1.avi'])
    --------
    for extracting the frames with default settings
    >>> deeplabcut.extract_outlier_frames('/analysis/project/reaching-task/config.yaml',['/analysis/project/video/reachinvideo1.avi'])
    --------
    for extracting the frames with kmeans
    >>> deeplabcut.extract_outlier_frames('/analysis/project/reaching-task/config.yaml',['/analysis/project/video/reachinvideo1.avi'],extractionalgorithm='kmeans')
    --------
    for extracting the frames with kmeans and epsilon = 5 pixels.
    >>> deeplabcut.extract_outlier_frames('/analysis/project/reaching-task/config.yaml',['/analysis/project/video/reachinvideo1.avi'],epsilon = 5,extractionalgorithm='kmeans')
    --------
    """

    cfg = auxiliaryfunctions.read_config(config)
    DLCscorer, DLCscorerlegacy = auxiliaryfunctions.GetScorerName(
        cfg, shuffle, trainFraction=cfg['TrainingFraction'][trainingsetindex])
    Videos = auxiliaryfunctions.Getlistofvideos(videos, videotype)
    for video in Videos:
        if destfolder is None:
            videofolder = str(Path(video).parents[0])
        else:
            videofolder = destfolder

        notanalyzed, dataname, DLCscorer = auxiliaryfunctions.CheckifNotAnalyzed(
            videofolder,
            str(Path(video).stem),
            DLCscorer,
            DLCscorerlegacy,
            flag='checking')
        if notanalyzed:
            print(
                "It seems the video has not been analyzed yet, or the video is not found! You can only refine the labels after the a video is analyzed. Please run 'analyze_video' first. Or, please double check your video file path"
            )
        else:
            Dataframe = pd.read_hdf(dataname, 'df_with_missing')
            scorer = Dataframe.columns.get_level_values(0)[
                0]  #reading scorer from
            nframes = np.size(Dataframe.index)
            # extract min and max index based on start stop interval.
            startindex = max([int(np.floor(nframes * cfg['start'])), 0])
            stopindex = min([int(np.ceil(nframes * cfg['stop'])), nframes])
            Index = np.arange(stopindex - startindex) + startindex
            #figure out body part list:
            bodyparts = auxiliaryfunctions.IntersectionofBodyPartsandOnesGivenbyUser(
                cfg, comparisonbodyparts)

            Indices = []
            if outlieralgorithm == 'uncertain':  #necessary parameters: considered body parts and
                for bpindex, bp in enumerate(bodyparts):
                    if bp in cfg[
                            'bodyparts']:  #filter [who knows what users put in...]
                        p = Dataframe[scorer][bp]['likelihood'].values[Index]
                        Indices.extend(
                            np.where(p < p_bound)[0] + startindex
                        )  # all indices between start and stop that are below p_bound.

            elif outlieralgorithm == 'jump':
                for bpindex, bp in enumerate(bodyparts):
                    if bp in cfg[
                            'bodyparts']:  #filter [who knows what users put in...]
                        dx = np.diff(Dataframe[scorer][bp]['x'].values[Index])
                        dy = np.diff(Dataframe[scorer][bp]['y'].values[Index])
                        # all indices between start and stop with jump larger than epsilon (leading up to this point!)
                        Indices.extend(
                            np.where((dx**2 + dy**2) > epsilon**2)[0] +
                            startindex + 1)
            elif outlieralgorithm == 'fitting':
                #deviation_dataname = str(Path(videofolder)/Path(dataname))
                # Calculate deviatons for video
                [d, o] = ComputeDeviations(Dataframe, cfg, bodyparts, scorer,
                                           dataname, p_bound, alpha, ARdegree,
                                           MAdegree)

                #Some heuristics for extracting frames based on distance:
                Indices = np.where(
                    d > epsilon
                )[0]  # time points with at least average difference of epsilon

                if len(Index) < cfg['numframes2pick'] * 2 and len(d) > cfg[
                        'numframes2pick'] * 2:  # if too few points qualify, extract the most distant ones.
                    Indices = np.argsort(d)[::-1][:cfg['numframes2pick'] * 2]
            elif outlieralgorithm == 'manual':
                wd = Path(config).resolve().parents[0]
                os.chdir(str(wd))
                from deeplabcut.refine_training_dataset import outlier_frame_extraction_toolbox

                outlier_frame_extraction_toolbox.show(config, video, shuffle,
                                                      Dataframe, scorer,
                                                      savelabeled)
            # Run always except when the outlieralgorithm == manual.
            if not outlieralgorithm == 'manual':
                Indices = np.sort(list(set(Indices)))  #remove repetitions.
                print("Method ", outlieralgorithm, " found ", len(Indices),
                      " putative outlier frames.")
                print("Do you want to proceed with extracting ",
                      cfg['numframes2pick'], " of those?")
                if outlieralgorithm == 'uncertain':
                    print(
                        "If this list is very large, perhaps consider changing the paramters (start, stop, p_bound, comparisonbodyparts) or use a different method."
                    )
                elif outlieralgorithm == 'jump':
                    print(
                        "If this list is very large, perhaps consider changing the paramters (start, stop, epsilon, comparisonbodyparts) or use a different method."
                    )
                elif outlieralgorithm == 'fitting':
                    print(
                        "If this list is very large, perhaps consider changing the paramters (start, stop, epsilon, ARdegree, MAdegree, alpha, comparisonbodyparts) or use a different method."
                    )

                if automatic == False:
                    askuser = input("yes/no")
                else:
                    askuser = '******'

                if askuser == 'y' or askuser == 'yes' or askuser == 'Ja' or askuser == 'ha':  # multilanguage support :)
                    #Now extract from those Indices!
                    ExtractFramesbasedonPreselection(
                        Indices, extractionalgorithm, Dataframe, dataname,
                        scorer, video, cfg, config, opencv,
                        cluster_resizewidth, cluster_color, savelabeled)
                else:
                    print(
                        "Nothing extracted, please change the parameters and start again..."
                    )
コード例 #3
0
def AnalyzeVideo(video,DLCscorer,DLCscorerlegacy,trainFraction,cfg,dlc_cfg,sess,inputs, outputs,pdindex,save_as_csv, destfolder=None,TFGPUinference=True,dynamic=(False,.5,10)):
    ''' Helper function for analyzing a video. '''
    print("Starting to analyze % ", video)
    vname = Path(video).stem
    if destfolder is None:
        destfolder = str(Path(video).parents[0])

    notanalyzed,dataname, DLCscorer=auxiliaryfunctions.CheckifNotAnalyzed(destfolder,vname,DLCscorer,DLCscorerlegacy)
    if notanalyzed:
        print("Loading ", video)
        cap=cv2.VideoCapture(video)
        if not cap.isOpened():
            raise IOError('Video could not be opened. Please check that the the file integrity.')
        fps = cap.get(5) #https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get
        nframes = int(cap.get(7))
        duration=nframes*1./fps
        size=(int(cap.get(4)),int(cap.get(3)))

        ny,nx=size
        print("Duration of video [s]: ", round(duration,2), ", recorded with ", round(fps,2),"fps!")
        print("Overall # of frames: ", nframes," found with (before cropping) frame dimensions: ", nx,ny)

        dynamic_analysis_state,detectiontreshold,margin=dynamic
        start = time.time()
        print("Starting to extract posture")
        if dynamic_analysis_state:
            PredictedData,nframes=GetPoseDynamic(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes,detectiontreshold,margin)
            #GetPoseF_GTF(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes,int(dlc_cfg["batch_size"]))
        else:
            if int(dlc_cfg["batch_size"])>1:
                if TFGPUinference:
                    PredictedData,nframes=GetPoseF_GTF(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes,int(dlc_cfg["batch_size"]))
                else:
                    PredictedData,nframes=GetPoseF(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes,int(dlc_cfg["batch_size"]))
            else:
                if TFGPUinference:
                    PredictedData,nframes=GetPoseS_GTF(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes)
                else:
                    PredictedData,nframes=GetPoseS(cfg,dlc_cfg, sess, inputs, outputs,cap,nframes)

        stop = time.time()
        if cfg['cropping']==True:
            coords=[cfg['x1'],cfg['x2'],cfg['y1'],cfg['y2']]
        else:
            coords=[0, nx, 0, ny]

        dictionary = {
            "start": start,
            "stop": stop,
            "run_duration": stop - start,
            "Scorer": DLCscorer,
            "DLC-model-config file": dlc_cfg,
            "fps": fps,
            "batch_size": dlc_cfg["batch_size"],
            "frame_dimensions": (ny, nx),
            "nframes": nframes,
            "iteration (active-learning)": cfg["iteration"],
            "training set fraction": trainFraction,
            "cropping": cfg['cropping'],
            "cropping_parameters": coords
            #"gpu_info": device_lib.list_local_devices()
        }
        metadata = {'data': dictionary}

        print("Saving results in %s..." %(Path(video).parents[0]))
        auxiliaryfunctions.SaveData(PredictedData[:nframes,:], metadata, dataname, pdindex, range(nframes),save_as_csv)
        return DLCscorer
    else:
        return DLCscorer
コード例 #4
0
def analyze_time_lapse_frames(config,directory,frametype='.png',shuffle=1,
                trainingsetindex=0,gputouse=None,save_as_csv=False,rgb=True):
    """
    Analyzed all images (of type = frametype) in a folder and stores the output in one file.

    You can crop the frames (before analysis), by changing 'cropping'=True and setting 'x1','x2','y1','y2' in the config file.

    Output: The labels are stored as MultiIndex Pandas Array, which contains the name of the network, body part name, (x, y) label position \n
            in pixels, and the likelihood for each frame per body part. These arrays are stored in an efficient Hierarchical Data Format (HDF) \n
            in the same directory, where the video is stored. However, if the flag save_as_csv is set to True, the data can also be exported in \n
            comma-separated values format (.csv), which in turn can be imported in many programs, such as MATLAB, R, Prism, etc.

    Parameters
    ----------
    config : string
        Full path of the config.yaml file as a string.

    directory: string
        Full path to directory containing the frames that shall be analyzed

    frametype: string, optional
        Checks for the file extension of the frames. Only images with this extension are analyzed. The default is ``.png``

    shuffle: int, optional
        An integer specifying the shuffle index of the training dataset used for training the network. The default is 1.

    trainingsetindex: int, optional
        Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml).

    gputouse: int, optional. Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU put None.
    See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries

    save_as_csv: bool, optional
        Saves the predictions in a .csv file. The default is ``False``; if provided it must be either ``True`` or ``False``

    rbg: bool, optional.
        Whether to load image as rgb; Note e.g. some tiffs do not alow that option in imread, then just set this to false.

    Examples
    --------
    If you want to analyze all frames in /analysis/project/timelapseexperiment1
    >>> deeplabcut.analyze_videos('/analysis/project/reaching-task/config.yaml','/analysis/project/timelapseexperiment1')
    --------

    If you want to analyze all frames in /analysis/project/timelapseexperiment1
    >>> deeplabcut.analyze_videos('/analysis/project/reaching-task/config.yaml','/analysis/project/timelapseexperiment1', frametype='.bmp')
    --------

    Note: for test purposes one can extract all frames from a video with ffmeg, e.g. ffmpeg -i testvideo.avi thumb%04d.png
    """
    if 'TF_CUDNN_USE_AUTOTUNE' in os.environ:
        del os.environ['TF_CUDNN_USE_AUTOTUNE'] #was potentially set during training

    if gputouse is not None:  # gpu selection
        os.environ['CUDA_VISIBLE_DEVICES'] = str(gputouse)

    vers = (tf.__version__).split('.')
    if int(vers[0]) == 1 and int(vers[1]) > 12:
        TF = tf.compat.v1
    else:
        TF = tf

    TF.reset_default_graph()
    start_path=os.getcwd() #record cwd to return to this directory in the end

    cfg = auxiliaryfunctions.read_config(config)
    trainFraction = cfg['TrainingFraction'][trainingsetindex]
    modelfolder=os.path.join(cfg["project_path"],str(auxiliaryfunctions.GetModelFolder(trainFraction,shuffle,cfg)))
    path_test_config = Path(modelfolder) / 'test' / 'pose_cfg.yaml'
    try:
        dlc_cfg = load_config(str(path_test_config))
    except FileNotFoundError:
        raise FileNotFoundError("It seems the model for shuffle %s and trainFraction %s does not exist."%(shuffle,trainFraction))
    # Check which snapshots are available and sort them by # iterations
    try:
      Snapshots = np.array([fn.split('.')[0]for fn in os.listdir(os.path.join(modelfolder , 'train'))if "index" in fn])
    except FileNotFoundError:
      raise FileNotFoundError("Snapshots not found! It seems the dataset for shuffle %s has not been trained/does not exist.\n Please train it before using it to analyze videos.\n Use the function 'train_network' to train the network for shuffle %s."%(shuffle,shuffle))

    if cfg['snapshotindex'] == 'all':
        print("Snapshotindex is set to 'all' in the config.yaml file. Running video analysis with all snapshots is very costly! Use the function 'evaluate_network' to choose the best the snapshot. For now, changing snapshot index to -1!")
        snapshotindex = -1
    else:
        snapshotindex=cfg['snapshotindex']

    increasing_indices = np.argsort([int(m.split('-')[1]) for m in Snapshots])
    Snapshots = Snapshots[increasing_indices]

    print("Using %s" % Snapshots[snapshotindex], "for model", modelfolder)

    ##################################################
    # Load and setup CNN part detector
    ##################################################

    # Check if data already was generated:
    dlc_cfg['init_weights'] = os.path.join(modelfolder , 'train', Snapshots[snapshotindex])
    trainingsiterations = (dlc_cfg['init_weights'].split(os.sep)[-1]).split('-')[-1]

    #update batchsize (based on parameters in config.yaml)
    dlc_cfg['batch_size'] = cfg['batch_size']

    # Name for scorer:
    DLCscorer,DLCscorerlegacy = auxiliaryfunctions.GetScorerName(cfg,shuffle,trainFraction,trainingsiterations=trainingsiterations)
    sess, inputs, outputs = predict.setup_pose_prediction(dlc_cfg)

    # update number of outputs and adjust pandas indices
    dlc_cfg['num_outputs'] = cfg.get('num_outputs', 1)

    xyz_labs_orig = ['x', 'y', 'likelihood']
    suffix = [str(s+1) for s in range(dlc_cfg['num_outputs'])]
    suffix[0] = '' # first one has empty suffix for backwards compatibility
    xyz_labs = [x+s for s in suffix for x in xyz_labs_orig]

    pdindex = pd.MultiIndex.from_product([[DLCscorer],
                                          dlc_cfg['all_joints_names'],
                                          xyz_labs],
                                         names=['scorer', 'bodyparts', 'coords'])

    if gputouse is not None: #gpu selectinon
            os.environ['CUDA_VISIBLE_DEVICES'] = str(gputouse)

    ##################################################
    # Loading the images
    ##################################################
    #checks if input is a directory
    if os.path.isdir(directory)==True:
        """
        Analyzes all the frames in the directory.
        """
        print("Analyzing all frames in the directory: ", directory)
        os.chdir(directory)
        framelist=np.sort([fn for fn in os.listdir(os.curdir) if (frametype in fn)])
        vname=Path(directory).stem
        notanalyzed,dataname,DLCscorer=auxiliaryfunctions.CheckifNotAnalyzed(directory,vname,DLCscorer,DLCscorerlegacy,flag='framestack')
        if notanalyzed:
            nframes = len(framelist)
            if nframes>0:
                start = time.time()

                PredictedData,nframes,nx,ny=GetPosesofFrames(cfg,dlc_cfg, sess, inputs, outputs,directory,framelist,nframes,dlc_cfg['batch_size'],rgb)
                stop = time.time()

                if cfg['cropping']==True:
                    coords=[cfg['x1'],cfg['x2'],cfg['y1'],cfg['y2']]
                else:
                    coords=[0, nx, 0, ny]

                dictionary = {
                    "start": start,
                    "stop": stop,
                    "run_duration": stop - start,
                    "Scorer": DLCscorer,
                    "config file": dlc_cfg,
                    "batch_size": dlc_cfg["batch_size"],
                    "num_outputs": dlc_cfg["num_outputs"],
                    "frame_dimensions": (ny, nx),
                    "nframes": nframes,
                    "cropping": cfg['cropping'],
                    "cropping_parameters": coords
                }
                metadata = {'data': dictionary}

                print("Saving results in %s..." %(directory))

                auxiliaryfunctions.SaveData(PredictedData[:nframes,:], metadata, dataname, pdindex, framelist,save_as_csv)
                print("The folder was analyzed. Now your research can truly start!")
                print("If the tracking is not satisfactory for some frome, consider expanding the training set.")
            else:
                print("No frames were found. Consider changing the path or the frametype.")

    os.chdir(str(start_path))
コード例 #5
0
def _analyze_frame_store(
    cfg: dict,
    frame_store_path: Path,
    video_name: Optional[str],
    dlc_scorer: str,
    dlc_scorer_legacy: str,
    predictor_cls: Type[Predictor],
    multi_output_format: str,
    num_outputs: int,
    train_frac: str,
    save_as_csv: bool,
    predictor_settings: Optional[Dict[str, Any]],
) -> str:
    # Check if the data was analyzed yet...
    v_name_sanitized = (Path(video_name).resolve().stem if
                        (video_name is not None) else "unknownVideo")
    print(v_name_sanitized)
    not_analyzed, data_name, dlc_scorer = auxiliaryfunctions.CheckifNotAnalyzed(
        str(frame_store_path.parent), v_name_sanitized, dlc_scorer,
        dlc_scorer_legacy)

    if not_analyzed:
        # Read the frame store into memory:
        with frame_store_path.open("rb") as fb:
            print(f"Processing '{frame_store_path.name}'")
            start = time.time()

            # Read in the header, setup the settings.
            try:
                frame_reader = frame_store_fmt.DLCFSReader(fb)
            except ValueError:
                frame_reader = h5_frame_store_fmt.DLCH5FSReader(fb)

            (
                num_f,
                f_h,
                f_w,
                f_rate,
                stride,
                vid_h,
                vid_w,
                off_y,
                off_x,
                bp_lst,
            ) = frame_reader.get_header().to_list()

            pd_index = GetPandasHeader(bp_lst, num_outputs,
                                       multi_output_format, dlc_scorer)

            predictor_settings = GetPredictorSettings(cfg, predictor_cls,
                                                      predictor_settings)

            video_metadata = {
                "fps":
                f_rate,
                "duration":
                float(num_f) / f_rate,
                "size": (vid_h, vid_w),
                "h5-file-name":
                data_name,
                "orig-video-path":
                str(video_name) if (video_name is not None) else
                None,  # This may be None if we were unable to find the video...
                "cropping-offset":
                None if (off_x is None or off_y is None) else (off_y, off_x),
                "dotsize":
                cfg["dotsize"],
                "colormap":
                cfg["colormap"],
                "alphavalue":
                cfg["alphavalue"],
                "pcutoff":
                cfg["pcutoff"],
            }

            # Create the plugin instance...
            print(
                f"Plugin {predictor_cls.get_name()} Settings: {predictor_settings}"
            )
            predictor_inst = predictor_cls(bp_lst, num_outputs, num_f,
                                           predictor_settings, video_metadata)

            # The pose prediction final output array...
            pose_prediction_data = np.zeros(
                (num_f, 3 * len(bp_lst) * num_outputs))

            # Begin running through frames...
            p_bar = tqdm.tqdm(total=num_f)
            frames_done = 0

            while frame_reader.has_next():
                frame = frame_reader.read_frames()
                pose = predictor_inst.on_frames(frame)
                if pose is not None:
                    # If the predictor returned a pose, add it to the final data.
                    pose_prediction_data[frames_done:frames_done + pose.
                                         get_frame_count()] = pose.get_all()
                    frames_done += pose.get_frame_count()

                p_bar.update()

            p_bar.close()

            # Post-Processing Phase:
            # Phase 2: Post processing...

            # Get all of the final poses that are still held by the predictor
            post_pbar = tqdm.tqdm(total=num_f - frames_done)
            final_poses = predictor_inst.on_end(post_pbar)
            post_pbar.close()

            # Add any post-processed frames
            if final_poses is not None:
                pose_prediction_data[frames_done:frames_done +
                                     final_poses.get_frame_count(
                                     )] = final_poses.get_all()
                frames_done += final_poses.get_frame_count()

            # Check and make sure the predictor returned all frames, otherwise throw an error.
            if frames_done != num_f:
                raise ValueError(
                    f"The predictor algorithm did not return the same amount of frames as are in the frame store.\n"
                    f"Expected Amount: {num_f}, Actual Amount Returned: {frames_done}"
                )

            stop = time.time()
            frame_reader.close()

            if cfg["cropping"]:
                coords = [cfg["x1"], cfg["x2"], cfg["y1"], cfg["y2"]]
            else:
                coords = [0, vid_w, 0, vid_h]

            sub_meta = {
                "start": start,
                "stop": stop,
                "run_duration": stop - start,
                "Scorer": dlc_scorer,
                "DLC-model-config file":
                None,  # We don't have access to this, so don't even try....
                "fps": f_rate,
                "num_outputs": num_outputs,
                "batch_size": 1,
                "multi_output_format": multi_output_format,
                "frame_dimensions": (f_h * stride, f_w * stride),
                "nframes": num_f,
                "iteration (active-learning)": cfg["iteration"],
                "training set fraction": train_frac,
                "cropping": cfg["cropping"],
                "cropping_parameters": coords,
            }
            metadata = {"data": sub_meta}

            # We are Done!!! Save data and return...
            auxiliaryfunctions.SaveData(
                pose_prediction_data,
                metadata,
                data_name,
                pd_index,
                range(num_f),
                save_as_csv,
            )

    return dlc_scorer