为语音听录配置语言识别和分割聚类

本指南演示了跨实时语音转文本、语音翻译、快速听录和批量听录的语言识别(LID)和说话人分割的单一配置方法。 它重点介绍两种常见的故障模式:

  • 依赖于默认语言检测而不限制候选区域设置。
  • 期望在未显式启用说话人分离的情况下获得说话人标签。

先决条件

  • 用于语音的 Azure AI 服务
  • 支持的语音区域
  • 对于 SDK 示例,可使用以下受支持的 SDK 配置之一:
  • 测试能够反映实际生产条件的音频,包括:
    • 简短陈述。
    • 背景噪音。
    • 预期说话人数。

在配置请求之前检查支持的区域设置

在设置candidateLocaleslocales或固定的源区域设置之前,请先使用这些区域设置列表:

对于使用多语言模型进行快速转录的情况(locales 为空或省略时),当前支持的输入区域设置为:

  • de-DEen-AUen-CAen-GBen-INen-USes-ESes-MXfr-CAfr-FRit-ITja-JPko-KRpt-BRzh-cn

如果音频区域设置不在该集合中,请提供一个固定的受支持区域设置。

为实时语音转文本或语音翻译配置语言标识(SDK)

有关使用实时语音转文本和语音翻译的完整 SDK 覆盖范围 AutoDetectSourceLanguageConfig (包括所有受支持的语言、开始模式与连续模式和自定义模型映射)请参阅 “实现语言识别”。

将 LID 与分割或脱机听录相结合时的要点包括:

  • 始终提供 candidateLocaleslocales 列表。 如果没有候选列表,后端将使用覆盖范围更广的模型,而这种模型在较短或含噪声的片段上表现较差。
  • 每种基础语言不要包含多个区域设置(例如,使用 en-USen-GB,不要同时使用两者)。
  • 如果 AutoDetectSourceLanguageResult.Language 在短片段中返回空值或异常值,请绕过自动检测,改为使用固定的 SpeechRecognitionLanguage

对于语音翻译,请从识别结果中的 PropertyId.SpeechServiceConnection_AutoDetectSourceLanguageResult 读取检测到的语言,而不是从 SpeechRecognitionLanguage 读取。 后者是必需的占位符,但在启用自动检测时,该服务目前会忽略它。

配置实时分割(SDK)

默认情况下,未启用实时说话人分离。 使用 ConversationTranscriber,并在事件中验证 SpeakerId

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using Microsoft.CognitiveServices.Speech.Transcription;

string endpoint = Environment.GetEnvironmentVariable("ENDPOINT");
string key = Environment.GetEnvironmentVariable("SPEECH_KEY");

var speechConfig = SpeechConfig.FromEndpoint(new Uri(endpoint), key);
speechConfig.SpeechRecognitionLanguage = "en-US";
speechConfig.SetProperty(
    PropertyId.SpeechServiceResponse_DiarizeIntermediateResults,
    "true");

using var audioConfig = AudioConfig.FromWavFileInput("meeting.wav");
using var transcriber = new ConversationTranscriber(speechConfig, audioConfig);

transcriber.Transcribing += (s, e) =>
{
    Console.WriteLine($"TRANSCRIBING: SpeakerId={e.Result.SpeakerId} Text={e.Result.Text}");
};

transcriber.Transcribed += (s, e) =>
{
    Console.WriteLine($"TRANSCRIBED: SpeakerId={e.Result.SpeakerId} Text={e.Result.Text}");
};

await transcriber.StartTranscribingAsync();
Console.ReadKey();
await transcriber.StopTranscribingAsync();

参考:ConversationTranscriberSpeechServiceResponse_DiarizeIntermediateResults

配置快速听录(REST 和 SDK)

对于快速听录,请同时显式设置语言和分割聚类行为。

REST 示例

curl --location "https://YourResourceName.cognitiveservices.azure.cn/speechtotext/transcriptions:transcribe?api-version=2025-10-15" \
  --header "Ocp-Apim-Subscription-Key: YourSpeechResourceKey" \
  --form 'audio=@"call.wav"' \
  --form 'definition={
    "locales": ["en-US", "es-ES"],
    "diarization": {
      "enabled": true,
      "maxSpeakers": 4
    }
  }'

参考:转录 - 转录

Python SDK 示例

from azure.core.credentials import AzureKeyCredential
from azure.ai.transcription import TranscriptionClient
from azure.ai.transcription.models import (
    TranscriptionContent,
    TranscriptionOptions,
    TranscriptionDiarizationOptions,
)

endpoint = "https://<your-resource>.cognitiveservices.azure.cn/"
api_key = "<your-key>"

client = TranscriptionClient(endpoint=endpoint, credential=AzureKeyCredential(api_key))

with open("call.wav", "rb") as audio_file:
    options = TranscriptionOptions(
        locales=["en-US", "es-ES"],
        diarization_options=TranscriptionDiarizationOptions(max_speakers=4),
    )
    result = client.transcribe(TranscriptionContent(definition=options, audio=audio_file))

    for phrase in result.phrases:
        print(f"locale={phrase.locale}, speaker={phrase.speaker}, text={phrase.text}")

参考:TranscriptionClientTranscriptionOptions、TranscriptionDiarizationOptions

配置批量转录(REST)

对于批处理作业,请显式设置所有三个项目:

  • locale 在未检测到任何语言时的回退选项。
  • properties.languageIdentification.candidateLocales 适用于 LID 范围。
  • 用于说话人标签的 properties.diarization.enabledproperties.diarization.maxSpeakers
{
  "displayName": "Batch LID + diarization",
  "locale": "en-US",
  "contentUrls": [
    "https://contoso.example/audio1.wav",
    "https://contoso.example/audio2.wav"
  ],
  "properties": {
    "languageIdentification": {
      "candidateLocales": ["en-US", "es-ES", "fr-FR"],
      "mode": "Single"
    },
    "diarization": {
      "enabled": true,
      "maxSpeakers": 4
    },
    "timeToLiveHours": 48
  }
}

使用以下内容提交该负载:

POST /speechtotext/transcriptions:submit?api-version=2025-10-15

参考:转写 - 提交

执行预检核对清单

在启用生产流量之前使用此清单:

  1. 验证区域设置是否受支持。
    • 确认 candidateLocales(或 locales)中的每个区域设置都出现在受支持的区域设置列表中。
  2. 验证语言检测字段。
    • 实时 SDK:确认 AutoDetectSourceLanguageResult (或属性 SpeechServiceConnection_AutoDetectSourceLanguageResult)已填充。
    • 快速或批量转写:确认每个短语都包含预期的 locale 值。
  3. 验证说话人标签字段。
    • 实时分割聚类:确认正在转录或已完成转录的事件中显示了 SpeakerIdspeakerId
    • 快速或批量转录:确认已存在短语级别的 speaker 值。
  4. 验证短音频或带噪音音频的表现。
    • 运行干扰示例集,并将检测到的语言与预期语言进行比较。
    • 如果检测不稳定,请改用固定的源区域设置重新运行,而不是使用自动检测。
  5. 验证说话人数量优化效果。
    • maxSpeakers 设置为切合实际的参与者上限。
    • 在降低或提高该值时,重新检查扬声器标签质量是否提高。