|
|
from pathlib import Path |
|
|
from typing import Dict |
|
|
import shutil |
|
|
from PIL import Image |
|
|
import glob |
|
|
import tempfile |
|
|
|
|
|
from sorghum_pipeline.pipeline import SorghumPipeline |
|
|
|
|
|
|
|
|
def run_pipeline_on_image(input_image_path: str, work_dir: str, save_artifacts: bool = True) -> Dict[str, str]: |
|
|
""" |
|
|
Run sorghum pipeline on a single image (no instance segmentation). |
|
|
Returns dict[label -> image_path] for gallery display. |
|
|
""" |
|
|
|
|
|
work = Path(work_dir) |
|
|
work.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
input_copy = work / Path(input_image_path).name |
|
|
shutil.copy(input_image_path, input_copy) |
|
|
|
|
|
|
|
|
|
|
|
pipeline = SorghumPipeline( |
|
|
config_path=str(Path("sorghum_pipeline/config.py")), |
|
|
enable_occlusion_handling=False, |
|
|
enable_instance_integration=False |
|
|
) |
|
|
|
|
|
|
|
|
results = pipeline.run( |
|
|
load_all_frames=False, |
|
|
segmentation_only=False, |
|
|
run_instance_segmentation=False, |
|
|
features_frame_only=None |
|
|
) |
|
|
|
|
|
|
|
|
outputs: Dict[str, str] = {} |
|
|
|
|
|
|
|
|
original = work / "original.png" |
|
|
Image.open(input_copy).convert("RGB").save(original) |
|
|
outputs["Original"] = str(original) |
|
|
|
|
|
|
|
|
for f in glob.glob(str(work / "**/*.png"), recursive=True): |
|
|
name = Path(f).stem |
|
|
if name.lower() not in outputs: |
|
|
outputs[name] = f |
|
|
|
|
|
return outputs |
|
|
|