import numpy as np import cv2 import json from sentinelhub import SHConfig, MimeType, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, geo_utils def upload_image(): def detect_deforestation(bbox, time_interval, output_dir, config=None): if config is None: # Configure SentinelHub with your credentials config = SHConfig() config.sh_client_id = '3e9277b3-aa11-4f98-a160-3bf006130cc7' config.sh_client_secret = 'CY>imU7J,97EniGj5jyRtz:l13@XL2*gfY+pvqRw' # Define the SentinelHub request parameters evalscript = """ // Import Sentinel-2 bands return [B02, B03, B04, B08]; """ request = SentinelHubRequest( evalscript=evalscript, input_data=[ SentinelHubRequest.input_data( data_collection=DataCollection.SENTINEL2_L1C, time_interval=time_interval, maxcc=0.2 ) ], responses=[ SentinelHubRequest.output_response('default', MimeType.TIFF), ], bbox=bbox, config=config, ) # Fetch Sentinel imagery sentinel_data = request.get_data() sentinel_data = np.array(sentinel_data[0]) # Calculate the NDVI (Normalized Difference Vegetation Index) nir = sentinel_data[:, :, 3] red = sentinel_data[:, :, 2] ndvi = (nir - red) / (nir + red) # Apply a threshold to identify deforested areas threshold = 0.2 deforestation_mask = (ndvi < threshold) # Create a color-coded PNG image deforestation_image = np.zeros_like(sentinel_data) deforestation_image[deforestation_mask] = [0, 0, 255, 255] # Red color for deforestation # Save the PNG image png_path = f"{output_dir}/deforestation.png" cv2.imwrite(png_path, deforestation_image) return png_path#, geojson_path # Example usage: longitude_min, latitude_min, longitude_max, latitude_max =-50.768464422847735,-7.073172070815588,-50.33313117089461,-6.653230308928273 bbox = BBox([longitude_min, latitude_min, longitude_max, latitude_max], crs="EPSG:4326") time_interval = ('2022-01-01', '2023-07-31') output_directory = 'SFTP' result = detect_deforestation(bbox, time_interval, output_directory) print(result)