Scipy Read Gray Image to 3 Channel

Python scipy.io.wavfile.read() Examples

The post-obit are 30 lawmaking examples for showing how to employ scipy.io.wavfile.read() . These examples are extracted from open source projects. You can vote upwardly the ones you like or vote downward the ones you lot don't like, and go to the original project or source file by following the links higher up each instance.

You may check out the related API usage on the sidebar.

Y'all may also desire to check out all bachelor functions/classes of the module scipy.io.wavfile , or endeavour the search function .

Instance 1

def fbank(wav_path, apartment=True):     """ Currently grabs log Mel filterbank, deltas and double deltas."""      (charge per unit, sig) = wav.read(wav_path)     if len(sig) == 0:         logger.warning("Empty wav: {}".format(wav_path))     fbank_feat = python_speech_features.logfbank(sig, rate, nfilt=xl)     energy = extract_energy(rate, sig)     feat = np.hstack([energy, fbank_feat])     delta_feat = python_speech_features.delta(feat, 2)     delta_delta_feat = python_speech_features.delta(delta_feat, 2)     all_feats = [feat, delta_feat, delta_delta_feat]     if non flat:         all_feats = np.array(all_feats)         # Make fourth dimension the offset dimension for easy length normalization padding         # after.         all_feats = np.swapaxes(all_feats, 0, 1)         all_feats = np.swapaxes(all_feats, 1, ii)     else:         all_feats = np.concatenate(all_feats, axis=1)      # Log Mel Filterbank, with delta, and double delta     feat_fn = wav_path[:-3] + "fbank.npy"     np.salvage(feat_fn, all_feats)          

Example ii

def wavfile_to_examples(wav_file):     """Convenience wrapper around waveform_to_examples() for a common WAV format.      Args:       wav_file: Cord path to a file, or a file-like object. The file       is assumed to comprise WAV audio information with signed 16-bit PCM samples.      Returns:       Come across waveform_to_examples.     """     try:         wav_file = BytesIO(wav_file)         sr, wav_data = wavfile.read(wav_file)     except IOError:         print("Error reading WAV file!")         print("The specified WAV file type is non supported past scipy.io.wavfile.read()")         sys.exit(ane)      if wav_data.dtype != np.int16:         raise TypeError('Bad sample type: %r' % wav_data.dtype)     samples = wav_data / 32768.0  # Convert to [-1.0, +i.0]     return waveform_to_examples(samples, sr)          

Example 3

def wavefile_to_waveform(wav_file, features_type):     data, sr = sf.read(wav_file)     if features_type == 'vggish':         tmp_name = str(int(np.random.rand(1)*1000000)) + '.wav'         sf.write(tmp_name, data, sr, subtype='PCM_16')         sr, wav_data = wavfile.read(tmp_name)         os.remove(tmp_name)         # sr, wav_data = wavfile.read(wav_file) # as washed in VGGish Audioset         affirm wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype         data = wav_data / 32768.0  # Catechumen to [-1.0, +1.0]        # at to the lowest degree one second of samples, if not repead-pad     src_repeat = information     while (src_repeat.shape[0] < sr):          src_repeat = np.concatenate((src_repeat, data), axis=0)         data = src_repeat[:sr]      return data, sr          

Example iv

def plotstft(audiopath, binsize=2**ten, plotpath=None, colormap="greyness", aqueduct=0, proper noun='tmp.png', alpha=1, kickoff=0):     samplerate, samples = wav.read(audiopath)     samples = samples[:, aqueduct]     due south = stft(samples, binsize)      sshow, freq = logscale_spec(s, cistron=1, sr=samplerate, alpha=blastoff)     sshow = sshow[2:, :]     ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel     timebins, freqbins = np.shape(ims)          ims = np.transpose(ims)     # ims = ims[0:256, offset:first+768] # 0-11khz, ~9s interval     ims = ims[0:256, :] # 0-11khz, ~10s interval     #impress "ims.shape", ims.shape          prototype = Image.fromarray(ims)      image = prototype.catechumen('L')     prototype.save(name)          

Instance 5

def read_file(filename):     """     Read moving ridge file as mono.      Args:         - filename (str) : wave file / path.      Returns:         tuple of sampling rate and audio data.     """     fs, sig = read(filename=filename)     if (sig.ndim == 1):         samples = sig     else:         samples = sig[:, 0]     return fs, samples          

