Spaces:
Running
Running
Update src/ai_processor.py
Browse files- src/ai_processor.py +165 -289
src/ai_processor.py
CHANGED
|
@@ -3,12 +3,14 @@
|
|
| 3 |
# Turn on deep logging: export LOGLEVEL=DEBUG SMARTHEAL_DEBUG=1
|
| 4 |
|
| 5 |
import os
|
|
|
|
| 6 |
import logging
|
| 7 |
from datetime import datetime
|
| 8 |
from typing import Optional, Dict, List, Tuple
|
| 9 |
|
| 10 |
-
# ---- Environment defaults
|
| 11 |
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
|
|
|
| 12 |
LOGLEVEL = os.getenv("LOGLEVEL", "INFO").upper()
|
| 13 |
SMARTHEAL_DEBUG = os.getenv("SMARTHEAL_DEBUG", "0") == "1"
|
| 14 |
|
|
@@ -26,20 +28,22 @@ logging.basicConfig(
|
|
| 26 |
def _log_kv(prefix: str, kv: Dict):
|
| 27 |
logging.debug(prefix + " | " + " | ".join(f"{k}={v}" for k, v in kv.items()))
|
| 28 |
|
| 29 |
-
# --- Spaces GPU
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
@_SPACES_GPU(enable_queue=True)
|
| 33 |
-
def smartheal_gpu_stub(ping: int = 0) -> str:
|
| 34 |
-
return "ready"
|
| 35 |
-
|
| 36 |
-
# ---- Paths / constants ----
|
| 37 |
UPLOADS_DIR = "uploads"
|
| 38 |
os.makedirs(UPLOADS_DIR, exist_ok=True)
|
| 39 |
|
| 40 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 41 |
YOLO_MODEL_PATH = "src/best.pt"
|
| 42 |
-
SEG_MODEL_PATH = "src/segmentation_model.h5"
|
| 43 |
GUIDELINE_PDFS = ["src/eHealth in Wound Care.pdf", "src/IWGDF Guideline.pdf", "src/evaluation.pdf"]
|
| 44 |
DATASET_ID = "SmartHeal/wound-image-uploads"
|
| 45 |
DEFAULT_PX_PER_CM = 38.0
|
|
@@ -53,35 +57,17 @@ SEG_THRESH = float(os.getenv("SEG_THRESH", "0.5"))
|
|
| 53 |
models_cache: Dict[str, object] = {}
|
| 54 |
knowledge_base_cache: Dict[str, object] = {}
|
| 55 |
|
| 56 |
-
# ----------
|
| 57 |
-
from contextlib import contextmanager
|
| 58 |
-
|
| 59 |
-
@contextmanager
|
| 60 |
-
def _no_cuda_env():
|
| 61 |
-
"""
|
| 62 |
-
Mask GPUs so any library imported/constructed in the main process
|
| 63 |
-
cannot see CUDA (required for Spaces Stateless GPU).
|
| 64 |
-
"""
|
| 65 |
-
prev = os.environ.get("CUDA_VISIBLE_DEVICES")
|
| 66 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
| 67 |
-
try:
|
| 68 |
-
yield
|
| 69 |
-
finally:
|
| 70 |
-
if prev is None:
|
| 71 |
-
os.environ.pop("CUDA_VISIBLE_DEVICES", None)
|
| 72 |
-
else:
|
| 73 |
-
os.environ["CUDA_VISIBLE_DEVICES"] = prev
|
| 74 |
-
|
| 75 |
-
# ---------- Lazy imports (wrapped where needed) ----------
|
| 76 |
def _import_ultralytics():
|
| 77 |
-
|
| 78 |
-
with _no_cuda_env():
|
| 79 |
-
from ultralytics import YOLO
|
| 80 |
return YOLO
|
| 81 |
|
| 82 |
def _import_tf_loader():
|
| 83 |
import tensorflow as tf
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
from tensorflow.keras.models import load_model
|
| 86 |
return load_model
|
| 87 |
|
|
@@ -105,116 +91,57 @@ def _import_hf_hub():
|
|
| 105 |
from huggingface_hub import HfApi, HfFolder
|
| 106 |
return HfApi, HfFolder
|
| 107 |
|
| 108 |
-
# ----------
|
| 109 |
-
|
| 110 |
-
You are SmartHeal Clinical Assistant, a wound-care decision-support system.
|
| 111 |
-
You analyze wound photographs and brief patient context to produce careful,
|
| 112 |
-
specific, guideline-informed recommendations WITHOUT diagnosing. You always:
|
| 113 |
-
- Use the measurements calculated by the vision pipeline as ground truth.
|
| 114 |
-
- Prefer concise, actionable steps tailored to exudate level, infection risk, and pain.
|
| 115 |
-
- Flag uncertainties and red flags that need escalation to a clinician.
|
| 116 |
-
- Avoid contraindicated advice; do not infer unseen comorbidities.
|
| 117 |
-
- Keep under 300 words and use the requested headings exactly.
|
| 118 |
-
- Tone: professional, clear, and conservative; no definitive medical claims.
|
| 119 |
-
- Safety: remind the user to seek clinician review for changes or red flags.
|
| 120 |
-
"""
|
| 121 |
-
|
| 122 |
-
SMARTHEAL_USER_PREFIX = """\
|
| 123 |
-
Patient: {patient_info}
|
| 124 |
-
Visual findings: type={wound_type}, size={length_cm}x{breadth_cm} cm, area={area_cm2} cm^2,
|
| 125 |
-
detection_conf={det_conf:.2f}, calibration={px_per_cm} px/cm.
|
| 126 |
-
Guideline context (snippets you can draw principles from; do not quote at length):
|
| 127 |
-
{guideline_context}
|
| 128 |
-
Write a structured answer with these headings exactly:
|
| 129 |
-
1. Clinical Summary (max 4 bullet points)
|
| 130 |
-
2. Likely Stage/Type (if uncertain, say 'uncertain')
|
| 131 |
-
3. Treatment Plan (specific dressing choices and frequency based on exudate/infection risk)
|
| 132 |
-
4. Red Flags (what to escalate and when)
|
| 133 |
-
5. Follow-up Cadence (days)
|
| 134 |
-
6. Notes (assumptions/uncertainties)
|
| 135 |
-
Keep to 220–300 words. Do NOT provide diagnosis. Avoid contraindicated advice.
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
-
# ---------- VLM (MedGemma replaced with Qwen2-VL) ----------
|
| 139 |
-
@_SPACES_GPU(enable_queue=True)
|
| 140 |
-
def _vlm_infer_gpu(messages, model_id: str, max_new_tokens: int, token: Optional[str]):
|
| 141 |
-
"""
|
| 142 |
-
Runs entirely inside a Spaces GPU worker. It's the ONLY place we allow CUDA init.
|
| 143 |
-
"""
|
| 144 |
-
from transformers import pipeline
|
| 145 |
-
import torch # Ensure torch is imported here
|
| 146 |
-
pipe = pipeline(
|
| 147 |
-
task="image-text-to-text",
|
| 148 |
-
model=model_id,
|
| 149 |
-
torch_dtype=torch.bfloat16, # Use torch_dtype from the working example
|
| 150 |
-
device_map="auto", # CUDA init happens here, safely in GPU worker
|
| 151 |
-
token=token,
|
| 152 |
-
trust_remote_code=True,
|
| 153 |
-
model_kwargs={"low_cpu_mem_usage": True},
|
| 154 |
-
)
|
| 155 |
-
out = pipe(text=messages, max_new_tokens=max_new_tokens, do_sample=False, temperature=0.2)
|
| 156 |
-
try:
|
| 157 |
-
txt = out[0]["generated_text"][-1].get("content", "")
|
| 158 |
-
except Exception:
|
| 159 |
-
txt = out[0].get("generated_text", "")
|
| 160 |
-
return (txt or "").strip() or "⚠️ Empty response"
|
| 161 |
-
|
| 162 |
-
def generate_medgemma_report( # kept name so callers don't change
|
| 163 |
patient_info: str,
|
| 164 |
visual_results: Dict,
|
| 165 |
guideline_context: str,
|
| 166 |
image_pil: Image.Image,
|
| 167 |
max_new_tokens: Optional[int] = None,
|
| 168 |
) -> str:
|
| 169 |
-
"""
|
| 170 |
-
MedGemma replacement using Qwen/Qwen2-VL-2B-Instruct via image-text-to-text.
|
| 171 |
-
Loads & runs ONLY inside a GPU worker to satisfy Stateless GPU constraints.
|
| 172 |
-
"""
|
| 173 |
-
if os.getenv("SMARTHEAL_ENABLE_VLM", "1") != "1":
|
| 174 |
return "⚠️ VLM disabled"
|
| 175 |
-
|
| 176 |
-
model_id = os.getenv("SMARTHEAL_VLM_MODEL", "Qwen/Qwen2-VL-2B-Instruct")
|
| 177 |
-
max_new_tokens = max_new_tokens or int(os.getenv("SMARTHEAL_VLM_MAX_TOKENS", "600"))
|
| 178 |
-
|
| 179 |
-
uprompt = SMARTHEAL_USER_PREFIX.format(
|
| 180 |
-
patient_info=patient_info,
|
| 181 |
-
wound_type=visual_results.get("wound_type", "Unknown"),
|
| 182 |
-
length_cm=visual_results.get("length_cm", 0),
|
| 183 |
-
breadth_cm=visual_results.get("breadth_cm", 0),
|
| 184 |
-
area_cm2=visual_results.get("surface_area_cm2", 0),
|
| 185 |
-
det_conf=float(visual_results.get("detection_confidence", 0.0)),
|
| 186 |
-
px_per_cm=visual_results.get("px_per_cm", "?"),
|
| 187 |
-
guideline_context=(guideline_context or "")[:900],
|
| 188 |
-
)
|
| 189 |
-
|
| 190 |
-
messages = [
|
| 191 |
-
{"role": "system", "content": [{"type": "text", "text": SMARTHEAL_SYSTEM_PROMPT}]},
|
| 192 |
-
{"role": "user", "content": [
|
| 193 |
-
{"type": "image", "image": image_pil},
|
| 194 |
-
{"type": "text", "text": uprompt},
|
| 195 |
-
]},
|
| 196 |
-
]
|
| 197 |
-
|
| 198 |
try:
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
except Exception as e:
|
| 202 |
-
logging.error(f"
|
| 203 |
return "⚠️ VLM error"
|
| 204 |
|
| 205 |
# ---------- Initialize CPU models ----------
|
| 206 |
def load_yolo_model():
|
| 207 |
YOLO = _import_ultralytics()
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
model = YOLO(YOLO_MODEL_PATH)
|
| 211 |
-
return model
|
| 212 |
-
|
| 213 |
def load_segmentation_model():
|
| 214 |
-
import tensorflow as tf
|
| 215 |
load_model = _import_tf_loader()
|
| 216 |
-
return load_model(SEG_MODEL_PATH, compile=False
|
| 217 |
-
|
| 218 |
|
| 219 |
def load_classification_pipeline():
|
| 220 |
pipe = _import_hf_cls()
|
|
@@ -236,7 +163,7 @@ def initialize_cpu_models() -> None:
|
|
| 236 |
if "det" not in models_cache:
|
| 237 |
try:
|
| 238 |
models_cache["det"] = load_yolo_model()
|
| 239 |
-
logging.info("✅ YOLO loaded (CPU
|
| 240 |
except Exception as e:
|
| 241 |
logging.error(f"YOLO load failed: {e}")
|
| 242 |
|
|
@@ -271,7 +198,6 @@ def initialize_cpu_models() -> None:
|
|
| 271 |
models_cache["embedding_model"] = None
|
| 272 |
logging.warning(f"Embeddings unavailable: {e}")
|
| 273 |
|
| 274 |
-
|
| 275 |
def setup_knowledge_base() -> None:
|
| 276 |
if "vector_store" in knowledge_base_cache:
|
| 277 |
return
|
|
@@ -359,6 +285,7 @@ def estimate_px_per_cm_from_exif(pil_img: Image.Image, default_px_per_cm: float
|
|
| 359 |
|
| 360 |
# ---------- Segmentation helpers ----------
|
| 361 |
def _imagenet_norm(arr: np.ndarray) -> np.ndarray:
|
|
|
|
| 362 |
mean = np.array([123.675, 116.28, 103.53], dtype=np.float32)
|
| 363 |
std = np.array([58.395, 57.12, 57.375], dtype=np.float32)
|
| 364 |
return (arr.astype(np.float32) - mean) / std
|
|
@@ -382,166 +309,112 @@ def _to_prob(pred: np.ndarray) -> np.ndarray:
|
|
| 382 |
p = 1.0 / (1.0 + np.exp(-p))
|
| 383 |
return p.astype(np.float32)
|
| 384 |
|
| 385 |
-
# ----
|
| 386 |
-
def _adaptive_prob_threshold(p: np.ndarray) -> float:
|
| 387 |
-
"""
|
| 388 |
-
Choose a threshold that avoids tiny blobs while not swallowing skin.
|
| 389 |
-
Try Otsu and the 90th percentile, clamp to [0.25, 0.65], pick by area heuristic.
|
| 390 |
-
"""
|
| 391 |
-
p01 = np.clip(p.astype(np.float32), 0, 1)
|
| 392 |
-
p255 = (p01 * 255).astype(np.uint8)
|
| 393 |
-
|
| 394 |
-
ret_otsu, _ = cv2.threshold(p255, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
| 395 |
-
thr_otsu = float(np.clip(ret_otsu / 255.0, 0.25, 0.65))
|
| 396 |
-
thr_pctl = float(np.clip(np.percentile(p01, 90), 0.25, 0.65))
|
| 397 |
-
|
| 398 |
-
def area_frac(thr: float) -> float:
|
| 399 |
-
return float((p01 >= thr).sum()) / float(p01.size)
|
| 400 |
-
|
| 401 |
-
af_otsu = area_frac(thr_otsu)
|
| 402 |
-
af_pctl = area_frac(thr_pctl)
|
| 403 |
-
|
| 404 |
-
def score(af: float) -> float:
|
| 405 |
-
target_low, target_high = 0.03, 0.10
|
| 406 |
-
if af < target_low: return abs(af - target_low) * 3.0
|
| 407 |
-
if af > target_high: return abs(af - target_high) * 1.5
|
| 408 |
-
return 0.0
|
| 409 |
-
|
| 410 |
-
return thr_otsu if score(af_otsu) <= score(af_pctl) else thr_pctl
|
| 411 |
-
|
| 412 |
-
def _grabcut_refine(bgr: np.ndarray, seed01: np.ndarray, iters: int = 3) -> np.ndarray:
|
| 413 |
-
"""Grow from a confident core into low-contrast margins."""
|
| 414 |
-
h, w = bgr.shape[:2]
|
| 415 |
-
gc = np.full((h, w), cv2.GC_PR_BGD, np.uint8)
|
| 416 |
-
k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
| 417 |
-
seed_dil = cv2.dilate(seed01, k, iterations=1)
|
| 418 |
-
gc[seed01.astype(bool)] = cv2.GC_PR_FGD
|
| 419 |
-
gc[seed_dil.astype(bool)] = cv2.GC_FGD
|
| 420 |
-
gc[0, :], gc[-1, :], gc[:, 0], gc[:, 1] = cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD
|
| 421 |
-
bgdModel = np.zeros((1, 65), np.float64)
|
| 422 |
-
fgdModel = np.zeros((1, 65), np.float64)
|
| 423 |
-
cv2.grabCut(bgr, gc, None, bgdModel, fgdModel, iters, cv2.GC_INIT_WITH_MASK)
|
| 424 |
-
return np.where((gc == cv2.GC_FGD) | (gc == cv2.GC_PR_FGD), 1, 0).astype(np.uint8)
|
| 425 |
-
|
| 426 |
def _fill_holes(mask01: np.ndarray) -> np.ndarray:
|
|
|
|
| 427 |
h, w = mask01.shape[:2]
|
| 428 |
ff = np.zeros((h + 2, w + 2), np.uint8)
|
| 429 |
m = (mask01 * 255).astype(np.uint8).copy()
|
| 430 |
cv2.floodFill(m, ff, (0, 0), 255)
|
| 431 |
m_inv = cv2.bitwise_not(m)
|
|
|
|
| 432 |
out = ((mask01 * 255) | m_inv) // 255
|
| 433 |
return out.astype(np.uint8)
|
| 434 |
|
| 435 |
-
|
| 436 |
-
"""Open → Close → Fill holes → Largest component (no dilation)."""
|
| 437 |
-
mask01 = (mask01 > 0).astype(np.uint8)
|
| 438 |
-
k3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 439 |
-
k5 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
|
| 440 |
-
mask01 = cv2.morphologyEx(mask01, cv2.MORPH_OPEN, k3, iterations=1)
|
| 441 |
-
mask01 = cv2.morphologyEx(mask01, cv2.MORPH_CLOSE, k5, iterations=1)
|
| 442 |
-
mask01 = _fill_holes(mask01)
|
| 443 |
-
# Keep largest component only
|
| 444 |
-
num, labels, stats, _ = cv2.connectedComponentsWithStats(mask01, 8)
|
| 445 |
-
if num > 1:
|
| 446 |
-
areas = stats[1:, cv2.CC_STAT_AREA]
|
| 447 |
-
if areas.size:
|
| 448 |
-
largest_idx = 1 + int(np.argmax(areas))
|
| 449 |
-
mask01 = (labels == largest_idx).astype(np.uint8)
|
| 450 |
-
return (mask01 > 0).astype(np.uint8)
|
| 451 |
-
|
| 452 |
-
# Global last debug dict (per-process)
|
| 453 |
_last_seg_debug: Dict[str, object] = {}
|
| 454 |
|
| 455 |
def segment_wound(image_bgr: np.ndarray, ts: str, out_dir: str) -> Tuple[np.ndarray, Dict[str, object]]:
|
| 456 |
"""
|
| 457 |
-
TF
|
| 458 |
-
Fallback: KMeans-Lab.
|
| 459 |
Returns (mask_uint8_0_255, debug_dict)
|
| 460 |
"""
|
| 461 |
-
|
| 462 |
-
|
| 463 |
|
| 464 |
seg_model = models_cache.get("seg", None)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
-
# --- Model path ---
|
| 467 |
if seg_model is not None:
|
| 468 |
try:
|
| 469 |
ishape = getattr(seg_model, "input_shape", None)
|
| 470 |
if not ishape or len(ishape) < 4:
|
| 471 |
raise ValueError(f"Bad seg input_shape: {ishape}")
|
| 472 |
th, tw = int(ishape[1]), int(ishape[2])
|
| 473 |
-
|
| 474 |
x = _preprocess_for_seg(image_bgr, (th, tw))
|
| 475 |
-
|
| 476 |
if SMARTHEAL_DEBUG:
|
| 477 |
-
|
| 478 |
-
cv2.imwrite(
|
| 479 |
|
|
|
|
| 480 |
pred = seg_model.predict(x, verbose=0)
|
| 481 |
-
if isinstance(pred, (list, tuple)):
|
| 482 |
-
|
| 483 |
-
p =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
|
| 485 |
-
heatmap_path = None
|
| 486 |
if SMARTHEAL_DEBUG:
|
| 487 |
hm = (np.clip(p, 0, 1) * 255).astype(np.uint8)
|
| 488 |
heat = cv2.applyColorMap(hm, cv2.COLORMAP_JET)
|
| 489 |
heatmap_path = os.path.join(out_dir, f"seg_pred_heatmap_{ts}.png")
|
| 490 |
cv2.imwrite(heatmap_path, heat)
|
| 491 |
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
logging.info(f"SegModel USED | thr={float(thr):.2f} core_frac={core_frac:.4f} final_frac={pos_frac:.4f}")
|
| 510 |
-
|
| 511 |
-
debug.update({
|
| 512 |
-
"used": "tf_model",
|
| 513 |
-
"reason": "ok",
|
| 514 |
-
"positive_fraction": pos_frac,
|
| 515 |
-
"thr": float(thr),
|
| 516 |
"heatmap_path": heatmap_path,
|
| 517 |
-
"roi_seen_by_model":
|
| 518 |
-
}
|
| 519 |
-
return (
|
| 520 |
|
| 521 |
except Exception as e:
|
| 522 |
-
|
| 523 |
-
|
| 524 |
|
| 525 |
-
# --- Fallback: KMeans
|
| 526 |
Z = image_bgr.reshape((-1, 3)).astype(np.float32)
|
| 527 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
| 528 |
_, labels, centers = cv2.kmeans(Z, 2, None, criteria, 5, cv2.KMEANS_PP_CENTERS)
|
| 529 |
centers_u8 = centers.astype(np.uint8).reshape(1, 2, 3)
|
| 530 |
centers_lab = cv2.cvtColor(centers_u8, cv2.COLOR_BGR2LAB)[0]
|
| 531 |
-
wound_idx = int(np.argmax(centers_lab[:, 1])) # maximize a* (
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
"
|
| 540 |
-
"
|
| 541 |
-
"
|
| 542 |
-
"
|
| 543 |
-
|
| 544 |
-
|
|
|
|
|
|
|
| 545 |
|
| 546 |
# ---------- Measurement + overlay helpers ----------
|
| 547 |
def largest_component_mask(binary01: np.ndarray, min_area_px: int = 50) -> np.ndarray:
|
|
@@ -554,6 +427,17 @@ def largest_component_mask(binary01: np.ndarray, min_area_px: int = 50) -> np.nd
|
|
| 554 |
largest_idx = 1 + int(np.argmax(areas))
|
| 555 |
return (labels == largest_idx).astype(np.uint8)
|
| 556 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 557 |
def measure_min_area_rect(mask01: np.ndarray, px_per_cm: float) -> Tuple[float, float, Tuple]:
|
| 558 |
contours, _ = cv2.findContours(mask01.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 559 |
if not contours:
|
|
@@ -567,23 +451,9 @@ def measure_min_area_rect(mask01: np.ndarray, px_per_cm: float) -> Tuple[float,
|
|
| 567 |
box = cv2.boxPoints(rect).astype(int)
|
| 568 |
return length_cm, breadth_cm, (box, rect[0])
|
| 569 |
|
| 570 |
-
def
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
contours, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 574 |
-
if not contours:
|
| 575 |
-
return 0.0, None
|
| 576 |
-
cnt = max(contours, key=cv2.contourArea)
|
| 577 |
-
poly_area_px2 = float(cv2.contourArea(cnt))
|
| 578 |
-
area_cm2 = round(poly_area_px2 / (max(px_per_cm, 1e-6) ** 2), 2)
|
| 579 |
-
return area_cm2, cnt
|
| 580 |
-
|
| 581 |
-
def clamp_area_with_minrect(cnt: np.ndarray, px_per_cm: float, area_cm2_poly: float) -> float:
|
| 582 |
-
rect = cv2.minAreaRect(cnt)
|
| 583 |
-
(w_px, h_px) = rect[1]
|
| 584 |
-
rect_area_px2 = float(max(w_px, 0.0) * max(h_px, 0.0))
|
| 585 |
-
rect_area_cm2 = rect_area_px2 / (max(px_per_cm, 1e-6) ** 2)
|
| 586 |
-
return round(min(area_cm2_poly, rect_area_cm2 * 1.05), 2)
|
| 587 |
|
| 588 |
def draw_measurement_overlay(
|
| 589 |
base_bgr: np.ndarray,
|
|
@@ -594,13 +464,16 @@ def draw_measurement_overlay(
|
|
| 594 |
thickness: int = 2
|
| 595 |
) -> np.ndarray:
|
| 596 |
"""
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
|
|
|
|
|
|
|
|
|
| 600 |
"""
|
| 601 |
overlay = base_bgr.copy()
|
| 602 |
|
| 603 |
-
#
|
| 604 |
mask255 = (mask01 * 255).astype(np.uint8)
|
| 605 |
mask3 = cv2.merge([mask255, mask255, mask255])
|
| 606 |
red = np.zeros_like(overlay); red[:] = (0, 0, 255)
|
|
@@ -608,7 +481,7 @@ def draw_measurement_overlay(
|
|
| 608 |
tinted = cv2.addWeighted(overlay, 1 - alpha, red, alpha, 0)
|
| 609 |
overlay = np.where(mask3 > 0, tinted, overlay)
|
| 610 |
|
| 611 |
-
#
|
| 612 |
cnts, _ = cv2.findContours(mask255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 613 |
if cnts:
|
| 614 |
cv2.drawContours(overlay, cnts, -1, (255, 255, 255), 2)
|
|
@@ -617,11 +490,19 @@ def draw_measurement_overlay(
|
|
| 617 |
cv2.polylines(overlay, [rect_box], True, (255, 255, 255), thickness)
|
| 618 |
pts = rect_box.reshape(-1, 2)
|
| 619 |
|
| 620 |
-
def midpoint(a, b):
|
|
|
|
|
|
|
|
|
|
| 621 |
e = [np.linalg.norm(pts[i] - pts[(i + 1) % 4]) for i in range(4)]
|
| 622 |
long_edge_idx = int(np.argmax(e))
|
|
|
|
|
|
|
|
|
|
| 623 |
mids = [midpoint(pts[i], pts[(i + 1) % 4]) for i in range(4)]
|
|
|
|
| 624 |
long_pair = (long_edge_idx, (long_edge_idx + 2) % 4)
|
|
|
|
| 625 |
short_pair = ((long_edge_idx + 1) % 4, (long_edge_idx + 3) % 4)
|
| 626 |
|
| 627 |
def draw_double_arrow(img, p1, p2):
|
|
@@ -635,6 +516,7 @@ def draw_measurement_overlay(
|
|
| 635 |
cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 4, cv2.LINE_AA)
|
| 636 |
cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
|
| 637 |
|
|
|
|
| 638 |
draw_double_arrow(overlay, mids[long_pair[0]], mids[long_pair[1]])
|
| 639 |
draw_double_arrow(overlay, mids[short_pair[0]], mids[short_pair[1]])
|
| 640 |
put_label(f"Length: {length_cm:.2f} cm", mids[long_pair[0]])
|
|
@@ -663,18 +545,12 @@ class AIProcessor:
|
|
| 663 |
"""
|
| 664 |
try:
|
| 665 |
px_per_cm, exif_meta = estimate_px_per_cm_from_exif(image_pil, DEFAULT_PX_PER_CM)
|
| 666 |
-
# Guardrails for calibration to avoid huge area blow-ups
|
| 667 |
-
px_per_cm = float(np.clip(px_per_cm, 20.0, 350.0))
|
| 668 |
-
if (exif_meta or {}).get("used") != "exif":
|
| 669 |
-
logging.warning(f"Calibration fallback used: px_per_cm={px_per_cm:.2f} (default). Prefer ruler/Aruco for accuracy.")
|
| 670 |
-
|
| 671 |
image_cv = cv2.cvtColor(np.array(image_pil.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 672 |
|
| 673 |
# --- Detection ---
|
| 674 |
det_model = self.models_cache.get("det")
|
| 675 |
if det_model is None:
|
| 676 |
raise RuntimeError("YOLO model not loaded")
|
| 677 |
-
# Force CPU inference and avoid CUDA touch
|
| 678 |
results = det_model.predict(image_cv, verbose=False, device="cpu")
|
| 679 |
if (not results) or (not getattr(results[0], "boxes", None)) or (len(results[0].boxes) == 0):
|
| 680 |
try:
|
|
@@ -702,23 +578,20 @@ class AIProcessor:
|
|
| 702 |
mask_u8_255, seg_debug = segment_wound(roi, ts, out_dir)
|
| 703 |
mask01 = (mask_u8_255 > 127).astype(np.uint8)
|
| 704 |
|
|
|
|
| 705 |
if mask01.any():
|
| 706 |
mask01 = _clean_mask(mask01)
|
| 707 |
logging.debug(f"Mask postproc: px_after={int(mask01.sum())}")
|
| 708 |
|
| 709 |
-
# --- Measurement
|
| 710 |
if mask01.any():
|
| 711 |
length_cm, breadth_cm, (box_pts, _) = measure_min_area_rect(mask01, px_per_cm)
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
surface_area_cm2 = clamp_area_with_minrect(largest_cnt, px_per_cm, area_poly_cm2)
|
| 715 |
-
else:
|
| 716 |
-
surface_area_cm2 = area_poly_cm2
|
| 717 |
-
|
| 718 |
anno_roi = draw_measurement_overlay(roi, mask01, box_pts, length_cm, breadth_cm)
|
| 719 |
segmentation_empty = False
|
| 720 |
else:
|
| 721 |
-
#
|
| 722 |
h_px = max(0, y2 - y1); w_px = max(0, x2 - x1)
|
| 723 |
length_cm = round(max(h_px, w_px) / px_per_cm, 2)
|
| 724 |
breadth_cm = round(min(h_px, w_px) / px_per_cm, 2)
|
|
@@ -742,7 +615,7 @@ class AIProcessor:
|
|
| 742 |
roi_mask_path = os.path.join(out_dir, f"roi_mask_{ts}.png")
|
| 743 |
cv2.imwrite(roi_mask_path, (mask01 * 255).astype(np.uint8))
|
| 744 |
|
| 745 |
-
# ROI overlay (mask
|
| 746 |
mask255 = (mask01 * 255).astype(np.uint8)
|
| 747 |
mask3 = cv2.merge([mask255, mask255, mask255])
|
| 748 |
red = np.zeros_like(roi); red[:] = (0, 0, 255)
|
|
@@ -785,7 +658,7 @@ class AIProcessor:
|
|
| 785 |
"seg_used": seg_debug.get("used"),
|
| 786 |
"seg_reason": seg_debug.get("reason"),
|
| 787 |
"positive_fraction": round(float(seg_debug.get("positive_fraction", 0.0)), 6),
|
| 788 |
-
"threshold": seg_debug.get("
|
| 789 |
"segmentation_empty": segmentation_empty,
|
| 790 |
"exif_px_per_cm": round(px_per_cm, 3),
|
| 791 |
}
|
|
@@ -801,7 +674,7 @@ class AIProcessor:
|
|
| 801 |
"detection_confidence": float(results[0].boxes.conf[0].cpu().item())
|
| 802 |
if getattr(results[0].boxes, "conf", None) is not None else 0.0,
|
| 803 |
"detection_image_path": detection_path,
|
| 804 |
-
"segmentation_image_path":
|
| 805 |
"segmentation_annotated_path": annotated_seg_path,
|
| 806 |
"segmentation_roi_path": segmentation_roi_path,
|
| 807 |
"roi_mask_path": roi_mask_path,
|
|
@@ -819,9 +692,12 @@ class AIProcessor:
|
|
| 819 |
vs = self.knowledge_base_cache.get("vector_store")
|
| 820 |
if not vs:
|
| 821 |
return "Knowledge base is not available."
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
|
|
|
|
|
|
|
|
|
| 825 |
lines: List[str] = []
|
| 826 |
for d in docs:
|
| 827 |
src = (d.metadata or {}).get("source", "N/A")
|
|
@@ -875,7 +751,7 @@ Automated analysis provides quantitative measurements; verify via clinical exami
|
|
| 875 |
)
|
| 876 |
if report and report.strip() and not report.startswith(("⚠️", "❌")):
|
| 877 |
return report
|
| 878 |
-
logging.warning("
|
| 879 |
return self._generate_fallback_report(patient_info, visual_results, guideline_context)
|
| 880 |
except Exception as e:
|
| 881 |
logging.error(f"Report generation failed: {e}")
|
|
|
|
| 3 |
# Turn on deep logging: export LOGLEVEL=DEBUG SMARTHEAL_DEBUG=1
|
| 4 |
|
| 5 |
import os
|
| 6 |
+
import time
|
| 7 |
import logging
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import Optional, Dict, List, Tuple
|
| 10 |
|
| 11 |
+
# ---- Environment defaults ----
|
| 12 |
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
| 13 |
+
os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")
|
| 14 |
LOGLEVEL = os.getenv("LOGLEVEL", "INFO").upper()
|
| 15 |
SMARTHEAL_DEBUG = os.getenv("SMARTHEAL_DEBUG", "0") == "1"
|
| 16 |
|
|
|
|
| 28 |
def _log_kv(prefix: str, kv: Dict):
|
| 29 |
logging.debug(prefix + " | " + " | ".join(f"{k}={v}" for k, v in kv.items()))
|
| 30 |
|
| 31 |
+
# --- Optional Spaces GPU stub (harmless) ---
|
| 32 |
+
try:
|
| 33 |
+
import spaces as _spaces
|
| 34 |
+
@_spaces.GPU(enable_queue=False)
|
| 35 |
+
def smartheal_gpu_stub(ping: int = 0) -> str:
|
| 36 |
+
return "ready"
|
| 37 |
+
logging.info("Registered @spaces.GPU stub (enable_queue=False).")
|
| 38 |
+
except Exception:
|
| 39 |
+
pass
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
UPLOADS_DIR = "uploads"
|
| 42 |
os.makedirs(UPLOADS_DIR, exist_ok=True)
|
| 43 |
|
| 44 |
HF_TOKEN = os.getenv("HF_TOKEN", None)
|
| 45 |
YOLO_MODEL_PATH = "src/best.pt"
|
| 46 |
+
SEG_MODEL_PATH = "src/segmentation_model.h5" # optional
|
| 47 |
GUIDELINE_PDFS = ["src/eHealth in Wound Care.pdf", "src/IWGDF Guideline.pdf", "src/evaluation.pdf"]
|
| 48 |
DATASET_ID = "SmartHeal/wound-image-uploads"
|
| 49 |
DEFAULT_PX_PER_CM = 38.0
|
|
|
|
| 57 |
models_cache: Dict[str, object] = {}
|
| 58 |
knowledge_base_cache: Dict[str, object] = {}
|
| 59 |
|
| 60 |
+
# ---------- Lazy imports ----------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def _import_ultralytics():
|
| 62 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
| 63 |
return YOLO
|
| 64 |
|
| 65 |
def _import_tf_loader():
|
| 66 |
import tensorflow as tf
|
| 67 |
+
try:
|
| 68 |
+
tf.config.set_visible_devices([], "GPU") # keep TF on CPU
|
| 69 |
+
except Exception:
|
| 70 |
+
pass
|
| 71 |
from tensorflow.keras.models import load_model
|
| 72 |
return load_model
|
| 73 |
|
|
|
|
| 91 |
from huggingface_hub import HfApi, HfFolder
|
| 92 |
return HfApi, HfFolder
|
| 93 |
|
| 94 |
+
# ---------- VLM (disabled by default) ----------
|
| 95 |
+
def generate_medgemma_report(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
patient_info: str,
|
| 97 |
visual_results: Dict,
|
| 98 |
guideline_context: str,
|
| 99 |
image_pil: Image.Image,
|
| 100 |
max_new_tokens: Optional[int] = None,
|
| 101 |
) -> str:
|
| 102 |
+
if os.getenv("SMARTHEAL_ENABLE_VLM", "0") != "1":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return "⚠️ VLM disabled"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
try:
|
| 105 |
+
from transformers import pipeline
|
| 106 |
+
pipe = pipeline(
|
| 107 |
+
task="image-text-to-text",
|
| 108 |
+
model="google/medgemma-4b-it",
|
| 109 |
+
device_map=None,
|
| 110 |
+
token=HF_TOKEN,
|
| 111 |
+
trust_remote_code=True,
|
| 112 |
+
model_kwargs={"low_cpu_mem_usage": True},
|
| 113 |
+
)
|
| 114 |
+
prompt = (
|
| 115 |
+
"You are a medical AI assistant. Analyze this wound image and patient data.\n\n"
|
| 116 |
+
f"Patient: {patient_info}\n"
|
| 117 |
+
f"Wound: {visual_results.get('wound_type', 'Unknown')} - "
|
| 118 |
+
f"{visual_results.get('length_cm', 0)}×{visual_results.get('breadth_cm', 0)} cm\n\n"
|
| 119 |
+
"Provide a structured report with:\n"
|
| 120 |
+
"1. Clinical Summary\n2. Treatment Recommendations\n3. Risk Assessment\n4. Monitoring Plan\n"
|
| 121 |
+
)
|
| 122 |
+
messages = [{"role": "user", "content": [
|
| 123 |
+
{"type": "image", "image": image_pil},
|
| 124 |
+
{"type": "text", "text": prompt},
|
| 125 |
+
]}]
|
| 126 |
+
out = pipe(text=messages, max_new_tokens=max_new_tokens or 600, do_sample=False, temperature=0.7)
|
| 127 |
+
if out and len(out) > 0:
|
| 128 |
+
try:
|
| 129 |
+
return out[0]["generated_text"][-1].get("content", "").strip() or "⚠️ Empty response"
|
| 130 |
+
except Exception:
|
| 131 |
+
return (out[0].get("generated_text", "") or "").strip() or "⚠️ Empty response"
|
| 132 |
+
return "⚠️ No output generated"
|
| 133 |
except Exception as e:
|
| 134 |
+
logging.error(f"❌ MedGemma generation error: {e}")
|
| 135 |
return "⚠️ VLM error"
|
| 136 |
|
| 137 |
# ---------- Initialize CPU models ----------
|
| 138 |
def load_yolo_model():
|
| 139 |
YOLO = _import_ultralytics()
|
| 140 |
+
return YOLO(YOLO_MODEL_PATH)
|
| 141 |
+
|
|
|
|
|
|
|
|
|
|
| 142 |
def load_segmentation_model():
|
|
|
|
| 143 |
load_model = _import_tf_loader()
|
| 144 |
+
return load_model(SEG_MODEL_PATH, compile=False)
|
|
|
|
| 145 |
|
| 146 |
def load_classification_pipeline():
|
| 147 |
pipe = _import_hf_cls()
|
|
|
|
| 163 |
if "det" not in models_cache:
|
| 164 |
try:
|
| 165 |
models_cache["det"] = load_yolo_model()
|
| 166 |
+
logging.info("✅ YOLO loaded (CPU)")
|
| 167 |
except Exception as e:
|
| 168 |
logging.error(f"YOLO load failed: {e}")
|
| 169 |
|
|
|
|
| 198 |
models_cache["embedding_model"] = None
|
| 199 |
logging.warning(f"Embeddings unavailable: {e}")
|
| 200 |
|
|
|
|
| 201 |
def setup_knowledge_base() -> None:
|
| 202 |
if "vector_store" in knowledge_base_cache:
|
| 203 |
return
|
|
|
|
| 285 |
|
| 286 |
# ---------- Segmentation helpers ----------
|
| 287 |
def _imagenet_norm(arr: np.ndarray) -> np.ndarray:
|
| 288 |
+
# expects RGB 0..255 -> float
|
| 289 |
mean = np.array([123.675, 116.28, 103.53], dtype=np.float32)
|
| 290 |
std = np.array([58.395, 57.12, 57.375], dtype=np.float32)
|
| 291 |
return (arr.astype(np.float32) - mean) / std
|
|
|
|
| 309 |
p = 1.0 / (1.0 + np.exp(-p))
|
| 310 |
return p.astype(np.float32)
|
| 311 |
|
| 312 |
+
# ---- Robust mask post-processing (for "proper" masking) ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
def _fill_holes(mask01: np.ndarray) -> np.ndarray:
|
| 314 |
+
# Flood-fill from border, then invert
|
| 315 |
h, w = mask01.shape[:2]
|
| 316 |
ff = np.zeros((h + 2, w + 2), np.uint8)
|
| 317 |
m = (mask01 * 255).astype(np.uint8).copy()
|
| 318 |
cv2.floodFill(m, ff, (0, 0), 255)
|
| 319 |
m_inv = cv2.bitwise_not(m)
|
| 320 |
+
# Combine original with filled holes
|
| 321 |
out = ((mask01 * 255) | m_inv) // 255
|
| 322 |
return out.astype(np.uint8)
|
| 323 |
|
| 324 |
+
# Global last debug dict (per-process) to attach into results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
_last_seg_debug: Dict[str, object] = {}
|
| 326 |
|
| 327 |
def segment_wound(image_bgr: np.ndarray, ts: str, out_dir: str) -> Tuple[np.ndarray, Dict[str, object]]:
|
| 328 |
"""
|
| 329 |
+
Attempts TF segmentation first; falls back to KMeans if needed.
|
|
|
|
| 330 |
Returns (mask_uint8_0_255, debug_dict)
|
| 331 |
"""
|
| 332 |
+
global _last_seg_debug
|
| 333 |
+
_last_seg_debug = {}
|
| 334 |
|
| 335 |
seg_model = models_cache.get("seg", None)
|
| 336 |
+
used = "fallback_kmeans"
|
| 337 |
+
reason = "no_model"
|
| 338 |
+
heatmap_path = None
|
| 339 |
+
saw_roi_path = None
|
| 340 |
|
|
|
|
| 341 |
if seg_model is not None:
|
| 342 |
try:
|
| 343 |
ishape = getattr(seg_model, "input_shape", None)
|
| 344 |
if not ishape or len(ishape) < 4:
|
| 345 |
raise ValueError(f"Bad seg input_shape: {ishape}")
|
| 346 |
th, tw = int(ishape[1]), int(ishape[2])
|
|
|
|
| 347 |
x = _preprocess_for_seg(image_bgr, (th, tw))
|
| 348 |
+
saw_roi = (cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB) if SEG_EXPECTS_RGB else image_bgr)
|
| 349 |
if SMARTHEAL_DEBUG:
|
| 350 |
+
saw_roi_path = os.path.join(out_dir, f"roi_for_seg_{ts}.png")
|
| 351 |
+
cv2.imwrite(saw_roi_path, (cv2.cvtColor(saw_roi, cv2.COLOR_RGB2BGR) if SEG_EXPECTS_RGB else saw_roi))
|
| 352 |
|
| 353 |
+
# Inference
|
| 354 |
pred = seg_model.predict(x, verbose=0)
|
| 355 |
+
if isinstance(pred, (list, tuple)):
|
| 356 |
+
pred = pred[0]
|
| 357 |
+
p = _to_prob(pred) # HxW
|
| 358 |
+
p = cv2.resize(p, (image_bgr.shape[1], image_bgr.shape[0])) # back to ROI size
|
| 359 |
+
|
| 360 |
+
# Debug stats
|
| 361 |
+
pmin, pmax, pmean = float(p.min()), float(p.max()), float(p.mean())
|
| 362 |
+
_log_kv("SEG_PROB_STATS", {"min": pmin, "max": pmax, "mean": pmean})
|
| 363 |
|
|
|
|
| 364 |
if SMARTHEAL_DEBUG:
|
| 365 |
hm = (np.clip(p, 0, 1) * 255).astype(np.uint8)
|
| 366 |
heat = cv2.applyColorMap(hm, cv2.COLORMAP_JET)
|
| 367 |
heatmap_path = os.path.join(out_dir, f"seg_pred_heatmap_{ts}.png")
|
| 368 |
cv2.imwrite(heatmap_path, heat)
|
| 369 |
|
| 370 |
+
# Threshold
|
| 371 |
+
thr = SEG_THRESH
|
| 372 |
+
mask = (p >= thr).astype(np.uint8) # 0/1
|
| 373 |
+
pos = int(mask.sum())
|
| 374 |
+
frac = pos / float(mask.size)
|
| 375 |
+
logging.info(f"SegModel USED | thr={thr} pos_px={pos} pos_frac={frac:.4f} ex_rgb={SEG_EXPECTS_RGB} norm={SEG_NORM}")
|
| 376 |
+
|
| 377 |
+
used = "tf_model"
|
| 378 |
+
reason = "ok"
|
| 379 |
+
|
| 380 |
+
_last_seg_debug = {
|
| 381 |
+
"used": used,
|
| 382 |
+
"reason": reason,
|
| 383 |
+
"input_shape": ishape,
|
| 384 |
+
"prob_min": pmin, "prob_max": pmax, "prob_mean": pmean,
|
| 385 |
+
"threshold": thr,
|
| 386 |
+
"positive_fraction": frac,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
"heatmap_path": heatmap_path,
|
| 388 |
+
"roi_seen_by_model": saw_roi_path,
|
| 389 |
+
}
|
| 390 |
+
return (mask * 255).astype(np.uint8), _last_seg_debug
|
| 391 |
|
| 392 |
except Exception as e:
|
| 393 |
+
reason = f"model_failed: {e}"
|
| 394 |
+
logging.warning(f"⚠️ Segmentation model prediction failed → fallback. Reason: {e}")
|
| 395 |
|
| 396 |
+
# --- Fallback: KMeans (k=2), pick 'reddest' cluster in Lab a* ---
|
| 397 |
Z = image_bgr.reshape((-1, 3)).astype(np.float32)
|
| 398 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
|
| 399 |
_, labels, centers = cv2.kmeans(Z, 2, None, criteria, 5, cv2.KMEANS_PP_CENTERS)
|
| 400 |
centers_u8 = centers.astype(np.uint8).reshape(1, 2, 3)
|
| 401 |
centers_lab = cv2.cvtColor(centers_u8, cv2.COLOR_BGR2LAB)[0]
|
| 402 |
+
wound_idx = int(np.argmax(centers_lab[:, 1])) # maximize a* (redness)
|
| 403 |
+
mask = (labels.reshape(image_bgr.shape[:2]) == wound_idx).astype(np.uint8)
|
| 404 |
+
|
| 405 |
+
pos = int(mask.sum()); frac = pos / float(mask.size)
|
| 406 |
+
logging.info(f"KMeans USED | pos_px={pos} pos_frac={frac:.4f}")
|
| 407 |
+
|
| 408 |
+
_last_seg_debug = {
|
| 409 |
+
"used": used,
|
| 410 |
+
"reason": reason,
|
| 411 |
+
"kmeans_centers_bgr": centers.tolist(),
|
| 412 |
+
"kmeans_centers_lab": centers_lab.astype(float).tolist(),
|
| 413 |
+
"positive_fraction": frac,
|
| 414 |
+
"heatmap_path": heatmap_path,
|
| 415 |
+
"roi_seen_by_model": saw_roi_path,
|
| 416 |
+
}
|
| 417 |
+
return (mask * 255).astype(np.uint8), _last_seg_debug
|
| 418 |
|
| 419 |
# ---------- Measurement + overlay helpers ----------
|
| 420 |
def largest_component_mask(binary01: np.ndarray, min_area_px: int = 50) -> np.ndarray:
|
|
|
|
| 427 |
largest_idx = 1 + int(np.argmax(areas))
|
| 428 |
return (labels == largest_idx).astype(np.uint8)
|
| 429 |
|
| 430 |
+
def _clean_mask(mask01: np.ndarray) -> np.ndarray:
|
| 431 |
+
"""Open→Close→Fill holes→Largest component."""
|
| 432 |
+
if mask01.dtype != np.uint8:
|
| 433 |
+
mask01 = mask01.astype(np.uint8)
|
| 434 |
+
k = np.ones((3, 3), np.uint8)
|
| 435 |
+
mask01 = cv2.morphologyEx(mask01, cv2.MORPH_OPEN, k, iterations=1)
|
| 436 |
+
mask01 = cv2.morphologyEx(mask01, cv2.MORPH_CLOSE, k, iterations=2)
|
| 437 |
+
mask01 = _fill_holes(mask01)
|
| 438 |
+
mask01 = largest_component_mask(mask01, min_area_px=30)
|
| 439 |
+
return (mask01 > 0).astype(np.uint8)
|
| 440 |
+
|
| 441 |
def measure_min_area_rect(mask01: np.ndarray, px_per_cm: float) -> Tuple[float, float, Tuple]:
|
| 442 |
contours, _ = cv2.findContours(mask01.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 443 |
if not contours:
|
|
|
|
| 451 |
box = cv2.boxPoints(rect).astype(int)
|
| 452 |
return length_cm, breadth_cm, (box, rect[0])
|
| 453 |
|
| 454 |
+
def count_area_cm2(mask01: np.ndarray, px_per_cm: float) -> float:
|
| 455 |
+
px_count = float(mask01.astype(bool).sum())
|
| 456 |
+
return round(px_count / (max(px_per_cm, 1e-6) ** 2), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
|
| 458 |
def draw_measurement_overlay(
|
| 459 |
base_bgr: np.ndarray,
|
|
|
|
| 464 |
thickness: int = 2
|
| 465 |
) -> np.ndarray:
|
| 466 |
"""
|
| 467 |
+
Draws:
|
| 468 |
+
1) Strong red mask overlay with white contour.
|
| 469 |
+
2) Min-area rectangle.
|
| 470 |
+
3) Two double-headed arrows:
|
| 471 |
+
- 'Length' along the longer side.
|
| 472 |
+
- 'Width' along the shorter side.
|
| 473 |
"""
|
| 474 |
overlay = base_bgr.copy()
|
| 475 |
|
| 476 |
+
# --- Strong overlay from mask (tinted red where mask==1) ---
|
| 477 |
mask255 = (mask01 * 255).astype(np.uint8)
|
| 478 |
mask3 = cv2.merge([mask255, mask255, mask255])
|
| 479 |
red = np.zeros_like(overlay); red[:] = (0, 0, 255)
|
|
|
|
| 481 |
tinted = cv2.addWeighted(overlay, 1 - alpha, red, alpha, 0)
|
| 482 |
overlay = np.where(mask3 > 0, tinted, overlay)
|
| 483 |
|
| 484 |
+
# Draw wound contour
|
| 485 |
cnts, _ = cv2.findContours(mask255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 486 |
if cnts:
|
| 487 |
cv2.drawContours(overlay, cnts, -1, (255, 255, 255), 2)
|
|
|
|
| 490 |
cv2.polylines(overlay, [rect_box], True, (255, 255, 255), thickness)
|
| 491 |
pts = rect_box.reshape(-1, 2)
|
| 492 |
|
| 493 |
+
def midpoint(a, b):
|
| 494 |
+
return (int((a[0] + b[0]) / 2), int((a[1] + b[1]) / 2))
|
| 495 |
+
|
| 496 |
+
# Edge lengths
|
| 497 |
e = [np.linalg.norm(pts[i] - pts[(i + 1) % 4]) for i in range(4)]
|
| 498 |
long_edge_idx = int(np.argmax(e))
|
| 499 |
+
short_edge_idx = (long_edge_idx + 1) % 2 # 0/1 map for pairs below
|
| 500 |
+
|
| 501 |
+
# Midpoints of opposite edges for arrows
|
| 502 |
mids = [midpoint(pts[i], pts[(i + 1) % 4]) for i in range(4)]
|
| 503 |
+
# Long side uses edges long_edge_idx and the opposite edge (i+2)
|
| 504 |
long_pair = (long_edge_idx, (long_edge_idx + 2) % 4)
|
| 505 |
+
# Short side uses the other pair
|
| 506 |
short_pair = ((long_edge_idx + 1) % 4, (long_edge_idx + 3) % 4)
|
| 507 |
|
| 508 |
def draw_double_arrow(img, p1, p2):
|
|
|
|
| 516 |
cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 4, cv2.LINE_AA)
|
| 517 |
cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
|
| 518 |
|
| 519 |
+
# Draw arrows and labels
|
| 520 |
draw_double_arrow(overlay, mids[long_pair[0]], mids[long_pair[1]])
|
| 521 |
draw_double_arrow(overlay, mids[short_pair[0]], mids[short_pair[1]])
|
| 522 |
put_label(f"Length: {length_cm:.2f} cm", mids[long_pair[0]])
|
|
|
|
| 545 |
"""
|
| 546 |
try:
|
| 547 |
px_per_cm, exif_meta = estimate_px_per_cm_from_exif(image_pil, DEFAULT_PX_PER_CM)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
image_cv = cv2.cvtColor(np.array(image_pil.convert("RGB")), cv2.COLOR_RGB2BGR)
|
| 549 |
|
| 550 |
# --- Detection ---
|
| 551 |
det_model = self.models_cache.get("det")
|
| 552 |
if det_model is None:
|
| 553 |
raise RuntimeError("YOLO model not loaded")
|
|
|
|
| 554 |
results = det_model.predict(image_cv, verbose=False, device="cpu")
|
| 555 |
if (not results) or (not getattr(results[0], "boxes", None)) or (len(results[0].boxes) == 0):
|
| 556 |
try:
|
|
|
|
| 578 |
mask_u8_255, seg_debug = segment_wound(roi, ts, out_dir)
|
| 579 |
mask01 = (mask_u8_255 > 127).astype(np.uint8)
|
| 580 |
|
| 581 |
+
# Robust post-processing to ensure "proper" masking
|
| 582 |
if mask01.any():
|
| 583 |
mask01 = _clean_mask(mask01)
|
| 584 |
logging.debug(f"Mask postproc: px_after={int(mask01.sum())}")
|
| 585 |
|
| 586 |
+
# --- Measurement ---
|
| 587 |
if mask01.any():
|
| 588 |
length_cm, breadth_cm, (box_pts, _) = measure_min_area_rect(mask01, px_per_cm)
|
| 589 |
+
surface_area_cm2 = count_area_cm2(mask01, px_per_cm)
|
| 590 |
+
# Final annotated ROI with mask + arrows + labels
|
|
|
|
|
|
|
|
|
|
|
|
|
| 591 |
anno_roi = draw_measurement_overlay(roi, mask01, box_pts, length_cm, breadth_cm)
|
| 592 |
segmentation_empty = False
|
| 593 |
else:
|
| 594 |
+
# Graceful fallback if seg failed: use ROI box as bounds
|
| 595 |
h_px = max(0, y2 - y1); w_px = max(0, x2 - x1)
|
| 596 |
length_cm = round(max(h_px, w_px) / px_per_cm, 2)
|
| 597 |
breadth_cm = round(min(h_px, w_px) / px_per_cm, 2)
|
|
|
|
| 615 |
roi_mask_path = os.path.join(out_dir, f"roi_mask_{ts}.png")
|
| 616 |
cv2.imwrite(roi_mask_path, (mask01 * 255).astype(np.uint8))
|
| 617 |
|
| 618 |
+
# ROI overlay (clear mask w/ white contour, no arrows)
|
| 619 |
mask255 = (mask01 * 255).astype(np.uint8)
|
| 620 |
mask3 = cv2.merge([mask255, mask255, mask255])
|
| 621 |
red = np.zeros_like(roi); red[:] = (0, 0, 255)
|
|
|
|
| 658 |
"seg_used": seg_debug.get("used"),
|
| 659 |
"seg_reason": seg_debug.get("reason"),
|
| 660 |
"positive_fraction": round(float(seg_debug.get("positive_fraction", 0.0)), 6),
|
| 661 |
+
"threshold": seg_debug.get("threshold", SEG_THRESH),
|
| 662 |
"segmentation_empty": segmentation_empty,
|
| 663 |
"exif_px_per_cm": round(px_per_cm, 3),
|
| 664 |
}
|
|
|
|
| 674 |
"detection_confidence": float(results[0].boxes.conf[0].cpu().item())
|
| 675 |
if getattr(results[0].boxes, "conf", None) is not None else 0.0,
|
| 676 |
"detection_image_path": detection_path,
|
| 677 |
+
"segmentation_image_path": segmentation_path,
|
| 678 |
"segmentation_annotated_path": annotated_seg_path,
|
| 679 |
"segmentation_roi_path": segmentation_roi_path,
|
| 680 |
"roi_mask_path": roi_mask_path,
|
|
|
|
| 692 |
vs = self.knowledge_base_cache.get("vector_store")
|
| 693 |
if not vs:
|
| 694 |
return "Knowledge base is not available."
|
| 695 |
+
try:
|
| 696 |
+
retriever = vs.as_retriever(search_kwargs={"k": 5})
|
| 697 |
+
docs = retriever.get_relevant_documents(query)
|
| 698 |
+
except Exception:
|
| 699 |
+
retriever = vs.as_retriever(search_kwargs={"k": 5})
|
| 700 |
+
docs = retriever.invoke(query)
|
| 701 |
lines: List[str] = []
|
| 702 |
for d in docs:
|
| 703 |
src = (d.metadata or {}).get("source", "N/A")
|
|
|
|
| 751 |
)
|
| 752 |
if report and report.strip() and not report.startswith(("⚠️", "❌")):
|
| 753 |
return report
|
| 754 |
+
logging.warning("MedGemma unavailable/invalid; using fallback.")
|
| 755 |
return self._generate_fallback_report(patient_info, visual_results, guideline_context)
|
| 756 |
except Exception as e:
|
| 757 |
logging.error(f"Report generation failed: {e}")
|