def postprocess(self, y): if self.labeled_segments: y, coordinates = y else: coordinates = [] if self.type == "auto": if isinstance(y, np.ndarray): dtype = "numpy" elif isinstance(y, PIL.Image.Image): dtype = "pil" elif isinstance(y, str): dtype = "file" elif isinstance(y, ModuleType): dtype = "plot" else: raise ValueError("Unknown type. Please choose from: 'numpy', 'pil', 'file', 'plot'.") else: dtype = self.type if dtype in ["numpy", "pil"]: if dtype == "pil": y = np.array(y) out_y = processing_utils.encode_array_to_base64(y) elif dtype == "file": out_y = processing_utils.encode_file_to_base64(y) elif dtype == "plot": out_y = processing_utils.encode_plot_to_base64(y) else: raise ValueError("Unknown type: " + dtype + ". Please choose from: 'numpy', 'pil', 'file', 'plot'.") return out_y, coordinates
def postprocess(self, y): if self.type in ["numpy", "file", "auto"]: if self.type == "numpy" or (self.type == "auto" and isinstance(y, tuple)): file = tempfile.NamedTemporaryFile() scipy.io.wavfile.write(file, y[0], y[1]) y = file.name return processing_utils.encode_file_to_base64(y, type="audio", ext="wav") else: raise ValueError("Unknown type: " + self.type + ". Please choose from: 'numpy', 'file'.")
def postprocess(self, y): if self.type in ["numpy", "pil"]: if self.type == "pil": y = np.array(y) return processing_utils.encode_array_to_base64(y) elif self.type == "file": return processing_utils.encode_file_to_base64(y) elif self.type == "plot": return processing_utils.encode_plot_to_base64(y) else: raise ValueError("Unknown type: " + self.type + ". Please choose from: 'numpy', 'pil', 'file', 'plot'.")
def postprocess(self, y): """ Parameters: y (str): file path Returns: (Dict[name: str, size: number, data: str]): JSON object with key 'name' for filename, 'data' for base64 url, and 'size' for filesize in bytes """ return { "name": os.path.basename(y), "size": os.path.getsize(y), "data": processing_utils.encode_file_to_base64(y) }
def postprocess(self, y): """ Parameters: y (str): path to video Returns: (str): base64 url data """ returned_format = y.split(".")[-1].lower() if self.type is not None and returned_format != self.type: output_file_name = y[0:y.rindex(".") + 1] + self.type ff = FFmpeg(inputs={y: None}, outputs={output_file_name: None}) ff.run() y = output_file_name return { "name": os.path.basename(y), "data": processing_utils.encode_file_to_base64(y) }
def get_interpretation_neighbors(self, x): file_obj = processing_utils.decode_base64_to_file(x) x = scipy.io.wavfile.read(file_obj.name) sample_rate, data = x leave_one_out_sets = [] duration = data.shape[0] boundaries = np.linspace(0, duration, self.interpretation_segments + 1).tolist() boundaries = [round(boundary) for boundary in boundaries] for index in range(len(boundaries) - 1): leave_one_out_data = np.copy(data) start, stop = boundaries[index], boundaries[index + 1] leave_one_out_data[start:stop] = 0 file = tempfile.NamedTemporaryFile() scipy.io.wavfile.write(file, sample_rate, leave_one_out_data) out_data = processing_utils.encode_file_to_base64(file.name, type="audio", ext="wav") leave_one_out_sets.append(out_data) return leave_one_out_sets, {}, True
def postprocess(self, y): return { "name": os.path.basename(y), "size": os.path.getsize(y), "data": processing_utils.encode_file_to_base64(y, header=False) }
def preprocess_example(self, x): return processing_utils.encode_file_to_base64(x, type="audio")
def process_example(self, example): return processing_utils.encode_file_to_base64(example)
def postprocess(self, y): return processing_utils.encode_file_to_base64(y, type="video")
def process_example(self, example): if os.path.exists(example): return processing_utils.encode_file_to_base64(example) else: return example
def restore_flagged_file(self, dir, file, encryption_key): data = processing_utils.encode_file_to_base64( os.path.join(dir, file), encryption_key=encryption_key) return {"name": file, "data": data}