Home

9. 📊 Custom metadata (image-level)

Why add metadata to images? 🤔

✅ It helps:

  1. Filter or sort images easily (e.g., red cars 🚗🔴, high edge density 🔍).
  2. Perform quick analysis with properties like "Aspect Ratio" 📐, generating insights 📊.
  3. Spot unusual values (e.g., high brightness ☀️💡) to identify interesting cases 🧐 or errors ⚠️.

9.1 Image-level metadata

We'll add the brightness score of a given image as custom metadata

!pip install opencv-python-headless -q
import cv2
from tenyks_sdk.sdk.utils import download_image
def compute_brightness_score(image_url: str) -> float:
    """Computes the normalized brightness score for an image from a URL.

    The score is between 0 and 1, where 0 means the image is completely dark,
    and 1 means the image is completely bright.
    """
    # Download the image
    image = download_image(image_url)

    # Convert the image from BGR to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Compute the mean pixel value (brightness)
    brightness = gray_image.mean() / 255.0

    return brightness


The score is between 0 and 1, where 0 means the image is completely dark,
and 1 means the image is completely bright.
"""
# Download the image
image = download_image(image_url)

# Convert the image from BGR to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Compute the mean pixel value (brightness)
brightness = gray_image.mean() / 255.0

return brightness

Let's retrieve some images based on our previous tag

search_generator = dataset.images_generator(filter="image_tag:[weather_rain]")

We iterate over all our images tagged as rain and add their corresponding brightness score

metadata_dict = {}  
for i, img in enumerate(search_generator):  
    image_brightness_score = compute_brightness_score(img.raw_image_url)  
    print(f"Image {i}: {image_brightness_score}")  
    metadata_dict[img.key] = image_brightness_score  
metadata_dict

""" Output:
Image 0: 0.5036217238562092
Image 1: 0.4344022385620915
Image 2: 0.40056416394335514
Image 3: 0.4520252505446623
Image 4: 0.44499033496732027
Image 5: 0.44669721132897605
Image 6: 0.4266421895424836
Image 7: 0.4282450816993464
Image 8: 0.42086995370370367
Image 9: 0.4281279357298475
Image 10: 0.45140638888888884
Image 11: 0.4272389433551198
Image 12: 0.44636882352941176
Image 13: 0.4641301388888889
Image 14: 0.4509909885620915
Image 15: 0.44325420751633987
Image 16: 0.4520734504357299
Image 17: 0.4255877423747277
Image 18: 0.44744475217864926
Image 19: 0.4484915223311547
Image 20: 0.43564426742919393
Image 21: 0.4484100462962963
Image 22: 0.444367355664488
"""

Finally, we save the custom metadata

dataset.save_image_metadata("brightness", metadata_dict)

""" Output:
2024-10-01 20:20:24,460 - Tenyks - INFO - Processing batch 1/1
INFO:Tenyks:Processing batch 1/1
2024-10-01 20:20:25,483 - Tenyks - INFO - Successfully updated custom metadata 'brightness' for 23 images in dataset 'nuImages'.
INFO:Tenyks:Successfully updated custom metadata 'brightness' for 23 images in dataset 'nuImages'.
2024-10-01 20:20:25,980 - Tenyks - INFO - Successfully added custom metadata 'brightness' to dataset 'nuImages'.
INFO:Tenyks:Successfully added custom metadata 'brightness' to dataset 'nuImages'.
"""