# This file was extracted from the HV SDK Docusaurus examples. # It is intended as a downloadable, runnable companion to the documentation. # Set HSI_EXAMPLE_BASE_DIR and related env vars to use your own data. # Source page: /hsi/hv_sdk/examples/classification#classify-one-scan-line # region: setup import os from pathlib import Path import joblib import numpy as np import qtec_hv_sdk as hs from qtec_hv_sdk.preprocessing import make_reference from qtec_hv_sdk.preprocessing import reflectance_calibration BASE_DIR = Path(os.environ.get("HSI_EXAMPLE_BASE_DIR", "/path/to/HSI_data/nuts")) if not BASE_DIR.exists(): raise SystemExit( "Run: 'export HSI_EXAMPLE_BASE_DIR=/path/to/HSI_data/' to setup the " "folder containing the example datacubes." ) TRAIN_CUBE = os.environ.get("HSI_EXAMPLE_TRAIN_CUBE", "mix1.pam") TEST_CUBE = os.environ.get("HSI_EXAMPLE_TEST_CUBE", "mix2.pam") DARK_REF = os.environ.get("HSI_EXAMPLE_DARK_REF", "dark_ref.pam") WHITE_REF = os.environ.get("HSI_EXAMPLE_WHITE_REF", "white_ref.pam") CLASSIFIER_MODEL_PATH = Path(os.environ.get("HSI_EXAMPLE_CLASSIFIER_MODEL", "pixel_classifier.joblib")) def make_references(): dark = hs.open(str(BASE_DIR / DARK_REF)) white = hs.open(str(BASE_DIR / WHITE_REF)) return make_reference(dark), make_reference(white) def open_reflectance_cube(cube_name=TRAIN_CUBE): dark_ref, white_ref = make_references() img = hs.open(str(BASE_DIR / cube_name)) return reflectance_calibration(img, white_ref, dark_ref, clip=True) def load_classifier_model(): if not CLASSIFIER_MODEL_PATH.exists(): raise SystemExit( f"Classifier model not found at {CLASSIFIER_MODEL_PATH}. " "Run 01_train_a_pixel_classifier_from_rois.py first, or set HSI_EXAMPLE_CLASSIFIER_MODEL." ) return joblib.load(CLASSIFIER_MODEL_PATH) # end region # region: example clf = load_classifier_model() test_reflectance = open_reflectance_cube(TEST_CUBE) line_index = 100 frame_arr = test_reflectance.array_plane(line_index, hs.lines) pixels_frame = frame_arr.T predicted = clf.predict(pixels_frame) classes, counts = np.unique(predicted, return_counts=True) print(dict(zip(classes, counts.astype(int)))) # end region