마음의 안정을 찾기 위하여 - [Delphi] Pocketsphinx 라이브러리를 이용한 Speech To Text
2495953
63
481
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
[Delphi] Pocketsphinx 라이브러리를 이용한 Speech To Text
Delphi/컴퍼넌트, 솔루션 소개 | 2024/05/23 23:41

  1. Pocketsphinx 라이브러리 다운로드 및 설정
  2. Delphi에서 사용할 수 있도록 Pocketsphinx를 DLL로 래핑
  3. Delphi에서 DLL을 호출하여 음성 인식 수행

1. Pocketsphinx 라이브러리 다운로드 및 설정

Pocketsphinx와 그 의존성을 다운로드합니다.
한국어 음성 인식 모델을 다운로드합니다.
Pocketsphinx 및 필요한 언어 모델을 다운로드하는 링크:

Pocketsphinx
한국어 모델


2. Pocketsphinx를 DLL로 래핑

Pocketsphinx는 C 라이브러리이므로, Delphi에서 사용하려면 이를 DLL로 컴파일해야 합니다. 다음은 간단한 C 코드를 통해 Pocketsphinx를 DLL로 래핑하는 예제입니다.

// pocketsphinx_wrapper.c
#include <pocketsphinx.h>

__declspec(dllexport) const char* recognize_from_file(const char* model_path, const char* dict_path, const char* lm_path, const char* wav_path) {
    ps_decoder_t *ps;
    cmd_ln_t *config;
    FILE *fh;
    char const *hyp;
    int16 buf[512];
    int rv;
    int32 score;

    config = cmd_ln_init(NULL, ps_args(), TRUE,
                         "-hmm", model_path,
                         "-dict", dict_path,
                         "-lm", lm_path,
                         NULL);
    ps = ps_init(config);

    fh = fopen(wav_path, "rb");
    if (fh == NULL) {
        return "Failed to open WAV file.";
    }

    rv = ps_start_utt(ps);

    while (!feof(fh)) {
        size_t nsamp;
        nsamp = fread(buf, 2, 512, fh);
        rv = ps_process_raw(ps, buf, nsamp, FALSE, FALSE);
    }

    rv = ps_end_utt(ps);
    hyp = ps_get_hyp(ps, &score);

    fclose(fh);
    ps_free(ps);
    cmd_ln_free_r(config);

    return hyp;
}



이 코드를 컴파일하여 pocketsphinx_wrapper.dll 파일을 생성합니다. 이를 위해 필요한 빌드 도구를 사용합니다.

3. Delphi에서 DLL 호출

Delphi에서 위 DLL을 호출하여 음성 인식을 수행하는 예제 코드입니다.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TRecognizeFromFile = function(modelPath, dictPath, lmPath, wavPath: PAnsiChar): PAnsiChar; stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
  DLLHandle: THandle;
  RecognizeFromFile: TRecognizeFromFile;
  Result: PAnsiChar;
  ModelPath, DictPath, LMPath, WavPath: AnsiString;
begin
  if OpenDialog1.Execute then
  begin
    WavPath := AnsiString(OpenDialog1.FileName);
    ModelPath := 'path\to\model';
    DictPath := 'path\to\dict';
    LMPath := 'path\to\language\model';

    DLLHandle := LoadLibrary('pocketsphinx_wrapper.dll');
    if DLLHandle <> 0 then
    begin
      @RecognizeFromFile := GetProcAddress(DLLHandle, 'recognize_from_file');
      if Assigned(RecognizeFromFile) then
      begin
        Result := RecognizeFromFile(PAnsiChar(ModelPath), PAnsiChar(DictPath), PAnsiChar(LMPath), PAnsiChar(WavPath));
        Memo1.Lines.Add(string(Result));
      end
      else
        ShowMessage('Failed to get recognize_from_file function address');
      FreeLibrary(DLLHandle);
    end
    else
      ShowMessage('Failed to load DLL');
  end;
end;

end.



[참고 자료]
Pocketsphinx 공식 문서
Delphi에서 DLL 호출 예제
이 예제는 Pocketsphinx를 사용하여 한글 음성을 텍스트로 변환하는 기본적인 과정을 설명합니다. Pocketsphinx 설정 및 모델 파일 경로는 실제 환경에 맞게 조정해야 합니다.
2024/05/23 23:41 2024/05/23 23:41
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [5][6][7][8][9][10][11][12][13] ... [1327] :
«   2025/04   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      
전체 (1327)
출판 준비 (0)
My-Pro... (41)
사는 ... (933)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (93)
C Builder (0)
Object... (0)
VC, MF... (10)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (6)
Database (12)
리눅스 (29)
Windows (26)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (22)
잡다한... (7)
프로젝트 (0)
Personal (0)
대통령... (13)
Link (2)