【编程之美12期】对着电脑说话,做自己想做的事情

 

程序员的惊奇之处就是别人用工具学习怎样做的时候,你早已用你的几行代码实现了自己想做的事情~


那你想不想有个听话的机器人呢?从最基本的开始~

对着电脑说出想打开的网页,比如百度,即可自动打开百度。


“编程之美,让你爱上编程的美。”

挑战下面编程题目,

一起体验编程的乐趣!

本期题目:


对着电脑说出想打开的网页,比如百度,即可自动打开百度。


例如,对着笔记本电脑吼一声“百度”,浏览器自动打开百度首页。

关键字:Speech to Text

参考思路:

1:获取电脑录音-->WAV文件 python record wav

2:录音文件-->文本

STT: Speech to Text

STT API Google API

3:文本-->电脑命令



编程之美独有福利:


这个栏目就是为了培养大家编程动手习惯并且提高编程能力的一个平台,我们现如今给参与这个栏目的提供如下福利:
  1. 源码分享
  2. 每周评比编程之星(奖励牛客大礼包一份,这回是充满惊喜,每个大礼包里不一定包括什么)
  3. 企业实习工作机会
  4. 独家名誉及待遇
  5. 参与开源项目
  6. 定期的分享讲座
等等等等!我们想进行一些不一样的玩法,让学习变得更有趣!无论你找到工作与否,即使是已经工作了,你也要有一个练手交流的平台~


当然啦,重要的是来练习自己的编程能力,分享代码,交流技术的过程,这个过程中,你提升的不只是一点点~

为了让牛友能够更高效,更好的学习,特意为大家建了一个群:牛客编程之美源码群 595665246,只给真正想参与这个栏目和真正想学习的人开放,会在群里定期分享源码,只让真正想学习的人来参加,所以只有参与栏目(在本栏目下发出自己的代码的)才能加,加的时候备注一下牛客昵称~


编程之星

每一期的玩法都不一样!每一期的编程之星的规则也不一样~~重要的是人人都参与进来

本期编程之星评比规则:由评委来评选~

注:因为目前群成员比较多,本着只让真正想参与的人进来的原则,可能后期会整理群啦,对这个没有兴趣的我们就会把位置让给真正有兴趣参加的人啦~

栏目介绍

编程之美,是牛客网推出的新栏目,每周推出一个项目供大家练手讨论交流。

如果你有想实现的项目问题,欢迎私信牛妹~

另外!另外!如果有好玩的项目题目可以私信牛妹,一经采用有奖励哦~~

如果你有写博客或者公众号的习惯,也欢迎加牛妹qq:1037532015私信。

