Fahimeh Orvati Nia
.
7c31b44
raw
history blame
3.05 kB
import gradio as gr
import tempfile
from pathlib import Path
from wrapper import run_pipeline_on_image
from PIL import Image
def process(file_obj):
if not file_obj:
return None, None, None, None, [], ""
with tempfile.TemporaryDirectory() as tmpdir:
# file_obj is a dict when using gr.File(type="file")
src = Path(file_obj.name)
ext = src.suffix.lstrip('.') or 'tif'
img_path = Path(tmpdir) / f"input.{ext}"
try:
img_bytes = src.read_bytes()
img_path.write_bytes(img_bytes)
except Exception:
# Fallback: save via PIL if direct copy fails
Image.open(src).save(img_path)
outputs = run_pipeline_on_image(str(img_path), tmpdir, save_artifacts=True)
def load_pil(path_str):
try:
if not path_str:
return None
im = Image.open(path_str)
copied = im.copy()
im.close()
return copied
except Exception:
return None
composite = load_pil(outputs.get('Composite'))
overlay = load_pil(outputs.get('Overlay'))
mask = load_pil(outputs.get('Mask'))
size_img = load_pil(str(Path(tmpdir) / 'results/size.size_analysis.png'))
# Texture LBP green path
lbp_path = Path(tmpdir) / 'texture_output/lbp_green.png'
texture_img = load_pil(str(lbp_path)) if lbp_path.exists() else None
order = ['NDVI', 'GNDVI', 'SAVI']
gallery_items = [load_pil(outputs[k]) for k in order if k in outputs]
stats_text = outputs.get('StatsText', '')
return size_img, composite, mask, overlay, texture_img, gallery_items, stats_text
with gr.Blocks() as demo:
gr.Markdown("# 🌿 Automated Plant Analysis Demo")
gr.Markdown("Upload a sorghum plant image to compute and visualize composite, mask, overlay, texture (LBP), vegetation indices, and statistics.")
with gr.Row():
with gr.Column():
# Use gr.File instead of gr.Image so TIFF is preserved
inp = gr.File(type="file", file_types=[".tif", ".tiff", ".png", ".jpg"], label="Upload Image")
run = gr.Button("Run Pipeline", variant="primary")
with gr.Row():
size_img = gr.Image(type="pil", label="Morphology Size", interactive=False)
composite_img = gr.Image(type="pil", label="Composite (Segmentation Input)", interactive=False)
mask_img = gr.Image(type="pil", label="Mask", interactive=False)
overlay_img = gr.Image(type="pil", label="Segmentation Overlay", interactive=False)
with gr.Row():
texture_img = gr.Image(type="pil", label="Texture LBP (Green Band)", interactive=False)
gallery = gr.Gallery(label="Vegetation Indices", columns=3, height="auto")
stats = gr.Textbox(label="Statistics", lines=4)
run.click(process, inputs=inp, outputs=[size_img, composite_img, mask_img, overlay_img, texture_img, gallery, stats])
if __name__ == "__main__":
demo.launch()