Using CLIP in a task type

CLIP can be used in a task type via the Python module cliputil.

For example Lucid.CLIP.Threshold uses the CLIP thr utility for thresholding an image:

The main script.py contains the boilerplate XamFlow input and output handling:

import xfworkerutil

import clip_thres

worker = xfworkerutil.XFWorkerJob()

inputs = worker.job['inputs']
threshold_lower = inputs['threshold_lower0']
threshold_upper = inputs['threshold_upper0']

# Download input files
image = worker.get_omni_image(inputs['image'])

seg_image_omni_path = worker.create_related_path(image.pixels_path, '_seg.omni')
seg_image_pixels_path = worker.create_related_path(image.pixels_path, '_seg.raw')
thres_temp_dir_path = worker.working_dir.path / "thres"

# CLIP
seg_image = clip_thres.thres_image(
    image,
    threshold_lower,
    threshold_upper,
    seg_image_omni_path,
    seg_image_pixels_path,
    thres_temp_dir_path)

# Upload output files
seg_image_output = worker.upload_omni_output(seg_image)

# Save outputs
outputs = {
    "segments": seg_image_output,
}
worker.finish(outputs)

The clip_thres.py helper module contains the call to cliputil.run_thr():

import pathlib

import cliputil
import omni
from typing import Optional


def thres_image(
        image: omni.Image,
        min: Optional[float],
        max: Optional[float],
        result_omni_path: pathlib.Path,
        result_raw_path: pathlib.Path,
        temp_folder_path: pathlib.Path) -> omni.Image:
    """CLIP Threshold Segmentation"""

    input_float32_raw_path = cliputil.get_or_extract_raw_float32_pixels_file(image, temp_folder_path)

    cliputil.run_thr(input_float32_raw_path,
             result_raw_path,
             min=min,
             max=max,
             bit1=True)

    result_segments = image.save_derived_segments(result_omni_path, result_raw_path)
    return result_segments