|
| 1 | +{ Copyright (c) 2025 Xiaomi Corporation } |
| 2 | + |
| 3 | +{ |
| 4 | +This file shows how to use a non-streaming Zipformer CTC model |
| 5 | +to decode files. |
| 6 | +
|
| 7 | +You can download the model files from |
| 8 | +https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models |
| 9 | +} |
| 10 | + |
| 11 | +program zipformer_ctc; |
| 12 | + |
| 13 | +{$mode objfpc} |
| 14 | + |
| 15 | +uses |
| 16 | + sherpa_onnx, |
| 17 | + DateUtils, |
| 18 | + SysUtils; |
| 19 | + |
| 20 | +var |
| 21 | + Wave: TSherpaOnnxWave; |
| 22 | + WaveFilename: AnsiString; |
| 23 | + |
| 24 | + Config: TSherpaOnnxOfflineRecognizerConfig; |
| 25 | + Recognizer: TSherpaOnnxOfflineRecognizer; |
| 26 | + Stream: TSherpaOnnxOfflineStream; |
| 27 | + RecognitionResult: TSherpaOnnxOfflineRecognizerResult; |
| 28 | + |
| 29 | + Start: TDateTime; |
| 30 | + Stop: TDateTime; |
| 31 | + |
| 32 | + Elapsed: Single; |
| 33 | + Duration: Single; |
| 34 | + RealTimeFactor: Single; |
| 35 | +begin |
| 36 | + Initialize(Config); |
| 37 | + |
| 38 | + Config.ModelConfig.ZipformerCtC.Model := './sherpa-onnx-zipformer-ctc-zh-int8-2025-07-03/model.int8.onnx'; |
| 39 | + Config.ModelConfig.Tokens := './sherpa-onnx-zipformer-ctc-zh-int8-2025-07-03/tokens.txt'; |
| 40 | + Config.ModelConfig.Provider := 'cpu'; |
| 41 | + Config.ModelConfig.NumThreads := 1; |
| 42 | + Config.ModelConfig.Debug := False; |
| 43 | + |
| 44 | + WaveFilename := './sherpa-onnx-zipformer-ctc-zh-int8-2025-07-03/test_wavs/0.wav'; |
| 45 | + |
| 46 | + Wave := SherpaOnnxReadWave(WaveFilename); |
| 47 | + |
| 48 | + Recognizer := TSherpaOnnxOfflineRecognizer.Create(Config); |
| 49 | + Stream := Recognizer.CreateStream(); |
| 50 | + Start := Now; |
| 51 | + |
| 52 | + Stream.AcceptWaveform(Wave.Samples, Wave.SampleRate); |
| 53 | + Recognizer.Decode(Stream); |
| 54 | + |
| 55 | + RecognitionResult := Recognizer.GetResult(Stream); |
| 56 | + |
| 57 | + Stop := Now; |
| 58 | + |
| 59 | + Elapsed := MilliSecondsBetween(Stop, Start) / 1000; |
| 60 | + Duration := Length(Wave.Samples) / Wave.SampleRate; |
| 61 | + RealTimeFactor := Elapsed / Duration; |
| 62 | + |
| 63 | + WriteLn(RecognitionResult.ToString); |
| 64 | + WriteLn(Format('NumThreads %d', [Config.ModelConfig.NumThreads])); |
| 65 | + WriteLn(Format('Elapsed %.3f s', [Elapsed])); |
| 66 | + WriteLn(Format('Wave duration %.3f s', [Duration])); |
| 67 | + WriteLn(Format('RTF = %.3f/%.3f = %.3f', [Elapsed, Duration, RealTimeFactor])); |
| 68 | + |
| 69 | + {Free resources to avoid memory leak. |
| 70 | +
|
| 71 | + Note: You don't need to invoke them for this simple script. |
| 72 | + However, you have to invoke them in your own large/complex project. |
| 73 | + } |
| 74 | + FreeAndNil(Stream); |
| 75 | + FreeAndNil(Recognizer); |
| 76 | +end. |
0 commit comments