· 5 years ago · May 09, 2020, 02:42 PM
1import subprocess
2import json
3
4def shexec(ex):
5 return subprocess.run(ex,shell=True)
6
7#Recognize an audio file using the IBM API in American English
8# -> filename: Path of file. Should be .wav or .flac
9# -> url: URL of your IBM speech recognition service
10# -> apikey: API key of your IBM speech recognition service
11# <- Returns: Result, or empty string if something went wrong
12def ibmrec(filename,url,apikey):
13 atype = filename.split(".")[-1]
14 fureq = "curl -s -X POST -u " +"\"apikey:" + apikey + "\"" + " --header \"Content-Type: audio/" + atype + "\" --data-binary @" +filename + " \"" + url + "/v1/recognize\"" + " -o out.json"
15 shexec(fureq)
16
17 with open("out.json", "r") as read_file:
18 data = json.load(read_file)
19
20 try:
21 grec = data["results"][0]["alternatives"][0]["transcript"].strip()
22 except:
23 return ""
24
25 return grec