1. 程式人生 > >ios開發-語音識別(科大訊飛)

ios開發-語音識別(科大訊飛)

在記錄事件的時候,使用者在不方便手寫的時候,我們可以利用語音錄入,轉成文字的形式記錄時間,是不是既方便又只能,現在做語音識別的有一些不錯的開放平臺供我們使用,科大訊飛平臺,百度語音平臺。科大訊飛的優勢在於大段大段的文字識別上,準確率較高。這篇部落格也主要講的是是訊飛語音SDK的使用。下面我們詳細看一下科大訊飛。

1.科大訊飛開放平臺

第一步:申請賬號ID
建立新應用(獲得後續的appid以及開通服務)
登入到訊飛開放平臺上,在使用者選單欄裡建立應用,這裡的登入也可以採用第三方方式,在建立應用的介面填寫相關的資訊即可,然後就會有一個SDK的下載連結,,如果沒有直接去SDK選項下載即可。

第二步:匯入訊飛SDK框架

下載下來SDK解壓後有三個資料夾:doc資料夾:不用多說肯定是開發文件;重要的是接下來的那兩個資料夾:一個是lib資料夾:存放科大訊飛SDK類庫,這就是我們要匯入的SDK;一個是sample的科大訊飛demo演示工程。

下面我們建立一個工程,將lib資料夾下的”iflyMSC.framework”拷貝到工程目錄,然後在工程中新增依賴庫,如下圖所示:
這裡寫圖片描述

第三步:開始進行語音識別

語音識別分兩種,分別用在不同場合,一個是介面提示的語音識別,一個是無介面提示的語音識別,這裡以有介面提示的語音識別為例先進性講解。

3.1匯入標頭檔案

#import "iflyMSC/IFlyMSC.h"

這裡寫圖片描述

#import "IFlyContact.h"
#import "IFlyDataUploader.h"
#import "IFlyDebugLog.h"
#import "IFlyISVDelegate.h"
#import "IFlyISVRecognizer.h"
#import "IFlyRecognizerView.h"
#import "IFlyRecognizerViewDelegate.h"
#import "IFlyResourceUtil.h"
#import "IFlySetting.h"
#import "IFlySpeechConstant.h"
#import 
"IFlySpeechError.h" #import "IFlySpeechEvaluator.h" #import "IFlySpeechEvaluatorDelegate.h" #import "IFlySpeechEvent.h" #import "IFlySpeechRecognizer.h" #import "IFlySpeechRecognizerDelegate.h" #import "IFlySpeechSynthesizer.h" #import "IFlySpeechSynthesizerDelegate.h" #import "IFlySpeechUnderstander.h" #import "IFlySpeechUtility.h" #import "IFlyTextUnderstander.h" #import "IFlyUserWords.h" #import "IFlyPcmRecorder.h" #import "IFlySpeechEvaluator.h" #import "IFlySpeechEvaluatorDelegate.h" #import "IFlyVoiceWakeuper.h" #import "IFlyVoiceWakeuperDelegate.h"

3.2登入訊飛伺服器

在使用訊飛的語音解析之前,需要進行使用者身份驗證,即登入訊飛伺服器,即訊飛伺服器需要根據你當前使用者的APPID才能同意你登入。程式碼如下:

//登陸語音平臺
    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"57e08eb8"];
    [IFlySpeechUtility createUtility:initString];

3.3建立有介面提示語音識別物件

//  Speech-JiKe
//
//  Created by rimi on 16/9/22.
//  Copyright © 2016年 LucioSui. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "iflyMSC/iflyMSC.h"
@class IFlySpeechRecognizer;

@interface ViewController : UIViewController<IFlySpeechRecognizerDelegate,IFlyRecognizerViewDelegate>

@property (nonatomic, strong) NSString *filePath;//音訊檔案路徑

@property (nonatomic, strong) IFlySpeechRecognizer *iFlySpeechRecognizer;//不帶介面的識別物件

@property (nonatomic, strong) IFlyRecognizerView *iflyRecognizerView;//帶介面的識別物件

@property (nonatomic, strong) NSString * result;

@property (nonatomic, assign) BOOL isCanceled;

@end

3.4初始化帶介面的識別物件

