1

我想实现客户端库 Google Cloud,但没有 Ionic 实现。

如何在 Ionic 上实现客户端库以使用 Google Speech To Text?

目前,我尝试实现 NodeJS 客户端库,但存在一些问题,因为论坛中的一些人说我们必须使用 Ionic 的 File 原生实现。使用 File 为我产生错误。

import {Injectable} from '@angular/core';
import {GoogleSpeechToText} from '@google-cloud/speech';
import {Observable} from 'rxjs';
import {Idea} from './idea.service';
import {File} from '@ionic-native/file/ngx';


@Injectable({
    providedIn: 'root'
})
export class GoogleSpeechToTextService {

    private ideas: Observable<Idea[]>;
    constructor(private GoogleSpeech: GoogleSpeechToText, private MyFile: File) {
    }

    async SpeechClient() {
        // @ts-ignore
        const client = new this.SpeechClient();
        const filename = './resources/audio/audio.raw';

        const file = File.readFileSync(filename);

        const audioBytes = file.toString('base64');
        const audio = {
            content: audioBytes
        };

        const config = {
            encoding: 'LINEAR16',
            sampleRateHertz: 1600,
            languageCode: 'en-US'
        };

        // @ts-ignore
        // @ts-ignore
        const request = {
            audio,
          config,
        };
        const [response] = await client.recognize(request);
        const transcription = response.results.map(result => result.alternative[0].transcript).join('\n');

    }
}

ERROR in ./node_modules/google-gax/build/src/operationsClient.js
Module not found: Error: Can't resolve './operations_client_config' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\google-gax\build\src'
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\google-auth-library\build\src\auth'
ERROR in ./node_modules/google-auth-library/build/src/auth/googleauth.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\google-auth-library\build\src\auth'
ERROR in ./node_modules/google-p12-pem/build/src/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\google-p12-pem\build\src'
ERROR in ./node_modules/gtoken/build/src/index.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\gtoken\build\src'
ERROR in ./node_modules/request/lib/har.js
Module not found: Error: Can't resolve 'fs' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\request\lib'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\forever-agent'
ERROR in ./node_modules/gaxios/node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\gaxios\node_modules\https-proxy-agent'
ERROR in ./node_modules/http-proxy-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\http-proxy-agent'
ERROR in ./node_modules/teeny-request/node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\teeny-request\node_modules\https-proxy-agent'
ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\tough-cookie\lib'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'net' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\tunnel-agent'
ERROR in ./node_modules/forever-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\forever-agent'
ERROR in ./node_modules/gaxios/node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\gaxios\node_modules\https-proxy-agent'
ERROR in ./node_modules/http-proxy-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\http-proxy-agent'
ERROR in ./node_modules/teeny-request/node_modules/https-proxy-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\teeny-request\node_modules\https-proxy-agent'
ERROR in ./node_modules/tunnel-agent/index.js
Module not found: Error: Can't resolve 'tls' in 'C:\Users\gdemay\Documents\Mobile-Hybrid2\node_modules\tunnel-agent'
i 「wdm」: Failed to compile.

    ERROR in src/app/services/google-speech-to-text.service.ts(22,27): error TS2339: Property 'readFileSync' does not exist on type 'typeof File'.


4

2 回答 2

1

部分对我有用的解决方案是去/node_modules/google_gax/build/src/operationsClient.js里面,我重命名

const configData = require('./operations_client_config');

const configData = require('./operations_client_config.json');

希望它可以帮助某人。

编辑:

如果有人使用 webpack 并遇到问题,请转到 webpack.config.js 并添加:

resolve: { extensions: [ '.json' ] }

于 2020-01-29T14:30:46.657 回答
1

我的理解是,不幸的是你不能。NodeJS 中的语音转文本 API 使用后端技术。

您需要使用Obvi在 API 上创建一个包装器。

我决定进一步玩这个,我设法通过创建一个网络套接字服务器来让它工作,然后将音频从 AudioContext 传输到网络套接字服务器,然后使用 Google Cloud Speech to Text API 客户端库,我能够将音频流式传输到谷歌和临时响应返回到浏览器。

于 2019-11-26T17:00:47.573 回答