brash6 commited on
Commit
90fb129
·
1 Parent(s): e640a55

first app version 1

Browse files
Files changed (3) hide show
  1. .streamlit/config.toml +2 -0
  2. app.py +90 -0
  3. requirements.txt +3 -0
.streamlit/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [server]
2
+ maxUploadSize = 400
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from io import BytesIO, TextIOWrapper
4
+ import numpy as np
5
+
6
+ from deepdespeckling.despeckling import get_denoiser, get_model_weights_path
7
+ from deepdespeckling.utils.constants import PATCH_SIZE, STRIDE_SIZE
8
+ from deepdespeckling.utils.load_cosar import cos2mat
9
+ from deepdespeckling.utils.utils import load_sar_image
10
+
11
+ st.set_page_config(layout="wide", page_title="Deepdespeckling")
12
+
13
+ st.write("## Despeckle your SAR images")
14
+ st.write(
15
+ "Try to upload a light image "
16
+ )
17
+ st.sidebar.write("## Upload and download :gear:")
18
+
19
+ # Download the fixed image
20
+
21
+
22
+ def convert_image(img):
23
+ buf = BytesIO()
24
+ img.save(buf, format="PNG")
25
+ byte_im = buf.getvalue()
26
+ return byte_im
27
+
28
+
29
+ def preprocess_for_png(image, threshold):
30
+ noisy_image = np.clip(image, 0, threshold)
31
+ noisy_image = noisy_image / threshold * 255
32
+ noisy_image = Image.fromarray(image.astype('float64')).convert('L')
33
+
34
+ return noisy_image
35
+
36
+
37
+ def preprocess_noisy_image(upload_path, denoiser):
38
+ image = cos2mat(upload_path)
39
+ noisy_image = np.array(image).reshape(
40
+ 1, np.size(image, 0), np.size(image, 1), 2)
41
+ noisy_image, _, _ = denoiser.preprocess_noisy_image(noisy_image)
42
+ threshold = np.mean(noisy_image) + 3 * np.std(noisy_image)
43
+
44
+ noisy_image = preprocess_for_png(noisy_image, threshold=threshold)
45
+
46
+ return image, noisy_image, threshold
47
+
48
+
49
+ def fix_image(upload_path):
50
+ model_name = "spotlight"
51
+ denoiser = get_denoiser(model_name=model_name)
52
+
53
+ image, noisy_image, threshold = preprocess_noisy_image(
54
+ upload_path=upload_path, denoiser=denoiser)
55
+
56
+ col1.write("Original Image :camera:")
57
+ col1.image(noisy_image)
58
+
59
+ model_weights_path = get_model_weights_path(model_name=model_name)
60
+ despeckled_image = denoiser.denoise_image(
61
+ image, model_weights_path, PATCH_SIZE, STRIDE_SIZE)["denoised"]["full"]
62
+
63
+ despeckled_image = preprocess_for_png(
64
+ despeckled_image, threshold=threshold)
65
+
66
+ col2.write("Despeckled Image")
67
+ col2.image(despeckled_image)
68
+ st.sidebar.markdown("\n")
69
+ st.sidebar.download_button("Download despeckled image", convert_image(
70
+ despeckled_image), "despeckled.png", "image/png")
71
+
72
+
73
+ def init_image():
74
+ col1.write("Noisy Image ")
75
+ col1.image("img/noisy.png")
76
+
77
+ col1.write("Despeckled Image")
78
+ col1.image("img/denoised.png")
79
+
80
+
81
+ col1, col2 = st.columns(2)
82
+ my_upload = st.sidebar.file_uploader("Upload an image", type=["cos"])
83
+
84
+ if my_upload is not None:
85
+ b = my_upload.getvalue()
86
+ with open("test.cos", "wb") as f:
87
+ f.write(b)
88
+ fix_image("test.cos")
89
+ else:
90
+ init_image()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ deepdespeckling
2
+ PIL
3
+ numpy