From 83522fce049767db03c667169680a9fd4cec68ed Mon Sep 17 00:00:00 2001
From: h4-rahman <hamidur2.rahman@live.uwe.ac.uk>
Date: Mon, 11 Mar 2024 08:30:53 +0000
Subject: [PATCH] Woring on python file for preprocessing and using a audio
 file for prediction using model through API request

---
 .gitignore                             |  3 +-
 myproject/debug.log                    |  1 +
 myproject/myapp/audio_preprocessing.py | 52 ++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 myproject/myapp/audio_preprocessing.py

diff --git a/.gitignore b/.gitignore
index e68b164..f48ddc7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@ myproject/node_modules
 myproject/env
 myproject/myapp/static/CACHE
 myproject/myapp/__pycache__
-.venv/
\ No newline at end of file
+.venv/
+.DS_Store
\ No newline at end of file
diff --git a/myproject/debug.log b/myproject/debug.log
index d9213e3..17a3183 100644
--- a/myproject/debug.log
+++ b/myproject/debug.log
@@ -400,3 +400,4 @@ Watching for file changes with StatReloader
 Watching for file changes with StatReloader
 /usr/src/app/myapp/views.py changed, reloading.
 Watching for file changes with StatReloader
+Watching for file changes with StatReloader
diff --git a/myproject/myapp/audio_preprocessing.py b/myproject/myapp/audio_preprocessing.py
new file mode 100644
index 0000000..24728a9
--- /dev/null
+++ b/myproject/myapp/audio_preprocessing.py
@@ -0,0 +1,52 @@
+import librosa
+import numpy as np
+import requests
+import json
+
+def get_windows(audio, window_size=22050):
+    start = 0
+    windows = []
+    audio_len = len(audio)
+    while start < audio_len:
+        if start + window_size > audio_len:
+            break
+        window_end = int(start + window_size)
+        windows.append(audio[start:window_end])
+        start += int(window_size / 2)
+    return windows
+
+def preprocess_audio_for_inference(audio_path):
+    audio, sr = librosa.load(audio_path, sr=22050)
+    windows = get_windows(audio)
+    preprocessed_windows = []
+    for window in windows:
+        mel = librosa.feature.melspectrogram(y=window, sr=sr)
+        mel_db = librosa.power_to_db(mel, ref=np.max)
+        # Ensure the shape matches your model's expected input
+        mel_db_resized = np.resize(mel_db, (128, 44))
+        mel_db_resized = np.expand_dims(mel_db_resized, axis=-1)  # Adding the channel dimension
+        preprocessed_windows.append(mel_db_resized)
+    return preprocessed_windows
+
+# Preprocess your audio file
+audio_path = './static/src/media/Casio Piano C5 1980s.wav'  # Update this path
+preprocessed_data = preprocess_audio_for_inference(audio_path)
+
+# TensorFlow Serving URL
+url = 'http://localhost:8501/v1/models/instrument_model:predict'
+
+# Prepare data for TensorFlow Serving
+data = json.dumps({"signature_name": "serving_default", "instances": [window.tolist() for window in preprocessed_data]})
+
+# Send request
+headers = {"Content-Type": "application/json"}
+response = requests.post(url, data=data, headers=headers)
+
+# Process response
+if response.status_code == 200:
+    predictions = response.json()['predictions']
+    # Process your predictions as needed
+    print(predictions)
+else:
+    print(f"Failed to get predictions, status code: {response.status_code}, response text: {response.text}")
+
-- 
GitLab