Download audio_processing.py from ashok2216/BirdSense: direct link, hf CLI and curl.
- Browser
- Download file 1.86 kB
-
https://huggingface.co/spaces/ashok2216/BirdSense/resolve/main/audio_processing.py
- Command line
-
hf download hf://spaces/ashok2216/BirdSense/audio_processing.py
-
curl -L -o audio_processing.py https://huggingface.co/spaces/ashok2216/BirdSense/resolve/main/audio_processing.py
1.86 kB
| '''Copyright 2024 Ashok Kumar | |
| Licensed under the Apache License, Version 2.0 (the "License"); | |
| you may not use this file except in compliance with the License. | |
| You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, software | |
| distributed under the License is distributed on an "AS IS" BASIS, | |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| See the License for the specific language governing permissions and | |
| limitations under the License.''' | |
| import os | |
| import matplotlib.pyplot as plt | |
| plt.style.use('dark_background') | |
| import seaborn as sns | |
| import numpy as np | |
| import librosa | |
| from IPython.display import Audio | |
| import pandas as pd | |
| # Function to extract features from audio file | |
| def extract_features(file_path): | |
| # Load audio file | |
| audio, sample_rate = librosa.load(file_path) | |
| # Extract features using Mel-Frequency Cepstral Coefficients (MFCC) | |
| mfccs = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40) | |
| # Flatten the features into a 1D array | |
| flattened_features = np.mean(mfccs.T, axis=0) | |
| return flattened_features | |
| # Function to load dataset and extract features | |
| def load_data_and_extract_features(data_dir): | |
| labels = [] | |
| features = [] | |
| # Loop through each audio file in the dataset directory | |
| for filename in os.listdir(data_dir): | |
| if filename.endswith('.wav'): | |
| file_path = os.path.join(data_dir, filename) | |
| # Extract label from filename | |
| label = filename.split('-')[0] | |
| labels.append(label) | |
| # Extract features from audio file | |
| feature = extract_features(file_path) | |
| features.append(feature) | |
| return np.array(features), np.array(labels) | |