日本語音声認識サンプル
準備
- 開発環境
- マイク。無ければ安いので購入してもいいかも。私はこんな感じのを使用してます。600〜1000円程度です。
- 「コントロールパネル」→「音声認識」(Win2000では「スピーチ」)→「音声認識」タブの「言語」を「Microsoft Japanese Recoginizer v5.1」に設定。
サンプルソース
このサンプルは、SAPI5.1の日本語エンジンを使用したサンプルがないかググったところ、ようやく発見した唯一ページを参考に作りました。グラマーファイルの作り方など詳しく解説されてます。
VB.NETで音声認識(その1:音声認識ライブラリを作る)ここには、上記サイトでVBを使って作成されているのをDelphiで書き直したものを載せています。
フォーム
ソース
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, SpeechLib_TLB, StdCtrls, ActiveX;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
RecoEnabledCheckBox: TCheckBox;
procedure RecoEnabledCheckBoxClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private 宣言 }
RecoContext: TSpSharedRecoContext;
Grammar: ISpeechRecoGrammar;
FRecoEnabled: Boolean;
procedure SetRecoEnabled(const Value: Boolean);
public
{ Public 宣言 }
procedure RecoContextFalseRecognition(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
const Result: ISpeechRecoResult);
procedure RecoContextHypothesis(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
const Result: ISpeechRecoResult);
procedure RecoContextRecognition(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
RecognitionType: TOleEnum; const Result: ISpeechRecoResult);
property RecoEnabled: Boolean read FRecoEnabled write SetRecoEnabled;
procedure New(FNM: string);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.New(FNM: string);
begin
//Grammerファイルがあるかどうか調べる
if not FileExists(FNM) then begin
MessageDlg('ファイルが見つかりません', mtInformation, [mbOK], 0);
Exit;
end;
//認識エンジンのイニシャライズ
try
//Grammar(文法)オブジェクト作成
Grammar := RecoContext.CreateGrammar(1);
//Dictaion(文章認識)は使わないのでInActiveに設定
Grammar.DictationLoad('', SLOStatic);
Grammar.DictationSetState(SGDSInactive);
//単語認識のグラマーファイルを読み込む
Grammar.CmdLoadFromFile(FNM, SLODynamic);
//単語認識を開始
Grammar.CmdSetRuleIdState(0, SGDSActive);
except
on E:Exception do MessageDlg('InitSpeech' + #13#10 + E.Message ,
mtInformation, [mbOK], 0);
end;
end;
procedure TForm1.SetRecoEnabled(const Value: Boolean);
begin
FRecoEnabled := Value;
if FRecoEnabled then
Grammar.CmdSetRuleIdState(0, SGDSActive)
else
Grammar.CmdSetRuleIdState(0, SGDSInactive);
end;
procedure TForm1.RecoContextFalseRecognition(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
const Result: ISpeechRecoResult);
begin
//認識失敗
end;
procedure TForm1.RecoContextHypothesis(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
const Result: ISpeechRecoResult);
begin
//仮認識
end;
procedure TForm1.RecoContextRecognition(ASender: TObject;
StreamNumber: Integer; StreamPosition: OleVariant;
RecognitionType: TOleEnum; const Result: ISpeechRecoResult);
var
RecCount: Integer;
Props: ISpeechPhraseProperties;
begin
Label3.Caption := '';
Label5.Caption := '';
Label7.Caption := '';
RecCount := Result.PhraseInfo.Properties.Count;
Label1.Caption := Result.PhraseInfo.Rule.Children.Item(0).Name;
Props := Result.PhraseInfo.Properties;
Label2.Caption := IntToStr(Props.Item(0).Value);
Label4.Caption := Result.PhraseInfo.GetText(0, 1, True);
Label6.Caption := Props.Item(0).Name;
if RecCount > 1 then begin
Label3.Caption := IntToStr(Props.Item(1).Value);
Label5.Caption := Result.PhraseInfo.GetText(1, 1, True);
Label7.Caption := Props.Item(1).Name;
end;
end;
procedure TForm1.RecoEnabledCheckBoxClick(Sender: TObject);
begin
RecoEnabled := RecoEnabledCheckBox.Checked;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
RecoContext := TSpSharedRecoContext.Create(Self);
RecoContext.OnRecognition := RecoContextRecognition;
RecoContext.OnFalseRecognition := RecoContextFalseRecognition;
RecoContext.OnHypothesis := RecoContextHypothesis;
New('SpeechGrammer.xml');
end;
end.
