-2

我使用下面的代码片段进行谷歌语音到文本识别,

var speech = require('google-speech-api');
var opts = {
  file: 'speech.mp3',
  key: '<Google API Key>'
};
speech(opts, function (err, results) {
  console.log(results);
  // [{result: [{alternative: [{transcript: '...'}]}]}]
});

然后我试着做

“npm 安装 google-speech-api”

从命令提示符。它给出错误。然后,我做了

“npm 安装 googleapis”

它成功了。我从命令提示符“node myspeech.js”执行了 Node.js 脚本......它抛出错误,

module.js:341
    throw err;


      ^

    Error: Cannot find module 'google-speech-api'
        at Function.Module._resolveFilename (module.js:339:15)
        at Function.Module._load (module.js:290:25)
        at Module.require (module.js:367:17)
        at require (internal/module.js:16:19)
        at Object.<anonymous> (C:\myspeechtest.js:1:76)
        at Module._compile (module.js:413:34)
        at Object.Module._extensions..js (module.js:422:10)
        at Module.load (module.js:357:32)
        at Function.Module._load (module.js:314:12)
        at Function.Module.runMain (module.js:447:10)
4

2 回答 2

1

正如您在错误日志中看到的:

npm ERR! code ENOGIT
npm ERR! not found: git npm
ERR! npm ERR! Failed using git.
npm ERR! This is most likely not a problem with npm itself.
npm ERR! Please check if you have git installed and in your PATH.

您需要在系统和 PATH 中安装 git。

对于 Windows,你可以使用git-bash,对于 Debian/Ubuntu,一个简单sudo apt-get install git的就可以了。

于 2016-07-09T22:57:45.520 回答
0
        const projectId = 'yourGoogleProjectId';
        let file="conf.json"//google exported this for you 
        var speech = require('@google-cloud/speech')({
            projectId: projectId,
            keyFilename: file
        });
        const fs = require('fs');
         const fileName = 'yourMp3FilePath';

       // Reads a local audio file and converts it to base64
       const fileMp3 = fs.readFileSync(fileName);
       const audioBytes = fileMp3.toString('base64');
        const audio = {
            content:audioBytes
        };
        const config = {
            encoding: 'AMR_WB',
            sampleRateHertz: 16000,
            languageCode: 'en-US'
        };
        const request = {
            audio: audio,
            config: config
        };
        speech.recognize(request)
            .then((results) => {
                const transcription = results[0].results[0].alternatives[0].transcript;
                console.log(`Transcription: `, transcription);
            })
            .catch((err) => {
                console.error('ERROR:', err);
            });
于 2017-08-19T15:40:32.393 回答