Ver código fonte

[Update] Convert audio format into .wav

- speech_recognition does not handle android .amr audio format need to be converted into .wav before processing.
- Using the system ffmpeg sys call ... 
- updating the lib requirements
ThiBuff 7 anos atrás
pai
commit
a6f621889c

+ 1 - 0
install/files/deb-packages_requirements.txt

@@ -21,3 +21,4 @@ sox
 libatlas3-base
 mplayer
 libav-tools
+ffmpeg

+ 2 - 1
install/rpi_install_kalliope.sh

@@ -27,7 +27,8 @@ echo "Installing system packages..."
 sudo apt-get update
 sudo apt-get install -y git python-dev libsmpeg0 libttspico-utils libsmpeg0 \
 flac dialog libffi-dev libffi-dev libssl-dev portaudio19-dev build-essential \
-libssl-dev libffi-dev sox libatlas3-base mplayer libyaml-dev libpython2.7-dev pulseaudio pulseaudio-utils libav-tools
+libssl-dev libffi-dev sox libatlas3-base mplayer libyaml-dev libpython2.7-dev pulseaudio pulseaudio-utils libav-tools \
+ffmpeg
 
 # this is used to help the RPI
 sudo apt-get install -y libportaudio0 libportaudio2 libportaudiocpp0 portaudio19-dev

+ 31 - 24
kalliope/core/RestAPI/FlaskAPI.py

@@ -2,6 +2,7 @@ import logging
 import os
 import threading
 
+import subprocess
 import time
 
 from kalliope.core.LIFOBuffer import LIFOBuffer
@@ -215,30 +216,36 @@ class FlaskAPI(threading.Thread):
                 "error": "No file provided"
             }
             return jsonify(error=data), 400
-        if file and self.allowed_file(file.filename):
-            # save the file
-            filename = secure_filename(file.filename)
-            base_path = os.path.join(self.app.config['UPLOAD_FOLDER'])
-            file.save(os.path.join(base_path, filename))
-
-            # now start analyse the audio with STT engine
-            audio_path = base_path + os.sep + filename
-            ol = OrderListener(callback=self.audio_analyser_callback, audio_file_path=audio_path)
-            ol.start()
-            ol.join()
-            # wait the Order Analyser processing. We need to wait in this thread to keep the context
-            while not self.order_analyser_return:
-                time.sleep(0.1)
-            self.order_analyser_return = False
-            if self.api_response is not None and self.api_response:
-                data = jsonify(self.api_response)
-                self.api_response = None
-                return data, 201
-            else:
-                data = {
-                    "error": "The given order doesn't match any synapses"
-                }
-                return jsonify(error=data), 400
+        # save the file
+        filename = secure_filename(file.filename)
+        base_path = os.path.join(self.app.config['UPLOAD_FOLDER'])
+        file.save(os.path.join(base_path, filename))
+
+        # now start analyse the audio with STT engine
+        audio_path = base_path + os.sep + filename
+        if not self.allowed_file(audio_path):
+            # Not allowed so convert into wav using ffmpeg
+            base = os.path.splitext(audio_path)[0]
+            new_file_path = base + ".wav"
+            # os.system("ffmpeg -i " + audio_path + " " + new_file_path) --> deprecated
+            subprocess.call(["ffmpeg", "-i", str(audio_path), str(new_file_path)], shell=True)
+            audio_path = new_file_path
+        ol = OrderListener(callback=self.audio_analyser_callback, audio_file_path=audio_path)
+        ol.start()
+        ol.join()
+        # wait the Order Analyser processing. We need to wait in this thread to keep the context
+        while not self.order_analyser_return:
+            time.sleep(0.1)
+        self.order_analyser_return = False
+        if self.api_response is not None and self.api_response:
+            data = jsonify(self.api_response)
+            self.api_response = None
+            return data, 201
+        else:
+            data = {
+                "error": "The given order doesn't match any synapses"
+            }
+            return jsonify(error=data), 400
 
     @requires_auth
     def shutdown_server(self):