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_egypt_estimate_image(arr, w, h, study_uid, study_field_id, pred_vals): num = 0 new_img = [[0]*w]*h 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"] data = np.zeros((h,w,3), dtype=np.uint8) total_pixels = h*w area_pixels_arr = [0,0,0,0,0] area_pixels = 0 #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(max(arr)) print(min(arr)) for i in arr: # if i >=2 and i < 6: # area_pixels_arr[0] = area_pixels_arr[0] + 1 # data[row, column] = crops_rgb[0] # area_pixels = area_pixels + 1 # elif i >=6 and i < 10: # area_pixels_arr[1] = area_pixels_arr[1] + 1 # data[row, column] = crops_rgb[1] # area_pixels = area_pixels + 1 # elif i >=10 and i < 13: # area_pixels_arr[2] = area_pixels_arr[2] + 1 # data[row, column] = crops_rgb[2] # area_pixels = area_pixels + 1 # elif i >=13 and i < 18: # area_pixels_arr[3] = area_pixels_arr[3] + 1 # data[row, column] = crops_rgb[3] # area_pixels = area_pixels + 1 # else: # area_pixels_arr[4] = area_pixels_arr[4] + 1 # data[row, column] = crops_rgb[4] # area_pixels = area_pixels+1 if i >= 1.5: area_pixels_arr[1] = area_pixels_arr[1] + 1 #data[row, column] = crops_rgb[1] 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_img[row][column] = i column = column + 1 # total_pixels = total_pixels + 1 if column%w == 0: row = row+1 column = 0 #print(row, column) img = Image.fromarray(data, 'RGB') file_name = study_uid + '/area_estimation.png' isdir = os.path.isdir(study_uid) if isdir != True: os.mkdir(study_uid) img.save(file_name) latestDay = 0 lastDayRef = db.reference('PaidMonitoredFields').child('PMF').child(study_uid).child(study_field_id).child('SensedDays').get() try: for (x,y) in lastDayRef.items(): if int(x) > int(latestDay): latestDay = x except: 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 = file_name #get_mask(study_uid,study_field_id,coordinates,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) img = cv2.imread(dem_file_name) kernel_int = 2 kernel = np.ones((kernel_int, kernel_int), np.uint8) opening = img m = 5 arr = np.array(img) nval = np.max(arr) + 1 counts, _ = np.histogram(arr, bins=range(nval + 1)) # Compute the set of neighbours for each number via binary dilation c = np.array([scipy.ndimage.morphology.binary_dilation(arr == i, iterations=-1) for i in range(nval)]) # Loop over the set of arrays with bad count and update them to the most common # neighbour for i in filter(lambda i: counts[i] < m, range(nval)): arr[arr == i] = np.argmax(np.sum(c[:, arr == i], axis=1)) im = Image.fromarray(arr) im.save(file_name) # i = 0 # while i < 1: # opening = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) # #opening = cv2.morphologyEx(opening, cv2.MORPH_OPEN, kernel) # i = i + 1 # # kernel = np.ones((1,1), np.uint8) # opening = cv2.dilate(opening, kernel, iterations = 1) i = 0 #while i < 1: #opening = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) # opening = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel) #i = i + 1 cv2.imwrite(dem_file_name, opening) area_pixels,total_pixels = get_mask(study_uid,study_field_id,coordinates,fieldmaxlat,fieldminlat,fieldmaxlong,fieldminlong) crop_area = (area_pixels/total_pixels)*int(field_area) crop_area = int(crop_area/10000) print((field_name + ' Area: ' + str(int(field_area/10000)) + ' Ha, Crop Area: ' + str(crop_area) + ' Ha')) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name) #print(new_img)