Google's speech recognition API in Objective-C
Asked Answered
T

3

6

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?

Transom answered 24/11, 2011 at 18:22 Comment(1)
Btw, you're misusing the addValue:forHTTPHeaderField: and the setHTTPBody: methods -- look up their usage in the docs.Ghazi
A
2

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

}

Atlanta answered 3/1, 2013 at 12:37 Comment(1)
I tried your code but getting the empty result string, what i have done is recorded a wav audio file then converted it to flac file and now using your code but no success, please guide me more on this.Teatime
G
0

Instead of bothering with the rather messy Cocoa implementation, check out my extremely easy-to-use (one function) C library for Google Speech: http://github.com/H2CO3/libsprec

Ghazi answered 22/6, 2012 at 2:38 Comment(2)
@VipinVijay just visit the link to the GitHub repo. There are two example programs.Ghazi
can you please provide me a link regarding thatScuttle
T
0

i was struggling with the same issue but later on i resolved it by referring to this link https://github.com/gillesdemey/google-speech-v2.and just replaced your NSMutableURLRequest *request with this NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"https://github.com/gillesdemey/google-speech-v2"]];

Teatime answered 18/9, 2014 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.