- 14 Sep 2023
- 2 Minutes to read
- Print
- DarkLight
- PDF
Update Metadata and Images
- Updated on 14 Sep 2023
- 2 Minutes to read
- Print
- DarkLight
- PDF
This article is deprecated.
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)
updated_media_ids.append(media['id'])
# 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:
- Update the images on your local drive.
- Upload the updated images using llens.media.upload().
- Add metadata to the images using llens.metadata.upload().
- Tag the data version of the outdated images as -1 using llens.metadata.upload().
- Remove the redundant images by applying the metadata filter in the LandingLens user interface.
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(
media_id,
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