Update app.py
Browse files
app.py
CHANGED
@@ -45,7 +45,26 @@ WIN_SIZE = 1024
|
|
45 |
WINDOW_TYPE = "hann"
|
46 |
FEATURE = "mel"
|
47 |
FMIN = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
|
|
49 |
|
50 |
def preprocess_and_save_spectrogram_from_signal(signal, image_path):
|
51 |
# Plot the spectrogram and save it
|
@@ -176,28 +195,15 @@ def model():
|
|
176 |
if st.button('Predict'):
|
177 |
st.write('Predicting using', selected_model)
|
178 |
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
# Generate a random number (e.g., a 6-digit number)
|
183 |
-
random_number = random.randint(100000, 999999)
|
184 |
-
|
185 |
-
# Create a new file name with the random number
|
186 |
-
file_name = f'input_{random_number}.png'
|
187 |
|
188 |
-
# Define the full path to the image
|
189 |
-
image_path = fr'C:\Users\jaini\PycharmProjects\nlp\DL\temp_images\{file_name}'
|
190 |
-
|
191 |
-
# Call the `saveMel` function with the updated image_path
|
192 |
-
# plot_spectrogram_and_save(y, image_path)
|
193 |
-
|
194 |
-
preprocess_and_save_spectrogram_from_signal(y, image_path)
|
195 |
-
# preprocess_and_save_spectrogram_from_signal_v2(y, image_path)
|
196 |
-
image_ = Image.open(image_path)
|
197 |
-
st.image(image_, caption='Specrogram', use_column_width=True)
|
198 |
|
|
|
|
|
|
|
|
|
199 |
# Load the selected model
|
200 |
-
|
201 |
model_path = os.path.join('model', selected_model)
|
202 |
model = tf.keras.models.load_model(model_path)
|
203 |
|
|
|
45 |
WINDOW_TYPE = "hann"
|
46 |
FEATURE = "mel"
|
47 |
FMIN = 0
|
48 |
+
def preprocess_and_return_spectrogram_from_signal(signal):
|
49 |
+
# Plot the spectrogram
|
50 |
+
plt.figure(figsize=(10, 4))
|
51 |
+
D = librosa.amplitude_to_db(np.abs(librosa.stft(signal)), ref=np.max)
|
52 |
+
librosa.display.specshow(D, y_axis="linear")
|
53 |
+
plt.colorbar(format="%+2.0f dB")
|
54 |
+
plt.title("Linear-frequency power spectrogram")
|
55 |
+
|
56 |
+
# Create a BytesIO object to capture the image data
|
57 |
+
image_buffer = io.BytesIO()
|
58 |
+
|
59 |
+
# Save the spectrogram image to the BytesIO object
|
60 |
+
plt.savefig(image_buffer, format="png", bbox_inches="tight")
|
61 |
+
plt.close()
|
62 |
+
|
63 |
+
# Reset the BytesIO object's position to the beginning
|
64 |
+
image_buffer.seek(0)
|
65 |
|
66 |
+
# Return the BytesIO object
|
67 |
+
return image_buffer
|
68 |
|
69 |
def preprocess_and_save_spectrogram_from_signal(signal, image_path):
|
70 |
# Plot the spectrogram and save it
|
|
|
195 |
if st.button('Predict'):
|
196 |
st.write('Predicting using', selected_model)
|
197 |
|
198 |
+
|
199 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
|
202 |
+
image_buffer = preprocess_and_return_spectrogram_from_signal(y)
|
203 |
+
image = Image.open(image_buffer)
|
204 |
+
st.image(image, caption='Spectrogram', use_column_width=True)
|
205 |
+
|
206 |
# Load the selected model
|
|
|
207 |
model_path = os.path.join('model', selected_model)
|
208 |
model = tf.keras.models.load_model(model_path)
|
209 |
|