Commit 42d0f66c authored by it20118068's avatar it20118068

Update flask api by Bhashitha

parent 818eeac2
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "86220008",
"metadata": {},
"outputs": [],
"source": [
"import joblib"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "dec33b7c",
"metadata": {},
"outputs": [],
"source": [
"# pip install flask"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "37a3b790",
"metadata": {},
"outputs": [],
"source": [
"# Load the trained model from the file\n",
"rf_classifier = joblib.load(\"trained_rf_model.joblib\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e649b59d",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import librosa\n",
"import numpy as np\n",
"from tensorflow.keras.preprocessing.sequence import pad_sequences\n",
"\n",
"# Define constants for audio processing\n",
"SAMPLE_RATE = 44100\n",
"NUM_MFCC = 13\n",
"MAX_LEN = 500 # Maximum length of audio features\n",
"\n",
"# Function to preprocess the audio clip\n",
"def preprocess_audio(audio_path):\n",
" # Load the audio file\n",
" audio, _ = librosa.load(audio_path, sr=SAMPLE_RATE, mono=True)\n",
"\n",
" # Normalize the audio to have maximum amplitude of 1\n",
" normalized_audio = audio / np.max(np.abs(audio))\n",
"\n",
" return normalized_audio\n",
"\n",
"# Function to extract MFCC features from audio data\n",
"def extract_mfcc(audio_data):\n",
" mfcc_features = librosa.feature.mfcc(y=audio_data, sr=SAMPLE_RATE, n_mfcc=NUM_MFCC)\n",
"\n",
" return mfcc_features.T\n",
"\n",
"\n",
"\n",
"# # Print the predicted label\n",
"# print(\"Predicted label:\", predicted_label)\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1697598d",
"metadata": {},
"outputs": [],
"source": [
"# Load the pre-recorded audio clip\n",
"audio_clip_path = \"Test/test2.wav\"\n",
"preprocessed_audio = preprocess_audio(audio_clip_path)\n",
"\n",
"# Extract MFCC features\n",
"mfcc_features = extract_mfcc(preprocessed_audio)\n",
"\n",
"# Pad or truncate the features to a fixed length\n",
"features_padded = pad_sequences([mfcc_features], maxlen=MAX_LEN, padding='post', truncating='post', dtype='float32')\n",
"\n",
"# Flatten the features array to match the expected shape\n",
"features_flattened = features_padded.reshape(1, -1)\n",
"\n",
"# Predict the label using the trained random forest classifier\n",
"predicted_label = rf_classifier.predict(features_flattened)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "247378b6",
"metadata": {},
"outputs": [],
"source": [
"from flask import Flask, jsonify, request\n",
"# from flask_cors import CORS, cross_origin\n",
"#import speech_recognition as sr\n",
"import nltk\n",
"\n",
"app = Flask(__name__)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "9d7e4618",
"metadata": {},
"outputs": [],
"source": [
"\n",
"# cors = CORS(app)\n",
"# app.config['CORS_HEADERS'] = 'Content-Type'\n",
"\n",
"@app.route('/transcribe-audio', methods=['POST'])\n",
"def transcribe_audio():\n",
" audio_file = request.files['fileData']\n",
" \n",
" # Load the pre-recorded audio clip\n",
"# audio_clip_path = \"Test/test2.wav\"\n",
" preprocessed_audio = preprocess_audio(audio_file)\n",
"\n",
" # Extract MFCC features\n",
" mfcc_features = extract_mfcc(preprocessed_audio)\n",
"\n",
" # Pad or truncate the features to a fixed length\n",
" features_padded = pad_sequences([mfcc_features], maxlen=MAX_LEN, padding='post', truncating='post', dtype='float32')\n",
"\n",
" # Flatten the features array to match the expected shape\n",
" features_flattened = features_padded.reshape(1, -1)\n",
"\n",
" # Predict the label using the trained random forest classifier\n",
" predicted_label = rf_classifier.predict(features_flattened)\n",
"\n",
" \n",
" \n",
"\n",
" # Print the predicted label\n",
"# print(\"Predicted label:\", predicted_label)\n",
" return jsonify({'message': 'Video playback completed.','sign':predicted_label}), 200\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df1eb8fb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * Serving Flask app '__main__'\n",
" * Debug mode: off\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
" * Running on http://127.0.0.1:5000\n",
"Press CTRL+C to quit\n",
"[2023-08-20 17:15:29,998] ERROR in app: Exception on /transcribe-audio [POST]\n",
"Traceback (most recent call last):\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\app.py\", line 2525, in wsgi_app\n",
" response = self.full_dispatch_request()\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\app.py\", line 1822, in full_dispatch_request\n",
" rv = self.handle_user_exception(e)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\app.py\", line 1820, in full_dispatch_request\n",
" rv = self.dispatch_request()\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\app.py\", line 1796, in dispatch_request\n",
" return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)\n",
" File \"C:\\Users\\Admin\\AppData\\Local\\Temp\\ipykernel_11320\\1471284434.py\", line 29, in transcribe_audio\n",
" return jsonify({'message': 'Video playback completed.','sign':predicted_label}), 200\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\json\\__init__.py\", line 342, in jsonify\n",
" return current_app.json.response(*args, **kwargs)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\json\\provider.py\", line 309, in response\n",
" f\"{self.dumps(obj, **dump_args)}\\n\", mimetype=mimetype\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\json\\provider.py\", line 230, in dumps\n",
" return json.dumps(obj, **kwargs)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\json\\__init__.py\", line 238, in dumps\n",
" **kw).encode(obj)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\json\\encoder.py\", line 199, in encode\n",
" chunks = self.iterencode(o, _one_shot=True)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\json\\encoder.py\", line 257, in iterencode\n",
" return _iterencode(o, 0)\n",
" File \"C:\\Users\\Admin\\anaconda3\\lib\\site-packages\\flask\\json\\provider.py\", line 122, in _default\n",
" raise TypeError(f\"Object of type {type(o).__name__} is not JSON serializable\")\n",
"TypeError: Object of type ndarray is not JSON serializable\n",
"127.0.0.1 - - [20/Aug/2023 17:15:30] \"POST /transcribe-audio?= HTTP/1.1\" 500 -\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" app.run() "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ec00cb4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0a9b5dce",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb0aaab3",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment