Advertisement
Guest User

Untitled

a guest
Sep 19th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. # Render speech to text using the google cloud speech engine.
  4. #
  5. # Kruft Industries Sept. 2016
  6. #
  7. #
  8. # Intended to replace work by the following(not sure where this is hosted):
  9. # GNU General Public License Version 2 Copyright (C) 2011 - 2012, Lefteris Zafiris <zaf.000@gmail.com>
  10. #
  11. #
  12. # The script takes as input flac files at 8kHz and returns the following values:
  13. # status : Return status. 0 means success, non zero values indicating different errors.
  14. #
  15. # Outputs a voice transcription that satisfies the input of sendmailmp3 for freepbx authored by the above Zafiris
  16. # I am by no means an expert with the perl language, Please forgive any blaring ugliness :)
  17.  
  18.  
  19. import base64;
  20. #use strict;
  21. use warnings;
  22. use LWP::UserAgent;
  23.  
  24.  
  25. if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
  26. print "Speech recognition using google cloud speech api.nn";
  27. print "Usage: $0 [FILES]nn";
  28. exit;
  29. }
  30.  
  31. my $url = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=API KEY HERE";
  32.  
  33.  
  34. my @file_list = @ARGV;
  35.  
  36. foreach my $file (@file_list) {
  37. print "Opening $filen";
  38. open(my $fh, "<", "$file") or die "Cant read file: $!";
  39. my $audio = do { local $/; <$fh> };
  40. close($fh);
  41. my $flac = "base64.b64encode($audio.read())";
  42. my $json = '{"config":{"encoding":"FLAC","sample_rate":8000,"language_code":"en-US"},"audio":{"content":"' . $flac . '"}}';
  43.  
  44. my $req = HTTP::Request->new( 'POST', $url );
  45. $req->header( 'Content-Type' => 'application/json' );
  46. $req->content( $json );
  47.  
  48. my $lwp = LWP::UserAgent->new;
  49. my $response = $lwp->request($req);
  50.  
  51. print $response->as_string; #debug output google's reply headers and message
  52.  
  53. last if (!$response->is_success);
  54.  
  55. print $response->content; #debug output the full transcript
  56.  
  57. my $doodle = $response->content;
  58. $doodle =~ s/.*"transcript"://g;
  59. $doodle =~ s/}],.*//g;
  60. $doodle =~ s/^{"result":[]}/{"result":/g;
  61. $doodle =~ s/R//g;
  62. $doodle =~ s/*/_/g;
  63. print $doodle;
  64. }
  65. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement