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