{"id":535,"date":"2020-05-12T16:24:06","date_gmt":"2020-05-12T08:24:06","guid":{"rendered":"https:\/\/www.iotch.xyz\/?p=535"},"modified":"2020-05-12T16:24:06","modified_gmt":"2020-05-12T08:24:06","slug":"%e5%b0%8f%e7%99%bd%e6%95%99%e7%a8%8b%e3%80%91%e5%9f%ba%e4%ba%8e%e6%a0%91%e8%8e%93%e6%b4%be%e7%9a%84%e6%99%ba%e8%83%bd%e8%af%ad%e9%9f%b3%e5%8a%a9%e6%89%8b-python","status":"publish","type":"post","link":"https:\/\/www.iotch.xyz\/?p=535","title":{"rendered":"\u5c0f\u767d\u6559\u7a0b\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python"},"content":{"rendered":"\n<p>\u3010\u5c0f\u767d\u6559\u7a0b\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python_Python_\u3010\u5c0f\u767d\u6559\u7a0b\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python-CSDN\u535a\u5ba2<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u3010\u5b8c\u6574\u6559\u7a0b\u96f6\u57fa\u7840\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"#3bpython3snowboy_1\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e3b+\u3001python3\u7cfb\u7edf\u81ea\u5e26\u3001\u767e\u5ea6\u8bed\u97f3\u8bc6\u522b\u4e0e\u5408\u6210\u3001\u8c03\u7528\u56fe\u7075\u673a\u5668\u4eba\uff08\u70ed\u8bcd\u5524\u9192\u5931\u8d25\uff0c\u53ef\u7528snowboy\uff09<\/a><\/li><li><a href=\"#1Windows__2\" target=\"_blank\"  rel=\"nofollow\" >1.Windows\u7cfb\u7edf\u4e0b\u3010\u4e0b\u8f7d+\u5b89\u88c5 \u6811\u8393\u6d3e\u5b98\u65b9\u7cfb\u7edf\u3011<\/a><\/li><li><a href=\"#2pyaudiosnowboy_39\" target=\"_blank\"  rel=\"nofollow\" >2.\u5b89\u88c5\u914d\u7f6epyaudio+snowboy<\/a><\/li><li><a href=\"#3_API__116\" target=\"_blank\"  rel=\"nofollow\" >3.\u767e\u5ea6\u8bed\u97f3\u8bc6\u522b API \u53ca\u4ee3\u7801\u5b9e\u73b0<\/a><\/li><li><a href=\"#4_173\" target=\"_blank\"  rel=\"nofollow\" >4.\u63a5\u5165\u56fe\u7075\u673a\u5668\u4eba<\/a><ul><li><a href=\"#_177\" target=\"_blank\"  rel=\"nofollow\" >\u6d4b\u8bd5\u5224\u65ad\u56fe\u7075\u673a\u5668\u4eba\u662f\u5426\u8c03\u7528\u6210\u529f<\/a><ul><li><a href=\"#_199\" target=\"_blank\"  rel=\"nofollow\" >\u56fe\u7075\u673a\u5668\u4eba\u8c03\u7528\u7684\u4ee3\u7801\uff1a<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u6811\u8393\u6d3e3b+\u3001python3\u7cfb\u7edf\u81ea\u5e26\u3001\u767e\u5ea6\u8bed\u97f3\u8bc6\u522b\u4e0e\u5408\u6210\u3001\u8c03\u7528\u56fe\u7075\u673a\u5668\u4eba\uff08\u70ed\u8bcd\u5524\u9192\u5931\u8d25\uff0c\u53ef\u7528snowboy\uff09<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1.Windows\u7cfb\u7edf\u4e0b\u3010\u4e0b\u8f7d+\u5b89\u88c5 \u6811\u8393\u6d3e\u5b98\u65b9\u7cfb\u7edf\u3011<\/h2>\n\n\n\n<p><strong>\u4e3b\u8981\u6d41\u7a0b\uff1a\u6811\u8393\u6d3e\u5b98\u7f51\u4e0b\u8f7d\u7cfb\u7edf\u2192\u5237\u5165 TF \u5361\u2192\u8bbe\u7f6e\u5f00\u542f\u663e\u793a\u5668\u548c SSH\u2192\u901a\u7535\u2192\u8fdb\u5165\u7cfb\u7edf<\/strong><br>1.Windows\u4e0b \u8fdb\u5165\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u7cfb\u7edf\u955c\u50cf\uff1a<a href=\"https:\/\/www.raspberrypi.org\/downloads\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/www.raspberrypi.org\/downloads\/<\/a><br><mark><em>\u9009\u62e9\u53f3\u8fb9\u7684\u7248\u672c<\/em><\/mark><br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718143557735.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\"><br><mark><em>\u9009\u62e9\u5de6\u8fb9\u7684\u7248\u672c<\/em><\/mark><br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718143948437.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\">2. Windows\u7cfb\u7edf\u4e0b\u7684\u5b89\u88c5<br>1\uff09\u9996\u5148\u5c06\u51c6\u5907\u597d\u7684 TF \u5361\u8fde\u63a5\u8bfb\u5361\u5668\uff0c\u63d2\u5165\u7535\u8111<br>2\uff09\u4e0b\u8f7d\u4e00\u4e2a\u683c\u5f0f\u5316 SD \u5361\u7684\u5de5\u5177\uff0c\u683c\u5f0f\u5316 SD \u5361<br>\u4e0b\u8f7d\u7f51\u5740\uff1a<a href=\"https:\/\/www.sdcard.org\/downloads\/formatter_4\/eula_windows\/\" target=\"_blank\"  rel=\"nofollow\" >https:\/\/www.sdcard.org\/downloads\/formatter_4\/eula_windows\/<\/a> \uff08\u70b9\u51fb Aceept \u5f00\u59cb\u4e0b\u8f7d\uff09<br>3\uff09\u4e0b\u8f7d Win32 DiskImager\uff0c\u8fd9\u662f\u4e00\u4e2a\u628a\u955c\u50cf\u5199\u5165 SD \u5361\u7684\u5de5\u5177<br>\u4e0b\u8f7d\u7f51\u5740\uff1a<a href=\"http:\/\/sourceforge.net\/projects\/win32diskimager\/\" target=\"_blank\"  rel=\"nofollow\" >http:\/\/sourceforge.net\/projects\/win32diskimager\/<\/a><\/p>\n\n\n\n<p><mark><em>\u9009\u62e9 raspberry.img \u7cfb\u7edf\u955c\u50cf\u5305\uff0c\u7136\u540e\u9009\u62e9 TF \u5361\uff0c\u70b9\u51fb Write \u5373\u5c06\u6811\u8393\u6d3e\u7cfb\u7edf\u5199\u5165SD\u5361<\/em><\/mark><br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718145247359.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\"><br>4\uff09\u5bf9 TF \u5361\u6570\u636e\u8fdb\u884c\u9884\u8bbe\u7f6e<\/p>\n\n\n\n<p><mark>a. \u5f00\u542f SSH \u8fdc\u7a0b<\/mark>\uff1a\uff08\u81ea 2016 \u5e74 9 \u6708\u5f00\u59cb\uff0craspberry \u9ed8\u8ba4\u5173\u95ed ssh \u8fde\u63a5\uff09<br>\u5728 TF \u5361\u5206\u533a\u91cc\u9762\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a \u201cssh\u201d \u7a7a\u6587\u4ef6\u5373\u53ef\uff08\u4e0d\u8981\u6709 txt \u540e\u7f00\uff01\uff09<br><mark>b. \u5f00\u542f\u5f3a\u5236 HDMI \u8f93\u51fa<\/mark>\uff1a\uff08\u5f88\u591a\u73b0\u5728\u7684\u663e\u793a\u5668\u5728\u6811\u8393\u6d3e\u4e0a\u5e76\u4e0d\u80fd\u8bc6\u522b\uff09<br>\u5728 TF \u5361\u5206\u533a\uff0c\u6253\u5f00 config.txt \u6587\u4ef6(\u5f00\u673a\u540e\u4f4d\u7f6e\uff1a \/boot\/config.txt)\uff0c\u4fee\u6539\u5982\u4e0b\uff1a<br>config_hdmi_boost=4 #\u5f00\u542f\u70ed\u63d2\u62d4<br>hdmi_mode=58 #\u5206\u8fa8\u7387\u4e3a\u9002\u5e94\u5c4f\u5e55\u5206\u8fa8\u7387\u7684\u9009\u62e9<br>hdmi_group=2 # 1:CEA \u7535\u89c6\u663e\u793a\u5668 2:DMT \u7535\u8111\u663e\u793a\u5668<br>disable_overscan=1#\u53ef\u4ee5\u53bb\u6389\u5f00\u673a\u5c4f\u5e55\u5468\u56f4\u7684\u9ed1\u8272\u8fb9\u6846<br>dtparam=audio=on<br><mark>c.\u8bbe\u7f6e\u65e0\u7ebf WI-FI \u8fde\u63a5<\/mark>\uff1a<br>\u5728 TF \u5361\u7684 boot \u5206\u533a\uff0c\u521b\u5efa wpa_supplicant.conf \u6587\u4ef6\uff0c\u52a0\u5165\u5982\u4e0b\u5185\u5bb9\uff1a<br>country=CN ctrl_interface=DIR=\/var\/run\/wpa_supplicant GROUP=netdev update_config=1<br>network={ ssid=\u201cwifiname\u201d psk=\u201cwifipassword\u201d priority=1 }<br><mark>d. \u63d2\u4e0a\u663e\u793a\u5668\u3001\u952e\u76d8\u3001\u9f20\u6807\uff0c\u4e0a\u7535<\/mark><br>\u5728\u57fa\u672c\u7684\u8bbe\u7f6e\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c06 TF \u5361\u63d2\u5165\u6811\u8393\u6d3e\uff0c\u63a5\u4e0aHDMI\u7ebf\u8fde\u4e0a\u663e\u793a\u5668\uff0c\u9f20\u6807\uff0c\u952e\u76d8\uff0c\u4e0a\u7535\uff0c\u5373\u53ef\u8fdb\u5165\u64cd\u4f5c\u754c\u9762\uff0c\u5982\u4e0b\uff1a<br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718150038476.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\"><\/p>\n\n\n\n<p><strong>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u7cfb\u7edf\u5c31\u88c5\u597d\u4e86\uff0c\u6574\u4e2a\u7cfb\u7edf\u4e0b\u8f7d\uff0c\u5b89\u88c5\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u5c0f\u7ec4\u9047\u5230\u5f88\u591a\u5947\u602a\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1aWiFi\u8fde\u63a5\u4e0d\u7a33\u5b9a\uff0c\u81ea\u52a8\u65ad\u5f00\uff1b\u8fdb\u4e0d\u53bb\u7cfb\u7edf\u7b49\u7b49\uff0c\u6211\u4eec\u7684\u767e\u5ea6\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u5199config\u6587\u4ef6\u7b49\uff0c\u90fd\u5c1d\u8bd5\u8fc7\uff0c\u4f46\u4ecd\u7136\u6cbb\u6807\u4e0d\u6cbb\u672c\uff01\uff01\uff01\u56e0\u6b64\uff0c\u6211\u4eec\u91c7\u53d6\u7684<mark>\u7b80\u5355\u7c97\u66b4\u7684\u65b9\u5f0f\u2014\u2014\u91cd\u88c5\u7cfb\u7edf\uff08\u5f88\u591a\u6b21\uff09\uff01\uff01\uff01<\/mark><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u5b89\u88c5\u914d\u7f6epyaudio+snowboy<\/h2>\n\n\n\n<p>\u8fd9\u4e2a\u5f88\u7b80\u5355\uff0c\u53c2\u8003<a href=\"https:\/\/blog.csdn.net\/weixin_44086593\/article\/details\/87879262\" target=\"_blank\"  rel=\"nofollow\" >\u6811\u8393\u6d3e3B+\u6307\u5357\uff08\u5341\u4e00\uff09\u5b89\u88c5\u914d\u7f6esnowboy<\/a>. <mark>\u6ce8\u610f\uff1a\u767e\u5ea6\u8bed\u97f3\u91c7\u6837\u7387\u4e3a16000\u3002<\/mark><\/p>\n\n\n\n<p><strong>\u8fdb\u884c\u6d4b\u8bd5\u64ad\u653e wave \u6587\u4ef6\u7684\u5b9e\u4f8b<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5f15\u5165\u5e93 \nimport wave \nimport pyaudio \n \n#\u5b9a\u4e49\u6570\u636e\u6d41\u5757 \nchunk = 1024 \n \nf = wave.open(r\"a.wav\",\"rb\") #\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00 wav \u6587\u4ef6 \np = pyaudio.PyAudio()  #\u6253\u5f00\u6570\u636e\u6d41 \nstream = p.open(format = p.get_format_from_width(f.getsampwidth()), \nchannels = f.getnchannels(), \nrate = f.getframerate(), \noutput = True  #\u8bfb\u53d6\u6570\u636e \ndata = f.readframes(chunk) \n#\u64ad\u653e \nwhile data !=\"\": \nstream.write(data) \ndata = f.readframes(chunk) \n \n#\u505c\u6b62\u6570\u636e\u6d41 \nstream.stop_stream() \nstream.close() \n#\u5173\u95ed PyAudio \n\n \n \np.terminate() \n\u5f55\u97f3\u5b9e\u4f8b: \n#\u5f15\u5165\u5e93 \nimport wave \nimport pyaudio \n \n#\u53c2\u6570\u5b9a\u4e49 \nCHUNK = 1024 \nFORMAT = pyaudio.paInt16 \nCHANNELS = 2 \nRATE = 44100 \nRECORD_SECONDS = 5 \nWAVE_OUTPUT_FILENAME = \"a.wav\" \n \n \np = pyaudio.PyAudio() \nstream = p.open(format=FORMAT, \nchannels=CHANNELS, \nrate=RATE, \ninput=True, \nframes_per_buffer=CHUNK) \n \nprint(\"* recording\") \n \nframes = &#91;] \nfor i in range(0, int(RATE \/ CHUNK * RECORD_SECONDS)): \ndata = stream.read(CHUNK) \nframes.append(data) \n \nprint(\"* done recording\") \n \nstream.stop_stream() \nstream.close() \np.terminate() \nwf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') \nwf.setnchannels(CHANNELS) \nwf.setsampwidth(p.get_sample_size(FORMAT)) \n\n \n \nwf.setframerate(RATE) \nwf.writeframes(b''.join(frames)) \nwf.close() \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u767e\u5ea6\u8bed\u97f3\u8bc6\u522b API \u53ca\u4ee3\u7801\u5b9e\u73b0<\/h2>\n\n\n\n<p>\u9996\u5148\u9700\u8981\u6ce8\u518c\u767e\u5ea6\u8bed\u97f3\u8bc6\u522b\u5f00\u53d1\u8005\u8d26\u53f7\uff0c\u5982\u4e0b\uff1a<br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718153128432.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\">\u5176\u6b21\uff0c\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5bfc\u5165 AipSpeech  AipSpeech \u662f\u8bed\u97f3\u8bc6\u522b\u7684 Python SDK \u5ba2\u6237\u7aef \nfrom aip import AipSpeech \nimport os \n \n''' \u4f60\u7684 APPID AK SK  \u53c2\u6570\u5728\u7533\u8bf7\u7684\u767e\u5ea6\u4e91\u8bed\u97f3\u670d\u52a1\u7684\u63a7\u5236\u53f0\u67e5\u770b''' \nAPP_ID = '114xxxx5' \nAPI_KEY = 'NYIvd23qqGAZxxxxxxxxxxxxxxx' \nSECRET_KEY = 'DcQWQ9Hxxxxxxxxxxxxxxxxxxxxxx' \n \n# \u65b0\u5efa\u4e00\u4e2a AipSpeech \nclient = AipSpeech(APP_ID, API_KEY, SECRET_KEY) \n \n \n# \u8bfb\u53d6\u6587\u4ef6 \ndef get_file_content(filePath):   #filePath  \u5f85\u8bfb\u53d6\u6587\u4ef6\u540d \n    with open(filePath, 'rb') as fp: \n        return fp.read()   \ndef stt(filename):         # \u8bed\u97f3\u8bc6\u522b \n    # \u8bc6\u522b\u672c\u5730\u6587\u4ef6 \n    result = client.asr(get_file_content(filename), \n                        'wav', \n                        16000, \n                        {'dev_pid': 1536,}      # dev_pid \u53c2\u6570\u8868\u793a\u8bc6\u522b\u7684\u8bed \u8a00\u7c7b\u578b 1536 \u8868\u793a\u666e\u901a\u8bdd \n                        ) \n    print result \n \n\n \n \n # \u89e3\u6790\u8fd4\u56de\u503c\uff0c\u6253\u5370\u8bed\u97f3\u8bc6\u522b\u7684\u7ed3\u679c \n    if result&#91;'err_msg']=='success.': \n        word = result&#91;'result']&#91;0].encode('utf-8')       # utf-8 \u7f16\u7801 \n        if word!='': \n            if word&#91;len(word)-3:len(word)]=='\uff0c': \n                print word&#91;0:len(word)-3] \n                with open('demo.txt','w') as f: \n                    f.write(word&#91;0:len(word)-3]) \n                f.close() \n            else: \n                print (word.decode('utf-8').encode('gbk')) \n                with open('demo.txt','w') as f: \n                    f.write(word) \n                f.close() \n        else: \n            print \"\u97f3\u9891\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u683c\u5f0f\u9519\u8bef\" \n    else: \n        print \"\u9519\u8bef\" \n \n# main \u51fd\u6570 \u8bc6\u522b\u672c\u5730\u5f55\u97f3\u6587\u4ef6 yahboom.wav \nif __name__ == '__main__': \n     \n    stt('test.wav') \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u63a5\u5165\u56fe\u7075\u673a\u5668\u4eba<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u56fe\u7075\u673a\u5668\u4eba\uff0c\u5982\u4e0b\uff1a<br><img decoding=\"async\" alt=\"Alt\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718153955142.png?x-oss-process=image\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTgxMzM1,size_16,color_FFFFFF,t_70\"><mark>\u6ce8\u610f\uff0c\u5bc6\u94a5\u8981\u4fdd\u6301\u5982\u4e0b\u9690\u85cf\u72b6\u6001\uff1a<\/mark><br><img decoding=\"async\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" src=\"https:\/\/img-blog.csdnimg.cn\/20190718154212439.png\"><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6d4b\u8bd5\u5224\u65ad\u56fe\u7075\u673a\u5668\u4eba\u662f\u5426\u8c03\u7528\u6210\u529f<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*- \nimport urllib \nimport json \ndef getHtml(url): \npage = urllib.urlopen(url) \nhtml = page.read() \n \nreturn html \nif __name__ == '__main__': \nkey = '\u4f60\u7684 api key' \napi = 'http:\/\/www.tuling123.com\/openapi\/api?key=' + key + '&amp; \n \ninfo=' \nwhile True: \ninfo = raw_input('\u6211: ') \nrequest = api + info \nresponse = getHtml(request) \ndic_json = json.loads(response) \nprint '\u673a\u5668\u4eba: '.decode('utf-8') + dic_json&#91;'text']| \n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u56fe\u7075\u673a\u5668\u4eba\u8c03\u7528\u7684\u4ee3\u7801\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>def AskTuling(tuling_api, tuling_key, content1): \n    text_input = content1 \n    req = { \n        \"perception\": \n    { \n        \"inputText\": \n        { \n            \"text\": text_input \n        }, \n        \"selfInfo\": \n        { \n            \"location\": \n            { \n                \"city\": \"\u5f90\u5dde\", \n                \"province\": \"\u6c5f\u82cf\", \n                \"street\": \"\u5927\u5b66\u8def\" \n            } \n        } \n    }, \n    \"userInfo\":  \n     { \n        \"apiKey\": \"d4c739c4b5b7422ab15dcc286b8b6734\", \n        \"userId\": \"123\" \n    } \n    } \n    # \u5c06\u5b57\u5178\u683c\u5f0f\u7684 req \u8f6c\u4e3a utf8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32 \n    req = json.dumps(req,cls=MyEncoder,indent=4).encode('utf8') \n    print('\\n' + '\u6b63\u5728\u8c03\u7528\u56fe\u7075\u673a\u5668\u4eba API...') \n    http_post = urllib.request.Request(tuling_api, data=req, headers={'content-type': 'application\/json'}) \n    response = urllib.request.urlopen(http_post) \n    print('\u5f97\u5230\u56de\u7b54\uff0c\u8f93\u51fa\u4e3a\u5b57\u5178\u683c\u5f0f\uff1a') \n    response_str = response.read().decode('utf8') \n    response_dic = json.loads(response_str) \n    intent_code = response_dic&#91;'intent']&#91;'code'] \n    # \u8fd4\u56de\u7f51\u9875\u7c7b\u7684\u8f93\u51fa\u65b9\u5f0f \n    if(intent_code == 10023): \n        results_url = response_dic&#91;'results']&#91;0]&#91;'values']&#91;'url'] \n        results_text = response_dic&#91;'results']&#91;1]&#91;'values']&#91;'text'] \n        answer = {\"code\": intent_code, \"text\": results_text, \"url\":results_url} \n        print(answer) \n        return(results_text) \n    # \u4e00\u822c\u7684\u8f93\u51fa\u65b9\u5f0f \n    else: \n        results_text = response_dic&#91;'results']&#91;0]&#91;'values']&#91;'text'] \n        answer = {\"code\": intent_code, \"text\": results_text} \n        print(answer) \n        return(results_text) \nif __name__ == '__main__': \n    while True: \n        record_to_file('demo.wav') \n        voice_text = baiduASR() \n        print ('voice_text:', voice_text) \n        answer_text = AskTuling(TULING_API, TULING_KEY, voice_text) \n        if baiduTTS(answer_text): \n            os.system(\"cvlc --play-and-exit %s\" % BAIDU_TTS_MP3) <\/code><\/pre>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/blog.csdn.net\/qq_43581335\/article\/details\/96433550\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/blog.csdn.net\/qq_43581335\/article\/details\/96433550<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"535\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u3010\u5c0f\u767d\u6559\u7a0b\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python_Python_\u3010\u5c0f\u767d\u6559\u7a0b\u3011\u57fa\u4e8e\u6811\u8393\u6d3e\u7684\u667a\u80fd\u8bed\u97f3\u52a9\u624b-python-CSDN\u535a\u5ba2 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-10"],"_links":{"self":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=535"}],"version-history":[{"count":0,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iotch.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}