Visitar URL original
Plot detector's bboxes if Top-Down architecture by maximpavliv · Pull Request #2791 · DeepLabCut/DeepLabCut · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Plot bounding boxes in create_labeled_video
  • Loading branch information
maximpavliv committed Dec 13, 2024
commit 8e93c93423f6d4d635b65040648365efff3683f8
8 changes: 8 additions & 0 deletions deeplabcut/modelzoo/video_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def video_inference_superanimal(
customized_pose_checkpoint: Optional[str] = None,
customized_detector_checkpoint: Optional[str] = None,
customized_model_config: Optional[str] = None,
plot_bboxes: bool = True,
):
"""
This function performs inference on videos using a pretrained SuperAnimal model.
Expand Down Expand Up @@ -150,6 +151,9 @@ def video_inference_superanimal(
customized_model_config (str):
Used for loading customized model config. Only supported in Pytorch

plot_bboxes (bool):
If using Top-Down approach, whether to plot the detector's bounding boxes. The default is True.

Raises:
NotImplementedError:
If the model is not found in the modelzoo.
Expand Down Expand Up @@ -356,6 +360,8 @@ def video_inference_superanimal(
cropping=cropping,
dest_folder=dest_folder,
output_suffix=output_suffix,
plot_bboxes=plot_bboxes,
bboxes_pcutoff=bbox_threshold,
)

# we prepare the pseudo dataset in the same folder of the target video
Expand Down Expand Up @@ -493,4 +499,6 @@ def video_inference_superanimal(
cropping=cropping,
dest_folder=dest_folder,
output_suffix=output_suffix,
plot_bboxes=plot_bboxes,
bboxes_pcutoff=bbox_threshold,
)
13 changes: 11 additions & 2 deletions deeplabcut/pose_estimation_pytorch/modelzoo/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def _video_inference_superanimal(
cropping: list[int] | None = None,
dest_folder: Optional[str] = None,
output_suffix: str = "",
plot_bboxes: bool = True,
bboxes_pcutoff: float = 0.9,
) -> dict:
"""
Perform inference on a video using a superanimal model from the model zoo specified by `superanimal_name`.
Expand Down Expand Up @@ -131,8 +133,6 @@ def _video_inference_superanimal(

output_json = output_h5.with_suffix(".json")
if len(output_suffix) > 0:
# str(output_h5).replace(".h5", "_before_adapt.json")
# str(output_h5).replace(".h5", "_after_adapt.json")
output_json = output_json.with_stem(output_h5.stem + output_suffix)

video = VideoIterator(video_path, cropping=cropping)
Expand All @@ -143,6 +143,12 @@ def _video_inference_superanimal(
detector_runner=detector_runner,
)

bbox_keys_in_predictions = {"bboxes", "bbox_scores"}
bboxes_list = [{key: value
for key, value in p.items()
if key in bbox_keys_in_predictions}
for i, p in enumerate(predictions)]

bbox = cropping
if cropping is None:
vid_w, vid_h = video.dimensions
Expand Down Expand Up @@ -176,6 +182,9 @@ def _video_inference_superanimal(
bbox=bbox,
cmap=colormap,
output_path=str(output_video),
plot_bboxes=plot_bboxes,
bboxes_list=bboxes_list,
bboxes_pcutoff=bboxes_pcutoff,
Comment thread
maximpavliv marked this conversation as resolved.
Resolved
)
print(f"Video with predictions was saved as {output_path}")

Expand Down
87 changes: 76 additions & 11 deletions deeplabcut/utils/auxiliaryfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,54 @@ def grab_files_in_folder(folder, ext="", relative=True):
yield file if relative else os.path.join(folder, file)


def filter_files_by_patterns(
folder: str | Path,
start_patterns: set[str] | None = None,
contain_patterns: set[str] | None = None,
end_patterns: set[str] | None = None
) -> List[Path]:
"""
Filters files in a folder based on start, contain, and end patterns.

Args:
folder (str | Path): The folder to search for files.

start_patterns (Set[str] | None): Patterns the filenames should start with.
If None or empty, this pattern is not taken into account.

contain_patterns (set[str]): Patterns the filenames should contain.
If None or empty, this pattern is not taken into account.

end_patterns (set[str]): Patterns the filenames should end with.
If None or empty, this pattern is not taken into account.

Returns:
List[Path]: List of files that match the criteria.
"""
folder = Path(folder) # Ensure the folder is a Path object
if not folder.is_dir():
raise ValueError(f"{folder} is not a valid directory.")

# Filter files based on the given patterns
matching_files = [
file
for file in folder.iterdir()
if file.is_file()
and (
not start_patterns
or any(file.name.startswith(start) for start in start_patterns)
) and (
not contain_patterns
or any(contain in file.name for contain in contain_patterns)
) and (
not end_patterns
or any(file.name.endswith(end) for end in end_patterns)
)
]

return matching_files


def get_video_list(filename, videopath, videtype):
"""Get list of videos in a path (if filetype == all), otherwise just a specific file."""
videos = list(grab_files_in_folder(videopath, videtype))
Expand Down Expand Up @@ -864,30 +912,47 @@ def check_if_not_evaluated(folder, DLCscorer, DLCscorerlegacy, snapshot):
return True, dataname, DLCscorer


def find_video_full_data(folder, videoname, scorer):
scorer_legacy = scorer.replace("DLC", "DeepCut")
full_files = filter_files_by_patterns(
folder=folder,
start_patterns={videoname+scorer, videoname+scorer_legacy},
contain_patterns={"full"},
end_patterns={"pickle"},
)
if not full_files:
raise FileNotFoundError(
f"No full data found in {folder} "
f"for video {videoname} and scorer {scorer}."
)
return full_files[0]


def find_video_metadata(folder, videoname, scorer):
"""For backward compatibility, let us search the substring 'meta'"""
scorer_legacy = scorer.replace("DLC", "DeepCut")
meta = [
file
for file in grab_files_in_folder(folder, "pickle")
if "meta" in file
and (
file.startswith(videoname + scorer)
or file.startswith(videoname + scorer_legacy)
)
]
if not len(meta):
meta_files = filter_files_by_patterns(
folder=folder,
start_patterns={videoname+scorer, videoname+scorer_legacy},
contain_patterns={"meta"},
end_patterns={"pickle"},
)
if not meta_files:
raise FileNotFoundError(
f"No metadata found in {folder} "
f"for video {videoname} and scorer {scorer}."
)
return os.path.join(folder, meta[0])
return meta_files[0]


def load_video_metadata(folder, videoname, scorer):
return read_pickle(find_video_metadata(folder, videoname, scorer))


def load_video_full_data(folder, videoname, scorer):
return read_pickle(find_video_full_data(folder, videoname, scorer))


def find_analyzed_data(folder, videoname, scorer, filtered=False, track_method=""):
"""Find potential data files from the hints given to the function."""
scorer_legacy = scorer.replace("DLC", "DeepCut")
Expand Down
Loading