Case half dozen

def triangle(t, randfunc=np.random.rand, t0_fac=None): # ramp up and then downwards     acme = (0.4 * randfunc() + 0.4) * np.random.choice([-1,i])     width = randfunc()/4 * t[-i]     # half-width really     t0 = 2*width + 0.4 * randfunc()*t[-ane] if t0_fac is None else t0_fac*t[-1]     x = acme * (1 - np.abs(t-t0)/width)     x[np.where(t < (t0-width))] = 0     x[np.where(t > (t0+width))] = 0     amp_n = (0.1*randfunc()+0.02)   # add together racket     return x + amp_n*pinknoise(t.shape[0])   # Prelude to read_audio_file # Tried lots of ways of doing this.. nearly are dull. #betoken, rate = librosa.load(filename, sr=sr, mono=True, res_type='kaiser_fast') # Librosa'southward reader is incredibly wearisome. do not use #signal, rate = torchaudio.load(filename)#, normalization=True)   # Torchaudio'southward reader is pretty fast but normalization is a trouble #signal = signal.numpy().flatten() #reader = io_methods.AudioIO   # Stylios' file reader. Haven't gotten it working yet #betoken, rate = reader.audioRead(filename, mono=Truthful) #signal, charge per unit = sf.read('existing_file.wav')          

Example seven

def __init__(self, path, sr=44100, ):         super(FileEffect, cocky).__init__()         print("  FileEffect: path = ",path)         if (path is None) or (not glob.glob(path+"/Railroad train/target*")) \             or (not glob.glob(path+"/Val/target*")) or ((not glob.glob(path+"/effect_info.ini"))):             print(f"Error: tin't file target output files or effect_info.ini in path = {path}")             sys.exit(ane)   # Yea, this is fatal          self.sr = sr         # read the effect info config file  "effect_info.ini"         config = configparser.ConfigParser()         config.read(path+'/effect_info.ini')         self.proper noun = config['event']['proper noun']+"(files)"   # tack on "(files)" to the outcome name         #TODO: note that employ of 'eval' below could be a potential security issue         self.knob_names = eval(config.get("consequence","knob_names"))         cocky.knob_ranges = np.array(eval(config.get("consequence","knob_ranges")))         try:             self.is_inverse = (Truthful == bool(config['event']['changed']) )             self.proper name = "De-"+cocky.proper noun         except:             pass   # Ignore errors we don't require that 'inverse' exist defined anywhere in the file          

Case viii

def wavread(fn):     """Emulate the parts of the matlab wavread function that we need.          y, Fs = wavread(fn)      y is the vector of sound samples, Fs is the frame rate.      Matlab's wavread is used by voicesauce to read in the wav files for     processing.  As a consequence, all the translated algorithms presume the     information from the wav file is in matlab form, which in this case means a double     precision float between -1 and 1.  The corresponding scipy function returns     the bodily integer PCM values from the file, which range betwixt -32768 and     32767.  (matlab's wavread *can* return the integers, simply does not past     default and voicesauce uses the default).  Consequently, after reading the     data using scipy's io.wavfile, we catechumen to bladder by dividing each integer     past 32768.      """     # For reference, I figured this out from:     # http://mirlab.org/jang/books/audiosignalprocessing/matlab4waveRead.asp?title=4-2%20Reading%20Wave%20Files     # Thirty: if we need to handle 8 bit files we'll need to notice them and     # special example them here.     Fs, y = wavfile.read(fn)     return y/numpy.float64(32768.0), Fs          

Instance nine

def test_read_1():     for mmap in [False, True]:         warn_ctx = WarningManager()         warn_ctx.__enter__()         try:             warnings.simplefilter('ignore', wavfile.WavFileWarning)             rate, data = wavfile.read(datafile('test-44100-le-1ch-4bytes.wav'),                                       mmap=mmap)         finally:             warn_ctx.__exit__()          assert_equal(rate, 44100)         assert_(np.issubdtype(data.dtype, np.int32))         assert_equal(information.shape, (4410,))          del information          

Example 10