全部评论
借助百度api,用python实现. 思路 使用python录音并保存为文件 调用api,得到识别结果 根据结果打开对应url 只贴了关键代码,完整代码请移步:https://github.com/missyoursmile/Command-a-computer-with-sound #定义命令字典(不知道为何百度会识别成这样,牛客识别不出来,用了部分识别) url_dict = { u'baidu': "https://www.baidu.com", u'牛': "https://www.nowcoder.com" } #录音并保存到文件(在另外一个文件中) wavfile = record_wave() #获取token access_token = requests.post(auth_url).json()['access_token'] res = None with open(wavfile,'rb') as f: r = requests.post(api_url+access_token, headers={'Content-Type': "audio/wav;rate=8000"}, data=f) res = r.json() if res['err_no'] != 0: #识别错误 res = None if not res: print "识别错误" exit() open_url = None for k in url_dict: if k in res['result'][0]: open_url = url_dict[k] print "识别结果: ",res['result'][0] if not open_url: print "没有对应的命令." exit() #调用系统命令,打开浏览器 os.system("explorer "+ open_url) 注意 只实现了Windows平台,其他平台请自行修改最后的系统调用 部分词汇可能难以识别,使用了部分匹配 公开了api key, 方便大家使用,请不要滥用,谢谢 如有好的离线语音识别库,请告诉我
点赞
送花
回复 分享
发布于 2017-02-18 23:23
使用百度在线语音识别RsetAPI 参考百度提供的API开发文档 import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class RecognizeDemo { private static final String serverURL = "http://vop.baidu.com/server_api"; private static String token = ""; private static final String testFileName = "fileName"; //put your own params here private static final String apiKey = "apiKey"; private static final String secretKey = "secretKey"; private static final String cuid = "MAC"; //cuid是设备的唯一标示 public static void main(String[] args) throws Exception { String rcg = ""; getToken(); rcg = recognize(); if (rcg.contains("百度")){ exe(); } } private static void getToken() throws Exception { String getTokenURL = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials" + "&client_id=" + apiKey + "&client_secret=" + secretKey; HttpURLConnection conn = (HttpURLConnection) new URL(getTokenURL).openConnection(); token = new JSONObject(getResponse(conn)).getString("access_token"); } private static String recognize() throws Exception { File speechFile = new File(testFileName); // 1、打开和URL之间的连接 HttpURLConnection conn = (HttpURLConnection) new URL(serverURL + "?cuid=" + cuid + "&token=" + token).openConnection(); ///2、向浏览器发送请求和数据 sendRequest(conn, speechFile); ///3、获取HttpURLConnection对象对应的输入流,获得浏览器传回来的数据 return getResponse(conn); } /** * 向浏览器发送请求和数据 * @param conn * @param file * @throws Exception */ private static void sendRequest(HttpURLConnection conn, File file) throws Exception { // 设置通用的请求属性 conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "audio/pcm; rate=8000"); // 发送POST请求必须设置如下两行 conn.setDoInput(true); conn.setDoOutput(true); // 获取HttpURLConnection对象对应的输出流,并向浏览器发送数据 DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.write(loadFile(file)); wr.flush(); wr.close(); } /** * 获得浏览器的响应 * @param conn * @return * @throws Exception */ private static String getResponse(HttpURLConnection conn) throws Exception { if (conn.getResponseCode() != 200) { // request error return ""; } // 获取HttpURLConnection对象对应的输入流 InputStream is = conn.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } private static void exe(){ try { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://www.baidu.com"); } catch (Exception e) { e.printStackTrace() ; } } /** * 读取字节流音频文件 * @param file * @return * @throws IOException */ private static byte[] loadFile(File file) throws IOException { if (!file.exists()){ throw new FileNotFoundException(file.getName()); } ByteArrayOutputStream baos = new ByteArrayOutputStream((int) file.length()); BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(file)); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = bis.read(buffer, 0, bufferSize)) != -1){ baos.write(buffer, 0, len); } }catch (IOException e){ e.printStackTrace(); }finally { bis.close(); baos.close(); } return baos.toByteArray(); } }
点赞
送花
回复 分享
发布于 2017-02-23 19:15
国泰君安
校招火热招聘中
官网直投
科大讯飞的语音听写包,读入文件都省去了,内部直接调用了系统的录音功能,太强大了 都是开发包的功劳啊~~,感觉自己什么都没有做,惭愧............ 工具类 package iflytekTest; import com.iflytek.cloud.speech.*; import java.io.IOException; public class SpeechLisAndWrite { private static StringBuffer mResult = new StringBuffer(); public static String res = ""; public static void Recognize() { if (SpeechRecognizer.getRecognizer() == null) SpeechRecognizer.createRecognizer(); SpeechRecognizer mIat = SpeechRecognizer.getRecognizer(); // 设置听写参数 mIat.setParameter(SpeechConstant.DOMAIN, "iat"); mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn"); mIat.setParameter(SpeechConstant.ACCENT, "mandarin"); mIat.setParameter( SpeechConstant.RESULT_TYPE, "plain" ); // 开始监听 mIat.startListening(mListenser); // 需要等待语音识别结果 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // 调用命令打开百度 if(res.equals("百度。")) { try { // rundll32 url.dll,FileProtocolHandler 表示打开默认浏览器 // 可换成 cmd /c start iexplore 表示打开IE String url = "http://www.baidu.com"; Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } catch (IOException e) { e.printStackTrace(); } } } private static RecognizerListener mListenser = new RecognizerListener() { public void onVolumeChanged(int i) { } public void onBeginOfSpeech() { System.out.println("开始听写"); } public void onEndOfSpeech() { System.out.println("结束听写"); } public void onResult(RecognizerResult recognizerResult, boolean b) { mResult.append(recognizerResult.getResultString()); if(b) { res += mResult.toString(); System.out.println(res); } } public void onError(SpeechError speechError) { } public void onEvent(int i, int i1, int i2, String s) { } }; } 启动类 package iflytekTest; import com.iflytek.cloud.speech.SpeechUtility; public class SpeechLisAndWriteTest { private static final String APPID = "XXXXXX"; // 这里改成自己的 public static void main(String[] args) { SpeechUtility.createUtility("appid=" + APPID); SpeechLisAndWrite.Recognize(); } }
点赞
送花
回复 分享
发布于 2017-02-24 16:35
求个java版的
点赞
送花
回复 分享
发布于 2017-02-20 16:55
去年的工作就是做这个,智能语音鼠标
点赞
送花
回复 分享
发布于 2017-02-23 15:57
不会
点赞
送花
回复 分享
发布于 2017-02-27 10:23
   本期编程之星获得者:偲念是壹種病   恭喜!!!将获得一套牛客定制礼品大礼包~
点赞
送花
回复 分享
发布于 2017-02-28 16:40

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务