【问题标题】:Google's speech recognition API in Objective-C谷歌在 Objective-C 中的语音识别 API
【发布时间】:2011-11-24 18:22:16
【问题描述】:

我想使用这个 Google API(仅用于测试):

https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US

我的问题是:我应该如何向这个 URL 发送 POST 请求?我正在使用:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *recDir = [paths objectAtIndex:0];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];


NSData *myData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];
//NSString *audio = [NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/recordTest.flac", recDir]];



NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
                                initWithURL:[NSURL 
                                             URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]];






[request setHTTPMethod:@"POST"];

//set headers

[request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"];

[request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"];

NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData];

[request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];

[request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"];



NSHTTPURLResponse* urlResponse = nil;  
NSError *error = [[NSError alloc] init];  
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];  
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSLog(@"The answer is: %@",result);

但我只得到了

{
"status":5,
"id":"fe6ba68a593f9919f5fd33e819d493a0-1",
"hypotheses":[
 HERE SHOULD BE THE TEXT
 ]
}

怎么了/我该怎么办?

【问题讨论】:

  • 顺便说一句,您误用了 addValue:forHTTPHeaderField:setHTTPBody: 方法——在文档中查看它们的用法。

标签: objective-c post speech-recognition http-request


【解决方案1】:

仅供参考 状态:0 – 正确, 状态:4 – 缺少音频文件, 状态:5 – 音频文件不正确。

  In place of ;

    NSString *requestBody = [[NSString alloc] initWithFormat:@"Content=%@", myData];
    [request setHTTPBody:[requestBody dataUsingEncoding:NSASCIIStringEncoding]];

随便用;

    [request setHTTPBody:myData];

这是工作代码供参考:

-(void)googleSTT{

  NSString *homeDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  NSString *filePath = [NSString stringWithFormat:@"%@/%@", homeDirectory, @"test.flac"];

  NSData *myData = [NSData dataWithContentsOfFile:filePath];


  NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                initWithURL:[NSURL
                                             URLWithString:@"https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US"]];

  [request setHTTPMethod:@"POST"];

  //set headers

  [request addValue:@"Content-Type" forHTTPHeaderField:@"audio/x-flac; rate=16000"];

  [request addValue:@"audio/x-flac; rate=16000" forHTTPHeaderField:@"Content-Type"];

  [request setHTTPBody:myData];

  [request setValue:[NSString stringWithFormat:@"%d",[myData length]] forHTTPHeaderField:@"Content-length"];

  NSHTTPURLResponse* urlResponse = nil;
  NSError *error = [[NSError alloc] init];
  NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

  NSLog(@"The answer is: %@",result);

}

【讨论】:

  • 我尝试了你的代码,但得到了空的结果字符串,我所做的是录制了一个 wav 音频文件,然后将其转换为 flac 文件,现在使用你的代码但没有成功,请指导我更多关于这个.
【解决方案2】:

不要为相当混乱的 Cocoa 实现而烦恼,请查看我用于 Google Speech 的极其易于使用(一个函数)的 C 库:http://github.com/H2CO3/libsprec

【讨论】:

  • @VipinVijay 只需访问 GitHub 存储库的链接。有两个示例程序。
  • 你能给我一个链接吗
【解决方案3】:

我在同样的问题上苦苦挣扎,但后来我通过引用此链接 https://github.com/gillesdemey/google-speech-v2.and 解决了它 用这个替换了你的 NSMutableURLRequest *request NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多