def detect_MIDI_notes(cocky):         """             The algorithm for computing midi notes from a given wav file.         """          (framerate, sample) = wav.read(self.wav_file)         # We need to alter the ii channels into ane considering STFT works merely         # for 1 channel. Nosotros could also do STFT for each channel separately.         monoChannel = sample.mean(centrality=1)         duration = getDuration(self.wav_file)         midi_notes = []          # Consider only files with a duration longer than 0.2 seconds.         if duration > 0.xviii:             frequency_power = self.calculateFFT(duration, framerate, monoChannel)             filtered_frequencies = [f for (f, p) in frequency_power]             #cocky.plot_power_spectrum(frequency_power)             #self.plot_power_spectrum_dB(frequency_power)             f0_candidates = self.get_pitch_candidates_remove_highest_peak(frequency_power)             midi_notes = self.matchWithMIDINotes(f0_candidates)         return midi_notes          

Case 11

def get_data():     """The data files come from the TSP dataset 16k set"""     _, human = wavfile.read('./wav_data/MA02_04.wav')     charge per unit, woman = wavfile.read('./wav_data/FA01_03.wav')     man = human.astype('float32')     woman = woman.astype('float32')     man_max = np.max(man)     woman_max = np.max(woman)     man /= man_max     woman /= woman_max     shortest = min(len(man), len(woman))     woman = woman[:shortest]     man = human being[:shortest]     np.random.seed(101)     dissonance = np.random.uniform(-one, 1, len(man))     sources = np.stack((woman, man, noise))     A = np.random.uniform(-1, 1, (3, three))     linear_mix = np.dot(A, sources)     pnl_mix = linear_mix.copy()     pnl_mix[0] = np.tanh(pnl_mix[0])     pnl_mix[1] = (pnl_mix[ane] + pnl_mix[i]**3) / 2     pnl_mix[two] = np.exp(pnl_mix[2])     return linear_mix, pnl_mix, A, sources          

Example 12

def mainParkinson(): 	general_feature_list = [] 	general_label_list = [] 	folder = raw_input('Give the proper noun of the folder that you lot desire to read data: ') 	if(folder == 'PD'): 		healthyCases = os.listdir('/habitation/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/PD') 		for x in healthyCases: 			wav = '/'+binder+'/'+str(10) 			mfcc_features,inputWav = mfcc_features_extraction(wav) 			mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list) 		folder = raw_input('Give the name of the folder that yous want to read information: ') 		if(binder == 'HC'): 			parkinsonCases = os.listdir('/dwelling house/gionanide/Theses_2017-2018_2519/Gkagkos/Audio_Files/HC') 			for 10 in parkinsonCases: 				wav = '/'+folder+'/'+str(x) 				mfcc_features,inputWav = mfcc_features_extraction(wav) 				mean_features(mfcc_features,inputWav,folder,general_feature_list,general_label_list) 		#print general_feature_list, general_label_list 		#writeFeatures(general_feature_list,general_label_list,wav,folder) 		classifyPHC(general_feature_list,general_label_list)          

Example thirteen

def load_wav(input_wav_file):     # Load the inputs that we're given     fs, audio = wav.read(input_wav_file)     assert fs == 16000     print('source dB', db(audio))     return audio          

Example fourteen

def load(path):         bps, data = wav.read(path)         if len(data.shape) != 1:             data = data[:,0] + information[:,ane]         return bps, information          

Example 15

def load(path):         bps, data = wav.read(path)         if len(information.shape) != 1:             information = data[:,0] + data[:,1]         return bps, data          

Case xvi

def load(path):     bps, data = wav.read(path)     if len(data.shape) != 1:         data = data[:,0] + data[:,1]     return bps, data          

Example 17

