Я пытаюсь использовать клиентскую библиотеку Python для преобразования текста в Google. Мои запросы попали в API нормально, но ответ пуст. Я использую аудио двоичные данные, отправленные с клиента. Вход микрофона записывается в течение 3 секунд, а затем отправляется с помощью запроса ajx.
Я пытался изменить кодировку, изменив ее на base64, но, похоже, ничего не дает успешного ответа.
Вот мой код Python
from flask import Flask, request, render_template
import io
import os
import sys
import json
import base64
# Imports the Google Cloud client library
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
app = Flask(__name__)
@app.route('/audio', methods=['PUT'])
def audio():
client = speech.SpeechClient()
content = request.files['audio'].read()
# with open('voice.wav', 'wb') as file:
# file.write(content)
# with open('voice.wav', 'rb') as file:
# content = file.read();
audio = types.RecognitionAudio(content=base64.b64encode(content))
config = types.RecognitionConfig(
encoding=enums.RecognitionConfig.AudioEncoding.FLAC,
sample_rate_hertz=48000,
language_code='en-US')
# Detects speech in the audio file
response = client.recognize(config, audio)
print(response, file=sys.stderr)
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript), file=sys.stderr)
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
И мой код JS
const recordAudio = () =>
new Promise(async resolve => {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream);
const audioChunks = [];
mediaRecorder.addEventListener("dataavailable", event => {
audioChunks.push(event.data);
});
const start = () => mediaRecorder.start();
const stop = () =>
new Promise(resolve => {
mediaRecorder.addEventListener("stop", () => {
const audioBlob = new Blob(audioChunks);
const audioUrl = URL.createObjectURL(audioBlob);
const audio = new Audio(audioUrl);
const play = () => audio.play();
resolve({ audioBlob, audioUrl, play });
});
mediaRecorder.stop();
});
resolve({ start, stop });
});
const sendAudio = (audioBlob) =>
new Promise(async resolve => {
var formData = new FormData();
formData.append('audio', audioBlob, 'audio')
$.ajax({
type: 'PUT',
url: '/audio',
data: formData,
processData: false,
contentType: false
}).done(function(data) {
console.log(data);
});
})
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
const handleAction = async () => {
const recorder = await recordAudio();
const actionButton = document.getElementById('action');
actionButton.disabled = true;
recorder.start();
await sleep(3000);
const audio = await recorder.stop();
audio.play();
await sendAudio(audio.audioBlob)
await sleep(3000);
actionButton
и HTML
<!doctype html>
<html>
<head>
<title>Record Audio Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1>Audio Recording Test</h1>
<p>Talk for 3 seconds, then you will hear your recording played back</p>
<script src="/static/index.js"></script>
<button id="action" onclick="handleAction()">Start recording...</button>
</body>
</html>