Fahimeh Orvati Nia commited on
Commit
0153931
·
1 Parent(s): 4697ca4

make the process faster

Browse files
Files changed (2) hide show
  1. app.py +2 -47
  2. sorghum_pipeline/pipeline.py +8 -17
app.py CHANGED
@@ -46,53 +46,8 @@ def process(file_path, preset_choice):
46
  # Fallback: save via PIL if direct copy fails
47
  Image.open(src).save(img_path)
48
 
49
- # Show input image immediately (read exactly like pipeline for correctness)
50
- try:
51
- import imghdr
52
- import tifffile # type: ignore
53
- import cv2 # type: ignore
54
-
55
- kind = imghdr.what(str(img_path))
56
- suffix = img_path.suffix.lower()
57
-
58
- arr = None
59
- if kind == "tiff" or suffix in [".tif", ".tiff"]:
60
- try:
61
- arr = tifffile.imread(str(img_path))
62
- except Exception:
63
- arr = cv2.imread(str(img_path), cv2.IMREAD_UNCHANGED)
64
- else:
65
- arr = cv2.imread(str(img_path), cv2.IMREAD_UNCHANGED)
66
-
67
- if arr is None:
68
- raise ValueError("Could not read image for preview")
69
- if arr.ndim > 3:
70
- arr = arr[..., 0]
71
- if arr.ndim == 3 and arr.shape[-1] == 1:
72
- arr = arr[..., 0]
73
-
74
- input_preview = Image.fromarray(arr)
75
- except Exception:
76
- try:
77
- preview_im = Image.open(img_path)
78
- input_preview = preview_im.copy()
79
- preview_im.close()
80
- except Exception:
81
- input_preview = None
82
-
83
- # Initial yield showing input preview
84
- yield (
85
- input_preview, # input image shown immediately
86
- None, # composite
87
- None, # mask
88
- None, # overlay
89
- None, # texture_img
90
- None, # hog_img
91
- None, # lac1_img
92
- [], # gallery_items
93
- None, # size_img
94
- "", # stats
95
- )
96
 
97
  # Helper to load PIL images
98
  def load_pil(path_str):
 
46
  # Fallback: save via PIL if direct copy fails
47
  Image.open(src).save(img_path)
48
 
49
+ # Don't show immediate preview - wait for pipeline's InputImage for correctness
50
+ input_preview = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  # Helper to load PIL images
53
  def load_pil(path_str):
sorghum_pipeline/pipeline.py CHANGED
@@ -155,28 +155,19 @@ class SorghumPipeline:
155
  # Create output directories early
156
  self.output_manager.create_output_directories()
157
 
158
- # Stage 1: Create composite
159
- logger.info("Stage 1: Creating composite...")
160
  plants = self.preprocessor.create_composites(plants)
161
- if progress_callback:
162
- progress_callback("composite", plants)
163
- # Save composite immediately for display
164
- for key, pdata in plants.items():
165
- self.output_manager.save_plant_results(key, pdata)
166
- yield {"plants": plants, "stage": "composite"}
167
-
168
- # Stage 2: Segmentation
169
- logger.info("Stage 2: Segmentation...")
170
  plants = self._segment(plants)
171
  if progress_callback:
172
  progress_callback("segmentation", plants)
173
- # Save mask and overlay
174
  for key, pdata in plants.items():
175
  self.output_manager.save_plant_results(key, pdata)
176
  yield {"plants": plants, "stage": "segmentation"}
177
 
178
- # Stage 3: Extract features (texture, vegetation, morphology)
179
- logger.info("Stage 3: Extracting features...")
180
  plants = self._extract_features(plants)
181
  if progress_callback:
182
  progress_callback("features", plants)
@@ -215,10 +206,10 @@ class SorghumPipeline:
215
  if green_band.ndim == 3 and green_band.shape[-1] == 1:
216
  green_band = green_band[..., 0]
217
 
218
- # Downsample to 256x256 max for faster texture computation
219
  h, w = green_band.shape[:2]
220
- if h > 256 or w > 256:
221
- scale = 256 / max(h, w)
222
  green_band = cv2.resize(green_band, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
223
  if mask is not None:
224
  mask_resized = cv2.resize(mask, (green_band.shape[1], green_band.shape[0]), interpolation=cv2.INTER_NEAREST)
 
155
  # Create output directories early
156
  self.output_manager.create_output_directories()
157
 
158
+ # Stage 1: Create composite + Segmentation (combined for speed)
159
+ logger.info("Stage 1: Creating composite and segmenting...")
160
  plants = self.preprocessor.create_composites(plants)
 
 
 
 
 
 
 
 
 
161
  plants = self._segment(plants)
162
  if progress_callback:
163
  progress_callback("segmentation", plants)
164
+ # Save composite, mask, overlay, and input image
165
  for key, pdata in plants.items():
166
  self.output_manager.save_plant_results(key, pdata)
167
  yield {"plants": plants, "stage": "segmentation"}
168
 
169
+ # Stage 2: Extract features (texture, vegetation, morphology)
170
+ logger.info("Stage 2: Extracting features...")
171
  plants = self._extract_features(plants)
172
  if progress_callback:
173
  progress_callback("features", plants)
 
206
  if green_band.ndim == 3 and green_band.shape[-1] == 1:
207
  green_band = green_band[..., 0]
208
 
209
+ # Downsample to 128x128 max for faster texture computation (2x speedup)
210
  h, w = green_band.shape[:2]
211
+ if h > 128 or w > 128:
212
+ scale = 128 / max(h, w)
213
  green_band = cv2.resize(green_band, None, fx=scale, fy=scale, interpolation=cv2.INTER_LINEAR)
214
  if mask is not None:
215
  mask_resized = cv2.resize(mask, (green_band.shape[1], green_band.shape[0]), interpolation=cv2.INTER_NEAREST)