What This Helps With
Automate the process of uploading custom metadata from a folder containing images and their metadata files to Visual Layer datasets, with automatic field discovery and type detection.
This is a specific use case example script. You can modify the field detection logic and processing to suit your particular needs. For advanced scenarios, custom metadata deletion, or additional support, contact Visual Layer for assistance.
Working with DICOM Medical Imaging Data?
For medical imaging workflows, check out our specialized DICOM converter and upload scripts that handle DICOM-specific field types, date/time formats, and medical metadata standards.
View Complete Script Code
Access the full custom metadata upload Python script with complete implementation, ready to copy and use.
How the Script Works
The automation script demonstrates the complete Visual Layer custom metadata API workflow:- Automatically exports dataset - Calls Visual Layer API to get filename-to-media_id mapping (no manual export needed!)
- Scans folder for metadata - Finds images and their corresponding
.metadata.jsonfiles - Discovers all fields - Auto-detects all metadata fields across your files (or uses manual specification)
- Analyzes field types - Intelligently detects data types (float, datetime, enum, multi-enum, link, string)
- Creates custom metadata fields - Creates fields in Visual Layer via API calls
- Uploads metadata values - Uploads values for each field using the proper JSON format
- Monitors upload progress - Waits for completion and reports status
Field Type Detection Logic
The script uses intelligent heuristics to detect field types in auto-discovery mode:| Field Type | Detection Criteria | Examples |
|---|---|---|
| float | Contains decimal point or scientific notation | 23.5, 1.2e-3, 0.95 |
| datetime | Parseable by pandas as date/time | 2024-01-15, 2024-01-15T10:30:00Z |
| link | Starts with http://, https://, or ftp:// | https://example.com, ftp://server.com |
| multi-enum | Array with β€20 unique values across all files | ["tag1", "tag2"] |
| enum | β€100 unique string values across all files | "approved", "pending", "rejected" |
| string | Default for everything else | Any text, nested objects (JSON-serialized) |
The script samples up to 100 values per field for accurate enum detection. If a field has more than 100 unique values, itβs treated as a string type instead of enum.
Key Features
Automatic Dataset Export:- No manual export step required - the script automatically fetches the filename-to-media_id mapping
- Automatically finds and processes ALL metadata fields without configuration
- Ideal when you want to upload everything
- Use
--fieldflag to specify only certain fields - Useful when you want selective upload or need to force specific types
- Auto-detects 6 field types (float, datetime, enum, multi-enum, link, string)
- Falls back to string type if conversion fails
- Automatically truncates strings to Visual Layerβs 255-character limit
- Handles nested objects by serializing to JSON
Adding vs. Updating Metadata:
- Adding new fields: In case custom metadata was already uploaded to a dataset, the script will add new fields.
- Updating existing fields: Currently updating existing metadata is not supported. Contact Visual Layer for assistance with updating existing metadata values
Prerequisites
Before using the script, ensure you have:- Visual Layer on-premises installation (script designed for no-authentication environments)
- Dataset in READY state in Visual Layer with images already uploaded
- Python environment with required packages:
pandas,requests - Folder structure with images and
.metadata.jsonfiles
Expected Folder Structure
The script expects images with corresponding metadata files:The
.metadata.json file naming convention can be customized in the scriptβs scan_folder() function to match your specific metadata storage format and naming patterns.Alternative: Single JSON File
If your metadata is in a single JSON file instead of individual files per image: Example JSON format:load_single_json_metadata() method to the FolderMetadataProcessor class and call it instead of scan_folder() + load_metadata_files() in the workflow.
View Single JSON Implementation
See the complete code example for handling single JSON files at the bottom of the page.
Metadata File Format
Each.metadata.json file should contain field-value pairs:
- Strings:
"approved","warehouse_a" - Numbers:
23.5,0.95,100 - Dates/Times:
"2024-01-15","2024-01-15T10:30:00Z" - Arrays:
["tag1", "tag2"](for multi-enum fields) - Objects:
{"key": "value"}(serialized to JSON string)
Expected Result in Visual Layer
After running the script, your custom metadata fields will be visible in Visual Layerβs interface for each image:
Installation and Usage
Install Dependencies
Basic Usage - Auto-Discovery Mode (Recommended)
The script automatically discovers and uploads ALL metadata fields:Manual Field Specification
Specify only certain fields with explicit types:Command Line Parameters
| Parameter | Required | Description | Default | |||||
|---|---|---|---|---|---|---|---|---|
--folder | β | Path to folder containing images and .metadata.json files | - | |||||
--dataset-id | β | Visual Layer dataset identifier | - | |||||
--base-url | β | Visual Layer installation URL | http://localhost:2080 | |||||
--field | β | Specify field name and type (repeatable). If omitted, auto-discovers all fields. Types: `string | float | datetime | enum | multi-enum | link` | - |
Example Commands
Auto-discover all fields (recommended):Script Output
The script provides detailed progress information:Auto-Discovery Mode Output
Manual Mode Output
Customizing the Script
The script is designed to be modified for your specific use case. Here are common customizations:Modifying Field Detection
You can customize how the script detects field types by modifying the_auto_detect_field_type() function:
Skipping Certain Fields
Modify thediscover_and_analyze_fields() function to skip fields:
Custom Value Conversion
Modify the_convert_value() function for custom data transformations:
Troubleshooting
Common Issues
No image + metadata pairs found:- Verify your folder contains images with corresponding
.metadata.jsonfiles - Check filename format:
image.jpgshould haveimage.jpg.metadata.json - Ensure proper file permissions
- Confirm your Visual Layer instance is accessible at the specified URL
- Check that the dataset ID is correct and the dataset is in READY state
- Verify the
/api/v1/dataset/{dataset_id}/export_media_idendpoint is accessible
- Check that specified field names exist in your metadata files
- Verify field types match your data (e.g., datetime fields have valid dates)
- Review enum fields donβt exceed 20 unique values (API limit)
- Large datasets may take time to process
- The script polls indefinitely - you can interrupt with Ctrl+C if needed
- Check Visual Layer logs for any processing errors
- Review the automatic field type detection results
- Use manual mode (
--field) to specify correct types - Modify the
_convert_value()function for custom data formats
- Visual Layer has a 255-character limit for string fields
- The script automatically truncates - check if this affects your data
- Consider using link type for long URLs
Getting Help
For advanced use cases, custom metadata deletion, or technical support:Contact Visual Layer Support
Reach out to Visual Layerβs support team for assistance with complex metadata workflows, custom field requirements, or troubleshooting upload issues.
Advantages Over Manual Upload
Automatic Export:- β Manual: Export dataset β download metadata.json β run script
- β Automatic: Just run script - it exports automatically
- β Manual: List all fields and types in configuration
- β Auto-discovery: Script finds all fields automatically
- β Manual: Guess field types or trial-and-error
- β Intelligent: Script detects types from actual data
- β Manual: Script fails on type conversion errors
- β Robust: Falls back to string, truncates long values
- β Both modes: Auto-discovery for convenience, manual for control
Related Resources
- Custom Metadata API Guide - Manual API workflow and field type details
- DICOM Converter - Specialized workflow for medical imaging metadata
- Creating Datasets - Guide to creating Visual Layer datasets