#問題点
次のプログラムを実行したところ、次のエラーが発生しました。どうしたら良いでしょうか?
参考:https://qiita.com/kinpira/items/75513eaab6eed19da9a3
#エラーの内容
>>> python3 recognize.py Please input recoding time>>> 5 1 ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map 1 RATE = 16000 chunk = 1024 RECORD_SECONDS = 5 RATE / chunk * RECORD_SECONDS = 78.125 Traceback (most recent call last): File "recognize.py", line 61, in <module> data = ''.join(all) TypeError: sequence item 0: expected str instance, bytes found
#ソースコード
python
1# -*- coding: utf-8 -*- 2#マイク0番からの入力を受ける。一定時間(RECROD_SECONDS)だけ録音し、ファイル名:mono.wavで保存する。 3 4import pyaudio 5import sys 6import time 7import wave 8import requests 9import os 10import json 11 12def recognize(): 13 url = "https://api.apigw.smt.docomo.ne.jp/amiVoice/v1/recognize?APIKEY={}".format(APIKEY) 14 files = {"a": open(PATH, 'rb'), "v":"on"} 15 r = requests.post(url, files=files) 16 message = r.json()['text'] 17 print(message) 18 return message 19 20if __name__ == '__main__': 21 chunk = 1024 22 FORMAT = pyaudio.paInt16 23 CHANNELS = 1 24 #PATH = '/var/tmp/tmp.wav' 25 APIKEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #DocomoAPI Key 26 CARD = 1 #OUTPUTの指定 27 DEVICE = 0 #OUTPUTの指定 28 29 #サンプリングレート、マイク性能に依存 30 RATE = 16000 31 #録音時間 32 RECORD_SECONDS = input('Please input recoding time>>>') 33 34 #pyaudio 35 print("1") 36 p = pyaudio.PyAudio() 37 print("1") 38 #マイク0番を設定 39 input_device_index = 0 40 #マイクからデータ取得 41 stream = p.open(format = FORMAT, 42 channels = CHANNELS, 43 rate = RATE, 44 input = True, 45 frames_per_buffer = chunk) 46 47 all = [] 48 49 A = int(RATE) / int(chunk) * int(RECORD_SECONDS) 50 51 print("RATE = " + str(RATE)) 52 print("chunk = " + str(chunk)) 53 print("RECORD_SECONDS = " + str(RECORD_SECONDS)) 54 print("RATE / chunk * RECORD_SECONDS = " + str(A)) 55 print("") 56 for i in range(0, int(A)): 57 data = stream.read(chunk) 58 all.append(data) 59 60 stream.close() 61 data = ''.join(all) 62 out = wave.open(PATH,'w') 63 out.setnchannels(1) #mono 64 out.setsampwidth(2) #16bits 65 out.setframerate(RATE) 66 out.writeframes(data) 67 out.close() 68 69 p.terminate() 70 71 message = recognize() 72 print(message) 73 74#参考 75#https://qiita.com/kinpira/items/75513eaab6eed19da9a3

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/27 01:31