def __init__(self, input_file_name, output_file_name): self.configure = Configure(0.7) self.output_file_name = output_file_name self.frames = wave.open(input_file_name, "rb") self.binary_wav_data = self.frames.readframes(self.frames.getnframes()) # 如果if False 那么就没有 digit_wav_data 了 if self.configure.FORMAT == pyaudio.paInt16: self.digit_wav_data = np.fromstring(self.binary_wav_data, dtype=np.short) else: print("Error!->Configure.FORMAT != pyaudio.paInt16:") sys.exit(-1) self.input_size = len(self.digit_wav_data) self.record_seconds = self.configure.RECORD_SECONDS self.rate = self.configure.RATE self.output_size = int(self.rate * self.record_seconds) #存储处理之后的结果,预测len(self.wave_data_after)==self.output_size self.wave_data_after = [] self.run() self.save_file()
def save_file(self): c=Configure() wf = wave.open(self.output_file_name, 'wb') wf.setnchannels(c.CHANNELS) wf.setsampwidth(pyaudio.get_sample_size(c.FORMAT)) wf.setframerate(c.RATE) wf.writeframes(b''.join(self.wave_data_after)) wf.close()
def __init__(self): self.counts = 1 self.cur_num=0#本次录音的编号,0对应“前进”(labels) self.labels=["前进","后退","左转","右转","停止","启动"] self.format_dir() self.label_len=len(self.labels) # 对应编号的次数 self.times=[1]*len(self.labels) self.user_name="" self.font_size=12 self.window = Tk() # 声明一个bar用来显示录音的进程 self.pro = Progressbar(self.window, length=200, style='black.Horizontal.TProgressbar') # labels: self.lb_name = Label(self.window, text="您的名字设置:", font=("Arial Bold", self.font_size)) self.lb_pro_left = Label(self.window, text="进度条:", font=("Arial Bold", self.font_size)) self.lb_pro_right = Label(self.window, text="0%", font=("Arial Bold", self.font_size)) # 需要设置text self.lb_please = Label(self.window,foreground='red',text="请说:"+self.labels[0], font=("Arial Bold", self.font_size)) # 这个设置成第i遍说相同的词,需要在初始化中进行更改 self.lb_times = Label(self.window, text="第1次", font=("Arial Bold", self.font_size)) self.lb_time_set = Label(self.window, text="时间长度设置:", font=("Arial Bold", self.font_size)) #Entry控件 self.et_name=Entry(self.window,width=15) self.et_name.insert(0,'default') self.et_time=Entry(self.window,width=15) self.et_time.insert(0, '1.5') self.begin_button=Button(self.window,text="录音",command=self.record_run) self.save_button=Button(self.window,text="保存",command=self.save_wav) #设置按钮的初始化状态 self.save_button['state']='disable' # 保存每次录音的中间结果,然后决定是不是要保存。 self.frames=[] self.p=[] self.configure=Configure() # 初始化每个组件的位置 self.place_all()
def run(self, binary_str, unit_time): """ :param binary_str: 外层录音所得到的二进制的字符串 :return: 返回识别的结果集合 """ # digit_data = np.fromstring(binary_str, np.int16) # plt.plot(list(range(len(digit_data))),digit_data) # plt.show() c = Configure(unit_time=unit_time) wf = wave.open('before.wav', 'wb') wf.setnchannels(c.CHANNELS) wf.setsampwidth(pyaudio.get_sample_size(c.FORMAT)) wf.setframerate(c.RATE) wf.writeframes(binary_str) wf.close() print("Identify ...") print("录音的时间长度 %.2f" % (len(binary_str) / 32000)) p = ShorterWav('before.wav', 'after.wav') result_dict = self.get_result('after.wav') # 识别语音指令 return result_dict
def recoder_and_decide(unit_time=0.3): pa = PyAudio() config = Configure(unit_time=unit_time) times = 0 result_before = [] binary_str = b"" pre_str = b"" temp_binary_str = b"" print("begin_____________________") is_recording,pre_is_recording = False, False test_zcr_count = 0 while times < 100 or True: stream = pa.open(format=config.FORMAT, channels=config.CHANNELS, rate=config.RATE, input=True, frames_per_buffer=config.CHUNK) frames = [] times += 1 pre_is_recording = is_recording pre_str = temp_binary_str stream.start_stream() for i in range(0, config.get_count_chunk()): data = stream.read(config.CHUNK) frames.append(data) stream.stop_stream() temp_binary_str = b''.join(frames) digit_data = np.fromstring(temp_binary_str, np.int16) zcr = ZCR(digit_data) zcr_flag,energy_flag = zcr.get_weather_start_record_state() is_recording = energy_flag or zcr_flag # is_recording = zcr_flag or energy_flag print(zcr_flag,energy_flag) if zcr_flag: test_zcr_count += 1 # print("zcr_rate = {},max_energy = {}".format(max(zcr_rate),max(energy))) # zcr_flag = max(zcr_rate) > zcr.zcr_threshold # energy_flag = max(energy) > zcr.energy_threshold # print("max_zcr={},max_energy={}".format(max(zcr_rate),max(energy))) # print("t={}s,z={},e={}".format(times*config.RECORD_SECONDS,zcr_flag,energy_flag)) # if zcr_flag or energy_flag: # is_recording = True # else # is_recording = False # result_before += list(digit_data) if is_recording: # print(type(binary_str),type(temp_binary_str)) if not pre_is_recording: binary_str = pre_str binary_str += temp_binary_str print("Recording,time=%s" % str(len(binary_str)/32000)) print("\n__________________________________________") # print("Recording@@@{}@@@{}".format(zcr_flag,energy_flag)) elif pre_is_recording: # stop recording and start recognizing print("test_zcr_count = %d" % test_zcr_count) test_zcr_count = 0 print("Recognizing......") sp = Recognition('./969_914.pb', './conv_labels.txt') recognized_dict = sp.run(binary_str,unit_time) print("识别结果:{}".format(recognized_dict)) binary_str = b"" else: print("Finding") # print("Finding+++{}+++{}".format(zcr_flag,energy_flag)) stream.stop_stream() stream.close() pa.terminate() return None
def read_commit_file(): with open(conf.commit_database) as f: content = f.readlines() set_log_files() content = [x.strip() for x in content] for line_number, line in enumerate(content, start=conf.start_bug): split_content = line.split(',') bug_version = split_content[conf.bug_ind] fix_version = split_content[conf.fix_ind] if conf.start_bug <= line_number <= conf.end_bug: try: run_conf = RunConfigure(conf.root_dir, conf.project_name, conf.clone_name, line_number, fix_version, bug_version) if exists(run_conf.trace_file): run_prediction(line_number, fix_version, bug_version, run_conf) except Exception as e: traceback.print_exc() write_to_log_error(e, str(line_number)) if __name__ == '__main__': if len(sys.argv) == 4: conf = Configure(sys.argv[1], sys.argv[2], sys.argv[3]) file_address = FilesAddress(conf.root_dir) read_commit_file()