def encode(self, south):     """Transform a string with a filename into a list of float32.      Args:       s: path to the file with a waveform.      Returns:       samples: listing of int16s     """     # Brand sure that the data is a single channel, 16bit, 16kHz wave.     # TODO(chorowski): the directory may not be writable, this should fallback     # to a temp path, and provide instructions for installing sox.     if s.endswith(".mp3"):       # TODO(dliebling) On Linux, cheque if libsox-fmt-mp3 is installed.       out_filepath = southward[:-4] + ".wav"       call([           "sox", "--guard", s, "-r", "16k", "-b", "16", "-c", "1", out_filepath       ])       s = out_filepath     elif not southward.endswith(".wav"):       out_filepath = s + ".wav"       if not os.path.exists(out_filepath):         telephone call(["sox", "-r", "16k", "-b", "16", "-c", "1", s, out_filepath])       southward = out_filepath     rate, information = wavfile.read(southward)     assert rate == cocky._sample_rate     affirm len(data.shape) == 1     if information.dtype non in [np.float32, np.float64]:       data = data.astype(np.float32) / np.iinfo(information.dtype).max     return data.tolist()          

Example 18

def plotstft(audiopath, binsize=2**10, plotpath=None, colormap="gray", channel=0, name='tmp.png', blastoff=i, offset=0):     samplerate, samples = wav.read(audiopath)     samples = samples[:, channel]     s = stft(samples, binsize)      sshow, freq = logscale_spec(s, factor=one, sr=samplerate, alpha=blastoff)     sshow = sshow[ii:, :]     ims = 20.*np.log10(np.abs(sshow)/10e-6) # amplitude to decibel     timebins, freqbins = np.shape(ims)          ims = np.transpose(ims)     ims = ims[0:256, outset:get-go+768] # 0-11khz, ~9s interval     #print "ims.shape", ims.shape          image = Image.fromarray(ims)      image = image.convert('L')     epitome.save(name)          

Example xix

def load_vgmwav(wav_fp):   fs, wav = wavread(wav_fp)   assert fs == 44100   if wav.ndim == 2:     wav = wav[:, 0]   wav = wav.astype(np.float32)   wav /= 32767.   render wav          

Example 20

def wavfile_to_examples(wav_file):   """Convenience wrapper around waveform_to_examples() for a common WAV format.    Args:     wav_file: String path to a file, or a file-like object. The file     is assumed to contain WAV audio data with signed 16-bit PCM samples.    Returns:     See waveform_to_examples.   """   sr, wav_data = wavfile.read(wav_file)   affirm wav_data.dtype == np.int16, 'Bad sample blazon: %r' % wav_data.dtype   samples = wav_data / 32768.0  # Convert to [-ane.0, +1.0]   return waveform_to_examples(samples, sr)          

Example 21

def process_file(wav_file):     sr, data = wavfile.read(wav_file)     if data.dtype != np.int16:         heighten TypeError('Bad sample type: %r' % data.dtype)      # local import to reduce start-up fourth dimension     from sound.processor import WavProcessor, format_predictions      with WavProcessor() every bit proc:         predictions = proc.get_predictions(sr, information)      print(format_predictions(predictions))          

Example 22

def rm_sil(voice_file, vad_obj):     """        This code snippet is basically taken from the repository            'https://github.com/wiseman/py-webrtcvad'         It removes the silence clips in a speech communication recording     """     audio, sample_rate = read_wave(voice_file)     frames = frame_generator(20, sound, sample_rate)     frames = list(frames)     segments = vad_collector(sample_rate, 20, l, vad_obj, frames)      if os.path.exists('tmp/'):        shutil.rmtree('tmp/')     bone.makedirs('tmp/')      wave_data = []     for i, segment in enumerate(segments):         segment_file = 'tmp/' + str(i) + '.wav'         write_wave(segment_file, segment, sample_rate)         wave_data.suspend(wavfile.read(segment_file)[1])     shutil.rmtree('tmp/')      if wave_data:        vad_voice = np.concatenate(wave_data).astype('int16')     return vad_voice          

Case 23

def get_f0s_from_list(conf, list_file, wav_dir):     """Get f0s from listfile      Parameters     ---------     conf : SpeakerYML,         Speaker dependent YAML class     listfile : str, path-like,         File path of the list file of the speaker     wav_dir : str, path-similar,         Directory path of the waveform      Returns     ---------     f0s : list, shape(`num_files`),         List of the f0s     """      # open list file     with open(list_file, 'r') as fp:         files = fp.readlines()      f0s = []     for f in files:         # open wave file         f = f.rstrip()         wavf = os.path.join(wav_dir, f + '.wav')         fs, ten = wavfile.read(wavf)         10 = np.array(x, dtype=np.float)         x = low_cut_filter(ten, fs, cutoff=70)         affirm fs == conf.wav_fs          print("Extract F0: " + wavf)         # constract FeatureExtractor clas         feat = FeatureExtractor(analyzer=conf.analyzer, fs=conf.wav_fs,                                 fftl=conf.wav_fftl, shiftms=conf.wav_shiftms,                                 minf0=conf.f0_minf0, maxf0=conf.f0_maxf0)         f0 = feat.analyze_f0(x)         f0s.append(f0)      return f0s          

