Swift3中的语音到文本转换

时间:2016-12-23 06:57:55

标签: ios swift swift3 voice-recognition speech-to-text

目前我正在开发一个项目,它必须将用户语音作为输入,并且必须实时转换为文本。我在JAVA Sphinx中尝试了很多示例项目,但我正在努力编写语法文件。因此,如果在SWIFT3中有任何可能的方法,请帮助我吗?

3 个答案:

答案 0 :(得分:2)

现在,从iOS10开始,Apple为iOS开发人员提供了此问题的最佳解决方案。现在,您可以将应用与SiriKItTutoiral here集成。管理文本到语音识别的所有Siri's责任以及使用该工具包的优点是,

  1. 随着iOS版本的更新,它变得越来越强大,一旦集成了它,就无需更改代码工作

  2. 与使用第三方相比,您的代码行也少了。

  3. 您不需要像处理第三部分一样管理套件,它负责管理SiriKit的所有内容。

答案 1 :(得分:2)

以下是如何使用SFSpeechRecognizer将语音转换为文字

的示例

首先import Speech文件中的.swift框架。

然后像这样回复代理SFSpeechRecognizerDelegate

public class ViewController: UIViewController, SFSpeechRecognizerDelegate {

然后声明以下属性

private let speechRecognizer = SFSpeechRecognizer(locale: Locale(identifier: "en-US"))!

private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?

private var recognitionTask: SFSpeechRecognitionTask?

private let audioEngine = AVAudioEngine()

完成此操作后,请确保您有权访问Speech recognition

现在使用此代码将语音转换为文本

let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)

recognitionRequest = SFSpeechAudioBufferRecognitionRequest()

guard let inputNode = audioEngine.inputNode else { fatalError("Audio engine has no input node") }
guard let recognitionRequest = recognitionRequest else { fatalError("Unable to created a SFSpeechAudioBufferRecognitionRequest object") }

// Configure request so that results are returned before audio recording is finished
recognitionRequest.shouldReportPartialResults = true

// A recognition task represents a speech recognition session.
// We keep a reference to the task so that it can be cancelled.
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest) { result, error in
    var isFinal = false

    if let result = result {

        //Here the the text of your voice
        print(result.bestTranscription.formattedString)
        isFinal = result.isFinal
    }

    if error != nil || isFinal {
        self.audioEngine.stop()
        inputNode.removeTap(onBus: 0)

        self.recognitionRequest = nil
        self.recognitionTask = nil

    }
}

let recordingFormat = inputNode.outputFormat(forBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
    self.recognitionRequest?.append(buffer)
}

audioEngine.prepare()

try audioEngine.start()

免责声明 - 该代码取自 - here

答案 2 :(得分:2)

您似乎没有对此主题进行过多次研究。

有很多方法可以达到你想要的效果......喜欢:

  • 苹果本身使用语音框架。您将获得语音框架工作herehere的教程,您还可以查看here

  • 上的框架详细信息
  • 使用OpenEars(一个用于语音识别的开源库)

希望这会对你有所帮助:)。