>  기사  >  백엔드 개발  >  PHP에서 음성 인식을 구현하는 방법

PHP에서 음성 인식을 구현하는 방법

WBOY
WBOY원래의
2023-06-11 22:24:431413검색

인공지능 기술이 지속적으로 발전하면서 음성인식 기술도 점점 대중화되고 있습니다. 웹 개발에서는 음성 인식 구현도 중요한 작업이 되었습니다.

PHP는 웹 개발 분야에서 널리 사용되는 언어로 음성 인식도 구현할 수 있습니다. 이번 글에서는 PHP에서 음성인식을 구현하는 방법을 소개하겠습니다.

  1. Baidu 음성 인식 API 사용

Baidu 음성 인식 API는 현재 가장 성숙하고 인기 있는 음성 인식 API 중 하나입니다. 음성인식 기능은 Baidu Speech Recognition API를 이용하여 쉽게 구현할 수 있습니다.

먼저 Baidu 개발자 플랫폼에 애플리케이션을 등록하고 생성한 후 해당 애플리케이션의 앱 ID와 앱 키를 받아야 합니다.

그런 다음 PHP를 사용하여 음성 파일을 Baidu Speech Recognition API로 전송하기 위한 POST 요청을 보내야 합니다. 다음은 샘플 코드입니다.

//语音文件路径
$audio_file = "audio.pcm";

//将语音文件读取为字符串
$file_content = file_get_contents($audio_file);

//设置POST请求的header
$header = array(
    "Content-Type: audio/pcm;rate=8000",
    "Content-Length: " . strlen($file_content),
    "Referer: http://yuyin.baidu.com/"
);

//构造POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://vop.baidu.com/server_api");
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_content);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

//设置App ID和App Key
$app_id = "your_app_id";
$app_key = "your_app_key";
curl_setopt($ch, CURLOPT_USERPWD, $app_id . ":" . $app_key);

//发送POST请求
$response = curl_exec($ch);

//解析返回结果
$result = json_decode($response, true);
if (isset($result['result'][0])) {
    echo $result['result'][0];
}
else {
    echo "识别失败";
}
  1. Google Speech Recognition API 사용

Baidu Speech Recognition API 외에도 Google Speech Recognition API는 매우 성숙하고 널리 사용되는 음성 인식 API입니다. 음성 인식 기능은 Google Speech Recognition API를 사용하여 쉽게 구현할 수도 있습니다.

먼저 Google Cloud Console에서 프로젝트를 만들고 Google Cloud Speech-to-Text API를 활성화해야 합니다.

그런 다음 PHP를 사용하여 음성 파일을 Google Speech Recognition API로 전송하는 POST 요청을 보내야 합니다. 다음은 샘플 코드입니다.

//语音文件路径
$audio_file = "audio.flac";

//将语音文件读取为字符串
$file_content = file_get_contents($audio_file);

//构造POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://speech.googleapis.com/v1/speech:recognize");
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);

//设置API密钥
$api_key = "your_api_key";
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
                                           "Authorization: Bearer ".$api_key));

//设置请求体
$request_data = array(
    "config" => array(
        "encoding" => "FLAC",
        "sampleRateHertz" => 16000,
        "languageCode" => "zh-CN",
    ),
    "audio" => array(
        "content" => base64_encode($file_content),
    ),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data));

//发送POST请求
$response = curl_exec($ch);

//解析返回结果
$result = json_decode($response, true);
if (isset($result['results'][0]['alternatives'][0]['transcript'])) {
    echo $result['results'][0]['alternatives'][0]['transcript'];
}
else {
    echo "识别失败";
}

Summary

위는 PHP에서 음성 인식을 구현하는 두 가지 방법, 즉 Baidu Speech Recognition API를 사용하는 것과 Google Speech Recognition API를 사용하는 것입니다. 이 두 가지 방법은 음성 인식을 구현하는 데 상대적으로 성숙하고 널리 사용됩니다. 어떤 방법을 선택할지는 실제 필요와 개인 선호도에 따라 선택할 수 있습니다.

위 내용은 PHP에서 음성 인식을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.