Google речь к тексту не работает на nodejs - PullRequest
3 голосов
/ 13 марта 2020

Я создал приложение для преобразования речи в текст. реагирует на внешний интерфейс и nodejs API.i записывает аудио от реакции и публикует его на nodejs. но результат Google API пуст. Как я могу это исправить?

почему получаются всегда пустые результаты?

это мой код.

ReactMi c Регистратор

<ReactMic
    record={record}
    className="sound-wave"
    onStop={onStop}
    onData={onData}
    strokeColor="#000000"
    backgroundColor="#FF4081"
    mimeType="audio/wav"/>
<button onClick={startRecording} type="button">Start</button>
<button onClick={stopRecording} type="button">Stop</button>

NodeJs API

app.post('/SpeechConvert', (req, res) => {

    const client = new speech.SpeechClient();

    console.log(req.files.file);

    req.files.file.mv('./input.wav',function (err) {
        if (err) {
            console.log(err);
        }
    })



    async function speechToText() {

        // The name of the audio file to transcribe
        const fileData = req.files.file.data;

        // Reads a local audio file and converts it to base64
        const file = fs.readFileSync('input.wav');
        const audioBytes = fileData.toString('base64');

        // console.log(audioBytes);
        // The audio file's encoding, sample rate in hertz, and BCP-47 language code
        const audio = {
            content: audioBytes,
        };
        const config = {
            enableAutomaticPunctuation: true,
            encoding: 'LINEAR16',
            sampleRateHertz: 44100,
            languageCode: 'en-US',
        };
        const request = {
            audio: audio,
            config: config,
        };

        // Detects speech in the audio file
        const [response] = await client.recognize(request);
        console.log(response);
        const transcription = response.results
            .map(result => result.alternatives[0].transcript)
            .join('\n');
        console.log(`Transcription: ${transcription}`);
        res.send({ 'transcription': transcription, 'msg': 'The Audio successfully converted to the text' });
    }

    speechToText().catch(console.error);

});

Может кто-нибудь помочь мне это исправить?

...