Download preprocessing/__init__.py from JsonDONSON/Machine-Sound-Anomaly-Detector: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/JsonDONSON/Machine-Sound-Anomaly-Detector/resolve/main/preprocessing/__init__.py
- Command line
-
hf download hf://spaces/JsonDONSON/Machine-Sound-Anomaly-Detector/preprocessing/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/JsonDONSON/Machine-Sound-Anomaly-Detector/resolve/main/preprocessing/__init__.py
2.56 kB
| """ | |
| =============================================================================== | |
| preprocessing/__init__.py β Audio Preprocessing Pipeline | |
| =============================================================================== | |
| This package contains all audio-level preprocessing steps that happen BEFORE | |
| feature extraction (mel spectrograms). The pipeline order is: | |
| Raw WAV β Resampling (JSON) β Normalization (JSON) | |
| β Noise Reduction (EL sir) β Silence Removal (EL sir) | |
| β Clean numpy array ready for feature extraction (sala7) | |
| OWNERS: EL sir (noise_reduction, silence_removal) | |
| JSON (normalization, resampling) | |
| =============================================================================== | |
| """ | |
| from preprocessing.resampling import resample_audio | |
| from preprocessing.normalization import normalize_volume | |
| from preprocessing.noise_reduction import reduce_noise | |
| from preprocessing.silence_removal import remove_silence | |
| def preprocess_audio(file_path): | |
| """ | |
| Full preprocessing pipeline: load β resample β normalize β denoise β trim. | |
| This is the main entry point that JSON's Dataset class and infer.py will call. | |
| It chains all four preprocessing steps in the correct order and returns | |
| a clean numpy array ready for mel spectrogram extraction. | |
| Parameters | |
| ---------- | |
| file_path : str | |
| Absolute path to the .wav file. | |
| Returns | |
| ------- | |
| audio : np.ndarray | |
| 1D numpy array of the preprocessed audio waveform. | |
| sr : int | |
| Sampling rate (always TARGET_SR after resampling). | |
| Pipeline Order & Rationale | |
| -------------------------- | |
| 1. Resample FIRST β all downstream steps assume a fixed sample rate. | |
| If we denoise before resampling, the noise profile frequency bins | |
| won't match after resampling. | |
| 2. Normalize SECOND β bring all recordings to the same loudness level | |
| before noise estimation, so the noise threshold is consistent. | |
| 3. Denoise THIRD β remove background hum/motor noise. | |
| 4. Trim LAST β now that noise is gone, energy-based silence detection | |
| can accurately find where the real machine sound starts/ends. | |
| """ | |
| # Step 1: Load and resample to TARGET_SR | |
| audio, sr = resample_audio(file_path) | |
| # Step 2: Volume normalization (peak or RMS) | |
| audio = normalize_volume(audio) | |
| # Step 3: Noise reduction | |
| audio = reduce_noise(audio, sr) | |
| # Step 4: Silence removal (trim leading/trailing silence) | |
| audio = remove_silence(audio, sr) | |
| return audio, sr | |