// 設定識別引數
-(void)initRecognizer
{
    NSLog(@"%s",__func__);

    if ([IATConfig sharedInstance].haveView == NO) {//無介面

        //單例模式,無UI的例項
        if (_iFlySpeechRecognizer == nil) {
            _iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];

            [_iFlySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];

            //設定聽寫模式
            [_iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
        }
        _iFlySpeechRecognizer.delegate = self;

        if (_iFlySpeechRecognizer != nil) {
            IATConfig *instance = [IATConfig sharedInstance];

            //設定最長錄音時間
            [_iFlySpeechRecognizer setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
            //設定後端點
            [_iFlySpeechRecognizer setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
            //設定前端點
            [_iFlySpeechRecognizer setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
            //網路等待時間
            [_iFlySpeechRecognizer setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];

            //設定取樣率,推薦使用16K
            [_iFlySpeechRecognizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];

            if ([instance.language isEqualToString:[IATConfig chinese]]) {
                //設定語言
                [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
                //設定方言
                [_iFlySpeechRecognizer setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
            }else if ([instance.language isEqualToString:[IATConfig english]]) {
                [_iFlySpeechRecognizer setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
            }
            //設定是否返回標點符號
            [_iFlySpeechRecognizer setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];

        }
    }else  {//有介面

        //單例模式,UI的例項
        if (_iflyRecognizerView == nil) {
            //UI顯示劇中
            _iflyRecognizerView= [[IFlyRecognizerView alloc] initWithCenter:self.view.center];

            [_iflyRecognizerView setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];

            //設定聽寫模式
            [_iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];

        }
        _iflyRecognizerView.delegate = self;

        if (_iflyRecognizerView != nil) {
            IATConfig *instance = [IATConfig sharedInstance];
            //設定最長錄音時間
            [_iflyRecognizerView setParameter:instance.speechTimeout forKey:[IFlySpeechConstant SPEECH_TIMEOUT]];
            //設定後端點
            [_iflyRecognizerView setParameter:instance.vadEos forKey:[IFlySpeechConstant VAD_EOS]];
            //設定前端點
            [_iflyRecognizerView setParameter:instance.vadBos forKey:[IFlySpeechConstant VAD_BOS]];
            //網路等待時間
            [_iflyRecognizerView setParameter:@"20000" forKey:[IFlySpeechConstant NET_TIMEOUT]];

            //設定取樣率,推薦使用16K
            [_iflyRecognizerView setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];
            if ([instance.language isEqualToString:[IATConfig chinese]]) {
                //設定語言
                [_iflyRecognizerView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
                //設定方言
                [_iflyRecognizerView setParameter:instance.accent forKey:[IFlySpeechConstant ACCENT]];
            }else if ([instance.language isEqualToString:[IATConfig english]]) {
                //設定語言
                [_iflyRecognizerView setParameter:instance.language forKey:[IFlySpeechConstant LANGUAGE]];
            }
            //設定是否返回標點符號
            [_iflyRecognizerView setParameter:instance.dot forKey:[IFlySpeechConstant ASR_PTT]];

        }
    }
}

3.5實現代理方法

#pragma mark 錯誤的回撥函式
- (void) onError:(IFlySpeechError *) error
{
    NSLog(@"%s",__func__);

    if ([IATConfig sharedInstance].haveView == NO ) {
        NSString *text ;

        if (self.isCanceled) {
            text = @"識別取消";

        } else if (error.errorCode == 0 ) {
            if (_result.length == 0) {
                text = @"無識別結果";
            }else {
                text = @"識別成功";
            }
        }else {
            text = [NSString stringWithFormat:@"發生錯誤:%d %@", error.errorCode,error.errorDesc];
            NSLog(@"%@",text);
        }

    }else {

        NSLog(@"errorCode:%d",[error errorCode]);
    }
}


//無介面,聽寫結果回撥
// results:聽寫結果
// isLast:表示最後一次

- (void)onResults:(NSArray *) results isLast:(BOOL)isLast
{

    _volumLabel.alpha = 0.0;

    NSMutableString *resultString = [[NSMutableString alloc] init];
    NSDictionary *dic = results[0];
    for (NSString *key in dic) {
        [resultString appendFormat:@"%@",key];
    }
    _result =[NSString stringWithFormat:@"%@",resultString];
    NSString * resultFromJson =  [ISRDataHelper stringFromJson:resultString];
    _textLabel.text = [NSString stringWithFormat:@"%@%@",_textLabel.text,resultFromJson];

    if (isLast){
        NSLog(@"聽寫結果(json):%@測試", self.result);
    }
    NSLog(@"_result=%@",_result);

}

// 有介面,聽寫結果回撥
// resultArray:聽寫結果
// isLast:表示最後一次

- (void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast
{
    _volumLabel.alpha = 0.0;

    NSMutableString *result = [[NSMutableString alloc] init];
    NSDictionary *dic = [resultArray objectAtIndex:0];

    for (NSString *key in dic) {
        [result appendFormat:@"%@",key];
    }
    _textLabel.text = [NSString stringWithFormat:@"%@",result];
}

3.6開始識別語音

音訊檔案儲存地址

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = [paths objectAtIndex:0];
    _filePath = [[NSString alloc] initWithFormat:@"%@",[cachePath stringByAppendingPathComponent:@"asr.pcm"]];

只提取了一部分程式碼,持續更新,後期上DEMO。