-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add options to set background color when exporting video #1328
Changes from all commits
e5a8333
045ff9d
4762308
d8bdb43
2a82d3c
bd64c2a
330018c
b668551
5a71262
f1eef11
c7ec0a1
cd1663f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import numpy as np | ||
import os | ||
import pytest | ||
import cv2 | ||
from sleap.io.dataset import Labels | ||
from sleap.io.visuals import ( | ||
save_labeled_video, | ||
|
@@ -63,6 +64,46 @@ 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 " | ||
f"--background {background} " | ||
"tests/data/json_format_v2/centered_pair_predictions.json".split() | ||
) | ||
sleap_render(args) | ||
assert ( | ||
os.path.exists(f"test_{background}.avi") | ||
and os.path.getsize(f"test_{background}.avi") > 0 | ||
) | ||
|
||
# Check if the background is set correctly if not original background | ||
if background != "original": | ||
saved_video_path = f"test_{background}.avi" | ||
cap = cv2.VideoCapture(saved_video_path) | ||
ret, frame = cap.read() | ||
|
||
# Calculate mean color of the channels | ||
b, g, r = cv2.split(frame) | ||
mean_b = np.mean(b) | ||
mean_g = np.mean(g) | ||
mean_r = np.mean(r) | ||
|
||
# Set threshold values. Color is white if greater than white threshold, black | ||
# if less than grey threshold and grey if in between both threshold values. | ||
white_threshold = 240 | ||
grey_threshold = 40 | ||
|
||
# Check if the average color is white, grey, or black | ||
if all(val > white_threshold for val in [mean_b, mean_g, mean_r]): | ||
background_color = "white" | ||
elif all(val < grey_threshold for val in [mean_b, mean_g, mean_r]): | ||
background_color = "black" | ||
else: | ||
background_color = "grey" | ||
assert background_color == background | ||
Comment on lines
+67
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new test function - # Calculate mean color of the channels
- b, g, r = cv2.split(frame)
- mean_b = np.mean(b)
- mean_g = np.mean(g)
- mean_r = np.mean(r)
+ # Calculate the most frequent color of the channels
+ b, g, r = cv2.split(frame)
+ mode_b = scipy.stats.mode(b, axis=None)[0][0]
+ mode_g = scipy.stats.mode(g, axis=None)[0][0]
+ mode_r = scipy.stats.mode(r, axis=None)[0][0] |
||
|
||
|
||
def test_sleap_render(centered_pair_predictions): | ||
args = ( | ||
"-o testvis.avi -f 2 --scale 1.2 --frames 1,2 --video-index 0 " | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code introduces a new feature to set the background color of the video. It checks if the provided
background
is not "original", and if so, it fills the frame with the specified color. However, there's no validation for the case whenbackground
is "original". If an invalid value is passed, the error will only be raised after some computation has already been done (i.e., after frames are loaded). To improve efficiency, consider validating thebackground
argument at the beginning of the function.