Pastuu's picture
Update app.py
8331e09
from huggingface_hub import hf_hub_download
config_path=hf_hub_download(repo_id="ibm-nasa-geospatial/Prithvi-100M-sen1floods11", filename="sen1floods11_Prithvi_100M.py", token=os.environ.get("token"))
ckpt=hf_hub_download(repo_id="ibm-nasa-geospatial/Prithvi-100M-sen1floods11", filename='sen1floods11_Prithvi_100M.pth', token=os.environ.get("token"))
import numpy as np
import gradio as gr
from PIL import Image
import rasterio
def cargar_imagen_tif(tifile):
try:
with rasterio.open(tifile, "r") as src:
data = src.read()
tuki = Image.fromarray(data[0]) # Convierte el arreglo raster a una imagen PIL
return convertir_a_blanco_y_negro(tuki) # Captura el valor de retorno de la función
except Exception as e:
return f"Error al cargar la imagen TIFF: {str(e)}"
def convertir_a_blanco_y_negro(input_img):
try:
img_array = np.array(input_img)
binary_img = np.zeros_like(img_array)
color_threshold = 50
for i in range(img_array.shape[0]):
for j in range(img_array.shape[1]):
pixel_color = img_array[i, j]
if np.all(pixel_color <= color_threshold):
binary_img[i, j] = 0
else:
binary_img[i, j] = 255
binary_img = Image.fromarray(np.uint8(binary_img))
return binary_img, "Hecho"
except Exception as e:
return f"Error al convertir a blanco y negro: {str(e)}"
demo = gr.Interface(
fn=cargar_imagen_tif,
inputs="file",
outputs=["image", "text"],
title="Conversión a Blanco y Negro",
description="Carga una imagen TIFF y conviértela a blanco y negro."
)
demo.launch()