Fahimeh Orvati Nia commited on
Commit
916b83d
·
1 Parent(s): 7c31b44
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -4,21 +4,25 @@ from pathlib import Path
4
  from wrapper import run_pipeline_on_image
5
  from PIL import Image
6
 
7
- def process(file_obj):
8
- if not file_obj:
9
- return None, None, None, None, [], ""
 
 
10
  with tempfile.TemporaryDirectory() as tmpdir:
11
- # file_obj is a dict when using gr.File(type="file")
12
- src = Path(file_obj.name)
13
  ext = src.suffix.lstrip('.') or 'tif'
14
  img_path = Path(tmpdir) / f"input.{ext}"
 
15
  try:
 
16
  img_bytes = src.read_bytes()
17
  img_path.write_bytes(img_bytes)
18
  except Exception:
19
  # Fallback: save via PIL if direct copy fails
20
  Image.open(src).save(img_path)
21
 
 
22
  outputs = run_pipeline_on_image(str(img_path), tmpdir, save_artifacts=True)
23
 
24
  def load_pil(path_str):
@@ -36,22 +40,32 @@ def process(file_obj):
36
  overlay = load_pil(outputs.get('Overlay'))
37
  mask = load_pil(outputs.get('Mask'))
38
  size_img = load_pil(str(Path(tmpdir) / 'results/size.size_analysis.png'))
 
39
  # Texture LBP green path
40
  lbp_path = Path(tmpdir) / 'texture_output/lbp_green.png'
41
  texture_img = load_pil(str(lbp_path)) if lbp_path.exists() else None
 
 
42
  order = ['NDVI', 'GNDVI', 'SAVI']
43
  gallery_items = [load_pil(outputs[k]) for k in order if k in outputs]
 
44
  stats_text = outputs.get('StatsText', '')
 
45
  return size_img, composite, mask, overlay, texture_img, gallery_items, stats_text
46
 
 
47
  with gr.Blocks() as demo:
48
  gr.Markdown("# 🌿 Automated Plant Analysis Demo")
49
- gr.Markdown("Upload a sorghum plant image to compute and visualize composite, mask, overlay, texture (LBP), vegetation indices, and statistics.")
50
 
51
  with gr.Row():
52
  with gr.Column():
53
- # Use gr.File instead of gr.Image so TIFF is preserved
54
- inp = gr.File(type="file", file_types=[".tif", ".tiff", ".png", ".jpg"], label="Upload Image")
 
 
 
 
55
  run = gr.Button("Run Pipeline", variant="primary")
56
 
57
  with gr.Row():
@@ -66,7 +80,11 @@ with gr.Blocks() as demo:
66
  gallery = gr.Gallery(label="Vegetation Indices", columns=3, height="auto")
67
  stats = gr.Textbox(label="Statistics", lines=4)
68
 
69
- run.click(process, inputs=inp, outputs=[size_img, composite_img, mask_img, overlay_img, texture_img, gallery, stats])
 
 
 
 
70
 
71
  if __name__ == "__main__":
72
  demo.launch()
 
4
  from wrapper import run_pipeline_on_image
5
  from PIL import Image
6
 
7
+
8
+ def process(file_path):
9
+ if not file_path:
10
+ return None, None, None, None, None, [], ""
11
+
12
  with tempfile.TemporaryDirectory() as tmpdir:
13
+ src = Path(file_path)
 
14
  ext = src.suffix.lstrip('.') or 'tif'
15
  img_path = Path(tmpdir) / f"input.{ext}"
16
+
17
  try:
18
+ # Copy raw uploaded bytes
19
  img_bytes = src.read_bytes()
20
  img_path.write_bytes(img_bytes)
21
  except Exception:
22
  # Fallback: save via PIL if direct copy fails
23
  Image.open(src).save(img_path)
24
 
25
+ # Run the full sorghum pipeline
26
  outputs = run_pipeline_on_image(str(img_path), tmpdir, save_artifacts=True)
27
 
28
  def load_pil(path_str):
 
40
  overlay = load_pil(outputs.get('Overlay'))
41
  mask = load_pil(outputs.get('Mask'))
42
  size_img = load_pil(str(Path(tmpdir) / 'results/size.size_analysis.png'))
43
+
44
  # Texture LBP green path
45
  lbp_path = Path(tmpdir) / 'texture_output/lbp_green.png'
46
  texture_img = load_pil(str(lbp_path)) if lbp_path.exists() else None
47
+
48
+ # Vegetation indices
49
  order = ['NDVI', 'GNDVI', 'SAVI']
50
  gallery_items = [load_pil(outputs[k]) for k in order if k in outputs]
51
+
52
  stats_text = outputs.get('StatsText', '')
53
+
54
  return size_img, composite, mask, overlay, texture_img, gallery_items, stats_text
55
 
56
+
57
  with gr.Blocks() as demo:
58
  gr.Markdown("# 🌿 Automated Plant Analysis Demo")
59
+ gr.Markdown("Upload a sorghum plant image (TIFF preferred) to compute and visualize composite, mask, overlay, texture (LBP), vegetation indices, and statistics.")
60
 
61
  with gr.Row():
62
  with gr.Column():
63
+ # Use File input to preserve raw TIFFs
64
+ inp = gr.File(
65
+ type="filepath",
66
+ file_types=[".tif", ".tiff", ".png", ".jpg"],
67
+ label="Upload Image"
68
+ )
69
  run = gr.Button("Run Pipeline", variant="primary")
70
 
71
  with gr.Row():
 
80
  gallery = gr.Gallery(label="Vegetation Indices", columns=3, height="auto")
81
  stats = gr.Textbox(label="Statistics", lines=4)
82
 
83
+ run.click(
84
+ process,
85
+ inputs=inp,
86
+ outputs=[size_img, composite_img, mask_img, overlay_img, texture_img, gallery, stats]
87
+ )
88
 
89
  if __name__ == "__main__":
90
  demo.launch()