Skip to content

Commit

Permalink
Add Copy layer, add templates support (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxnt authored May 7, 2024
1 parent d514edb commit c90b06d
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 6 deletions.
27 changes: 27 additions & 0 deletions src/compute/layers/processing/CopyLayer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8

from typing import List, Tuple
from src.compute.dtl_utils.item_descriptor import ImageDescriptor
from src.compute.Layer import Layer
from supervisely import Annotation


class CopyLayer(Layer):
action = "copy"

layer_settings = {"required": ["settings"], "properties": {"settings": {}}}

def __init__(self, config, net):
Layer.__init__(self, config, net=net)

def modifies_data(self):
return False

def process(self, data_el):
yield data_el

def process_batch(self, data_els: List[Tuple[ImageDescriptor, Annotation]]):
yield data_els

def has_batch_processing(self) -> bool:
return True
1 change: 1 addition & 0 deletions src/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

PRESETS_PATH = os.path.join("/" + TEAM_FILES_PATH + "/presets", MODALITY_TYPE)

PIPELINE_ACTION = os.getenv("modal.state.pipelineAction", None)
FILTERED_ENTITIES = []
if PROJECT_ID is not None:
FILTERED_ENTITIES = os.getenv("modal.state.selectedEntities", [])
Expand Down
10 changes: 8 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from src.ui.dtl.actions.input.videos_project.videos_project import VideosProjectAction
from src.ui.dtl.actions.input.filtered_project.filtered_project import FilteredProjectAction

from src.templates import templates, load_template

from src.compute.dtl_utils.item_descriptor import ItemDescriptor, ImageDescriptor
from src.utils import LegacyProjectItem

Expand Down Expand Up @@ -103,7 +105,11 @@ def generate_preview_for_project(layer):
layer.set_preview_loading(False)


if g.PROJECT_ID and len(g.FILTERED_ENTITIES) == 0:
if g.PIPELINE_ACTION is not None:
template = templates[g.MODALITY_TYPE].get(g.PIPELINE_ACTION, None)
if template is not None:
load_template(template)
elif g.PROJECT_ID and len(g.FILTERED_ENTITIES) == 0:
ds_name = "*"
if g.DATASET_ID:
ds: DatasetInfo = g.api.dataset.get_info_by_id(g.DATASET_ID)
Expand All @@ -122,7 +128,7 @@ def generate_preview_for_project(layer):
nodes_flow.add_node(node)
generate_preview_for_project(layer)

if g.PROJECT_ID and len(g.FILTERED_ENTITIES) > 0:
elif g.PROJECT_ID and len(g.FILTERED_ENTITIES) > 0:
pr: ProjectInfo = g.api.project.get_info_by_id(g.PROJECT_ID)
src = [f"{pr.name}/*"]
layer = create_new_layer(FilteredProjectAction.name)
Expand Down
68 changes: 68 additions & 0 deletions src/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from src.ui.tabs.presets import apply_json
import src.globals as g
from supervisely import ProjectInfo, DatasetInfo


project_info = None
project_name = None
src = None
if g.PROJECT_ID is not None:
ds_name = "*"
if g.DATASET_ID:
ds: DatasetInfo = g.api.dataset.get_info_by_id(g.DATASET_ID)
ds_name = ds.name
pr: ProjectInfo = g.api.project.get_info_by_id(g.PROJECT_ID)
src = [f"{pr.name}/{ds_name}"]

move = [
{
"action": "filtered_project",
"src": src,
"dst": "$filtered_project_1",
"settings": {
"project_id": g.PROJECT_ID,
"filtered_entities_ids": g.FILTERED_ENTITIES,
"classes_mapping": "default",
"tags_mapping": "default",
},
},
{
"action": "move",
"src": ["$filtered_project_1"],
"dst": "$move_2",
"settings": {"move_confirmation": False},
},
]

copy = [
{
"action": "filtered_project",
"src": src,
"dst": "$filtered_project_1",
"settings": {
"project_id": g.PROJECT_ID,
"filtered_entities_ids": g.FILTERED_ENTITIES,
"classes_mapping": "default",
"tags_mapping": "default",
},
},
{
"action": "copy",
"src": ["$filtered_project_1"],
"dst": "$copy_2",
"settings": {},
},
]


def load_template(template):
apply_json(template)


templates = {
"images": {
"move": move,
"copy": copy,
},
"videos": {},
}
4 changes: 3 additions & 1 deletion src/ui/dtl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
# New
from .actions.input.filtered_project.filtered_project import FilteredProjectAction
from .actions.other.move.move import MoveAction
from .actions.other.copy.copy import CopyAction
from .actions.other_augs.pixelate.pixelate import PixelateAction
from .actions.other_augs.elastic_transform.elastic_transform import ElasticTransformAction

Expand Down Expand Up @@ -183,7 +184,7 @@
IfAction.name,
],
NEURAL_NETWORKS: [DeployYOLOV8Action.name, ApplyNNInferenceAction.name],
OTHER: [DatasetAction.name, DummyAction.name, MoveAction.name],
OTHER: [DatasetAction.name, DummyAction.name, CopyAction.name, MoveAction.name],
SAVE_ACTIONS: [
CreateNewProjectAction.name,
AddToExistingProjectAction.name,
Expand Down Expand Up @@ -250,6 +251,7 @@
# Other layers
DatasetAction.name: DatasetAction,
DummyAction.name: DummyAction,
CopyAction.name: CopyAction,
MoveAction.name: MoveAction,
# Save layers
CreateNewProjectAction.name: CreateNewProjectAction,
Expand Down
17 changes: 17 additions & 0 deletions src/ui/dtl/actions/other/copy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copy

`Copy` layer copies the input data to the selected destination.

### Settings

<details>
<summary>JSON view</summary>
<pre>
{
"action": "copy",
"src": ["$images_project_1"],
"dst": "$copy_2",
"settings": {}
}
</pre>
</details>
34 changes: 34 additions & 0 deletions src/ui/dtl/actions/other/copy/copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Optional
from os.path import realpath, dirname

from src.ui.dtl import OtherAction
from src.ui.dtl.Layer import Layer
from src.ui.dtl.utils import get_layer_docs


class CopyAction(OtherAction):
name = "copy"
title = "Copy"
docs_url = ""
description = "Copies the input data to the selected destination."
md_description = get_layer_docs(dirname(realpath(__file__)))

@classmethod
def create_new_layer(cls, layer_id: Optional[str] = None):
def get_settings(options_json: dict) -> dict:
return {}

def create_options(src: list, dst: list, settings: dict) -> dict:
return {
"src": [],
"dst": [],
"settings": [],
}

return Layer(
action=cls,
id=layer_id,
create_options=create_options,
get_settings=get_settings,
need_preview=False,
)
6 changes: 3 additions & 3 deletions src/ui/tabs/presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ def save_json_button_cb():
nodes_state = nodes_flow.get_nodes_state_json()

# Init layers data
layres_ids = ui_utils.init_layers(nodes_state)
all_layers_ids = layres_ids["all_layers_ids"]
layers_ids = ui_utils.init_layers(nodes_state)
all_layers_ids = layers_ids["all_layers_ids"]

ui_utils.init_src(edges)

Expand Down Expand Up @@ -210,7 +210,7 @@ def apply_json(dtl_json):

layer = ui_utils.create_new_layer(action_name)
ids.append(layer.id)
if action_name in actions_list[SOURCE_ACTIONS]:
if action_name in actions_list[SOURCE_ACTIONS] or action_name == "filtered_project":
data_layers_ids.append(layer.id)

# update src and dst
Expand Down

0 comments on commit c90b06d

Please sign in to comment.