Upload Images
  • 14 Sep 2023
  • 3 Minutes to read
  • Dark
    Light
  • PDF

Upload Images

  • Dark
    Light
  • PDF

 

This article is deprecated.

Article Summary

Note:
The LandingLens CLI has been deprecated. We recommend using the Python SDK instead. Additionally, the CLI is only supported in the Classic Flow version of LandingLens.

Let's say you have some images and would like to upload them to LandingLens. Instead of uploading from the LandingLens user interface (UI), you could upload the images directly with a script. In this case, you should understand the following two methods of the LandingLens CLI: 

Method One: LandingLens.media.upload()

from landinglens import LandingLens

llens = LandingLens()

res=llens.media.upload(path, validate_extensions=True)
'''
Upload media to platform.

Parameters
----------

path: str
    Path to the local file.
split: str
    Set this media to one split ('train'/'dev'/'test'), '' represents Unassigned and is the default
classification_name: str
    Set the media's classification if the project type is Classification or Anomaly Detection
object_detection_xml: str
    Path to the Pascal VOC xml file for object detection project
seg_mask: str
    Path to the segmentation mask file for segmentation project
seg_defect_map: str
    Path to the segmentation defect_map.json file for segmentation project
nothing_to_label: bool
    Set the media's label as OK, valid for object detection and segmetation project
project_id: str
    Project id for which to fetch the media. Defaults to None - in this case,
    ll ens will read project id from the config file.
validate_extensions: bool
    Defaults to True. Files other than jpg/jpeg/png/bmp will be skipped.
    If set to False, will try to upload all files. Behavior of platform
    for unexpected extensions may not be correct

Returns
-------
dict{
    "error_count": int
        # failed file count
    "files_with_errors": dict
        # failed filename and its error message
    "medias": list[dict[str, any]]
        Lists of media information
    "num_uploaded": int
        The number of uploaded media
}
'''

Method Two: LandingLens.media.ls()

from landinglens import LandingLens

llens = LandingLens()

res=llens.media.ls(offset=0, limit=1000, no_pagination=True)
'''
Fetch media. Apply pagination, unless otherwise specified.

Parameters
----------
offset: int
    Defaults to 0. As in standard pagination.
limit: int
    Max 1000. Defaults to 1000. As in standard pagination.
no_pagination: bool
    If set to true, llens will fetch all media for a project given the filters.
    Limit and offset parameters will be ignored. Defaults to False.
**metadata:
    Kwargs used as metadata that will be used for server side filtering of the results.

Returns
-------
dict{
    "medias": list[dict[str, any]]
        Lists of media information
    "num_requested": int
        limit - offset
    "count": int
        The number of media, len(media)
    "offset": int
        The number of offset when executing the method llens.media.ls()
    "limit": int
        The number of limit when executing the method llens.media.ls()
}
'''

New Folder Structure

.
├── upload_media.py
├── data
|   ├── new_dataset.csv
|   ├── image1.png
|   ├── image2.png
| └── image3.png

Upload Images with Split and Label Information

The llens.media.upload() method supports uploading images along with their split and label information.

All LandingLens Project Types are supported:

Notes:
  • The server refers to the MD5 checksum to prevent duplicate uploads. If a duplication is detected, the server will display a 409 (Conflict) error. 
  • Folder upload only supports raw images. It does not support labels.

Split

The split can be directly assigned when uploading. Pass in the split name "train", "dev", or "test".

If no split or split = "", then the split will be unassigned.

Classification

Pass in the defect name to classification_name parameter, this Class will be created if it does not exist.

# defect named defect-1, split is 'dev'
llens.media.upload("./upload_test/classification/defect-1.bmp",
classification_name="defect-1",
split="dev",
project_id=1839885)

# defect named defect-2, split is 'test'
llens.media.upload("./upload_test/classification/defect-2.jpg",
classification_name="defect-2",
split="test",
project_id=1839885)

Anomaly Detection

This process is similar to Classification. Pass to classification_name and indicate whether the images are normal or abnormal.

# normal, no split means 'unassigned'
llens.media.upload("./upload_test/anomaly detection/normal.jpg",
classification_name="normal",
project_id=1839886)

# abnormal
llens.media.upload("./upload_test/anomaly detection/abnormal.jpg",
classification_name="abnormal",
split="train",
project_id=1839886)

Object Detection

Pass the Pascal VOC XML file (.xml)to create the bounding boxes.

If nothing_to_label is set to True, the image will be labeled as "nothing-to-label".

# Has defect
llens.media.upload("./upload_test/object detection/image06.jpg",
object_detection_xml="./upload_test/object detection/image06.xml",
split="train",
project_id=1839814)

# Nothing to label
llens.media.upload("./upload_test/object detection/image02.bmp",
nothing_to_label=True,
split="dev",
project_id=1839814)

Segmentation

Pass the Segmentation Defect Map (.json) and the Segmentation Mask (.png) to create Segmentation areas.

If nothing_to_label is set to True, the image will be labeled as "nothing-to-label".

# Has defect
llens.media.upload("./upload_test/segmentation/Images/CHNCXR_0168_0.png",
seg_mask="./upload_test/segmentation/Segmentations/CHNCXR_0168_0.png",
seg_defect_map="./upload_test/segmentation/defect_map.json",
split="test",
project_id=1839884)

# Nothing to label
llens.media.upload("./upload_test/segmentation/Images/CHNCXR_0298_0.png",
nothing_to_label=True,
split="test",
project_id=1839884)

Upload Metadata

After uploading images to a Project, you can add metadata to them by using a script.

SDK method: LandingLens.metadata.upload()

from landinglens import LandingLens

llens = LandingLens()

res=llens.metadata.upload(media_ids, **input_metadata)
'''
Upload metadata with media IDs to platform.

Parameters
----------
media_ids: Union[int, List[int]]

Returns
-------
dict{
    "project_id": int
    "metadata": dict
    "media_ids": list[int]
}
'''

Use Cases

from landinglens import LandingLens

# Instantiate the llens client
llens = LandingLens()

# Fetch all media
resp_ls=llens.media.ls(no_pagination=True)['medias']

# Iterate the media block by block
for media in media:
media_id = media['id']
media_name = media['name']

    # Add the metadata 'defect_type' according to the media_name
    res = llens.metadata.upload(
            media_id,
defect_type=int(media_name.split('.')[0][5:])
        )

    # Show the updated defect_type
    print(res['metadata']['defect_type'])
 # The output would be the number in the file name, i.e.
# if the media name is 'image1.png', the printing output will be 1

Was this article helpful?