chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
@@ -0,0 +1,36 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from typing import Any
|
||||
|
||||
import attrs
|
||||
from cvat_sdk.api_client import models
|
||||
|
||||
from shared.tasks.interface import ITaskSpec
|
||||
from shared.tasks.utils import parse_frame_step
|
||||
|
||||
|
||||
@attrs.define
|
||||
class TaskSpecBase(ITaskSpec):
|
||||
_params: dict | models.TaskWriteRequest
|
||||
_data_params: dict | models.DataRequest
|
||||
size: int = attrs.field(kw_only=True)
|
||||
|
||||
@property
|
||||
def frame_step(self) -> int:
|
||||
return parse_frame_step(getattr(self, "frame_filter", ""))
|
||||
|
||||
def __getattr__(self, k: str) -> Any:
|
||||
notfound = object()
|
||||
|
||||
for params in [self._params, self._data_params]:
|
||||
if isinstance(params, dict):
|
||||
v = params.get(k, notfound)
|
||||
else:
|
||||
v = getattr(params, k, notfound)
|
||||
|
||||
if v is not notfound:
|
||||
return v
|
||||
|
||||
raise AttributeError(k)
|
||||
@@ -0,0 +1,10 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class SourceDataType(Enum):
|
||||
images = "images"
|
||||
video = "video"
|
||||
@@ -0,0 +1,19 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
from abc import ABCMeta, abstractmethod
|
||||
|
||||
from cvat_sdk.api_client import models
|
||||
from PIL import Image
|
||||
|
||||
from shared.tasks.enums import SourceDataType
|
||||
|
||||
|
||||
class ITaskSpec(models.ITaskWriteRequest, models.IDataRequest, metaclass=ABCMeta):
|
||||
size: int
|
||||
frame_step: int
|
||||
source_data_type: SourceDataType
|
||||
|
||||
@abstractmethod
|
||||
def read_frame(self, i: int) -> Image.Image: ...
|
||||
@@ -0,0 +1,43 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import io
|
||||
from collections.abc import Callable
|
||||
from contextlib import closing
|
||||
from typing import ClassVar
|
||||
|
||||
import attrs
|
||||
from PIL import Image
|
||||
|
||||
from shared.tasks.base import TaskSpecBase
|
||||
from shared.tasks.enums import SourceDataType
|
||||
from shared.utils.helpers import read_video_file
|
||||
|
||||
|
||||
@attrs.define
|
||||
class VideoTaskSpec(TaskSpecBase):
|
||||
source_data_type: ClassVar[SourceDataType] = SourceDataType.video
|
||||
chapters: list[dict]
|
||||
|
||||
_get_video_file: Callable[[], io.IOBase] = attrs.field(kw_only=True)
|
||||
|
||||
def read_frame(self, i: int) -> Image.Image:
|
||||
with closing(read_video_file(self._get_video_file())) as reader:
|
||||
for _ in range(i + 1):
|
||||
frame = next(reader)
|
||||
|
||||
return frame
|
||||
|
||||
|
||||
@attrs.define
|
||||
class ImagesTaskSpec(TaskSpecBase):
|
||||
source_data_type: ClassVar[SourceDataType] = SourceDataType.images
|
||||
|
||||
_get_frame: Callable[[int], bytes] = attrs.field(kw_only=True)
|
||||
get_related_files: Callable[[int], dict[str, bytes]] | None = attrs.field(
|
||||
kw_only=True, default=None
|
||||
)
|
||||
|
||||
def read_frame(self, i: int) -> Image.Image:
|
||||
return Image.open(io.BytesIO(self._get_frame(i)))
|
||||
@@ -0,0 +1,23 @@
|
||||
# Copyright (C) CVAT.ai Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
|
||||
from collections.abc import Iterable
|
||||
|
||||
|
||||
def parse_frame_step(frame_filter: str) -> int:
|
||||
return int((frame_filter or "step=1").split("=")[1])
|
||||
|
||||
|
||||
def to_rel_frame(abs_frame: int, *, frame_step: int, task_start_frame: int) -> int:
|
||||
return (abs_frame - task_start_frame) // frame_step
|
||||
|
||||
|
||||
def to_rel_frames(
|
||||
abs_frames: Iterable[int], *, frame_step: int, task_start_frame: int
|
||||
) -> list[int]:
|
||||
return [
|
||||
to_rel_frame(f, frame_step=frame_step, task_start_frame=task_start_frame)
|
||||
for f in abs_frames
|
||||
]
|
||||
Reference in New Issue
Block a user