Fahimeh Orvati Nia commited on
Commit
4c1c4a7
·
1 Parent(s): 49abd9f
app.py CHANGED
@@ -2,13 +2,39 @@ import gradio as gr
2
  import tempfile
3
  from pathlib import Path
4
  from wrapper import run_pipeline_on_image
 
 
 
5
 
6
  def show_preview(image):
7
- """Show the original uploaded image as-is."""
8
  if image is None:
9
  return None
10
- # Return original image without conversion
11
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def process(image):
14
  if image is None:
 
2
  import tempfile
3
  from pathlib import Path
4
  from wrapper import run_pipeline_on_image
5
+ import numpy as np
6
+ from PIL import Image
7
+ from itertools import product
8
 
9
  def show_preview(image):
10
+ """Show pseudo-RGB composite preview."""
11
  if image is None:
12
  return None
13
+
14
+ try:
15
+ # Process to pseudo-RGB composite (same as pipeline does)
16
+ d = image.size[0] // 2
17
+ boxes = [(j, i, j + d, i + d) for i, j in product(range(0, image.height, d), range(0, image.width, d))]
18
+ stack = np.stack([np.array(image.crop(box), dtype=float) for box in boxes], axis=-1)
19
+ green, red, red_edge, nir = np.split(stack, 4, axis=-1)
20
+
21
+ # Pseudo-RGB: (green, red_edge, red)
22
+ composite = np.concatenate([green, red_edge, red], axis=-1)
23
+
24
+ # Normalize to uint8
25
+ composite = np.nan_to_num(composite, nan=0.0, posinf=0.0, neginf=0.0)
26
+ ptp = np.ptp(composite)
27
+ if ptp > 0:
28
+ normalized = (composite - composite.min()) / (ptp + 1e-6) * 255
29
+ else:
30
+ normalized = np.zeros_like(composite)
31
+ composite_uint8 = np.clip(normalized, 0, 255).astype(np.uint8)
32
+
33
+ # Convert to PIL for display
34
+ return Image.fromarray(composite_uint8)
35
+ except Exception as e:
36
+ # Fallback: return original if processing fails
37
+ return image
38
 
39
  def process(image):
40
  if image is None:
sorghum_pipeline/data/preprocessor.py CHANGED
@@ -15,8 +15,9 @@ class ImagePreprocessor:
15
  def convert_to_uint8(self, arr: np.ndarray) -> np.ndarray:
16
  """Convert to uint8."""
17
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
18
- if arr.ptp() > 0:
19
- normalized = (arr - arr.min()) / (arr.ptp() + 1e-6) * 255
 
20
  else:
21
  normalized = np.zeros_like(arr)
22
  return np.clip(normalized, 0, 255).astype(np.uint8)
 
15
  def convert_to_uint8(self, arr: np.ndarray) -> np.ndarray:
16
  """Convert to uint8."""
17
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
18
+ ptp = np.ptp(arr)
19
+ if ptp > 0:
20
+ normalized = (arr - arr.min()) / (ptp + 1e-6) * 255
21
  else:
22
  normalized = np.zeros_like(arr)
23
  return np.clip(normalized, 0, 255).astype(np.uint8)
sorghum_pipeline/features/texture.py CHANGED
@@ -75,8 +75,9 @@ class TextureExtractor:
75
  def _convert_to_uint8(self, arr: np.ndarray) -> np.ndarray:
76
  """Convert to uint8."""
77
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
78
- if arr.ptp() > 0:
79
- normalized = (arr - arr.min()) / (arr.ptp() + 1e-6) * 255
 
80
  else:
81
  normalized = np.zeros_like(arr)
82
  return np.clip(normalized, 0, 255).astype(np.uint8)
 
75
  def _convert_to_uint8(self, arr: np.ndarray) -> np.ndarray:
76
  """Convert to uint8."""
77
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
78
+ ptp = np.ptp(arr)
79
+ if ptp > 0:
80
+ normalized = (arr - arr.min()) / (ptp + 1e-6) * 255
81
  else:
82
  normalized = np.zeros_like(arr)
83
  return np.clip(normalized, 0, 255).astype(np.uint8)
sorghum_pipeline/output/manager.py CHANGED
@@ -136,8 +136,9 @@ class OutputManager:
136
  def _normalize_to_uint8(self, arr: np.ndarray) -> np.ndarray:
137
  """Normalize to uint8."""
138
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
139
- if arr.ptp() > 0:
140
- normalized = (arr - arr.min()) / (arr.ptp() + 1e-6) * 255
 
141
  else:
142
  normalized = np.zeros_like(arr)
143
  return np.clip(normalized, 0, 255).astype(np.uint8)
 
136
  def _normalize_to_uint8(self, arr: np.ndarray) -> np.ndarray:
137
  """Normalize to uint8."""
138
  arr = np.nan_to_num(arr, nan=0.0, posinf=0.0, neginf=0.0)
139
+ ptp = np.ptp(arr)
140
+ if ptp > 0:
141
+ normalized = (arr - arr.min()) / (ptp + 1e-6) * 255
142
  else:
143
  normalized = np.zeros_like(arr)
144
  return np.clip(normalized, 0, 255).astype(np.uint8)