Skip to content
Open
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
3 changes: 3 additions & 0 deletions ml/data_processing/audio_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def augment_all_audio(
if "TM" in augmentations_to_perform:
spectrogram = self.time_mask(spectrogram, time_mask)

#TODO Uncomment this and run
#spectrogram = processor.augment_hue(spectrogram);

# Save image file
save_path = os.path.join(save_directory, filepath[:-4] + "_aug.png")
print(save_path)
Expand Down
20 changes: 20 additions & 0 deletions ml/data_processing/spectrogram_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
This module provides the `SpectrogramProcessor` class for converting audio files
into spectrograms, normalizing them, and extracting features.
"""
import random

import numpy as np
import librosa
Expand All @@ -13,6 +14,8 @@
from pydub import AudioSegment
import multiprocessing
from concurrent.futures import ProcessPoolExecutor
import cv2



class SpectrogramProcessor(ImageProcessor):
Expand Down Expand Up @@ -219,6 +222,23 @@ def apply_stft(self, audio_clip: np.ndarray) -> np.ndarray:

return log_spectro

def augment_hue(self, spectrogram: np.ndarray) -> np.ndarray:
# Randomly choose a hue shift from the range [-0.2, 0.2]
hue_shift = random.uniform(-0.2, 0.2)
augmented_spectrogram = self.change_hue(spectrogram, hue_shift)
return augmented_spectrogram

def change_hue(self, image: np.ndarray, hue_shift: float) -> np.ndarray:
# Convert to HSV color space
hsv_image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

# Apply hue shift
hsv_image[..., 0] = (hsv_image[..., 0].astype(np.float32) + hue_shift * 180) % 180

# Convert back to RGB
augmented_image = cv2.cvtColor(hsv_image.astype(np.uint8), cv2.COLOR_HSV2RGB)
return augmented_image

def plot_spectrogram(self, filename, spectrogram) -> None:
"""Plots the spectrogram using Matplotlib

Expand Down