Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl
- # Render speech to text using the google cloud speech engine.
- #
- # Kruft Industries Sept. 2016
- #
- #
- # Intended to replace work by the following(not sure where this is hosted):
- # GNU General Public License Version 2 Copyright (C) 2011 - 2012, Lefteris Zafiris <zaf.000@gmail.com>
- #
- #
- # The script takes as input flac files at 8kHz and returns the following values:
- # status : Return status. 0 means success, non zero values indicating different errors.
- #
- # Outputs a voice transcription that satisfies the input of sendmailmp3 for freepbx authored by the above Zafiris
- # I am by no means an expert with the perl language, Please forgive any blaring ugliness :)
- import base64;
- #use strict;
- use warnings;
- use LWP::UserAgent;
- if (!@ARGV || $ARGV[0] eq '-h' || $ARGV[0] eq '--help') {
- print "Speech recognition using google cloud speech api.nn";
- print "Usage: $0 [FILES]nn";
- exit;
- }
- my $url = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=API KEY HERE";
- my @file_list = @ARGV;
- foreach my $file (@file_list) {
- print "Opening $filen";
- open(my $fh, "<", "$file") or die "Cant read file: $!";
- my $audio = do { local $/; <$fh> };
- close($fh);
- my $flac = "base64.b64encode($audio.read())";
- my $json = '{"config":{"encoding":"FLAC","sample_rate":8000,"language_code":"en-US"},"audio":{"content":"' . $flac . '"}}';
- my $req = HTTP::Request->new( 'POST', $url );
- $req->header( 'Content-Type' => 'application/json' );
- $req->content( $json );
- my $lwp = LWP::UserAgent->new;
- my $response = $lwp->request($req);
- print $response->as_string; #debug output google's reply headers and message
- last if (!$response->is_success);
- print $response->content; #debug output the full transcript
- my $doodle = $response->content;
- $doodle =~ s/.*"transcript"://g;
- $doodle =~ s/}],.*//g;
- $doodle =~ s/^{"result":[]}/{"result":/g;
- $doodle =~ s/R//g;
- $doodle =~ s/*/_/g;
- print $doodle;
- }
- exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement