世博会:无论如何将语音添加到文本?

问题描述 投票:3回答:1

我希望在我的Expo应用程序中包含语音到文本。有api可用,如谷歌的文字和沃森等的演讲......

有没有人想出如何在他们的Expo或React-Native应用程序中包含Speech-to-Text的解决方案或任何建议?

我查看了各种github repos,它们为React-Native应用程序提供了Speech-to-Text,但它们看起来并不适合生产,并且是严格的React-Native解决方案,因为您需要访问Java / Swift代码。

如果这是唯一的选择,但如果可能的话,我更倾向于采用世博会的解决方案。

问候,埃米尔

react-native expo
1个回答
0
投票

如果你想在expo上实现Speech-To-Text,那么你需要创建一个api并部署它,否则你需要分离项目并使用库react-native-google-speech-api

这是我使用谷歌应用程序引擎,谷歌云存储和谷歌语音文本实现。

const format = require('util').format;
const fs = require('fs');
const express = require('express');
const multer  = require('multer');
const requestHttp = require('request');
const {Storage} = require('@google-cloud/storage');

// Instantiate a storage client
const storage = new Storage();

// const upload = multer();
const app = express();

// Imports the Google Cloud client library
const speech = require('@google-cloud/speech');

// Creates a client
const client = new speech.SpeechClient();

/**
 * TODO(developer): Uncomment the following lines before running the sample.
 */
const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

  const upload = multer({
    storage: multer.memoryStorage(),
    limits: {
      fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
    },
  });

  const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);


app.post('/upload', upload.single('file') , async (req, res) => {

    const file = await req.file
    if (!file) {
      const error = new Error('Please upload a file')
      error.httpStatusCode = 400
      return next(error)
    }

    // Create a new blob in the bucket and upload the file data.
    const blob = bucket.file(req.file.originalname);
    const blobStream = blob.createWriteStream({
      resumable: false,
    });

    blobStream.on('error', err => {
      next(err);
    });

    blobStream.on('finish', async () => {
        // The public URL can be used to directly access the file via HTTP.
        const publicUrl = await format(
          `https://storage.googleapis.com/${bucket.name}/${blob.name}`
        );
        const request = {
            config: {
              encoding: encoding,
              sampleRateHertz: sampleRateHertz,
              languageCode: languageCode,
            },
            audio: {
                uri: 'gs://YOUR-Bucket-Name/File-name.ext'
            } 
          };
        // Stream the audio to the Google Cloud Speech API
        const [response] = await client.recognize(request);
        const transcription = response.results
          .map(result => result.alternatives[0].transcript)
          .join('\n');
        console.log(`Transcription: `, transcription);
        res.status(200)
            .send({
            success: 'true',
            message: 'Text retrieved successfully',
            text: transcription
            })
            .end();

      });
      blobStream.end(req.file.buffer);
});


const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`App listening on port ${PORT}`);
  console.log('Press Ctrl+C to quit.');
});
© www.soinside.com 2019 - 2024. All rights reserved.