Spaces:
Runtime error
Runtime error
Skip completely silent slices
Browse files- audio_to_images.py +13 -0
audio_to_images.py
CHANGED
|
@@ -1,14 +1,19 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import io
|
|
|
|
| 4 |
import argparse
|
| 5 |
|
|
|
|
| 6 |
import pandas as pd
|
| 7 |
from tqdm.auto import tqdm
|
| 8 |
from datasets import Dataset, DatasetDict, Features, Image, Value
|
| 9 |
|
| 10 |
from audiodiffusion.mel import Mel
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def main(args):
|
| 14 |
mel = Mel(x_res=args.resolution,
|
|
@@ -32,6 +37,11 @@ def main(args):
|
|
| 32 |
image = mel.audio_slice_to_image(slice)
|
| 33 |
assert (image.width == args.resolution
|
| 34 |
and image.height == args.resolution)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
with io.BytesIO() as output:
|
| 36 |
image.save(output, format="PNG")
|
| 37 |
bytes = output.getvalue()
|
|
@@ -43,6 +53,9 @@ def main(args):
|
|
| 43 |
"slice": slice,
|
| 44 |
}])
|
| 45 |
finally:
|
|
|
|
|
|
|
|
|
|
| 46 |
ds = Dataset.from_pandas(
|
| 47 |
pd.DataFrame(examples),
|
| 48 |
features=Features({
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import io
|
| 4 |
+
import logging
|
| 5 |
import argparse
|
| 6 |
|
| 7 |
+
import numpy as np
|
| 8 |
import pandas as pd
|
| 9 |
from tqdm.auto import tqdm
|
| 10 |
from datasets import Dataset, DatasetDict, Features, Image, Value
|
| 11 |
|
| 12 |
from audiodiffusion.mel import Mel
|
| 13 |
|
| 14 |
+
logging.basicConfig(level=logging.WARN)
|
| 15 |
+
logger = logging.getLogger('audio_to_images')
|
| 16 |
+
|
| 17 |
|
| 18 |
def main(args):
|
| 19 |
mel = Mel(x_res=args.resolution,
|
|
|
|
| 37 |
image = mel.audio_slice_to_image(slice)
|
| 38 |
assert (image.width == args.resolution
|
| 39 |
and image.height == args.resolution)
|
| 40 |
+
# skip completely silent slices
|
| 41 |
+
if all(np.frombuffer(image.tobytes(), dtype=np.uint8) == 255):
|
| 42 |
+
logger.warn('File %s slice %d is completely silent',
|
| 43 |
+
audio_file, slice)
|
| 44 |
+
continue
|
| 45 |
with io.BytesIO() as output:
|
| 46 |
image.save(output, format="PNG")
|
| 47 |
bytes = output.getvalue()
|
|
|
|
| 53 |
"slice": slice,
|
| 54 |
}])
|
| 55 |
finally:
|
| 56 |
+
if len(examples) == 0:
|
| 57 |
+
logger.warn('No valid audio files were found.')
|
| 58 |
+
return
|
| 59 |
ds = Dataset.from_pandas(
|
| 60 |
pd.DataFrame(examples),
|
| 61 |
features=Features({
|