Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 29 additions & 0 deletions batbot/spectrogram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,20 @@ def calculate_harmonic_and_echo_flags(
return harmonic_flag, harmonic_peak, echo_flag, echo_peak


def create_masked_image(stft_db, costs, kernel=11):
weights = costs.copy()
weights[weights < weights.mean()] = 0
weights[weights > 0] = weights.max()
weights = weights.astype(np.float32)
weights = cv2.GaussianBlur(
weights, (kernel, kernel), sigmaX=4, sigmaY=4, borderType=cv2.BORDER_DEFAULT
)
weights /= weights.max()
masked = stft_db * weights
masked = normalize_stft(masked, None, np.uint8)
return masked


# @lp
def compute_wrapper(
wav_filepath,
Expand Down Expand Up @@ -1823,6 +1837,8 @@ def compute_wrapper(

output_paths = []
compressed_paths = []
mask_paths = []
masked_paths = []
if not fast_mode:
datas = [
(output_paths, 'jpg', stft_db),
Expand All @@ -1834,6 +1850,14 @@ def compute_wrapper(
(compressed_paths, 'compressed.jpg', segments['stft_db']),
]

# Create masked image
if 'costs' in segments and 'stft_db' in segments:
masked = create_masked_image(segments['stft_db'], segments['costs'])
datas += [
(mask_paths, 'mask.jpg', segments['costs']),
(masked_paths, 'masked.jpg', masked),
]

for accumulator, tag, data in datas:
if data.dtype != np.uint8:
data_ = data.astype(np.float32)
Expand Down Expand Up @@ -1863,6 +1887,8 @@ def compute_wrapper(
'spectrogram': {
'uncompressed.path': output_paths,
'compressed.path': compressed_paths,
'mask.path': mask_paths,
'masked.path': masked_paths,
},
'global_threshold.amp': int(round(255.0 * (global_threshold / max_value))),
'sr.hz': int(sr),
Expand All @@ -1886,6 +1912,9 @@ def compute_wrapper(
'width.px': segments['stft_db'].shape[1],
'height.px': segments['stft_db'].shape[0],
}
if 'costs' in segments and 'stft_db' in segments:
metadata['size']['mask'] = metadata['size']['compressed']
metadata['size']['masked'] = metadata['size']['compressed']

metadata_path = f'{out_file_stem}.metadata.json'
with open(metadata_path, 'w') as metafile:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ python_requires = >=3.7

[options.entry_points]
console_scripts =
batbot = batbot.batbot:cli
batbot = batbot.batbot_cli:cli

[tool:pytest]
minversion = 5.4
Expand Down