我如何将文本从语音直接发送到另一个端点或来自我的语音资源的Azure函数? 我正在使用Azure语音服务资源使用Microsoft-CognitiveServices-Speech-SDK从我的MIC转录实时音频。我想将抄录的文本发送到另一个端点(或AZ ...

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

i尝试了以下节点。JS代码将语音转换为文本使用

microsoft-cognitiveservices-speech-sdk

。我使用麦克风录制了音频,将其保存为.wav文件,然后通过javascript中的HTTP触发的Azure函数将抄录的文本发送到另一个端点。

Index.js:

azure speech-recognition speech-to-text azure-speech microsoft-speech-api
1个回答
0
投票
const fs = require('fs'); const sdk = require("microsoft-cognitiveservices-speech-sdk"); const axios = require('axios'); const speechKey = "<speechKey>"; const speechRegion = "<speechRegion>"; const speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, speechRegion); speechConfig.speechRecognitionLanguage = "en-US"; const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("kamsp.wav")); const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig); const functionEndpoint = "http://localhost:7071/api/ProcessSpeech"; speechRecognizer.recognized = async (s, e) => { if (e.result.reason === sdk.ResultReason.RecognizedSpeech) { console.log(`RECOGNIZED: Text=${e.result.text}`); try { await axios.post(functionEndpoint, { text: e.result.text }, { headers: { 'Content-Type': 'application/json' } }); console.log("Transcription sent to function."); } catch (error) { console.error("Error sending transcription:", error); } } }; speechRecognizer.sessionStopped = (s, e) => { speechRecognizer.stopContinuousRecognitionAsync(); }; speechRecognizer.startContinuousRecognitionAsync();

Httptrigger1.js:

const { app } = require('@azure/functions'); let latestTranscription = ""; app.http('processSpeech', { methods: ['GET', 'POST'], authLevel: 'anonymous', handler: async (request, context) => { context.log("Speech-to-text function triggered"); if (request.method === "POST") { try { const requestBody = await request.json(); const text = requestBody.text; if (text) { latestTranscription = text; context.log(`Received transcription: ${text}`); return { body: "Transcription received", status: 200 }; } return { body: "No transcription received", status: 400 }; } catch (error) { context.log(`Error: ${error}`); return { body: "Error processing transcription", status: 500 }; } } else if (request.method === "GET") { return { body: `Latest Transcription: ${latestTranscription}`, status: 200 }; } } });

NODEJS输出:

HPPT触发功能输出:

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.