This is a machine learning-based system for classifying music into different genres. The system uses audio features extracted from music files to train a Random Forest Classifier.
- Extracts various audio features including MFCCs, spectral centroids, chroma features, and spectral rolloff
- Uses Random Forest Classifier for genre prediction
- Supports both training and prediction modes
- Can save and load trained models
- Supports MP3 and WAV audio formats
- Python 3.8 or higher
- Required packages are listed in
requirements.txt
- Clone this repository
- Install the required packages:
pip install -r requirements.txt.
├── README.md
├── requirements.txt
├── music_classifier.py
└── data/
├── genre1/
│ ├── song1.mp3
│ ├── song2.wav
│ └── ...
├── genre2/
│ ├── song1.mp3
│ └── ...
└── ...
-
Prepare your dataset:
- Create a
datadirectory - Inside the
datadirectory, create subdirectories for each genre - Place audio files (MP3 or WAV) in their respective genre directories
- Create a
-
Train the model:
from music_classifier import MusicClassifier
classifier = MusicClassifier()
X, y = classifier.prepare_dataset("data")
classifier.train(X, y)
classifier.save_model()- Make predictions:
prediction = classifier.predict("path/to/audio/file.mp3")
print(f"Predicted genre: {prediction}")The system extracts the following features from audio files:
- Mel-frequency cepstral coefficients (MFCCs)
- Spectral centroid
- Chroma features
- Spectral rolloff
The system uses a Random Forest Classifier with 100 trees. The features are preprocessed using StandardScaler before training.