Skip to main content

Initialize a Project

info

This is equivalent to running the init command from the CLI.

Initialize Project Without Labels

The following code snippet will:

  1. Find the files that are named "*.jpg" within your current working directory. Here, you can use any logic to find exactly the files you wish to include.
  2. Specify where to store the Encord Active project. It is a good Idea to keep multiple projects within the same directory. That way, you can easily jump between them within the UI.
  3. Create the project. Note that the symlinks option determines whether to copy all the files (symlinks=False) or use symlinks to reference the files where they are stored already to save disk space (symlinks=True).
  4. Run essential metrics for the UI to work.
Initialize project without labels
from pathlib import Path
from typing import List

from encord_active.lib.metrics.execute import run_metrics
from encord_active.lib.metrics.types import EmbeddingType
from encord_active.lib.project.local import ProjectExistsError, init_local_project

# 1. Choose images to import
image_files: List[Path] = list(Path.cwd().glob("**/*.jpg")) # Find your image files

# 2. Choose where to place the project
projects_dir = Path("path/to/where/you/store/projects")

# 3. Create the project
try:
project_path: Path = init_local_project(
files = image_files,
target = projects_dir,
project_name = "<your project title>",
symlinks = True,
)
except ProjectExistsError as e:
print(e) # A project already exist with that name at the given path.
exit()


# 4. Run an essential metric
# This is necessary for the UI to function properly
run_metrics(
filter_func=lambda x: isinstance(x, AreaMetric),
data_dir=project_path,
use_cache_only=True,
)

You can also choose to run all the metrics that do not depend on any labels. That is done in the following code snippet:

from encord_active.lib.metrics.execute import run_metrics_by_embedding_type
run_metrics_by_embedding_type(
EmbeddingType.IMAGE,
data_dir=project_path,
use_cache_only=True
)

At this stage, you will have a project that you can open with the CLI command:

encord-active visualize
tip

For the visualize command to work, you need to have your terminals working directory be path/to/where/you/store/projects.

Initialize Project With labels

If you have defined a LabelTransformer as described in the import section, you can reuse it here. Provide the transformer and potentially label files to the init_local_project in order to also include labels:

Initialize project with labels
from pathlib import Path
from typing import List

from encord_active.lib.metrics.execute import run_metrics
from encord_active.lib.metrics.types import EmbeddingType
from encord_active.lib.project.local import ProjectExistsError, init_local_project

# 1. Choose images and label files to import
image_files: List[Path] = list(Path.cwd().glob("**/*.jpg")) # Find your image files
label_files: List[Path] = list(Path.cwd().glob("**/*.json")) # Find your label files

# 2. Choose where to place the project
projects_dir = Path("path/to/where/you/store/projects")

# 3. Create the project
try:
project_path: Path = init_local_project(
files = image_files,
target = projects_dir,
project_name = "<your project title>",
symlinks = True,
label_transformer=MyTransformer(),
label_paths=label_files,
)
except ProjectExistsError as e:
print(e) # A project already exist with that name at the given path.
exit()


# 4. Run an essential metric
# This is necessary for the UI to function properly
run_metrics(
filter_func=lambda x: isinstance(x, AreaMetric),
data_dir=project_path,
use_cache_only=True,
)

At this stage, you will have a project that you can open with the CLI command:

encord-active visualize
tip

For the visualize command to work, you need to have your terminals working directory be path/to/where/you/store/projects.