def _read_output(self): for i, fname in enumerate(self._output_file_list): \ fname = fname.split('num')[-1].split('.raw')[0] if self._dataindx == fname: # print(self._output_file_list[i]) dataname = os.path.dirname(self._output_file_list[i]) + \ '/' + os.path.basename(self._output_file_list[i]).split('.')[0] + '.bin' if os.path.exists(dataname): feat_spec = np.load(dataname.replace('.bin', '.npy')).item() feat_shape = feat_spec['shape'] feat_max = feat_spec['max'] feat = utils.read_raw(dataname).reshape( feat_shape) * np.float32(feat_max) else: data = utils.read_raw(self._output_file_list[i]) if config.parallel: feat = self.lpsd_dist_p( data, self._dist_num, is_patch=False ) # (The number of samples, config.freq_size, 1, 1) else: feat = self.lpsd_dist( data, self._dist_num, is_patch=False ) # (The number of samples, config.freq_size, 1, 1) utils.write_bin(feat, np.max(np.abs(feat)), dataname) feat_spec = { 'shape': feat.shape, 'max': np.max(np.abs(feat)) } np.save(dataname.replace('.bin', ''), feat_spec) # plt.subplot(212) # S = librosa.amplitude_to_db( # librosa.stft(data, hop_length=config.win_step, # win_length=config.win_size, n_fft=config.nfft), ref=np.max) # ld.specshow(S, y_axis='linear', hop_length=config.win_step, sr=config.fs) # # plt.show() # data, _ = librosa.load(self._output_file_list[i], config.fs) break # print('output') # if self._is_shuffle: # # feat = np.reshape(feat, (-1, self._batch_size, feat.shape[1], feat.shape[2], feat.shape[3])) # feat = np.reshape(feat[self._perm_indx, :], (-1, config.freq_size, 1, 1)) '''''' if self._is_shuffle: feat = feat[self._perm_indx, :] return np.squeeze(feat)
def format(self, raw): ''' Raw is the output of the psycopg2 query, a list of lists of lists etc... ''' data = Data() data.key = [row[0] for row in raw] data.data_id = [row[1] for row in raw] data.filename = [row[3] for row in raw] #The follwing writes each binary to a file for row in raw: write_bin(self.write_bin_dir + row[3], row[2]) return data
def face_register(self, cvImg, faceId): save_path = self.faceDataDir + str(faceId) if not os.path.exists(save_path): os.mkdir(save_path) retDict = {} featFn = save_path + '/feat.bin' feat_dict = self.face_feature(cvImg) if feat_dict['flag'] != 'failure': write_bin(feat_dict['feature'], featFn) retDict['flag'] = 'successful' else: retDict['flag'] = 'feature' imgFn = save_path + '/face.jpg' cv2.imwrite(imgFn, cvImg) return retDict
def prepare_args(input_no, poc, poc_fmt): global TmpFolder # prepare the arguments arg_num = len(poc_fmt) arg_list = [] for arg_no in range(arg_num): if poc_fmt[arg_no][0] == 'bfile': # write the list into binary file content = np.asarray(poc[poc_fmt[arg_no][1]: poc_fmt[arg_no][1]+poc_fmt[arg_no][2]]).astype(np.int) tmp_filepath = os.path.join(TmpFolder, 'tmp_%d' % input_no) utils.write_bin(tmp_filepath, content) arg_list.append(tmp_filepath) elif poc_fmt[arg_no][0] == 'int': arg_list.append(int(poc[poc_fmt[arg_no][1]])) elif poc_fmt[arg_no][0] == 'float': arg_list.append(float(poc[poc_fmt[arg_no][1]])) elif poc_fmt[arg_no][0] == 'str': # concatenate all the chars together arg_list.append(''.join(poc[poc_fmt[arg_no][1]: poc_fmt[arg_no][1]+poc_fmt[arg_no][2]])) else: raise Exception("ERROR: Unknown poc_fmt -> %s" % poc_fmt[arg_no][0]) return arg_list
def _read_input(self, input_file_dir): # print("num_file: %.2d" % self._num_file) # self._dataindx = input_file_dir.split("num")[1].split('.')[0] # dataname = os.path.dirname(input_file_dir) + '/' + os.path.basename(input_file_dir).split('.')[0] + '.npy' if self._is_training: # print("num_file: %.3d" % self._num_file) self._dataindx = input_file_dir.split("num")[1].split('.')[0] dataname = os.path.dirname( input_file_dir) + '/' + os.path.basename(input_file_dir).split( '.')[0] + '.bin' if os.path.exists(dataname): # print("num_file: %.2d load..." % self._num_file) feat_spec = np.load(dataname.replace('.bin', '.npy')).item() feat_size = feat_spec['feat_size'] feat_shape = feat_spec['feat_shape'] phase_shape = feat_spec['phase_shape'] feat_max = feat_spec['max'] feat_phase = utils.read_raw(dataname) * np.float32(feat_max) feat = feat_phase[0:feat_size].reshape(feat_shape) phase = feat_phase[feat_size:].reshape(phase_shape) # print("num_file: %.2d load... done" % self._num_file) else: print("num_file: %.2d feature extraction..." % self._num_file) # data, _ = librosa.load(input_file_dir, config.fs) data = utils.read_raw(input_file_dir) # plt.subplot(211) # # S = librosa.amplitude_to_db( # librosa.stft(data, hop_length=config.win_step, # win_length=config.win_size, n_fft=config.nfft), ref=np.max) # ld.specshow(S, y_axis='linear', hop_length=config.win_step, sr=config.fs) if config.parallel: feat, phase = self.lpsd_dist_p(data, self._dist_num, is_patch=True) else: feat, phase = self.lpsd_dist(data, self._dist_num, is_patch=True) feat_size = feat.size feat_phase = np.concatenate( (feat.reshape(-1), phase.reshape(-1))) utils.write_bin(feat_phase, np.max(np.abs(feat_phase)), dataname) feat_spec = { 'feat_size': feat_size, 'phase_shape': phase.shape, 'feat_shape': feat.shape, 'max': np.max(np.abs(feat_phase)) } np.save(dataname.replace('.bin', ''), feat_spec) print("num_file: %.2d feature extraction... done." % self._num_file) else: data = np.load(input_file_dir) # data, _ = librosa.load(input_file_dir, config.fs) if config.parallel: feat, phase = self.lpsd_dist_p(data, self._dist_num) else: feat, phase = self.lpsd_dist(data, self._dist_num) # feat shape: (num samples, config.time_width, config.freq_size, 1) # if self._is_shuffle: # feat = np.reshape(feat, (-1, self._batch_size, feat.shape[1], feat.shape[2], feat.shape[3])) # self._perm_indx = perm_indx = np.random.permutation(feat.shape[0]) # feat = np.reshape(feat[perm_indx, :], (-1, config.time_width, config.freq_size, 1)) '''''' if self._is_shuffle: self._perm_indx = perm_indx = np.random.permutation(feat.shape[0]) feat = feat[perm_indx, :] self.num_samples = np.shape(feat)[0] if self._is_val: phase = np.zeros(feat.shape) return feat, phase
fs=config.fs) # plt.plot(recon_speech) # plt.show() # lab = np.reshape(np.asarray(lab), [-1, 1]) test_dr.reader_initialize() break return recon_speech if __name__ == '__main__': input_dir = os.path.abspath('./data/train/noisy') logs_dir = os.path.abspath('./logs' + '/logs_' + time.strftime("%Y-%m-%d-%H-%M-%S")) model_dir = os.path.abspath('./enhanced_wav/enhance_model') save_dir = os.path.abspath('./enhanced_wav') gs.freeze_graph(logs_dir, model_dir, 'model_1/pred,model_1/labels,model_1/cost') input_file_list = sorted(glob.glob(input_dir + '/*.raw')) graph_name = sorted(glob.glob(save_dir + '/*.pb'))[-1] norm_path = os.path.abspath('./data/train/norm') for wav_dir in input_file_list: recon_speech = speech_enhance(wav_dir, graph_name) file_dir = save_dir + '/' + os.path.basename(wav_dir).replace( 'noisy', 'enhanced') utils.write_bin(recon_speech, np.max(np.abs(recon_speech)), file_dir)