from PIL import Image import numpy as np from google.cloud import storage import os from firebase_admin import db from get_mask import get_mask import cv2 import scipy.ndimage def make_area_estimation_image(arr, study_uid, study_field_id, threshold, latestDay, portion_num, row_num, col_num): num = 0 row = 0 column = 0 m = 25 storage_client = storage.Client() bucket_name = 'farmbase-b2f7e.appspot.com' map_fields = db.reference('PaidMonitoredFields').child('PMF').child(study_uid).child(study_field_id).get() coordinates = map_fields["Coordinates"] fieldmaxlat = map_fields["FieldMaxLat"] fieldminlat = map_fields["FieldMinLat"] fieldmaxlong = map_fields["FieldMaxLong"] fieldminlong = map_fields["FieldMinLong"] try: field_name = map_fields["FieldDescription"] except: field_name = map_fields["FieldAddress"] field_area = map_fields["FieldArea"] main_img_name = study_uid + "/" + study_field_id + "/area_estimate_ndvi_" + field_name + ".png" portion_img_name = study_uid + "/" + study_field_id + "/area_estimate_ndvi_" + field_name + "_"+ str(row_num)+ str(col_num)+ ".png" rvi_img = cv2.imread(main_img_name) h,w,c = rvi_img.shape data = np.zeros((h,w,3), dtype=np.uint8) new_rvi_data = data total_pixels = h*w area_pixels_arr = [0,0,0,0,0] area_pixels = 0 #rvi_img = cv2.imread("area_estimate_rvi.tiff") #crops_rgb = [[0,255,255],[0,255,0],[255,0,0],[0,0,255],[0,0,0]] crops_rgb = [[0,0,0],[0,255,0]] #ranges = [[2,6],[6,10],[10,14],[14,18],[-2,2]] print('min_max',min(arr),max(arr)) print(arr) # print(min(arr)) for i in arr: if i >= threshold: area_pixels_arr[1] = area_pixels_arr[1] + 1 data[row, column] = crops_rgb[1] new_rvi_data[row, column] = [rvi_img[row][column][2],rvi_img[row][column][1],rvi_img[row][column][0]] #data[row, column] = 1 area_pixels = area_pixels + 1 else: area_pixels_arr[0] = area_pixels_arr[0] + 1 #data[row, column] = 0 data[row, column] = crops_rgb[0] new_rvi_data[row, column] = crops_rgb[0] # new_img[row][column] = i column = column + 1 # total_pixels = total_pixels + 1 if column%w == 0: row = row+1 column = 0 #print(row, column) #new_rvi_data = rvi_img #temp_rvi_img = new_rvi_data img = Image.fromarray(data, 'RGB') file_name = main_img_name #temp_rvi_img = Image.fromarray(new_rvi_data, 'RGB') #rvi_file_name = main_img_name isdir = os.path.isdir(study_uid) if isdir != True: os.mkdir(study_uid) img.save(file_name) img.save(portion_img_name) #temp_rvi_img.save(portion_img_name) latestDay = 0 lastDayRef = db.reference('PaidMonitoredFields').child('PMF').child(study_uid).child(study_field_id).child('SensedDays').get() if latestDay == None: try: for (x,y) in lastDayRef.items(): if int(x) > int(latestDay): latestDay = x except: latestDay = 20202020 latestDay = 10101010 db.reference('PaidMonitoredFields').child('PMF').child(study_uid).child(study_field_id).child('SensedDays').child(str(latestDay)).set("yes") latestDay = str(latestDay) destination_blob_name_dem = 'PaidMonitoredFields/'+study_uid+'/'+study_field_id+'/'+latestDay+'/hybrid' dem_file_name = main_img_name new_area_pixels, new_total_pixels = get_mask(study_uid,study_field_id,coordinates,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong, field_name) #crop_area = (area_pixels_arr[1]/(area_pixels_arr[0] + area_pixels_arr[1]))*int(field_area) crop_area = (new_area_pixels/new_total_pixels)*int(field_area) crop_area = int(crop_area/10000) print_text = (field_name + ' Area: ' + str(int(field_area/10000)) + ' Ha, Crop Area: ' + str(crop_area) + ' Ha') print(print_text) # try: # db.reference('EgyptResults').child(field_name).set(print_text) # except: # awa = 1 bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name)