from PIL import Image import numpy as np from google.cloud import storage from firebase_admin import db import cv2 def make_area_estimate_image(arr, w, h, study_uid, study_field_id, pred_vals): num = 0 new_img = [[0]*w]*h row = 0 column = 0 storage_client = storage.Client() bucket_name = 'farmbase-b2f7e.appspot.com' data = np.zeros((h,w,3), dtype=np.uint8) for i in arr: if i in pred_vals: data[row, column] = [0, 255,0] new_img[row][column] = i column = column + 1 if column%w == 0: row = row+1 column = 0 #print(row, column) img = Image.fromarray(data, 'RGB') file_name = study_uid + '/' + study_field_id + '/area_estimation.png' img.save(file_name) img.save('marea.png') 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 = 'area1.png' img = cv2.imread(dem_file_name) kernel = np.ones((2,2), np.uint8) opening = img 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.erode(opening, kernel, iterations = 1) cv2.imwrite(dem_file_name, opening) bucket = storage_client.get_bucket(bucket_name) blob = bucket.blob(destination_blob_name_dem) blob.upload_from_filename(dem_file_name) #print(new_img)