Ejemplo n.º 1
0
    def find_configs(self) -> List[ConfigFileInfo]:
        """Load configs from all saved paths."""
        configs = []

        # Collect all configs from specified directories, sorted from most recently modified to least
        for config_dir in filter(lambda d: os.path.exists(d), self.dir_paths):
            # Find all json files in dir and subdirs to specified depth
            json_files = sleap_utils.find_files_by_suffix(
                config_dir, ".json", depth=self.search_depth
            )

            # Sort files, starting with most recently modified
            json_files.sort(key=lambda f: f.stat().st_mtime, reverse=True)

            # Load the configs from files
            for json_path in [file.path for file in json_files]:
                cfg_info = self.try_loading_path(json_path)
                if cfg_info:
                    configs.append(cfg_info)

        # Push old configs to the end of the list, while preserving the time-based order otherwise
        configs = [c for c in configs if not c.filename.startswith("old.")] + [
            c for c in configs if c.filename.startswith("old.")
        ]

        return configs
Ejemplo n.º 2
0
    def read(cls, file: FileHandle, *args, **kwargs,) -> Labels:
        filename = file.filename

        # Load data from the YAML file
        project_data = yaml.load(file.text, Loader=yaml.SafeLoader)

        # Create skeleton which we'll use for each video
        skeleton = Skeleton()
        skeleton.add_nodes(project_data["bodyparts"])

        # Get subdirectories of videos and labeled data
        root_dir = os.path.dirname(filename)
        videos_dir = os.path.join(root_dir, "videos")
        labeled_data_dir = os.path.join(root_dir, "labeled-data")

        with os.scandir(labeled_data_dir) as file_iterator:
            data_subdirs = [file.path for file in file_iterator if file.is_dir()]

        labeled_frames = []

        # Each subdirectory of labeled data corresponds to a video.
        # We'll go through each and import the labeled frames.

        for data_subdir in data_subdirs:
            csv_files = find_files_by_suffix(
                data_subdir, prefix="CollectedData", suffix=".csv"
            )

            if csv_files:
                csv_path = csv_files[0]

                # Try to find a full video corresponding to this subdir.
                # If subdirectory is foo, we look for foo.mp4 in videos dir.

                shortname = os.path.split(data_subdir)[-1]
                video_path = os.path.join(videos_dir, f"{shortname}.mp4")

                if os.path.exists(video_path):
                    video = Video.from_filename(video_path)
                else:
                    # When no video is found, the individual frame images
                    # stored in the labeled data subdir will be used.
                    print(
                        f"Unable to find {video_path} so using individual frame images."
                    )
                    video = None

                # Import the labeled fraems
                labeled_frames.extend(
                    LabelsDeepLabCutCsvAdaptor.read_frames(
                        FileHandle(csv_path), full_video=video, skeleton=skeleton
                    )
                )

            else:
                print(f"No csv data file found in {data_subdir}")

        return Labels(labeled_frames=labeled_frames)
Ejemplo n.º 3
0
    def find_configs_in_dir(self, dir: Text, depth: int = 1):
        # Find all json files in dir and subdirs to specified depth
        json_files = sleap_utils.find_files_by_suffix(dir,
                                                      ".json",
                                                      depth=depth)

        # Sort files, starting with most recently modified
        json_files.sort(key=lambda f: f.stat().st_mtime, reverse=True)

        # Get just the paths for the files we found
        json_paths = [file.path for file in json_files]

        configs = []
        for json_path in json_paths:
            cfg_info = self.try_loading_path(json_path)
            if cfg_info:
                configs.append(cfg_info)

        return configs
Ejemplo n.º 4
0
    def find_configs(self) -> List[ConfigFileInfo]:
        """Load configs from all saved paths."""
        configs = []

        # Collect all configs from specified directories, sorted from most recently modified to least
        for config_dir in filter(lambda d: os.path.exists(d), self.dir_paths):
            # Find all json files in dir and subdirs to specified depth
            json_files = sleap_utils.find_files_by_suffix(
                config_dir, ".json", depth=self.search_depth)

            # Keep only the training_config.json if it was found in the same folder.
            json_files_grouped = []
            for _, g in groupby(sorted(json_files, key=lambda f: f.path),
                                lambda p: Path(p).parent):
                grp_files = list(g)
                grp_names = [Path(f.path).name for f in grp_files]
                if "training_config.json" in grp_names:
                    fn = grp_files[grp_names.index("training_config.json")]
                else:
                    fn = grp_files[0]
                json_files_grouped.append(fn)

            json_files = json_files_grouped

            # Sort files, starting with most recently modified
            json_files.sort(key=lambda f: f.stat().st_mtime, reverse=True)

            # Load the configs from files
            for json_path in [file.path for file in json_files]:
                cfg_info = self.try_loading_path(json_path)
                if cfg_info:
                    configs.append(cfg_info)

        # Push old configs to the end of the list, while preserving the time-based order otherwise
        configs = [c for c in configs if not c.filename.startswith("old.")
                   ] + [c for c in configs if c.filename.startswith("old.")]

        return configs