我在Google Cloud Client库php中使用SpeechRecoginition,流式传输吗?
我正在为Google Cloud Client库PHP使用SpeechRecoginition。
如有示例,请参阅此文档。
我们已经测试并确认了本地文件的正常操作。
但是我想通过实时发送语音文件而不是存储的文件来进行语音认可。
我想使用Mediarecorder JS将数据传递给PHP,分析来自PHP的声音,然后将其传递给客户端。
这是 document 我引用了关于Mediarecord JS。
[client.js]
var mediaRecorder;
navigator.mediaDevices.getUserMedia(constraints).then(stream=>{
var options = {
type: 'video',
mimeType: 'video/webm'
};
try{
mediaRecorder.stop()
}catch(e){}
mediaRecorder=null;
let order = 0;
mediaRecorder = new MediaRecorder(stream,options);
mediaRecorder.ondataavailable =function(e) {
if(e.data&&e.data.size>0) {
recordedChunks.push(event.data);
var reader = new FileReader();
reader.readAsArrayBuffer(e.data);
reader.onloadend = async function(event) {
let arrayBuffer = reader.result;
let uint8View = new Uint8Array(arrayBuffer);
let response = await fetch('server.php', {
method: 'POST',
body: JSON.stringify({
chunk: uint8View,
order: order
})
});
order += 1;
}
}
}
mediaRecorder.start(250);
})
[server.php]
<?php
require($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
$speechClient = new Google\Cloud\Speech\V1\SpeechClient();
try {
$recognitionConfig = new RecognitionConfig();
$recognitionConfig->setEncoding(AudioEncoding::FLAC);
$recognitionConfig->setSampleRateHertz(44100);
$recognitionConfig->setLanguageCode('ko-KR');
$config = new StreamingRecognitionConfig();
$config->setConfig($recognitionConfig);
//$audioResource = fopen('localfile path', 'r'); //original
//receive DATA
$chunk = $_POST['chunk'];
$binarydata = pack("C*", ...$chunk);
$audioResource = $binarydata;
$responses = $speechClient->recognizeAudioStream($config, $audioResource);
foreach ($responses as $element) {
//recognition result send client
}
} catch(Exception $e) {
var_dump($e);
}
我想从[client.js]接收视频数据,并在[server.php]中实时分析音频。
在中国订单而不是本地文件分析中创建的数组类型数据需要SecedRecogniton。
请指教。
I am using SpeechRecoginition for google cloud client library PHP.
For examples, refer to this document.
We have tested and confirmed the normal operation with local files.
But I want to SpeechRecognition by sending a voice file in real time, not a stored file.
I want to pass data to PHP using MediaRecorder js, analyze the voice coming from PHP, and deliver it back to the client.
This is the document I referenced regarding MediaRecord js.
[client.js]
var mediaRecorder;
navigator.mediaDevices.getUserMedia(constraints).then(stream=>{
var options = {
type: 'video',
mimeType: 'video/webm'
};
try{
mediaRecorder.stop()
}catch(e){}
mediaRecorder=null;
let order = 0;
mediaRecorder = new MediaRecorder(stream,options);
mediaRecorder.ondataavailable =function(e) {
if(e.data&&e.data.size>0) {
recordedChunks.push(event.data);
var reader = new FileReader();
reader.readAsArrayBuffer(e.data);
reader.onloadend = async function(event) {
let arrayBuffer = reader.result;
let uint8View = new Uint8Array(arrayBuffer);
let response = await fetch('server.php', {
method: 'POST',
body: JSON.stringify({
chunk: uint8View,
order: order
})
});
order += 1;
}
}
}
mediaRecorder.start(250);
})
[server.php]
<?php
require($_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php');
use Google\Cloud\Speech\V1\RecognitionConfig\AudioEncoding;
use Google\Cloud\Speech\V1\RecognitionConfig;
use Google\Cloud\Speech\V1\StreamingRecognitionConfig;
$speechClient = new Google\Cloud\Speech\V1\SpeechClient();
try {
$recognitionConfig = new RecognitionConfig();
$recognitionConfig->setEncoding(AudioEncoding::FLAC);
$recognitionConfig->setSampleRateHertz(44100);
$recognitionConfig->setLanguageCode('ko-KR');
$config = new StreamingRecognitionConfig();
$config->setConfig($recognitionConfig);
//$audioResource = fopen('localfile path', 'r'); //original
//receive DATA
$chunk = $_POST['chunk'];
$binarydata = pack("C*", ...$chunk);
$audioResource = $binarydata;
$responses = $speechClient->recognizeAudioStream($config, $audioResource);
foreach ($responses as $element) {
//recognition result send client
}
} catch(Exception $e) {
var_dump($e);
}
I want to receive video data from [client.js] and analyze the audio in real time in [server.php].
SpeechRecogniton is needed for arrayBuffer type data created in mediaRecorder rather than local file analysis.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论