Spaces:
Sleeping
Sleeping
Add application file
Browse files- README.md +4 -4
- app.py +87 -0
- img/denoised.png +3 -0
- img/noisy.png +3 -0
- packages.txt +1 -0
- requirements.txt +4 -0
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
title: Deepdespeckling
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: streamlit
|
7 |
-
sdk_version: 1.31.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
1 |
---
|
2 |
title: Deepdespeckling
|
3 |
+
emoji: π
|
4 |
+
colorFrom: yellow
|
5 |
+
colorTo: red
|
6 |
sdk: streamlit
|
7 |
+
sdk_version: 1.31.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.load_cosar import cos2mat
|
8 |
+
from deepdespeckling.utils.constants import PATCH_SIZE, STRIDE_SIZE
|
9 |
+
|
10 |
+
st.set_page_config(layout="wide", page_title="Deepdespeckling")
|
11 |
+
|
12 |
+
st.write("## Despeckle your SAR images")
|
13 |
+
st.write(
|
14 |
+
"Try to upload a light image "
|
15 |
+
)
|
16 |
+
st.sidebar.write("## Upload and download :gear:")
|
17 |
+
|
18 |
+
|
19 |
+
def convert_image(img):
|
20 |
+
buf = BytesIO()
|
21 |
+
img.save(buf, format="PNG")
|
22 |
+
byte_im = buf.getvalue()
|
23 |
+
return byte_im
|
24 |
+
|
25 |
+
|
26 |
+
def preprocess_for_png(image, threshold):
|
27 |
+
noisy_image = np.clip(image, 0, threshold)
|
28 |
+
noisy_image = noisy_image / threshold * 255
|
29 |
+
noisy_image = Image.fromarray(image.astype('float64')).convert('L')
|
30 |
+
|
31 |
+
return noisy_image
|
32 |
+
|
33 |
+
|
34 |
+
def preprocess_noisy_image(upload_path, denoiser):
|
35 |
+
image = cos2mat(upload_path)
|
36 |
+
noisy_image = np.array(image).reshape(
|
37 |
+
1, np.size(image, 0), np.size(image, 1), 2)
|
38 |
+
noisy_image, _, _ = denoiser.preprocess_noisy_image(noisy_image)
|
39 |
+
threshold = np.mean(noisy_image) + 3 * np.std(noisy_image)
|
40 |
+
|
41 |
+
noisy_image = preprocess_for_png(noisy_image, threshold=threshold)
|
42 |
+
|
43 |
+
return image, noisy_image, threshold
|
44 |
+
|
45 |
+
|
46 |
+
def fix_image(upload_path):
|
47 |
+
model_name = "spotlight"
|
48 |
+
denoiser = get_denoiser(model_name=model_name)
|
49 |
+
|
50 |
+
image, noisy_image, threshold = preprocess_noisy_image(
|
51 |
+
upload_path=upload_path, denoiser=denoiser)
|
52 |
+
|
53 |
+
col1.write("Original Image :camera:")
|
54 |
+
col1.image(noisy_image)
|
55 |
+
|
56 |
+
model_weights_path = get_model_weights_path(model_name=model_name)
|
57 |
+
despeckled_image = denoiser.denoise_image(
|
58 |
+
image, model_weights_path, PATCH_SIZE, STRIDE_SIZE)["denoised"]["full"]
|
59 |
+
|
60 |
+
despeckled_image = preprocess_for_png(
|
61 |
+
despeckled_image, threshold=threshold)
|
62 |
+
|
63 |
+
col2.write("Despeckled Image")
|
64 |
+
col2.image(despeckled_image)
|
65 |
+
st.sidebar.markdown("\n")
|
66 |
+
st.sidebar.download_button("Download despeckled image", convert_image(
|
67 |
+
despeckled_image), "despeckled.png", "image/png")
|
68 |
+
|
69 |
+
|
70 |
+
def init_image():
|
71 |
+
col1.write("Noisy Image ")
|
72 |
+
col1.image("img/noisy.png")
|
73 |
+
|
74 |
+
col1.write("Despeckled Image")
|
75 |
+
col1.image("img/denoised.png")
|
76 |
+
|
77 |
+
|
78 |
+
col1, col2 = st.columns(2)
|
79 |
+
my_upload = st.sidebar.file_uploader("Upload an image", type=["cos"])
|
80 |
+
|
81 |
+
if my_upload is not None:
|
82 |
+
b = my_upload.getvalue()
|
83 |
+
with open("test.cos", "wb") as f:
|
84 |
+
f.write(b)
|
85 |
+
fix_image("test.cos")
|
86 |
+
else:
|
87 |
+
init_image()
|
img/denoised.png
ADDED
Git LFS Details
|
img/noisy.png
ADDED
Git LFS Details
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
libgdal-dev
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
deepdespeckling
|
2 |
+
pillow
|
3 |
+
numpy
|
4 |
+
GDAL==3.6.2
|