File size: 2,805 Bytes
3834351
4768cde
dd1d7f5
a32df56
4c1c4a7
 
 
3834351
49abd9f
4c1c4a7
49abd9f
 
4c1c4a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49abd9f
4768cde
3834351
 
4768cde
49abd9f
 
 
 
4768cde
 
dd1d7f5
 
 
 
 
3834351
9226311
d807150
 
 
 
 
 
 
 
 
 
 
 
 
49abd9f
4768cde
3834351
 
dd1d7f5
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
import gradio as gr
import tempfile
from pathlib import Path
from wrapper import run_pipeline_on_image
import numpy as np
from PIL import Image
from itertools import product

def show_preview(image):
    """Show pseudo-RGB composite preview."""
    if image is None:
        return None
    
    try:
        # Process to pseudo-RGB composite (same as pipeline does)
        d = image.size[0] // 2
        boxes = [(j, i, j + d, i + d) for i, j in product(range(0, image.height, d), range(0, image.width, d))]
        stack = np.stack([np.array(image.crop(box), dtype=float) for box in boxes], axis=-1)
        green, red, red_edge, nir = np.split(stack, 4, axis=-1)
        
        # Pseudo-RGB: (green, red_edge, red)
        composite = np.concatenate([green, red_edge, red], axis=-1)
        
        # Normalize to uint8
        composite = np.nan_to_num(composite, nan=0.0, posinf=0.0, neginf=0.0)
        ptp = np.ptp(composite)
        if ptp > 0:
            normalized = (composite - composite.min()) / (ptp + 1e-6) * 255
        else:
            normalized = np.zeros_like(composite)
        composite_uint8 = np.clip(normalized, 0, 255).astype(np.uint8)
        
        # Convert to PIL for display
        return Image.fromarray(composite_uint8)
    except Exception as e:
        # Fallback: return original if processing fails
        return image

def process(image):
    if image is None:
        return []
    with tempfile.TemporaryDirectory() as tmpdir:
        # Save PIL image preserving original format
        # Determine extension from image format
        ext = image.format.lower() if image.format else 'png'
        img_path = Path(tmpdir) / f"input.{ext}"
        image.save(img_path)
        outputs = run_pipeline_on_image(str(img_path), tmpdir, save_artifacts=True)
        # Keep order consistent: return exactly the 7 images
        order = [
            'NDVI', 'ARI', 'GNDVI', 'LBP', 'HOG', 'Lacunarity', 'SizeAnalysis'
        ]
        return [outputs[k] for k in order if k in outputs]

with gr.Blocks() as demo:
    gr.Markdown("# 🌿 Sorghum Plant Analysis Demo")
    gr.Markdown("Upload a sorghum plant image to analyze vegetation indices, texture features, and morphology.")
    
    with gr.Row():
        with gr.Column():
            inp = gr.Image(type="pil", label="Upload Image")
            run = gr.Button("Run Pipeline", variant="primary")
        with gr.Column():
            preview = gr.Image(type="pil", label="Uploaded Image Preview", interactive=False)
    
    gallery = gr.Gallery(label="Analysis Results", columns=3, height="auto")
    
    # Update preview when image is uploaded
    inp.change(fn=show_preview, inputs=inp, outputs=preview)
    run.click(process, inputs=inp, outputs=gallery)

if __name__ == "__main__":
    demo.launch()