Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google's speech recognition API in Objective-C

i would like to use this Google API (for testing only):

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

My question is: how should I send a POST request to this URL? I'm using:

 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);

But I get only

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

Whats wrong / what should I do?

like image 257
Smoothie Avatar asked Nov 24 '11 18:11

Smoothie


1 Answers

FYI status: 0 – correct
, status: 4 – missing audio file, 
status: 5 – incorrect audio file.

  In place of ;

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

Just Use;

    [request setHTTPBody:myData];

Here is Working code for reference:

-(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);

}

like image 153
mhrrt Avatar answered Oct 22 '22 01:10

mhrrt