File size: 4,018 Bytes
e3dc165 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import numpy as np
import librosa
import math
from tensorflow import keras
import streamlit as st
import time
import pyautogui
def get_mfcc(audio_signal, num_mfcc=13, n_fft=2048, hop_length=512, num_segments=5):
new_data = {
"mfcc": []
}
SAMPLE_RATE = 22050
signal,sample_rate = librosa.load(audio_signal,sr = SAMPLE_RATE)
TRACK_DURATION = int(librosa.get_duration(signal)) # measured in seconds
SAMPLES_PER_TRACK = SAMPLE_RATE * TRACK_DURATION
samples_per_segment = int(SAMPLES_PER_TRACK / num_segments)
num_mfcc_vectors_per_segment = math.ceil(samples_per_segment / hop_length)
for d in range(num_segments):
start = samples_per_segment * d
finish = start + samples_per_segment
mfcc = librosa.feature.mfcc(signal[start:finish], sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)
mfcc = mfcc.T
if len(mfcc) == num_mfcc_vectors_per_segment:
new_data["mfcc"].append(mfcc.tolist())
return new_data["mfcc"]
def prediction(mfcc):
cnn_model = keras.models.load_model('music-gen-classify-v1.h5')
mfcc = np.array(mfcc)
mfcc = mfcc[...,np.newaxis]
prediction = cnn_model.predict(mfcc)
return max(np.argmax(prediction,axis = 1))
def get_genre(prediction):
pred = ''
if prediction == 0:
pred = 'Blues'
elif prediction == 1:
pred = 'Classical'
elif prediction == 2:
pred = 'Country'
elif prediction == 3:
pred = 'Disco'
elif prediction == 4:
pred = 'Hip Hop'
elif prediction == 5:
pred = 'Jazz'
elif prediction == 6:
pred = 'Metal'
elif prediction == 7:
pred = 'Pop'
elif prediction == 8:
pred = 'Reggae'
elif prediction == 9:
pred = 'Rock'
return pred
def main():
st.set_page_config(layout='wide',page_title='Klasifikasi Genre Musik',page_icon='🎵')
st.title('Prediksi Genre Musik')
st.markdown('Website ini bisa memprediksi hingga 10 genre yaitu Blues, Classical, Country, Disco, Hiphop,Jazz, Metal, Pop, Reggae, dan Rock. Tentu prediksi genre ini tidak selalu tepat karena lagu itu multi genre, tapi selamat mencoba!')
selected_item = st.selectbox('Pilih Sample Musik Atau Upload Musikmu !',['Sample Musik','Upload'])
if selected_item is not None:
if selected_item == 'Upload':
files = st.file_uploader('Format Musik ".wav" berdurasi maksimal 30sec', type='wav', accept_multiple_files=False)
if files is not None:
audio,sr = librosa.load(files,sr = 22050)
duration = int(librosa.get_duration(audio))
if 'file_uploaded' not in st.session_state:
st.session_state['file_uploaded'] = True
for percent_complete in range(100):
time.sleep(0.01)
bar.progress(i+1)
i = i+1
pyautogui.hotkey('ctrl', 'F5')
elif st.session_state['file_uploaded'] == True:
st.audio(files, format="audio/wav", start_time=0)
elif selected_item == 'Sample Musik':
selected_file = st.selectbox("Pilih Sample", ['Blues','Jazz','Country','Classical','Hiphop','Metal','Pop','Reggae','Rock'])
files = f'Data/upload/user/{selected_file}.wav'
st.audio(files, format="audio/wav", start_time=0)
submitted = st.button("Prediksi")
if submitted:
with st.spinner('Tunggu Sebentar'):
signal = files
mfcc_for_track = get_mfcc(signal)
predict = prediction(mfcc_for_track)
genre = get_genre(int(predict))
st.success('Proses Prediksi Selesai!')
st.markdown(f'Genre Musikmu adalah 🎵 : Musik **{genre}** ')
if __name__ == '__main__':
main() |