Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix import #635

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions swanlab/data/modules/audio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,24 @@
音频模块
"""
from swankit.core import MediaBuffer, DataSuite as D, MediaType
from typing import Union
from typing import Union, Any

try:
# noinspection PyPackageRequirements
import soundfile as sf
# noinspection PyPackageRequirements
import numpy as np

InputType = Union[str, np.ndarray]
except ImportError:
sf, np = None, None
InputType = Union[str, Any]


class Audio(MediaType):
SF_SUPPORT_DTYPE = [np.dtype(d) for d in ["float32", "float64", "int16", "int32"]]
SF_SUPPORT_DTYPE = [np.dtype(d) for d in ["float32", "float64", "int16", "int32"]] if sf else []

def __init__(
self,
data_or_path: Union[str, np.ndarray],
sample_rate: int = 44100,
caption: str = None,
):
def __init__(self, data_or_path: InputType, sample_rate: int = 44100, caption: str = None):
"""Audio class constructor

Parameters
Expand Down
12 changes: 8 additions & 4 deletions swanlab/data/modules/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
from PIL import Image as PILImage
# noinspection PyPackageRequirements
from matplotlib import pyplot as plt

InputType = Union[str, np.ndarray, PILImage.Image, plt.plot]

except ImportError:
np, PILImage, plt = None, None, None
InputType = Union[str, Any]


def is_pytorch_tensor_typename(typename: str) -> bool:
Expand Down Expand Up @@ -48,11 +52,11 @@ def convert_size(size=None):


class Image(MediaType):
ACCEPT_FORMAT = ["png", "jpg", "jpeg", "bmp"]
ACCEPT_FORMAT = ["png", "jpg", "jpeg", "bmp"] if PILImage else []

def __init__(
self,
data_or_path: Union[str, np.ndarray, PILImage.Image, plt.plot],
data_or_path: InputType,
mode: str = None,
caption: str = None,
file_type: str = None,
Expand Down Expand Up @@ -154,7 +158,7 @@ def __init__(
"Tensor or matplotlib figure."
)

self.image_data = self.resize(image_data, self.size)
self.image_data = self.__resize(image_data, self.size)
"""
转换为矩阵后的数据
"""
Expand All @@ -172,7 +176,7 @@ def __convert_file_type(self, file_type: str = None):

return file_type

def resize(self, image: PILImage.Image, size=None) -> PILImage.Image:
def __resize(self, image, size=None):
"""将图像调整大小"""
if size is None:
self.size = image.size
Expand Down