Skip to main content

Tutorials and Examples

This section contains complete, copyable HV SDK workflows. The examples use Python and cover the common path from opening a datacube, calibration, and visualization through PCA, classification, regression, and streamed camera pipelines.

SDK version

These tutorials and downloadable scripts target the HV SDK v1 Python API, currently 1.0.0-beta.2. If you have older examples that use import hsi, see the v1 migration guide.

Refer to the HV SDK Usage Guide for conceptual details about lazy operations, interleave, calibration, and streaming. And for the complete API reference, see the official HV SDK documentation, which also contains several guides on how to work with the framework.

Example Sections

GoalStart here
Open, inspect, calibrate, or visualize a datacubeBasics
Clean spectra, select bands, bin, crop, save processed cubes, or run simple spectral analysisPreprocessing and Analysis
Load HV Explorer annotations, extract ROI spectra, or plot ROIs on preview imagesAnnotations and ROIs
Fit PCA models, inspect loadings and scores, or explore dominant spectral variationPrincipal Component Analysis
Train pixel classifiers from ROIs or exported spectra, classify by spectral-angle matching, or prepare classifier models for camera pipelinesClassification
Predict continuous values such as fat, moisture, or concentrationRegression
Build live or simulated camera pipelines, stream frames, calibrate streamed data, test simulated stage-controller workflows, or apply saved models line by lineStreaming

Example Data

The downloadable scripts use the same sample datacubes as the HV Explorer tutorial. Download the cubes, dark/white references, and annotation files from HV Explorer example data, then point HSI_EXAMPLE_BASE_DIR to the folder where you stored them.

Running Downloaded Scripts

Downloaded scripts read paths from environment variables, so you can test them without editing the files. Set HSI_EXAMPLE_BASE_DIR to the folder containing your example datacubes, dark reference, white reference, annotations, and model files:

export HSI_EXAMPLE_BASE_DIR=/path/to/HSI_data/nuts
python 01_open_and_inspect_a_datacube.py

Install the Python packages used by most examples in the same environment as the HV SDK:

python -m pip install numpy matplotlib scikit-learn joblib

The streamed classifier visualization example also needs OpenCV and a graphical display:

python -m pip install opencv-python

Environment variables

VariableUsed byMeaning
HSI_EXAMPLE_BASE_DIRAll downloaded scriptsFolder containing the example datacubes and references.
HSI_EXAMPLE_CUBESingle-cube examplesCube filename for examples that use one cube.
HSI_EXAMPLE_TRAIN_CUBE / HSI_EXAMPLE_TEST_CUBEPCA and classificationTraining and test cube filenames.
HSI_EXAMPLE_DARK_REF / HSI_EXAMPLE_WHITE_REFCalibrated examplesDark and white reference capture filenames.
HSI_EXAMPLE_ANNOTATIONSROI examplesHV Explorer annotations JSON.
HSI_EXAMPLE_MEAN_SPECTRAClassificationExported mean spectra CSV, default mix1_spectra.csv.
HSI_EXAMPLE_CLASSIFIER_MODELClassification and streamingSaved classifier model path.
HSI_EXAMPLE_HV_EXPLORER_MODELClassification.pkl classifier exported from HV Explorer.
HSI_EXAMPLE_HV_EXPLORER_ANNOTATIONSClassificationAnnotations .json exported alongside the HV Explorer model.
HSI_EXAMPLE_REGRESSION_MODELRegression and streamingSaved regression model path.
HSI_EXAMPLE_REGRESSION_VMIN / HSI_EXAMPLE_REGRESSION_VMAXRegressionDisplay range for regression prediction maps.
HSI_EXAMPLE_PCA_MODELPCASaved PCA model path.
HSI_EXAMPLE_ROI_PCA_MODELPCASaved ROI-fitted PCA model path.
HSI_EXAMPLE_OUTPUTSaving examplesOutput path for scripts that write processed data.
HSI_EXAMPLE_MILK_TRAIN_CUBERegressionMilk training cube filename.
HSI_EXAMPLE_MILK_TEST_CUBERegression and streamingMilk test cube filename.
HSI_EXAMPLE_MILK_ANNOTATIONSRegressionMilk annotations JSON.
HSI_EXAMPLE_TARGET_PROPERTYRegressionNumeric annotation property to predict.
HSI_EXAMPLE_CAMERA_IPStreamingCamera IP address. If unset, camera examples use simulated data when possible.
HSI_EXAMPLE_VIDEO_DEVICEqamlib streamingOptional direct V4L2 device for the qamlib example.
HSI_EXAMPLE_N_LINESStreamingNumber of lines to capture in finite camera examples.
HSI_EXAMPLE_EXPOSURE_US / HSI_EXAMPLE_FRAMERATEStreamingCamera exposure and framerate overrides.
HSI_EXAMPLE_HORIZONTAL_CROP / HSI_EXAMPLE_BANDSStreamingSDK camera crop and band selection overrides.
HSI_EXAMPLE_QAMLIB_CROPqamlib streamingDirect V4L2 crop as left,top,width,height.

Generated output files

Several examples save fitted models or processed output files:

  • pca_model.joblib: created by the first PCA example.
  • roi_pca_model.joblib: created by the ROI PCA fitting example.
  • pixel_classifier.joblib: created by the first classification example.
  • regression_model.joblib: created by the first regression example.

Use the corresponding environment variables above when you want those files to be written somewhere specific, for example /tmp/pixel_classifier.joblib.

Common Failures

  • The script still uses the default data folder. Make sure you exported the variable, use export HSI_EXAMPLE_BASE_DIR=/path/to/HSI_data/nuts.
  • A saved model file is missing. Run the first example in that section, or set the matching model path variable such as HSI_EXAMPLE_CLASSIFIER_MODEL, HSI_EXAMPLE_REGRESSION_MODEL, or HSI_EXAMPLE_PCA_MODEL.
  • A model gives strange results on a new cube. Check that training and prediction use the same preprocessing, band count, band order, and wavelength calibration.
  • Camera calibration fails or gives odd values. Make sure the dark and white references use the same interleave as the camera stream; the streaming examples convert references to BIL before calibration.
  • Regression maps look flat or all one color. A few extrapolated values may dominate the color scale. Use HSI_EXAMPLE_REGRESSION_VMIN and HSI_EXAMPLE_REGRESSION_VMAX to set the displayed range.

Where To Go Next

  • Use the HV SDK Usage Guide for conceptual details about lazy operations, interleave, calibration, and streaming.
  • Use the official API documentation for full class and function references.
  • Use HV Explorer to create ROIs, then export those annotations for supervised training workflows.