IT|軟體|應用|語音辨識|即時說話口譯 DIY-2 搭配 RESTFul API 應用

 
 
[接線]
3.3(必須 3.3v)
3.3
11(BCM 17)
電阻
3(GND)
接地
rec.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
import RPi.GPIO as GPIO
import pyaudio
import wave
import os
import sys
 
def rec_fun():
    # 隱藏錯誤消息,因為會有一堆ALSA和JACK錯誤消息,但其實能正常錄音
    os.close(sys.stderr.fileno())
   
    BUTT = 17    # 開始錄音的按鈕:一邊接GPIO17,一邊接地
    GPIO.setmode(GPIO.BCM)
    # 設GPIO26腳為輸入腳,電平拉高,也就是說26腳一旦讀到低電平,說明按了按鈕
    GPIO.setup(BUTT, GPIO.IN, pull_up_down = GPIO.PUD_UP)
 
    # wav文件是由若干個CHUNK組成的,CHUNK我們就理解成數據包或者數據片段。
    CHUNK = 512
    FORMAT = pyaudio.paInt16  # pyaudio.paInt16表示我們使用量化位數16位來進行錄音
    RATE = 44100  # 採樣率44.1k,每秒採樣44100個點。
    WAVE_OUTPUT_FILENAME = "command.wav"
    print('請按住按鈕開始錄音...')
    GPIO.wait_for_edge(BUTT, GPIO.FALLING)
 
    # To use PyAudio, first instantiate PyAudio using pyaudio.PyAudio(), which sets up the portaudio system.
    p = pyaudio.PyAudio()
    stream = p.open(format = FORMAT,
                    channels = 1,    # cloud speecAPI只支持單聲道
                    rate = RATE,
                    input = True,
                    frames_per_buffer = CHUNK)
    print("錄音中...")
 
    frames = []
    # 按住按鈕錄音,放開時結束
    while GPIO.input(BUTT) == 0:
        data = stream.read(CHUNK)
        frames.append(data)
    print("錄音完成,輸出文件:" + WAVE_OUTPUT_FILENAME + '\n')
    stream.stop_stream()
    stream.close()
    p.terminate()
 
    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
    wf.setnchannels(1)
    wf.setsampwidth(p.get_sample_size(FORMAT))    # Returns the size (in bytes) for the specified sample format.
    wf.setframerate(RATE)
    wf.writeframes(b''.join(frames))
    wf.close()
   
    return
 
# 可以直接運行rec.py進行測試,同時保證該文件import時不會自動運行
if __name__ == '__main__':
    rec_fun()
 
執行按鈕錄音程式
python3 rec.py
 
播放
aplay command.wav
 
 
配合 API
 
apirec.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
from flask import Flask, jsonify,make_response,request,abort
from flask.ext.httpauth import HTTPBasicAuth
 
 
import time
import os
from os import path
import speech_recognition as sr
from textblob import TextBlob
from gtts import gTTS
 
app = Flask(__name__, static_url_path="")
auth = HTTPBasicAuth()
 
fromLanguage = "zh-TW"
toLanguage = "en"
 
 
def text_speaker(content):
    tts = gTTS(text=content, lang=fromLanguage)
    tts.save("tts.mp3")
    os.system('omxplayer -p -o local tts.mp3')
    #time.sleep(0.5)
    return None
 
text_speaker("您好,我是語音辨識助理,啟動服務中...")
 
@auth.get_password
def get_password(username):
    if username == 'pi':
        return '999999'
    return None
 
@app.errorhandler(400)
def not_found(error):
    return make_response(jsonify({'error': 'Bad request'}), 400)
 
@auth.error_handler
def unauthorized():
    return make_response(jsonify({'error': 'Unauthorized'}), 403)
    # return 403 instead of 401 to prevent browsers from displaying the default auth dialog
 
@app.errorhandler(404)
def not_found(error):
    return make_response(jsonify({'error': 'Not found'}), 404)
 
@app.route('/todo/api/v1.0/tasks', methods=['POST'])
@auth.login_required
def create_task():
    if not request.json or not 'content' in request.json:
        abort(400)
    task = {
        'content': request.json['content'],
        'status': 'Created'
    }
    text_speaker(request.json['content'])
    return jsonify(task), 201
 
 
if __name__ == '__main__':
    text_speaker("服務啟動成功,語音辨識助理開始為您服務。")
    app.run(host='0.0.0.0', port=5000,debug=False)
 
 
text_speaker("結束語音辨識助理服務,謝謝您的使用。")
 
 
[參考]