File size: 1,746 Bytes
9d30a24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from fast_dash import FastDash, UploadImage, Image, Upload
import PIL
import numpy as np

from app import func
import tempfile
import base64

examples = ["India_900498_S2Hand", "Spain_7370579_S2Hand", "USA_430764_S2Hand"]

def detect_water(select_an_example: str = examples, input_tiff_image: Upload = None) -> (Image, Image):
    """
    NASA and IBM recently uploaded their foundation model on Hugging Face, Pritivi, at https://huggingface.co/ibm-nasa-geospatial. 
    This demo, built with Fast Dash, showcases a version of Prithvi that they finetuned to detect water from satellite images.
    Select an example or upload your own TIFF image.
    If uploading your own image, read the format details at https://huggingface.co/ibm-nasa-geospatial/Prithvi-100M-sen1floods11.
    """

    # If example is selected
    if input_tiff_image is None:
        input_satellite_image = PIL.Image.open(os.path.join("examples", f"{select_an_example}.png"))
        water_prediction_mask = PIL.Image.open(os.path.join("examples", f"{select_an_example}_result.png"))

    # If file is uploaded, run inference
    else:
        with tempfile.NamedTemporaryFile(delete=False) as f:
            contents = input_tiff_image.encode("utf8").split(b";base64,")[1]
            f.write(base64.decodebytes(contents))

        input_satellite_image, water_prediction_mask = func(f)

        input_satellite_image = PIL.Image.fromarray(np.uint8(input_satellite_image)).convert('RGB')
        water_prediction_mask = PIL.Image.fromarray(np.uint8(water_prediction_mask)).convert('RGB')

    return input_satellite_image, water_prediction_mask

app = FastDash(detect_water, theme="cosmo", port=7860)
server = app.server

if __name__ == "__main__":
    app.run()