def image_boxes( asset_path: str, tensor_image, tensor_boxes, rescale=1, dataformats="CHW", asset_rel_path: str = None, ): if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN tensor_image = to_np(tensor_image) tensor_image = convert_to_HWC(tensor_image, dataformats) tensor_boxes = to_np(tensor_boxes) tensor_image = tensor_image.astype(np.float32) * calculate_scale_factor( tensor_image ) return make_image( asset_path, tensor_image.astype(np.uint8), rescale=rescale, rois=tensor_boxes, asset_rel_path=asset_rel_path, )
def histogram(values, bins, max_bins=None): if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN values = to_np(values).astype(float) if values.size == 0: raise ValueError("The input has no element.") values = values.reshape(-1) counts, limits = np.histogram(values, bins=bins) num_bins = len(counts) if max_bins is not None and num_bins > max_bins: subsampling = num_bins // max_bins subsampling_remainder = num_bins % subsampling if subsampling_remainder != 0: counts = np.pad( counts, pad_width=[[0, subsampling - subsampling_remainder]], mode="constant", constant_values=0, ) counts = counts.reshape(-1, subsampling).sum(axis=-1) if counts.size == 0: logger.warning("Tracking an empty histogram") return UNKNOWN return V1EventHistogram(values=values, counts=counts)
def metric(value): if isinstance(value, float): return value if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN value = to_np(value) assert value.squeeze().ndim == 0, "scalar should be 0D" return float(value)
def video(asset_path, tensor, fps=4, content_type="gif"): if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN tensor = to_np(tensor) tensor = prepare_video(tensor) # If user passes in uint8, then we don't need to rescale by 255 scale_factor = calculate_scale_factor(tensor) tensor = tensor.astype(np.float32) tensor = (tensor * scale_factor).astype(np.uint8) return make_video(asset_path, tensor, fps, content_type)
def image(asset_path, data, rescale=1, dataformats="CHW"): if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN tensor = to_np(data) tensor = convert_to_HWC(tensor, dataformats) # Do not assume that user passes in values in [0, 255], use data type to detect scale_factor = calculate_scale_factor(tensor) tensor = tensor.astype(np.float32) tensor = (tensor * scale_factor).astype(np.uint8) return make_image(asset_path, tensor, rescale=rescale)
def audio(asset_path: str, tensor, sample_rate=44100, asset_rel_path: str = None): if not np: logger.warning(NUMPY_ERROR_MESSAGE) return UNKNOWN tensor = to_np(tensor) tensor = tensor.squeeze() if abs(tensor).max() > 1: print("warning: audio amplitude out of range, auto clipped.") tensor = tensor.clip(-1, 1) assert tensor.ndim == 1, "input tensor should be 1 dimensional." tensor_list = [int(32767.0 * x) for x in tensor] import wave import struct check_or_create_path(asset_path, is_dir=False) wave_write = wave.open(asset_path, "wb") wave_write.setnchannels(1) wave_write.setsampwidth(2) wave_write.setframerate(sample_rate) tensor_enc = b"" for v in tensor_list: tensor_enc += struct.pack("<h", v) wave_write.writeframes(tensor_enc) wave_write.close() return V1EventAudio( sample_rate=sample_rate, num_channels=1, length_frames=len(tensor_list), path=asset_rel_path or asset_path, content_type="audio/wav", )