Skip to content

wav to tfrecord

Seung Ho, Choi edited this page Nov 24, 2019 · 2 revisions
import tensorflow as tf
import os
import librosa
def _bytes_feature(value):
    return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def create_tfrecord(data_path):
    print("Start converting...")
    options = tf.python_io.\
        TFRecordOptions(compression_type=tf.python_io.TFRecordCompressionType.GZIP)
    
    
    file_list = os.listdir(data_path)

    for i, file in enumerate(file_list):
        writer = tf.python_io.TFRecordWriter(\
        path="./source_data/tfrecord" + str(i),
        options=options
        )
        
        print(data_path + file)
        audio = librosa.load(data_path + file)
        print(type(audio[0]))

#         audio = wave.open(data_path + file, 'r')
#         print(data_path + file)
#         # 데이터셋을 미리 전처리하여 TFRecord로 저장
#         audio = audio_process.get_audio(audio_file_path)
        example = tf.train.Example(
            features=tf.train.Features(
                feature={
                    "audio": _bytes_feature(audio[0].tostring()),
                }
            )
        )
        writer.write(example.SerializeToString())
        if i==3:
            break
    writer.close()
    print("Done...")
    
create_tfrecord('/home/seungho/tobigs/ToBigs_Project/data/super_son/')
Clone this wiki locally