Created
June 12, 2019 19:25
-
-
Save jesseengel/dedb8bc19212d4b982fb51f879728a0d to your computer and use it in GitHub Desktop.
Wav2TFRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cStringIO import StringIO | |
import os | |
import numpy as np | |
import scipy.io.wavfile | |
import tensorflow as tf | |
output_path = os.path.expanduser('~/Desktop/test.tfrecord') | |
sample_path = os.path.expanduser('~/Desktop/test.wav') | |
record_writer = tf.python_io.TFRecordWriter(output_path) | |
wav_contents = tf.gfile.Open(sample_path).read() | |
sample_rate, audio = scipy.io.wavfile.read(StringIO(wav_contents)) | |
# Put in range [-1, 1] | |
float_normalizer = float(np.iinfo(np.int16).max) | |
audio = audio / float_normalizer | |
# Convert to mono | |
audio = np.mean(audio, axis=-1) | |
print(audio) | |
print(audio.shape) | |
example = tf.train.Example(features=tf.train.Features(feature={ | |
'sample_rate': tf.train.Feature( | |
int64_list=tf.train.Int64List(value=[sample_rate])), | |
'audio': tf.train.Feature(float_list=tf.train.FloatList(value=audio.tolist())), | |
})) | |
record_writer.write(example.SerializeToString()) |
Sorry don't know exactly what that is. Seems like the "sample_path" can't
be decoded with utf-8 (might be a python 2/3 thing or a mac path vs. pc
path).
Good luck,
Jesse
…On Tue, Jul 16, 2019 at 7:50 AM Valentin Pere ***@***.***> wrote:
Hello jesseengel and thank you for your code.
I tried this to convert a wav file to tfrecord but I have this error :
[image: image]
<https://user-images.githubusercontent.com/50982703/61304573-e9468c00-a7d8-11e9-90d4-ce03887c3789.png>
Do you know where does that come from ? I did few research on the net but
couldn't find an answer ...
Thanks
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/dedb8bc19212d4b982fb51f879728a0d?email_source=notifications&email_token=AANFCCPA7N26UDHIWE774RTP7XN4FA5CNFSM4IEB2EW2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFVMPS#gistcomment-2971897>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AANFCCNHY5PDORDLTXEH6TTP7XN4FANCNFSM4IEB2EWQ>
.
if we use BytesIO for this, will it work?? @jesseengel
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello jesseengel and thank you for your code.
I tried this to convert a wav file to tfrecord but I have this error :
Do you know where does that come from ? I did few research on the net but couldn't find an answer ...
Thanks