API распознавания речи Google в Objective-C - PullRequest
6 голосов
/ 24 ноября 2011

Я хотел бы использовать этот API Google (только для тестирования):

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

Мой вопрос: как мне отправить запрос POST на этот URL? Я использую:

 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
 ]
}

Что не так / что мне делать?

Ответы [ 3 ]

2 голосов
/ 03 января 2013

FYI статус: 0 - правильно, статус: 4 - отсутствует аудиофайл, статус: 5 - неверный аудиофайл.

  In place of ;

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

Просто используйте;

    [request setHTTPBody:myData];

Вот рабочий код для справки:

- (Недействительными) 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);

}

0 голосов
/ 18 сентября 2014

Я боролся с той же проблемой, но позже решил ее, обратившись к этой ссылке https://github.com/gillesdemey/google-speech-v2.and просто заменил ваш запрос NSMutableURLRequest * этим NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];

0 голосов
/ 22 июня 2012

Вместо того, чтобы возиться с довольно грязной реализацией Cocoa, ознакомьтесь с моей чрезвычайно простой в использовании (одной функцией) библиотекой C для Google Speech: http://github.com/H2CO3/libsprec

...