File size: 2,523 Bytes
1d17d86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import streamlit as st
from PIL import Image
from io import BytesIO, TextIOWrapper
import numpy as np

from deepdespeckling.despeckling import get_denoiser, get_model_weights_path
from deepdespeckling.utils.load_cosar import cos2mat
from deepdespeckling.utils.constants import PATCH_SIZE, STRIDE_SIZE

st.set_page_config(layout="wide", page_title="Deepdespeckling")

st.write("## Despeckle your SAR images")
st.write(
    "Try to upload a light image "
)
st.sidebar.write("## Upload and download :gear:")


def convert_image(img):
    buf = BytesIO()
    img.save(buf, format="PNG")
    byte_im = buf.getvalue()
    return byte_im


def preprocess_for_png(image, threshold):
    noisy_image = np.clip(image, 0, threshold)
    noisy_image = noisy_image / threshold * 255
    noisy_image = Image.fromarray(image.astype('float64')).convert('L')

    return noisy_image


def preprocess_noisy_image(upload_path, denoiser):
    image = cos2mat(upload_path)
    noisy_image = np.array(image).reshape(
        1, np.size(image, 0), np.size(image, 1), 2)
    noisy_image, _, _ = denoiser.preprocess_noisy_image(noisy_image)
    threshold = np.mean(noisy_image) + 3 * np.std(noisy_image)

    noisy_image = preprocess_for_png(noisy_image, threshold=threshold)

    return image, noisy_image, threshold


def fix_image(upload_path):
    model_name = "spotlight"
    denoiser = get_denoiser(model_name=model_name)

    image, noisy_image, threshold = preprocess_noisy_image(
        upload_path=upload_path, denoiser=denoiser)

    col1.write("Original Image :camera:")
    col1.image(noisy_image)

    model_weights_path = get_model_weights_path(model_name=model_name)
    despeckled_image = denoiser.denoise_image(
        image, model_weights_path, PATCH_SIZE, STRIDE_SIZE)["denoised"]["full"]

    despeckled_image = preprocess_for_png(
        despeckled_image, threshold=threshold)

    col2.write("Despeckled Image")
    col2.image(despeckled_image)
    st.sidebar.markdown("\n")
    st.sidebar.download_button("Download despeckled image", convert_image(
        despeckled_image), "despeckled.png", "image/png")


def init_image():
    col1.write("Noisy Image ")
    col1.image("img/noisy.png")

    col1.write("Despeckled Image")
    col1.image("img/denoised.png")


col1, col2 = st.columns(2)
my_upload = st.sidebar.file_uploader("Upload an image", type=["cos"])

if my_upload is not None:
    b = my_upload.getvalue()
    with open("test.cos", "wb") as f:
        f.write(b)
    fix_image("test.cos")
else:
    init_image()