Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from drought_detection.utilities import transform_user_img, make_fig, get_dataframe_data
|
6 |
+
STORAGE_LOCATION = f'gs://wagon-data-batch913-drought_detection/SavedModel/Model_3band_RGB' # GCP path
|
7 |
+
model = tf.saved_model.load(STORAGE_LOCATION)
|
8 |
+
|
9 |
+
def predict(image):
|
10 |
+
image = transform_user_img(image)
|
11 |
+
with tf.compat.v1.Session(graph=tf.Graph()) as sess:
|
12 |
+
tf.compat.v1.saved_model.loader.load(sess, [tf.compat.v1.saved_model.tag_constants.SERVING], STORAGE_LOCATION)
|
13 |
+
graph = tf.compat.v1.get_default_graph()
|
14 |
+
prediction = sess.run('StatefulPartitionedCall:0',
|
15 |
+
feed_dict={'serving_default_keras_layer_input:0': image})
|
16 |
+
return prediction
|
17 |
+
def drought_prediction(image):
|
18 |
+
prediction = predict(image)
|
19 |
+
result = max(prediction[0])
|
20 |
+
if result == prediction[0][0]:
|
21 |
+
return "Drought :desert: :sweat: :sunny: : Looks like your region is likely suffering from drought and there's not enough plant life in the area to feed any cows."
|
22 |
+
elif result == prediction[0][1]:
|
23 |
+
return "Drought risk :cow: :warning: Looks like your region is at risk of drought. The forage quality can only feed one cow per 20sqm area."
|
24 |
+
elif result == prediction[0][2]:
|
25 |
+
return "Possible drought risk :cow: :seedling: :cow: : Looks like your region is not suffering from a drought, however, it can likely only feed 2 cows per 20sqm area."
|
26 |
+
elif result == prediction[0][3]:
|
27 |
+
return "No Drought :herb: :cow: :cow: :cow: :deciduous_tree: : Looks like your region is healthy and can feed at least 3 cows per 20sqm area! Happy foraging!"
|
28 |
+
|
29 |
+
def image_prediction(image):
|
30 |
+
df = get_dataframe_data(predict(image))
|
31 |
+
x = df.iloc[:, 2]
|
32 |
+
y = df.iloc[:, 1]
|
33 |
+
fig = make_fig(df, x, y)
|
34 |
+
return fig
|
35 |
+
|
36 |
+
def image_upload(file):
|
37 |
+
img = Image.open(file.name)
|
38 |
+
return img
|
39 |
+
|
40 |
+
inputs = gr.inputs.Image(upload=False)
|
41 |
+
outputs = gr.outputs.Image(type="plotly")
|
42 |
+
title = "Drought conditions in Northern Kenya"
|
43 |
+
description = "Applying deep learning and computer vision for drought resilience, using satellite images and human expert labels to detect drought conditions in Northern Kenya"
|
44 |
+
|
45 |
+
gr.Interface(fn=image_prediction, inputs=inputs, outputs=outputs, title=title, description=description, allow_flagging=False).launch()
|
46 |
+
|