### Example Usage of S2cloudless Output Functions Source: https://context7.com/kappazeta/s2cloudless/llms.txt Demonstrates how to use the save_validation_outputs and save_cvat_outputs functions with example mask and probability map data. ```python # Example usage mask = cloud_masks[0] probs = cloud_probs[0] # For validation workflows save_validation_outputs(mask, probs, "T32TQM_20230101T123456") # For CVAT annotation workflows save_cvat_outputs(mask, probs, "/path/to/S2CLOUDLESS_DATA") ``` -------------------------------- ### Conda Environment Management and Verification Source: https://context7.com/kappazeta/s2cloudless/llms.txt Commands to create, activate, and verify the s2cloudless conda environment. Ensures the s2cloudless library is installed correctly. ```bash # Create and activate the conda environment conda env create -f environment.yml conda activate s2cloudless # Verify installation python -c "from s2cloudless import S2PixelCloudDetector; print('s2cloudless installed successfully')" ``` -------------------------------- ### Conda Environment Specification Source: https://context7.com/kappazeta/s2cloudless/llms.txt Defines the dependencies for the s2cloudless pipeline using a conda environment file. Install with: conda env create -f environment.yml. ```yaml # environment.yml - Install with: conda env create -f environment.yml name: s2cloudless channels: - conda-forge - defaults dependencies: - python>=3.5 - numpy - pillow - rasterio - matplotlib - pip - pip: - s2cloudless ``` -------------------------------- ### Initialize and Use S2PixelCloudDetector Source: https://context7.com/kappazeta/s2cloudless/llms.txt Demonstrates how to load Sentinel-2 bands, stack them, normalize reflectance values, initialize the S2PixelCloudDetector with specific parameters, and generate cloud probability maps and binary masks. ```python # Load required bands (B01, B02, B04, B05, B08, B8A, B09, B10, B11, B12) input_folder = "/path/to/IMG_DATA" identifier = "T32TQM_20230101T123456_" B01 = load_band(f"{input_folder}/{identifier}B01.jp2", 6) # 60m -> 10m B02 = load_band(f"{input_folder}/{identifier}B02.jp2", 1) # 10m native B04 = load_band(f"{input_folder}/{identifier}B04.jp2", 1) # 10m native B05 = load_band(f"{input_folder}/{identifier}B05.jp2", 2) # 20m -> 10m B08 = load_band(f"{input_folder}/{identifier}B08.jp2", 1) # 10m native B8A = load_band(f"{input_folder}/{identifier}B8A.jp2", 2) # 20m -> 10m B09 = load_band(f"{input_folder}/{identifier}B09.jp2", 6) # 60m -> 10m B10 = load_band(f"{input_folder}/{identifier}B10.jp2", 6) # 60m -> 10m B11 = load_band(f"{input_folder}/{identifier}B11.jp2", 2) # 20m -> 10m B12 = load_band(f"{input_folder}/{identifier}B12.jp2", 2) # 20m -> 10m # Stack bands and normalize to reflectance values (0-1 range) bands = np.array([np.dstack(( B01[0]/10000.0, B02[0]/10000.0, B04[0]/10000.0, B05[0]/10000.0, B08[0]/10000.0, B8A[0]/10000.0, B09[0]/10000.0, B10[0]/10000.0, B11[0]/10000.0, B12[0]/10000.0 ))]) # Initialize cloud detector with parameters optimized for 10m resolution # For 60m resolution use: average_over=4, dilation_size=2 cloud_detector = S2PixelCloudDetector( threshold=0.4, # Probability threshold for cloud classification average_over=22, # Averaging kernel size for smoothing dilation_size=11 # Morphological dilation for cloud expansion ) # Generate cloud probability maps (values 0.0-1.0) cloud_probs = cloud_detector.get_cloud_probability_maps(bands) print(f"Cloud probability shape: {cloud_probs.shape}") # (1, height, width) print(f"Probability range: {cloud_probs.min():.3f} - {cloud_probs.max():.3f}") # Generate binary cloud masks (values 0 or 1) cloud_masks = cloud_detector.get_cloud_masks(bands).astype(np.uint8) cloud_percentage = (cloud_masks.sum() / cloud_masks.size) * 100 print(f"Cloud coverage: {cloud_percentage:.2f}%") ``` -------------------------------- ### Run S2cloudless Cloud Detector Script Source: https://context7.com/kappazeta/s2cloudless/llms.txt Command-line script to process Sentinel-2 L1C products and generate cloud masks and probability maps. ```APIDOC ## Run S2cloudless Cloud Detector Script The `run_s2cloudless.py` script processes a Sentinel-2 L1C product stored locally and generates cloud detection outputs. It requires the path to the IMG_DATA folder containing the JP2 spectral band files and a mode selection for output format. ### Usage Examples ```bash # Run cloud detection in validation mode (rescaled outputs with colormap) python run_s2cloudless.py --input /path/to/S2A_MSIL1C_20230101T123456_N0509_R123_T32TQM_20230101T134567.SAFE/GRANULE/L1C_T32TQM_A012345_20230101T123456/IMG_DATA --mode validation # Run cloud detection in CVAT-VSM mode (native resolution grayscale outputs) python run_s2cloudless.py --input /path/to/S2A_MSIL1C_20230101T123456_N0509_R123_T32TQM_20230101T134567.SAFE/GRANULE/L1C_T32TQM_A012345_20230101T123456/IMG_DATA --mode CVAT-VSM ``` ### Parameters #### Command-line Arguments - `--input` (string) - Required - Path to the IMG_DATA folder containing JP2 spectral band files. - `--mode` (string) - Required - Output mode. Options: `validation`, `CVAT-VSM`. ``` -------------------------------- ### Run S2cloudless in CVAT-VSM Mode Source: https://context7.com/kappazeta/s2cloudless/llms.txt Execute the s2cloudless script to process Sentinel-2 L1C data and generate cloud detection outputs in CVAT-VSM mode, providing native resolution grayscale outputs suitable for annotation pipelines. ```bash python run_s2cloudless.py --input /path/to/S2A_MSIL1C_20230101T123456_N0509_R123_T32TQM_20230101T134567.SAFE/GRANULE/L1C_T32TQM_A012345_20230101T123456/IMG_DATA --mode CVAT-VSM ``` -------------------------------- ### Load and Resample Sentinel-2 Bands Source: https://context7.com/kappazeta/s2cloudless/llms.txt A Python function to load Sentinel-2 spectral band data using rasterio and resample it to a specified scale factor. This is a prerequisite for using the S2PixelCloudDetector. ```python from s2cloudless import S2PixelCloudDetector import numpy as np import rasterio from rasterio.warp import Resampling # Load and resample Sentinel-2 bands to 10m resolution def load_band(filepath, scale_factor): with rasterio.open(filepath) as dataset: return dataset.read( out_shape=(dataset.count, int(dataset.height * scale_factor), int(dataset.width * scale_factor)), resampling=Resampling.bilinear ) ``` -------------------------------- ### Save CVAT Outputs (Native Resolution) Source: https://context7.com/kappazeta/s2cloudless/llms.txt Saves binary masks and probability maps for CVAT annotation workflows. Outputs are at native resolution, with masks as binary (0 or 1) and probabilities as grayscale (0-255). ```python def save_cvat_outputs(mask, probability_map, output_folder): # Save binary mask (0 or 1 values, native 1830x1830 resolution) im_mask = Image.fromarray(mask.astype(np.uint8)) im_mask.save(f"{output_folder}/s2cloudless_prediction.png") # Save probability as grayscale (0-255 range) prob_gray = (probability_map * 255).astype(np.uint8) im_prob = Image.fromarray(prob_gray) im_prob.save(f"{output_folder}/s2cloudless_probability.png") ``` -------------------------------- ### Run S2cloudless in Validation Mode Source: https://context7.com/kappazeta/s2cloudless/llms.txt Execute the s2cloudless script to process Sentinel-2 L1C data and generate cloud detection outputs in validation mode, which includes rescaled outputs with a colormap. ```bash python run_s2cloudless.py --input /path/to/S2A_MSIL1C_20230101T123456_N0509_R123_T32TQM_20230101T134567.SAFE/GRANULE/L1C_T32TQM_A012345_20230101T123456/IMG_DATA --mode validation ``` -------------------------------- ### S2PixelCloudDetector API Source: https://context7.com/kappazeta/s2cloudless/llms.txt The core cloud detection functionality provided by the `S2PixelCloudDetector` class. ```APIDOC ## S2PixelCloudDetector API The core cloud detection functionality is provided by the `S2PixelCloudDetector` class from the s2cloudless library. It accepts multi-band imagery arrays and returns cloud probability maps and binary masks based on configurable threshold and morphological parameters. ### Initialization ```python from s2cloudless import S2PixelCloudDetector cloud_detector = S2PixelCloudDetector( threshold=0.4, # Probability threshold for cloud classification average_over=22, # Averaging kernel size for smoothing dilation_size=11 # Morphological dilation for cloud expansion ) ``` ### Parameters #### Initialization Parameters - **threshold** (float) - Optional - Probability threshold for cloud classification. Defaults to 0.4. - **average_over** (int) - Optional - Averaging kernel size for smoothing. Defaults to 22. For 60m resolution use: `average_over=4`. - **dilation_size** (int) - Optional - Morphological dilation for cloud expansion. Defaults to 11. For 60m resolution use: `dilation_size=2`. ### Methods #### `get_cloud_probability_maps(bands)` Generates cloud probability maps. - **bands** (numpy.ndarray) - Required - Multi-band imagery array. Expected shape: (1, height, width, num_bands). **Returns**: numpy.ndarray - Cloud probability maps with values ranging from 0.0 to 1.0. Shape: (1, height, width). #### `get_cloud_masks(bands)` Generates binary cloud masks. - **bands** (numpy.ndarray) - Required - Multi-band imagery array. Expected shape: (1, height, width, num_bands). **Returns**: numpy.ndarray - Binary cloud masks with values 0 (clear) or 1 (cloud). Shape: (1, height, width). ### Example Usage ```python import numpy as np import rasterio from rasterio.warp import Resampling from s2cloudless import S2PixelCloudDetector # Load and resample Sentinel-2 bands to 10m resolution def load_band(filepath, scale_factor): with rasterio.open(filepath) as dataset: return dataset.read( out_shape=(dataset.count, int(dataset.height * scale_factor), int(dataset.width * scale_factor)), resampling=Resampling.bilinear ) # Load required bands (B01, B02, B04, B05, B08, B8A, B09, B10, B11, B12) input_folder = "/path/to/IMG_DATA" identifier = "T32TQM_20230101T123456_" B01 = load_band(f"{input_folder}/{identifier}B01.jp2", 6) # 60m -> 10m B02 = load_band(f"{input_folder}/{identifier}B02.jp2", 1) # 10m native B04 = load_band(f"{input_folder}/{identifier}B04.jp2", 1) # 10m native B05 = load_band(f"{input_folder}/{identifier}B05.jp2", 2) # 20m -> 10m B08 = load_band(f"{input_folder}/{identifier}B08.jp2", 1) # 10m native B8A = load_band(f"{input_folder}/{identifier}B8A.jp2", 2) # 20m -> 10m B09 = load_band(f"{input_folder}/{identifier}B09.jp2", 6) # 60m -> 10m B10 = load_band(f"{input_folder}/{identifier}B10.jp2", 6) # 60m -> 10m B11 = load_band(f"{input_folder}/{identifier}B11.jp2", 2) # 20m -> 10m B12 = load_band(f"{input_folder}/{identifier}B12.jp2", 2) # 20m -> 10m # Stack bands and normalize to reflectance values (0-1 range) bands = np.array(np.dstack(( B01[0]/10000.0, B02[0]/10000.0, B04[0]/10000.0, B05[0]/10000.0, B08[0]/10000.0, B8A[0]/10000.0, B09[0]/10000.0, B10[0]/10000.0, B11[0]/10000.0, B12[0]/10000.0 ))) # Initialize cloud detector with parameters optimized for 10m resolution cloud_detector = S2PixelCloudDetector( threshold=0.4, average_over=22, dilation_size=11 ) # Generate cloud probability maps (values 0.0-1.0) cloud_probs = cloud_detector.get_cloud_probability_maps(bands) print(f"Cloud probability shape: {cloud_probs.shape}") print(f"Probability range: {cloud_probs.min():.3f} - {cloud_probs.max():.3f}") # Generate binary cloud masks (values 0 or 1) cloud_masks = cloud_detector.get_cloud_masks(bands).astype(np.uint8) cloud_percentage = (cloud_masks.sum() / cloud_masks.size) * 100 print(f"Cloud coverage: {cloud_percentage:.2f}%") ``` ``` -------------------------------- ### Save Validation Outputs (Rescaled) Source: https://context7.com/kappazeta/s2cloudless/llms.txt Saves binary masks and probability maps for validation workflows. Masks are rescaled to 10980x10980 with 255 values, and probability maps use the inferno colormap. ```python def save_validation_outputs(mask, probability_map, identifier): # Save binary mask (0 or 255 values, rescaled to 10980x10980) mask_255 = np.where(mask == 1, 255, 0).astype(np.uint8) im_mask = Image.fromarray(mask_255) im_mask = im_mask.resize((10980, 10980), Image.NEAREST) im_mask.save(f"{identifier}_s2cloudless_prediction.png") # Save probability map with inferno colormap plt.figure(figsize=(15, 15)) plt.imshow(probability_map, cmap=plt.cm.inferno) plt.savefig(f"{identifier}_s2cloudless_probability.png") plt.close() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.