def find_img_value(fieldid, satellite, imageType,bounds, from_date, to_date,CLIENT_ID,CLIENT_SECRET,INSTANCE_ID, max_num): from sentinelhub import WebFeatureService, BBox, CRS, DataSource, MimeType, CRS, BBox, WmsRequest,DataCollection from sentinelhub import SHConfig from firebase_admin import credentials from firebase_admin import db from statistics import mean from PIL import Image, ImageFilter import os.path import PIL import numpy as np import time import traceback #from scipy import ndimag config = SHConfig() print(imageType) img_values_arr = [] cred = credentials.Certificate('servicekey2.json') access_key = 'AKIAIPCM5ZR7FRHMY3MA' secret_key = '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' if CLIENT_ID and CLIENT_SECRET: config.sh_client_id = CLIENT_ID config.sh_client_secret = CLIENT_SECRET config.instance_id = INSTANCE_ID img_width = 25 search_bbox = BBox(bbox=bounds, crs=CRS.WGS84) search_time_interval = (from_date, to_date) avg_im = 0 i = 0 prev_date = '90101010' iterator_wms = WebFeatureService(search_bbox,search_time_interval,data_collection=satellite,maxcc=1.0,config=config) for tile_info in iterator_wms: #print(tile_info["properties"]["date"]) try: s1_request = WmsRequest(data_collection=satellite,layer=imageType,bbox=search_bbox,time=tile_info["properties"]["date"],width=img_width,image_format=MimeType.TIFF,config=config) s1_data = s1_request.get_data() im = s1_data[-1] # new_dir = 'gt_input/'+ str(fieldid) # file_name = new_dir + '/' + tile_info["properties"]["date"] + '.tiff' # isdir = os.path.isdir(new_dir) # if isdir != True: # os.mkdir(new_dir) # aqa = 1 #im = PIL.Image.fromarray(s1_data[-1]) #im.save(file_name) new_date = tile_info["properties"]["date"] aqa = 1 #if aqa == 1: prev_day = prev_date.replace("-","") new_day = new_date.replace("-","") #print((str(prev_day) + ", " + str(new_day))) if int(prev_day)-int(new_day) > 4: print(new_day) prev_date = new_date #if str(prev_date) != str(new_date): w,h = len(im[0]), len(im) #print(w,h) pixel_values = [] z_num = 0 for p in range(0,h-1): for q in range(0,w-1): pixel_value = im[p,q,0] if im[p,q,0] == 0: z_num = z_num + 1 pixel_values.append(pixel_value) #im = PIL.Image.fromarray(s1_data[-1],"L") #im_array = np.array(im) avg_im = round(100*np.mean(pixel_values))/100 #print(('z_n' + str(z_num))) if z_num < int(w*h): if i < max_num: img_values_arr.append(avg_im) i = i +1 else: break except Exception as e: print(e) print(traceback.format_exc()) adda = 1 #print(avg_im) time.sleep(0.25) #print(img_values_arr) if i < max_num: print(i) return None else: return img_values_arr