Example 24

def test_shifter(self):         path = dirpath + '/data/test16000.wav'         fs, x = wavfile.read(path)         for f0rate in (0.5, 0.75, 1.0, 1.5, 2.0):             if f0rate < 1:                 completion = True             else:                 completion = Simulated             shifter = Shifter(fs, f0rate=f0rate, shiftms=ten)             transformed_x = shifter.f0transform(ten, completion=completion)             affirm len(x) == len(transformed_x)              if saveflag:                 fpath = path + str(f0rate) + '.wav'                 wavfile.write(fpath, fs, transformed_x.astype(np.int16))          

Case 25

def test_wsola(self):         path = dirpath + '/data/test16000.wav'         fs, x = wavfile.read(path)         for speech_rate in (0.5, 0.75, i.0, one.five, 2.0):             wsola = WSOLA(fs, speech_rate)             wsolaed_x = wsola.duration_modification(x)             assert int(len(x) / speech_rate) == len(wsolaed_x)              if saveflag:                 fpath = path + str(speech_rate) + '.wav'                 wavfile.write(fpath, fs, wsolaed_x.astype(np.int16))          

Example 26

def test_mod_power(self):         path = os.path.join(dirpath, 'information', 'test16000.wav')         fs, x = wavfile.read(path)         af = FeatureExtractor(analyzer='world', fs=fs, shiftms=5)         f0, _, ap = af.clarify(x)         mcep = af.mcep(dim=24, alpha=0.42)          rmcep = mcep         cvmcep = mcep * one.50          modified_cvmcep = mod_power(cvmcep, rmcep, alpha=0.42)          assert modified_cvmcep.shape == cvmcep.shape          

Instance 27

def test_anasyn_16000(self):         path = dirpath + '/data/test16000.wav'         fs, x = wavfile.read(path)         af = FeatureExtractor(analyzer='globe', fs=fs, shiftms=5, fftl=1024)         f0, spc, ap = af.analyze(x)         mcep = af.mcep(dim=24, alpha=0.42)          assert len(np.nonzero(f0)[0]) > 0         assert spc.shape == ap.shape          # synthesize F0, mcep, ap         synth = Synthesizer(fs=fs, fftl=1024, shiftms=5)         wav = synth.synthesis(f0, mcep, ap, alpha=0.42)         nun_check(wav)          

Example 28

def test_anasyn_44100(cocky):         path = dirpath + '/data/test44100.wav'         fs, ten = wavfile.read(path)         af = FeatureExtractor(analyzer='world', fs=fs, shiftms=5, minf0=100, fftl=2048)         f0, spc, ap = af.clarify(ten)         mcep = af.mcep(dim=twoscore, alpha=0.50)          affirm len(np.nonzero(f0)[0]) > 0         assert spc.shape == ap.shape          # mcep synthesis         synth = Synthesizer(fs=fs, fftl=2048, shiftms=5)         wav = synth.synthesis(f0, mcep, ap, blastoff=0.50)         nun_check(wav)          

Case 29

def test_spc_and_npow(self):         path = dirpath + '/data/test16000.wav'         fs, x = wavfile.read(path)         af = FeatureExtractor(analyzer='world', fs=fs, shiftms=5)         _, spc, _ = af.analyze(x)         npow = af.npow()         assert spc.shape[0] == npow.shape[0]          

Example 30

def _loadWAVWithScipy(fileName): 		""" Load samples & sample rate from WAV file """ 		inputData = read(fileName) 		samples = inputData[1] 		sampleRate = inputData[0]  		return samples, sampleRate          

mashburntheyinecaut.blogspot.com

Source: https://www.programcreek.com/python/example/93227/scipy.io.wavfile.read

0 Response to "Scipy Read Gray Image to 3 Channel"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel