Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from zipfile import ZipFile
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from tensorflow import keras
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
from keras import backend as K
|
| 8 |
+
import cv2
|
| 9 |
+
import tempfile
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
st.title("Satellite Image Segmentation with Dense-UNet")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@st.cache(allow_output_mutation=True)
|
| 17 |
+
def loading_model():
|
| 18 |
+
model = load_model('satellitesegment.h5')
|
| 19 |
+
#model._make_predict_function()
|
| 20 |
+
#model.summary()
|
| 21 |
+
session = K.get_session()
|
| 22 |
+
return model,session
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
@st.cache
|
| 26 |
+
def upload_img(image):
|
| 27 |
+
img_npy = np.array(image)
|
| 28 |
+
#img_npy = img_npy.reshape((1,512,512,3))
|
| 29 |
+
|
| 30 |
+
return img_npy
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
uploaded_file = st.file_uploader("Choose an image...", type=['tif'])
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if uploaded_file is not None:
|
| 37 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 38 |
+
tfile.write(uploaded_file.read())
|
| 39 |
+
t_img = Image.open(tfile.name)
|
| 40 |
+
|
| 41 |
+
image = cv2.imread(tfile.name,-1)
|
| 42 |
+
|
| 43 |
+
st.image(t_img, caption='Uploaded Image.', use_column_width=False)
|
| 44 |
+
|
| 45 |
+
button = st.button("Let's Predict Image")
|
| 46 |
+
|
| 47 |
+
if button:
|
| 48 |
+
t = st.empty()
|
| 49 |
+
t.markdown('## İmage is segmenting...')
|
| 50 |
+
#t.markdown(f'{image.shape}')
|
| 51 |
+
model,session = loading_model()
|
| 52 |
+
K.set_session(session)
|
| 53 |
+
image = np.array(image,dtype='uint16').reshape((1,512,512,3))
|
| 54 |
+
result_img = model.predict(image)
|
| 55 |
+
result_img = result_img[:,:,:,:]>0.5
|
| 56 |
+
result_img = result_img[0,:,:,0]
|
| 57 |
+
result_img = Image.fromarray(result_img)
|
| 58 |
+
t.markdown('## Segmentation result: ')
|
| 59 |
+
st.image(result_img, caption='Predicted Image.', use_column_width=False)
|