• 21 Dec 2022
  • 2 Minutes to read
  • Dark
  • PDF


  • Dark
  • PDF

Article summary

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 SDK to make your updates.

Use Case: Update Metadata

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)))

Update Images and Metadata

After the previous use cases, you've learned how to upload images and retrieve the properties of those images. This section will dive deeper into more complex use cases.

The previous use case discussed how to change the Class of "image2.png" from "2" to "5". On data management, it would be a little confusing. Previously, the defect type could be displayed in file names but this rule doesn't seem to be applied on the updated images.

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

  1. Update local images.
  2. Upload the updated images by llens.media.upload().
  3. Fill the metadata of the latest upload from step 2 by llens.metadata.upload().
  4. Tag the data version of the out-of-date media as -1 through llens.metadata.upload().
  5. Remove the redundant images on LandingLens UI by applying metadata filter.
The metadata must already be created in LandingLens.

Folder Structure

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

Use Cases: 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.

New Folder Structure

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

Was this article helpful?