#get the image #get total pixels #get colored_pixels where alpha is 1 #get field_area #get crop field_area from tempfile import tempdir import firebase_admin from firebase_admin import credentials from firebase_admin import db import pandas as pdf from sklearn.metrics import classification_report, confusion_matrix, accuracy_score import statsmodels.api as sm import seaborn as sns sns.set() import traceback from sklearn.ensemble import RandomForestRegressor from sklearn.model_selection import train_test_split import csv import time import json import numpy as np from sklearn.cluster import KMeans from sentinelhub import WebFeatureService, BBox, CRS, MimeType, CRS, BBox, WmsRequest,DataCollection import traceback from PIL import Image import numpy as np from google.cloud import storage import cv2 storage_client = storage.Client() bucket_name = 'farmbase-b2f7e.appspot.com' cred = credentials.Certificate('servicekey.json') x = 0.0003 y = 0.0003 fromdate = '20220701' todate = '20220715' s1_images = ['IW-VH-DB'] s1_images = ['NDVI'] s2_images = [] # s1_images = ["B02", "B03", "B04", "B05"] #s2_images = ["RVI-NEW"] dates = [ '10101010', '20202020', '30303030', '40404040'] dates = ['10101010'] try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') uid = 'wEJQVaxiZZaaId2aJe6lRJ8Zd0S2' uid = 'JgbkYJkMVsRXpSndLsDlabDomOp2' #uid = 'a7X8A4BkmbeUkkZ5HyHLcqljyA12' uid = 'VWoPJ2nfWDRlrYQo9dmctdLHeJN2' uid = 'xk6kws4YMkdeBAstSWDNsd42w9m2' fields_obj = db.reference('PaidMonitoredFields').child('PMF').child(uid).get() crops = ['Wheat', 'Sugarbeet', 'Clover', 'Barley'] crops = ['Quinoa'] crops = ['Cotton'] dates = ['10101010'] num = 0 www = 0 for single_date in dates: crop = crops[num] for(fieldid, field_obj) in fields_obj.items(): if www == 0: total_pixels, area_pixels = 0,0 bucket = storage_client.get_bucket(bucket_name) destination_blob_name = 'PaidMonitoredFields/'+uid+'/'+fieldid+'/'+single_date+'/hybrid' print(destination_blob_name) blob = bucket.blob(destination_blob_name) file_name = 'temp_img.png' name = field_obj["FieldDescription"] field_area = int(field_obj["FieldArea"])/10000 blob.download_to_filename(file_name) img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED) h,w,c = img.shape b_channel, g_channel, r_channel, alpha_channel = cv2.split(img) for y in range(0,h): for x in range(0,w): if alpha_channel[y][x] == 255: if g_channel[y][x] != 0 or r_channel[y][x] != 0 or b_channel[y][x] != 0: area_pixels = area_pixels + 1 total_pixels = total_pixels + 1 crop_area = round((area_pixels/total_pixels)*field_area) print([area_pixels,total_pixels, h, w, c]) print(('Name: ' + name + ', Area: ' + str(field_area) + ", Crop Area: " + str(crop_area))) #db.reference('EgyptResults').child(crop).child(name).set(crop_area) num = num + 1