简体   繁体   English

在PHP中使用Google Speech Recognition API

[英]Using Google Speech Recognition API with PHP

I want to make a simple script that uses google speech recognition, I have tried various ways so that I can access the API with Request Body, but has always failed. 我想制作一个使用Google语音识别的简单脚本,我尝试了各种方法以便可以使用Request Body访问API,但始终失败。

Example URL: https://content-speech.googleapis.com/v1/speech:recognize?prettyPrint=true&alt=json&key=AIzaSyD-XXXXXXXXXXXXXXXXX-Al7hLQDbugrDcw 范例网址: https : //content-speech.googleapis.com/v1/speech : recognize?prettyPrint=true&alt=json&key=AIzaSyD-XXXXXXXXXXXXXXXXXXXX-Al7hLQDbugrDcw

JSON example: JSON示例:

{
"audio": {
    "content": "ZkxhQwAAACIEg....more......FzSi6SngVApeE="
  },
  "config": {
    "encoding": "FLAC",
    "sampleRateHertz": 16000,
    "languageCode": "en-US"
  }
}

Documentation: https://cloud.google.com/speech-to-text/docs/basics 文档: https : //cloud.google.com/speech-to-text/docs/basics

Code samples: https://cloud.google.com/speech-to-text/docs/samples 代码示例: https : //cloud.google.com/speech-to-text/docs/samples

Consider using a library: 考虑使用库:

Google Cloud Client Library PHP (also has code samples): https://googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.61.0/speech/speechclient Google Cloud Client Library PHP(也有代码示例): https : //googlecloudplatform.github.io/google-cloud-php/#/docs/google-cloud/v0.61.0/speech/speechclient

Speech-to-Text Client Libraries https://cloud.google.com/speech-to-text/docs/reference/libraries#client-libraries-install-php 语音转文字客户端库https://cloud.google.com/speech-to-text/docs/reference/libraries#client-libraries-install-php

Speaker is a 3.party library you could use: https://github.com/duncan3dc/speaker 演讲者是您可以使用的3.party库: https : //github.com/duncan3dc/speaker

Quick example from google/cloud-speech: 来自google / cloud-speech的快速示例:

Install: 安装:

composer require google/cloud-speech

Sample: 样品:

  # Includes the autoloader for libraries installed with composer
require __DIR__ . '/vendor/autoload.php';

# Imports the Google Cloud client library
use Google\Cloud\Speech\SpeechClient;

# Your Google Cloud Platform project ID
$projectId = 'YOUR_PROJECT_ID';

# Instantiates a client
$speech = new SpeechClient([
    'projectId' => $projectId,
    'languageCode' => 'en-US',
]);

# The name of the audio file to transcribe
$fileName = __DIR__ . '/resources/audio.raw';

# The audio file's encoding and sample rate
$options = [
    'encoding' => 'LINEAR16',
    'sampleRateHertz' => 16000,
];

# Detects speech in the audio file
$results = $speech->recognize(fopen($fileName, 'r'), $options);

foreach ($results as $result) {
    echo 'Transcription: ' . $result->alternatives()[0]['transcript'] . PHP_EOL;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM