Update Metadata and Images
  • 14 Sep 2023
  • 2 Minutes to read
  • Dark
  • PDF

Update Metadata and Images

  • Dark
  • PDF


This article is deprecated.

Article Summary

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.

Use Case: Update Metadata

Let's say that you uploaded images and metadata, but you discovered that a class was misclassified. Suppose that the image name is "image2.png" and its class should be "5". You can use the LandingLens CLI to update the class name. 

from landinglens import LandingLens

# Instantiate the llens client
llens = LandingLens()

# Suppose the image name is 'image2.png', and you need to change its defect type to 5
file_name = 'image2.png'
defect_type = 5

# Record the updated ones
updated_media_ids = []

# Iterate the media block by block
for media in llens.media.ls(no_pagination=True)['medias']:
if media['name'] == file_name:
# Change the defect type
llens.metadata.upload(media['id'], defect_type=defect_type)

# Show the number of the updated media
print("#{} of images have been changed".format(len(updated_media_ids)))

Use Case: Update Images and Metadata

The previous use case discussed how to change the class of "image2.png" from "2" to "5". However, the defect types could previously be displayed in the file names, but this rule might no longer apply to the updated images.  

In this case, to maintain the consistency, you can use LandingLens CLI and the user interface to complete the task:

  1. Update the images on your local drive.
  2. Upload the updated images using llens.media.upload().
  3. Add metadata to the images using llens.metadata.upload().
  4. Tag the data version of the outdated images as -1 using llens.metadata.upload().
  5. Remove the redundant images by applying the metadata filter in the LandingLens user interface.
The metadata must already be created in LandingLens.

Use Case: Change the Folder Structure

Let's say this is the current folder structure:

├── update_media_and_metadata.py
├── data
|   ├── image1.png
|   ├── image2.png
| └── image3.png

You can use the LandingLens CLI to change the folder structure.

import os
from landinglens import LandingLens

# Instantiate the llens client
llens = LandingLens()

for media in llens.media.ls(no_pagination = True)['medias'][-3:]:
    file_path_ori = os.path.join('data', media['name'])
    file_path, file_ext = os.path.splitext(file_path_ori)

    # Find out the inconsistent defect type between the media on platform and the local files
if media['metadata']['defect_type'] != int(file_path[-1]):

        # 1. Rename the local file
file_path_new = ''.join((file_path[:-1], str(media['metadata']['defect_type']), file_ext))
        os.rename(file_path_ori, file_path_new)

        # 2. Upload the local renamed image and fetch its media ID on platform
media_id = llens.media.upload(file_path_new)['medias'][0]['id']

        # 3. Update the metadata of the latest uploaded one
        resp_new = llens.metadata.upload(
data_version = media['metadata']['data_version'],
defect_type = media['metadata']['defect_type'],
timestamp = media['metadata']['timestamp'],
variance = media['metadata']['variance']
  # assert resp_new['metadata']['defect_type'] == int(llens.media.ls(no_pagination = True)['medias'][-1]['name'].split('.')[0][5:])

        # 4. Tag the data version of the original media as -1 by its ID, meaning out-of-date
resp_ori = llens.metadata.upload(media['id'], data_version = -1)
  # assert resp_ori['metadata']['data_version'] != resp_new['metadata']['data_version']
        # assert resp_ori['media_ids'][0] != resp_new['media_ids'][0]

        # 5. Use LandingLens UI to remove media with defect_type = 0.

Here is the new folder structure:

├── update_media_and_metadata.py
├── data
|   ├── image1.png
|   ├── image3.png
| └── image5.png

Was this article helpful?