Merge pull request #25 from elgeish/feature/microphone_fixes

Fixed microphone shortcut and preprocessing
This commit is contained in:
aliabid94 2020-07-14 09:13:55 -07:00 committed by GitHub
commit e1e42a6858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,15 +4,18 @@ This module defines various classes that can serve as the `input` to an interfac
automatically added to a registry, which allows them to be easily referenced in other parts of the code. automatically added to a registry, which allows them to be easily referenced in other parts of the code.
""" """
from abc import ABC, abstractmethod import datetime
from gradio import preprocessing_utils, validation_data import json
import numpy as np import os
import PIL.Image, PIL.ImageOps
import time import time
import warnings import warnings
import json from abc import ABC, abstractmethod
import datetime
import os import numpy as np
import PIL.Image
import PIL.ImageOps
import scipy.io.wavfile
from gradio import preprocessing_utils, validation_data
# Where to find the static resources associated with each template. # Where to find the static resources associated with each template.
# BASE_INPUT_INTERFACE_TEMPLATE_PATH = 'static/js/interfaces/input/{}.js' # BASE_INPUT_INTERFACE_TEMPLATE_PATH = 'static/js/interfaces/input/{}.js'
@ -269,20 +272,32 @@ class Image(AbstractInput):
class Microphone(AbstractInput): class Microphone(AbstractInput):
def __init__(self, label=None): def __init__(self, preprocessing=None, label=None):
super().__init__(label) super().__init__(label)
if preprocessing is None or preprocessing == "mfcc":
self.preprocessing = preprocessing
else:
raise ValueError("unexpected value for preprocessing", preprocessing)
@classmethod
def get_shortcut_implementations(cls):
return {
"microphone": {},
}
def preprocess(self, inp): def preprocess(self, inp):
""" """
By default, no pre-processing is applied to a microphone input file By default, no pre-processing is applied to a microphone input file
""" """
file_obj = preprocessing_utils.decode_base64_to_wav_file(inp) file_obj = preprocessing_utils.decode_base64_to_wav_file(inp)
mfcc_array = preprocessing_utils.generate_mfcc_features_from_audio_file(file_obj.name) if self.preprocessing == "mfcc":
return mfcc_array return preprocessing_utils.generate_mfcc_features_from_audio_file(file_obj.name)
_, signal = scipy.io.wavfile.read(file_obj.name)
return signal
# Automatically adds all shortcut implementations in AbstractInput into a dictionary. # Automatically adds all shortcut implementations in AbstractInput into a dictionary.
shortcuts = {} shortcuts = {}
for cls in AbstractInput.__subclasses__(): for cls in AbstractInput.__subclasses__():
for shortcut, parameters in cls.get_shortcut_implementations().items(): for shortcut, parameters in cls.get_shortcut_implementations().items():
shortcuts[shortcut] = cls(**parameters) shortcuts[shortcut] = cls(**parameters)