I want to get an audio file in js and then send this file to python and then recognise the text being spoken. I know that tools exist in js, but I need server-side recognition in order to secure the interface in an online mini-game.
main.js
async function try_case(click) {
var el = click.target;
el.innerHTML = microphoneHtml;
for (var i = 0; i < items.length; i++) {
items[i].removeEventListener('click', try_case);
}
navigator.mediaDevices.getUserMedia({ audio: true })
.then(function(stream) {
var mediaRecorder = new MediaRecorder(stream);
mediaRecorder.start();
var audioChunks = [];
mediaRecorder.addEventListener("dataavailable", function(event) {
audioChunks.push(event.data);
});
mediaRecorder.addEventListener("stop", function() {
document.getElementsByClassName('micro-listener')[0].classList.add('exit');
setTimeout(function() {
el.innerHTML = "";
}, 500);
var audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
var formData = new FormData();
formData.append('audio', audioBlob);
fetch(`/dashboard/games/memowordrize/${sessionID}/record`, {
method: 'POST',
headers: {
'X-CSRFToken': csrf_token
},
body: formData
})
.then(response => response.text())
.then(data => {
console.log(data);
});
});
setTimeout(function() {
mediaRecorder.stop();
}, 3000);
});
return;
}
memowordrize.py
@memowordrize_bp.route('/dashboard/games/memowordrize/<string:session_id>/record', methods=['POST'])
@check_game
def record(session_id):
try:
if 'audio' not in request.files:
return jsonify({
"code": 403,
"message": "Aucun fichier audio n'a été trouvé!",
"result": []
})
audio_file = request.files['audio']
audio_data = audio_file.read()
recognizer = sr.Recognizer()
audio = sr.AudioData(audio_data, 16000, 2)
text = recognizer.recognize_google(audio, language="fr-FR")
print(text)
return jsonify({
"code": 200,
"message": "Le texte a été reconnu!"
})
except Exception as e:
logging.error("An error has occured: " + str(e))
return jsonify({
"code": 500,
"message": "Une erreur s'est produite!",
"result": []
}), 500
Everything works (I didn’t put in all my code), it’s from the line text = recognizer.recognize_google(audio, language="fr-FR")
that an error is detected.
The error is captured but nothing is displayed to indicate: ERROR:root:An error has occured:
. But nothing is indicated. No indication on the net or by chatGpt.