Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,91 +0,0 @@
|
|
1 |
-
# https://github.com/ayushkumarshah/Guitar-Chords-recognition
|
2 |
-
# https://github.com/ayushkumarshah/Guitar-Chords-recognition/blob/master/app.py
|
3 |
-
# https://raw.githubusercontent.com/ayushkumarshah/Guitar-Chords-recognition/master/app.py
|
4 |
-
|
5 |
-
import time, os
|
6 |
-
import logging
|
7 |
-
import streamlit as st
|
8 |
-
import numpy as np
|
9 |
-
import librosa, librosa.display
|
10 |
-
import matplotlib.pyplot as plt
|
11 |
-
from PIL import Image
|
12 |
-
from settings import IMAGE_DIR, DURATION, WAVE_OUTPUT_FILE
|
13 |
-
from src.sound import sound
|
14 |
-
from src.model import CNN
|
15 |
-
from setup_logging import setup_logging
|
16 |
-
|
17 |
-
setup_logging()
|
18 |
-
logger = logging.getLogger('app')
|
19 |
-
|
20 |
-
def init_model():
|
21 |
-
cnn = CNN((128, 87))
|
22 |
-
cnn.load_model()
|
23 |
-
return cnn
|
24 |
-
|
25 |
-
def get_spectrogram(type='mel'):
|
26 |
-
logger.info("Extracting spectrogram")
|
27 |
-
y, sr = librosa.load(WAVE_OUTPUT_FILE, duration=DURATION)
|
28 |
-
ps = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)
|
29 |
-
logger.info("Spectrogram Extracted")
|
30 |
-
format = '%+2.0f'
|
31 |
-
if type == 'DB':
|
32 |
-
ps = librosa.power_to_db(ps, ref=np.max)
|
33 |
-
format = ''.join[format, 'DB']
|
34 |
-
logger.info("Converted to DB scale")
|
35 |
-
return ps, format
|
36 |
-
|
37 |
-
def display(spectrogram, format):
|
38 |
-
plt.figure(figsize=(10, 4))
|
39 |
-
librosa.display.specshow(spectrogram, y_axis='mel', x_axis='time')
|
40 |
-
plt.title('Mel-frequency spectrogram')
|
41 |
-
plt.colorbar(format=format)
|
42 |
-
plt.tight_layout()
|
43 |
-
st.pyplot(clear_figure=False)
|
44 |
-
|
45 |
-
def main():
|
46 |
-
title = "Guitar Chord Recognition"
|
47 |
-
st.title(title)
|
48 |
-
image = Image.open(os.path.join(IMAGE_DIR, 'app_guitar.jpg'))
|
49 |
-
st.image(image, use_column_width=True)
|
50 |
-
|
51 |
-
if st.button('Record'):
|
52 |
-
with st.spinner(f'Recording for {DURATION} seconds ....'):
|
53 |
-
sound.record()
|
54 |
-
st.success("Recording completed")
|
55 |
-
|
56 |
-
if st.button('Play'):
|
57 |
-
# sound.play()
|
58 |
-
try:
|
59 |
-
audio_file = open(WAVE_OUTPUT_FILE, 'rb')
|
60 |
-
audio_bytes = audio_file.read()
|
61 |
-
st.audio(audio_bytes, format='audio/wav')
|
62 |
-
except:
|
63 |
-
st.write("Please record sound first")
|
64 |
-
|
65 |
-
if st.button('Classify'):
|
66 |
-
cnn = init_model()
|
67 |
-
with st.spinner("Classifying the chord"):
|
68 |
-
chord = cnn.predict(WAVE_OUTPUT_FILE, False)
|
69 |
-
st.success("Classification completed")
|
70 |
-
st.write("### The recorded chord is **", chord + "**")
|
71 |
-
if chord == 'N/A':
|
72 |
-
st.write("Please record sound first")
|
73 |
-
st.write("\n")
|
74 |
-
|
75 |
-
# Add a placeholder
|
76 |
-
if st.button('Display Spectrogram'):
|
77 |
-
# type = st.radio("Scale of spectrogram:",
|
78 |
-
# ('mel', 'DB'))
|
79 |
-
if os.path.exists(WAVE_OUTPUT_FILE):
|
80 |
-
spectrogram, format = get_spectrogram(type='mel')
|
81 |
-
display(spectrogram, format)
|
82 |
-
else:
|
83 |
-
st.write("Please record sound first")
|
84 |
-
|
85 |
-
if __name__ == '__main__':
|
86 |
-
main()
|
87 |
-
# for i in range(100):
|
88 |
-
# # Update the progress bar with each iteration.
|
89 |
-
# latest_iteration.text(f'Iteration {i+1}')
|
90 |
-
# bar.progress(i + 1)
|
91 |
-
# time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|