1硫麻、前言
在這之前瞎疼,我相信大家都知道CIDetector能夠做到人臉檢測這一功能吧仲吏,注意是檢測并不是識(shí)別濒析,如果要做到識(shí)別的話也即區(qū)別人臉的異同就要用到OpenCV了正什,說了這么多就是要引出蘋果在今年新推出的Speech新框架,能夠做到語音識(shí)別~~~額号杏,其實(shí)只是能夠做到把一段語音轉(zhuǎn)化成文字婴氮。
2、框架主要類
#import <Foundation/Foundation.h>
#import <Speech/SFSpeechRecognitionResult.h>
#import <Speech/SFSpeechRecognitionRequest.h>
#import <Speech/SFSpeechRecognitionTask.h>
#import <Speech/SFSpeechRecognitionTaskHint.h>
#import <Speech/SFSpeechRecognizer.h>
#import <Speech/SFTranscriptionSegment.h>
#import <Speech/SFTranscription.h>
3盾致、怎么用
#import "NFSpeechViewController.h"
#import <Speech/Speech.h>
@interface NFSpeechViewController ()<SFSpeechRecognitionTaskDelegate>
@property (nonatomic ,strong) SFSpeechRecognitionTask *recognitionTask;
@property (nonatomic ,strong) SFSpeechRecognizer *speechRecognizer;
@property (nonatomic ,strong) UILabel *recognizerLabel;
@end
@implementation NFSpeechViewController
- (void)dealloc {
[self.recognitionTask cancel];
self.recognitionTask = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//0.0獲取權(quán)限
//0.1在info.plist里面配置
/*
typedef NS_ENUM(NSInteger, SFSpeechRecognizerAuthorizationStatus) {
SFSpeechRecognizerAuthorizationStatusNotDetermined,
SFSpeechRecognizerAuthorizationStatusDenied,
SFSpeechRecognizerAuthorizationStatusRestricted,
SFSpeechRecognizerAuthorizationStatusAuthorized,
};
*/
[SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
switch (status) {
case SFSpeechRecognizerAuthorizationStatusNotDetermined:
NSLog(@"NotDetermined");
break;
case SFSpeechRecognizerAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case SFSpeechRecognizerAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
case SFSpeechRecognizerAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
default:
break;
}
}];
//1.創(chuàng)建SFSpeechRecognizer識(shí)別實(shí)例
self.speechRecognizer = [[SFSpeechRecognizer alloc] initWithLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
//2.創(chuàng)建識(shí)別請(qǐng)求
SFSpeechURLRecognitionRequest *request = [[SFSpeechURLRecognitionRequest alloc] initWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"游子吟.mp3" ofType:nil]]];
//3.開始識(shí)別任務(wù)
self.recognitionTask = [self recognitionTaskWithRequest1:request];
}
- (SFSpeechRecognitionTask *)recognitionTaskWithRequest0:(SFSpeechURLRecognitionRequest *)request{
return [self.speechRecognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
if (!error) {
NSLog(@"語音識(shí)別解析正確--%@", result.bestTranscription.formattedString);
}else {
NSLog(@"語音識(shí)別解析失敗--%@", error);
}
}];
}
- (SFSpeechRecognitionTask *)recognitionTaskWithRequest1:(SFSpeechURLRecognitionRequest *)request{
return [self.speechRecognizer recognitionTaskWithRequest:request delegate:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark- SFSpeechRecognitionTaskDelegate
// Called when the task first detects speech in the source audio
- (void)speechRecognitionDidDetectSpeech:(SFSpeechRecognitionTask *)task {
}
// Called for all recognitions, including non-final hypothesis
- (void)speechRecognitionTask:(SFSpeechRecognitionTask *)task didHypothesizeTranscription:(SFTranscription *)transcription {
}
// Called only for final recognitions of utterances. No more about the utterance will be reported
- (void)speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishRecognition:(SFSpeechRecognitionResult *)recognitionResult {
NSDictionary *attributes = @{
NSFontAttributeName:[UIFont systemFontOfSize:18],
};
CGRect rect = [recognitionResult.bestTranscription.formattedString boundingRectWithSize:CGSizeMake(self.view.bounds.size.width - 100, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
self.recognizerLabel.text = recognitionResult.bestTranscription.formattedString;
self.recognizerLabel.frame = CGRectMake(50, 120, rect.size.width, rect.size.height);
}
// Called when the task is no longer accepting new audio but may be finishing final processing
- (void)speechRecognitionTaskFinishedReadingAudio:(SFSpeechRecognitionTask *)task {
}
// Called when the task has been cancelled, either by client app, the user, or the system
- (void)speechRecognitionTaskWasCancelled:(SFSpeechRecognitionTask *)task {
}
// Called when recognition of all requested utterances is finished.
// If successfully is false, the error property of the task will contain error information
- (void)speechRecognitionTask:(SFSpeechRecognitionTask *)task didFinishSuccessfully:(BOOL)successfully {
if (successfully) {
NSLog(@"全部解析完畢");
}
}
#pragma mark- getter
- (UILabel *)recognizerLabel {
if (!_recognizerLabel) {
_recognizerLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 120, self.view.bounds.size.width - 100, 100)];
_recognizerLabel.numberOfLines = 0;
_recognizerLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
_recognizerLabel.adjustsFontForContentSizeCategory = YES;
_recognizerLabel.textColor = [UIColor orangeColor];
[self.view addSubview:_recognizerLabel];
}
return _recognizerLabel;
}
@end
上面一坨代碼就是新框架的一個(gè)簡單的不能在簡單的例子主经,你可以復(fù)制代碼試一試,你可以創(chuàng)建一個(gè)和我同名的類庭惜,這樣直接拷貝上面代碼就可以了罩驻。
4、步驟解析
首先护赊、我們要做的就是獲取權(quán)限惠遏,蘋果對(duì)權(quán)限安全這一面是做了很大的改進(jìn)的,通過iOS10關(guān)于權(quán)限的新特性不難看出來骏啰。
通過上圖节吮,我們需要在info.plist配置文件中添加對(duì)應(yīng)的權(quán)限,這次iOS10更新之后我們終于不需要像以前那樣Copy判耕、Paste了透绩,因?yàn)橛休斎胩崾玖藒~~~開心
其次、接下來就是創(chuàng)建SFSpeechRecognizer實(shí)例了
這里需要提一下的就是本地化語言的坑壁熄,如果你不知道
[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]
@"zh_CN"字符串的話帚豪,你可以使用
[SFSpeechRecognizer supportedLocales];
進(jìn)行查看,選取合適的使用草丧,這里@"zh"在iOS9之后就不是簡體中文了狸臣,而是TW繁體中文,簡體中文就要使用@"zh_CN"了!!!!
for (NSLocale *lacal in [SFSpeechRecognizer supportedLocales].allObjects) {
NSLog(@"countryCode:%@ languageCode:%@ ", lacal.countryCode, lacal.languageCode);
}
第2步 創(chuàng)建識(shí)別請(qǐng)求昌执,就簡單了直接把要識(shí)別的語音url傳進(jìn)去就行了(這里使用的是本地資源)
第3步中開始識(shí)別任務(wù)我用的是delegate的方式在代理方法中去做處理固棚,蘋果還提供了block方式。對(duì)應(yīng)的代碼:
- (SFSpeechRecognitionTask *)recognitionTaskWithRequest0:(SFSpeechURLRecognitionRequest *)request{
return [self.speechRecognizer recognitionTaskWithRequest:request resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {
if (!error) {
NSLog(@"語音識(shí)別解析正確--%@", result.bestTranscription.formattedString);
}else {
NSLog(@"語音識(shí)別解析失敗--%@", error);
}
}];
}
- (SFSpeechRecognitionTask *)recognitionTaskWithRequest1:(SFSpeechURLRecognitionRequest *)request{
return [self.speechRecognizer recognitionTaskWithRequest:request delegate:self];
}
怎么識(shí)別我已經(jīng)在代碼新的很清楚了仙蚜,按照步驟來就可以了,注釋也很清楚厂汗。
好啦~~
趕緊試試去吧~~