Lightly Utilities

0.0.2 · active · verified Thu Apr 16

A utility package for the Lightly computer vision framework, providing helper functions primarily for data reformatting, such as preparing bounding box annotations for web visualization. It is currently at version 0.0.2 and has an irregular release cadence, often tied to releases of the main `lightly` library.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `reformat_for_web` function to convert a list of annotations and an image path into a format suitable for web visualization, saving the output to a specified directory. It creates a dummy image and annotations for a runnable example.

import os
import numpy as np
from PIL import Image
from lightly_utils import reformat_for_web

# 1. Create a dummy image file for demonstration
image_filename = "dummy_image.jpg"
Image.fromarray(np.zeros((100, 100, 3), dtype=np.uint8)).save(image_filename)

# 2. Define dummy annotations in the expected format
# Each dictionary represents annotations for one image.
# 'boxes': list of [x_norm, y_norm, width_norm, height_norm]
# 'labels': list of integer class IDs
annotations = [
    {'boxes': [[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.1, 0.1]], 'labels': [0, 1]},
    # In a real scenario, you'd have more entries for more images
]

# 3. Define an output directory
output_dir = "output_web_visuals"
os.makedirs(output_dir, exist_ok=True)

# 4. Call the utility function to reformat data for web visualization
print(f"Reformatting annotations for image: {image_filename}")
reformat_for_web(
    annotations=annotations, # This assumes one image's annotations for simplicity
    image_path=image_filename,
    output_dir=output_dir,
)

print(f"Web-ready data (e.g., image.json) has been written to: {os.path.abspath(output_dir)}")

# Optional: Clean up created files/directories
# os.remove(image_filename)
# import shutil
# shutil.rmtree(output_dir)

view raw JSON →