Skip to content

Commit

Permalink
Add test function and update cli docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shrivaths16 committed Sep 11, 2023
1 parent 4762308 commit d8bdb43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
3 changes: 3 additions & 0 deletions docs/guides/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ optional arguments:
--distinctly_color DISTINCTLY_COLOR
Specify how to color instances. Options include: "instances",
"edges", and "nodes" (default: "instances")
--background BACKGROUND
Specify the type of background to be used to save the videos.
Options: original, black, white and grey. (default: "original")
```

## Debugging
Expand Down
25 changes: 19 additions & 6 deletions sleap/io/visuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def reader(
video: Video,
frames: List[int],
scale: float = 1.0,
background="Original",
background: str = "original",
):
"""Read frame images from video and send them into queue.
Expand All @@ -48,6 +48,7 @@ def reader(
None.
"""

background = background.lower()
cv2.setNumThreads(usable_cpu_count())

total_count = len(frames)
Expand All @@ -71,15 +72,17 @@ def reader(
loaded_chunk_idxs, video_frame_images = video.get_frames_safely(
frames_idx_chunk
)
if background != "Original":
if background != "original":
# fill the frame with the color
if background == "Black":
if background == "black":
fill = 0
elif background == "Grey":
elif background == "grey":
fill = 127
else:
elif background == "white":
# background == White
fill = 255
else:
raise ValueError(f"Invalid background color: {background}")
video_frame_images = video_frame_images * 0 + fill

if not loaded_chunk_idxs:
Expand Down Expand Up @@ -514,7 +517,7 @@ def save_labeled_video(
fps: int = 15,
scale: float = 1.0,
crop_size_xy: Optional[Tuple[int, int]] = None,
background: str = "Original",
background: str = "original",
show_edges: bool = True,
edge_is_wedge: bool = False,
marker_size: int = 4,
Expand Down Expand Up @@ -714,6 +717,15 @@ def main(args: list = None):
"and 'nodes' (default: 'nodes')"
),
)
parser.add_argument(
"--background",
type=str,
default="original",
help=(
"Specify the type of background to be used to save the videos."
"Options for background: original, black, white and grey"
),
)
args = parser.parse_args(args=args)
labels = Labels.load_file(
args.data_path, video_search=[os.path.dirname(args.data_path)]
Expand Down Expand Up @@ -749,6 +761,7 @@ def main(args: list = None):
marker_size=args.marker_size,
palette=args.palette,
distinctly_color=args.distinctly_color,
background=args.background,
)

print(f"Video saved as: {filename}")
Expand Down
10 changes: 10 additions & 0 deletions tests/io/test_visuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ def test_serial_pipeline(centered_pair_predictions, tmpdir):
)


@pytest.mark.parametrize("background", ["original", "black", "white", "grey"])
def test_sleap_render_with_different_backgrounds(background):
args = (
f"-o test_{background}.avi -f 2 --scale 1.2 --frames 1,2 --video-index 0 --background {background} "
"tests/data/json_format_v2/centered_pair_predictions.json".split()
)
sleap_render(args)
assert os.path.exists(f"test_{background}.avi")


def test_sleap_render(centered_pair_predictions):
args = (
"-o testvis.avi -f 2 --scale 1.2 --frames 1,2 --video-index 0 "
Expand Down

0 comments on commit d8bdb43

Please sign in to comment.