SmartHeal commited on
Commit
8d515fa
·
verified ·
1 Parent(s): 9a6c1cd

Update src/ai_processor.py

Browse files
Files changed (1) hide show
  1. src/ai_processor.py +874 -556
src/ai_processor.py CHANGED
@@ -1,58 +1,426 @@
1
- # src/ai_processor.py
 
 
 
2
  import os
3
  import logging
 
 
 
 
 
 
 
 
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
- from datetime import datetime
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
- # ---- Safe env defaults (do NOT init CUDA in main) ----
10
- os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
11
- os.environ.setdefault("CUDA_VISIBLE_DEVICES", "") # mask GPU in main
12
- HF_HUB_DISABLE_TELEMETRY = os.environ.get("HF_HUB_DISABLE_TELEMETRY", "1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Light imports that won't trigger CUDA
17
- from transformers import pipeline
18
- from ultralytics import YOLO
 
 
 
19
 
20
- # TensorFlow: keep on CPU in main process
21
- import tensorflow as tf
22
- try:
23
- tf.config.set_visible_devices([], "GPU")
24
- except Exception:
25
- pass
 
 
 
 
 
 
 
 
26
 
27
- # LangChain bits (match your old code; no function name change)
28
- from langchain_community.document_loaders import PyPDFLoader
29
- from langchain.text_splitter import RecursiveCharacterTextSplitter
30
- from langchain_community.embeddings import HuggingFaceEmbeddings
31
- from langchain_community.vectorstores import FAISS
 
 
32
 
33
- from huggingface_hub import HfApi, HfFolder
 
 
 
 
 
 
34
 
35
- # Spaces (ZeroGPU)
36
- try:
37
- import spaces
38
- SPACES_AVAILABLE = True
39
- except Exception:
40
- spaces = None
41
- SPACES_AVAILABLE = False
 
 
 
 
 
 
 
 
42
 
43
- from .config import Config
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- # ----------------------------- utils -----------------------------
47
- def _largest_component(mask01: np.ndarray, min_area_px: int = 50) -> np.ndarray:
48
- num, labels, stats, _ = cv2.connectedComponentsWithStats(mask01.astype(np.uint8), 8)
49
- if num <= 1:
50
- return (mask01 > 0).astype(np.uint8)
51
- areas = stats[1:, cv2.CC_STAT_AREA]
52
- if areas.size == 0 or areas.max() < min_area_px:
53
- return (mask01 > 0).astype(np.uint8)
54
- idx = 1 + int(np.argmax(areas))
55
- return (labels == idx).astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  def _fill_holes(mask01: np.ndarray) -> np.ndarray:
58
  h, w = mask01.shape[:2]
@@ -64,610 +432,560 @@ def _fill_holes(mask01: np.ndarray) -> np.ndarray:
64
  return out.astype(np.uint8)
65
 
66
  def _clean_mask(mask01: np.ndarray) -> np.ndarray:
 
67
  mask01 = (mask01 > 0).astype(np.uint8)
68
  k3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
69
  k5 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
70
  mask01 = cv2.morphologyEx(mask01, cv2.MORPH_OPEN, k3, iterations=1)
71
  mask01 = cv2.morphologyEx(mask01, cv2.MORPH_CLOSE, k5, iterations=1)
72
  mask01 = _fill_holes(mask01)
73
- mask01 = _largest_component(mask01)
 
 
 
 
 
 
74
  return (mask01 > 0).astype(np.uint8)
75
 
76
- def _grabcut_refine(bgr: np.ndarray, seed01: np.ndarray, iters: int = 3) -> np.ndarray:
77
- h, w = bgr.shape[:2]
78
- gc = np.full((h, w), cv2.GC_PR_BGD, np.uint8)
79
- k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
80
- seed_dil = cv2.dilate(seed01, k, iterations=1)
81
- gc[seed01.astype(bool)] = cv2.GC_PR_FGD
82
- gc[seed_dil.astype(bool)] = cv2.GC_FGD
83
- gc[0, :], gc[-1, :], gc[:, 0], gc[:, -1] = cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD
84
- bgdModel = np.zeros((1, 65), np.float64)
85
- fgdModel = np.zeros((1, 65), np.float64)
86
- cv2.grabCut(bgr, gc, None, bgdModel, fgdModel, iters, cv2.GC_INIT_WITH_MASK)
87
- return np.where((gc == cv2.GC_FGD) | (gc == cv2.GC_PR_FGD), 1, 0).astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
- def _fallback_segment(roi_bgr: np.ndarray) -> np.ndarray:
90
- """Robust OpenCV fallback: Lab 2-cluster (maximize a*), then GrabCut grow + cleanup."""
91
- Z = roi_bgr.reshape((-1, 3)).astype(np.float32)
92
  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
93
  _, labels, centers = cv2.kmeans(Z, 2, None, criteria, 5, cv2.KMEANS_PP_CENTERS)
94
  centers_u8 = centers.astype(np.uint8).reshape(1, 2, 3)
95
  centers_lab = cv2.cvtColor(centers_u8, cv2.COLOR_BGR2LAB)[0]
96
- wound_idx = int(np.argmax(centers_lab[:, 1])) # reddest cluster
97
- seed01 = (labels.reshape(roi_bgr.shape[:2]) == wound_idx).astype(np.uint8)
98
- gc01 = _grabcut_refine(roi_bgr, seed01, iters=3)
99
- return _clean_mask(gc01)
100
-
101
- def _safe_load_seg_model(path: str):
102
- """Try multiple loaders to survive Keras 3 / TF 2.15 / h5 mismatches."""
103
- if not os.path.exists(path):
104
- return None
105
- try:
106
- # Keras legacy API (present in TF 2.13+ with legacy shim)
107
- from tensorflow import keras as tfk
108
- if hasattr(tfk, "saving") and hasattr(tfk.saving, "legacy"):
109
- return tfk.saving.legacy.load_model(path, compile=False)
110
- except Exception:
111
- pass
112
- try:
113
- # tf.keras standard loader
114
- from tensorflow.keras.models import load_model as tf_load_model
115
- return tf_load_model(path, compile=False)
116
- except Exception as e:
117
- logging.warning(f"Segmentation model failed to load with legacy + tf.keras: {e}")
118
- return None
119
-
120
-
121
- # ----------------------------- main class -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  class AIProcessor:
123
  def __init__(self):
124
- self.models_cache = {}
125
- self.knowledge_base_cache = {}
126
- self.config = Config()
127
- self.px_per_cm = self.config.PIXELS_PER_CM
128
- self._initialize_models()
129
-
130
- def _initialize_models(self):
131
- """Initialize all AI models except GPU VLM (that one loads inside the GPU worker)."""
132
- try:
133
- # HF token
134
- if self.config.HF_TOKEN:
135
- HfFolder.save_token(self.config.HF_TOKEN)
136
- logging.info("✅ HF token set")
137
-
138
- # YOLO (CPU-only in main)
139
- try:
140
- self.models_cache["det"] = YOLO(self.config.YOLO_MODEL_PATH)
141
- logging.info("✅ YOLO loaded (CPU; CUDA masked in main)")
142
- except Exception as e:
143
- logging.warning(f"YOLO not available: {e}")
144
-
145
- # Segmentation (safe loader, stays on CPU)
146
- try:
147
- seg = _safe_load_seg_model(self.config.SEG_MODEL_PATH)
148
- if seg is None:
149
- raise RuntimeError("segmentation file missing or incompatible")
150
- self.models_cache["seg"] = seg
151
- logging.info("✅ Segmentation model loaded (CPU)")
152
- except Exception as e:
153
- self.models_cache["seg"] = None
154
- logging.warning(f"Segmentation unavailable: {e}")
155
-
156
- # Wound classifier (CPU)
157
- try:
158
- self.models_cache["cls"] = pipeline(
159
- "image-classification",
160
- model="Hemg/Wound-classification",
161
- token=self.config.HF_TOKEN,
162
- device="cpu",
163
- )
164
- logging.info("✅ Classifier loaded (CPU)")
165
- except Exception as e:
166
- self.models_cache["cls"] = None
167
- logging.warning(f"Classifier unavailable: {e}")
168
-
169
- # Embeddings for KB (CPU)
170
- try:
171
- self.models_cache["embedding_model"] = HuggingFaceEmbeddings(
172
- model_name="sentence-transformers/all-MiniLM-L6-v2",
173
- model_kwargs={"device": "cpu"},
174
- )
175
- logging.info("✅ Embeddings loaded (CPU)")
176
- except Exception as e:
177
- self.models_cache["embedding_model"] = None
178
- logging.warning(f"Embeddings unavailable: {e}")
179
-
180
- self._load_knowledge_base()
181
- except Exception as e:
182
- logging.error(f"Error initializing models: {e}")
183
-
184
- def _load_knowledge_base(self):
185
- """Load guideline PDFs into a FAISS vector store."""
186
  try:
187
- documents = []
188
- for pdf_path in self.config.GUIDELINE_PDFS:
189
- if os.path.exists(pdf_path):
190
- try:
191
- loader = PyPDFLoader(pdf_path)
192
- docs = loader.load()
193
- documents.extend(docs)
194
- logging.info(f"Loaded PDF: {pdf_path}")
195
- except Exception as e:
196
- logging.warning(f"PDF load failed ({pdf_path}): {e}")
197
-
198
- if documents and self.models_cache.get("embedding_model"):
199
- splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
200
- chunks = splitter.split_documents(documents)
201
- vectorstore = FAISS.from_documents(chunks, self.models_cache["embedding_model"])
202
- self.knowledge_base_cache["vectorstore"] = vectorstore
203
- logging.info(f"✅ Knowledge base ready ({len(chunks)} chunks)")
204
- else:
205
- self.knowledge_base_cache["vectorstore"] = None
206
- logging.warning("Knowledge base not available (no PDFs or embeddings).")
207
- except Exception as e:
208
- logging.warning(f"Knowledge base loading error: {e}")
209
- self.knowledge_base_cache["vectorstore"] = None
210
 
211
- # ------------------------ vision core ------------------------
212
- def perform_visual_analysis(self, image_pil):
213
- """Perform comprehensive visual analysis of wound image."""
214
- try:
215
  image_cv = cv2.cvtColor(np.array(image_pil.convert("RGB")), cv2.COLOR_RGB2BGR)
216
 
217
- # YOLO detection
218
- if "det" not in self.models_cache or self.models_cache["det"] is None:
219
- raise ValueError("YOLO detection model not available.")
220
- results = self.models_cache["det"].predict(image_cv, verbose=False, device="cpu")
 
 
221
  if (not results) or (not getattr(results[0], "boxes", None)) or (len(results[0].boxes) == 0):
222
- raise ValueError("No wound detected in the image.")
 
 
 
 
223
 
224
- box = results[0].boxes.xyxy[0].cpu().numpy().astype(int)
225
  x1, y1, x2, y2 = [int(v) for v in box]
226
  x1, y1 = max(0, x1), max(0, y1)
227
  x2, y2 = min(image_cv.shape[1], x2), min(image_cv.shape[0], y2)
228
- region_cv = image_cv[y1:y2, x1:x2].copy()
 
 
 
 
 
 
229
 
230
- # Save detection vis
231
- os.makedirs(os.path.join(self.config.UPLOADS_DIR, "analysis"), exist_ok=True)
232
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  det_vis = image_cv.copy()
234
  cv2.rectangle(det_vis, (x1, y1), (x2, y2), (0, 255, 0), 2)
235
- detection_image_path = os.path.join(self.config.UPLOADS_DIR, "analysis", f"detection_{ts}.png")
236
- cv2.imwrite(detection_image_path, det_vis)
237
- detection_image_pil = Image.fromarray(cv2.cvtColor(det_vis, cv2.COLOR_BGR2RGB))
238
-
239
- # --- segmentation ---
240
- length = breadth = area = 0.0
241
- segmentation_image_pil = None
242
- segmentation_image_path = None
243
-
244
- mask01 = None
245
- seg_model = self.models_cache.get("seg", None)
246
- if seg_model is not None:
247
- try:
248
- ishape = getattr(seg_model, "input_shape", None)
249
- th, tw = int(ishape[1]), int(ishape[2]) if ishape and len(ishape) >= 3 else (224, 224)
250
- resized = cv2.resize(region_cv, (tw, th), interpolation=cv2.INTER_LINEAR)
251
- x = np.expand_dims(resized.astype(np.float32) / 255.0, 0)
252
- pred = seg_model.predict(x, verbose=0)
253
- if isinstance(pred, (list, tuple)):
254
- pred = pred[0]
255
- p = np.squeeze(pred)
256
- # sigmoid if raw
257
- if p.max() > 1.0 or p.min() < 0.0:
258
- p = 1.0 / (1.0 + np.exp(-p))
259
- p = cv2.resize(p.astype(np.float32), (region_cv.shape[1], region_cv.shape[0]), interpolation=cv2.INTER_LINEAR)
260
- # adaptive threshold
261
- p255 = (np.clip(p, 0, 1) * 255).astype(np.uint8)
262
- thr_val, _ = cv2.threshold(p255, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
263
- thr = float(np.clip(thr_val / 255.0, 0.25, 0.65))
264
- seed01 = (p >= thr).astype(np.uint8)
265
- if seed01.sum() == 0:
266
- seed01 = (p >= max(thr - 0.1, 0.15)).astype(np.uint8)
267
- gc01 = _grabcut_refine(region_cv, seed01, iters=3)
268
- mask01 = _clean_mask(gc01)
269
- except Exception as e:
270
- logging.warning(f"Segmentation model failed; using OpenCV fallback: {e}")
271
- mask01 = _fallback_segment(region_cv)
272
- else:
273
- mask01 = _fallback_segment(region_cv)
274
-
275
- # overlay + measurements
276
- overlay = region_cv.copy()
277
- red = overlay.copy(); red[:] = (0, 0, 255)
278
- if mask01 is not None and mask01.any():
279
- mask255 = (mask01 * 255).astype(np.uint8)
280
- mask3 = cv2.merge([mask255, mask255, mask255])
281
- tinted = cv2.addWeighted(region_cv, 0.45, red, 0.55, 0)
282
- overlay = np.where(mask3 > 0, tinted, region_cv)
283
  cnts, _ = cv2.findContours(mask255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
284
- if cnts:
285
- cnt = max(cnts, key=cv2.contourArea)
286
- x, y, w, h = cv2.boundingRect(cnt)
287
- length = round(h / self.px_per_cm, 2)
288
- breadth = round(w / self.px_per_cm, 2)
289
- area = round(cv2.contourArea(cnt) / (self.px_per_cm ** 2), 2)
290
- cv2.drawContours(overlay, [cnt], -1, (255, 255, 255), 2)
291
-
292
- segmentation_image_path = os.path.join(self.config.UPLOADS_DIR, "analysis", f"segmentation_{ts}.png")
293
  seg_full = image_cv.copy()
294
- seg_full[y1:y2, x1:x2] = overlay
295
- cv2.imwrite(segmentation_image_path, seg_full)
296
- segmentation_image_pil = Image.fromarray(cv2.cvtColor(seg_full, cv2.COLOR_BGR2RGB))
 
 
 
 
 
 
 
 
 
297
 
298
- # classification
299
  wound_type = "Unknown"
300
- if self.models_cache.get("cls") is not None:
 
301
  try:
302
- region_pil = Image.fromarray(cv2.cvtColor(region_cv, cv2.COLOR_BGR2RGB))
303
- preds = self.models_cache["cls"](region_pil)
304
  if preds:
305
  wound_type = max(preds, key=lambda x: x.get("score", 0)).get("label", "Unknown")
306
  except Exception as e:
307
- logging.warning(f"Wound classification error: {e}")
308
-
309
- conf = 0.0
310
- try:
311
- conf = float(results[0].boxes.conf[0].cpu().item())
312
- except Exception:
313
- pass
 
 
 
 
 
314
 
315
  return {
316
  "wound_type": wound_type,
317
- "length_cm": float(length),
318
- "breadth_cm": float(breadth),
319
- "surface_area_cm2": float(area),
320
- "detection_confidence": conf,
321
- "bounding_box": [int(x1), int(y1), int(x2), int(y2)],
322
- "detection_image_path": detection_image_path,
323
- "detection_image_pil": detection_image_pil,
324
- "segmentation_image_path": segmentation_image_path,
325
- "segmentation_image_pil": segmentation_image_pil,
 
 
 
 
 
 
326
  }
327
-
328
  except Exception as e:
329
- logging.error(f"Visual analysis error: {e}", exc_info=True)
330
- raise ValueError(f"Visual analysis failed: {str(e)}")
331
 
332
- # ------------------------ KB / RAG ------------------------
333
- def query_guidelines(self, query: str):
334
- """Query the knowledge base for relevant guidelines"""
335
  try:
336
- vector_store = self.knowledge_base_cache.get("vectorstore")
337
- if not vector_store:
338
- return "Knowledge base unavailable - clinical guidelines not loaded"
339
-
340
- retriever = vector_store.as_retriever(search_kwargs={"k": 10})
341
- try:
342
- docs = retriever.invoke(query)
343
- except Exception:
344
- # old API fallback
345
- docs = retriever.get_relevant_documents(query)
346
-
347
- if not docs:
348
- return "No relevant guidelines found for the query"
349
-
350
- out = []
351
  for d in docs:
352
- meta = d.metadata or {}
353
- src = meta.get("source", "Unknown")
354
- page = meta.get("page", "N/A")
355
- content = (d.page_content or "").strip()
356
- out.append(f"Source: {src}, Page: {page}\nContent: {content}")
357
- return "\n\n".join(out)
358
  except Exception as e:
359
- logging.error(f"Guidelines query error: {e}")
360
- return f"Error querying guidelines: {str(e)}"
361
-
362
- # ------------------------ Reporting (VLM + fallback) ------------------------
363
- def generate_final_report(self, patient_info, visual_results, guideline_context, image_pil, max_new_tokens=None):
364
- """Generate comprehensive medical report using a VLM if available (loaded inside GPU worker), else fallback."""
365
- try:
366
- # If medgemma/qwen pipeline wasn't cached by GPU worker, fallback right away.
367
- if "medgemma_pipe" not in self.models_cache or self.models_cache["medgemma_pipe"] is None:
368
- return self._generate_fallback_report(patient_info, visual_results, guideline_context)
369
-
370
- max_tokens = max_new_tokens or self.config.MAX_NEW_TOKENS
371
- detection_path = visual_results.get("detection_image_path", "")
372
- segmentation_path = visual_results.get("segmentation_image_path", "")
373
 
374
- prompt = f"""
375
- # Wound Care Report
376
 
377
- ## Patient Information
378
  {patient_info}
379
 
380
- ## Visual Analysis Summary
381
- - Wound Type: {visual_results.get('wound_type', 'Unknown')}
382
- - Length: {visual_results.get('length_cm', 0)} cm
383
- - Breadth: {visual_results.get('breadth_cm', 0)} cm
384
- - Surface Area: {visual_results.get('surface_area_cm2', 0)} cm²
385
- - Detection Confidence: {visual_results.get('detection_confidence', 0):.2f}
386
-
387
- ## Clinical Reference
388
- {guideline_context}
389
-
390
- You are SmartHeal-AI Agent, a world-class wound care AI specialist trained in clinical wound assessment and guideline-based treatment planning.
391
- Generate a concise, actionable, evidence-based report with: Clinical Summary, Dressing/Medication Recommendations, Key Risk Factors, and Prognosis & Monitoring. Avoid generic advice; tailor to the data above.
392
- """.strip()
393
-
394
- content_list = [{"type": "text", "text": prompt}]
395
- if image_pil:
396
- content_list.insert(0, {"type": "image", "image": image_pil})
397
- if visual_results.get("detection_image_pil"):
398
- content_list.append({"type": "image", "image": visual_results["detection_image_pil"]})
399
- if visual_results.get("segmentation_image_pil"):
400
- content_list.append({"type": "image", "image": visual_results["segmentation_image_pil"]})
401
-
402
- messages = [
403
- {
404
- "role": "system",
405
- "content": [{"type": "text", "text": "You are a medical AI assistant specializing in wound care. Be precise, objective, and recommendation-focused."}],
406
- },
407
- {
408
- "role": "user",
409
- "content": content_list,
410
- },
411
- ]
412
-
413
- out = self.models_cache["medgemma_pipe"](
414
- text=messages,
415
- max_new_tokens=int(max_tokens),
416
- do_sample=False,
 
 
 
 
 
417
  )
418
- generated = ""
419
- try:
420
- generated = (out[0]["generated_text"][-1].get("content", "") or "").strip()
421
- except Exception:
422
- generated = (out[0].get("generated_text", "") or "").strip()
423
-
424
- if generated:
425
- images_sec = f"\n\n## Analysis Images\n- Detection: {detection_path}\n- Segmentation: {segmentation_path}\n"
426
- return images_sec + generated
427
-
428
  return self._generate_fallback_report(patient_info, visual_results, guideline_context)
429
  except Exception as e:
430
- logging.error(f"VLM report generation error: {e}")
431
  return self._generate_fallback_report(patient_info, visual_results, guideline_context)
432
 
433
- def _generate_fallback_report(self, patient_info, visual_results, guideline_context):
434
- detection_path = visual_results.get("detection_image_path", "Not available")
435
- segmentation_path = visual_results.get("segmentation_image_path", "Not available")
436
- return f"""
437
- # Wound Analysis Report
438
- ## Patient Information
439
- {patient_info}
440
-
441
- ## Visual Analysis Results
442
- - **Wound Type**: {visual_results.get('wound_type', 'Unknown')}
443
- - **Dimensions**: {visual_results.get('length_cm', 0)} cm × {visual_results.get('breadth_cm', 0)} cm
444
- - **Surface Area**: {visual_results.get('surface_area_cm2', 0)} cm²
445
- - **Detection Confidence**: {visual_results.get('detection_confidence', 0):.2f}
446
-
447
- ## Analysis Images
448
- - **Detection Image**: {detection_path}
449
- - **Segmentation Image**: {segmentation_path}
450
-
451
- ## Assessment
452
- Automated measurements provided. Verify via clinical exam.
453
-
454
- ## Recommendations
455
- - Cleanse wound; choose dressing per moisture/infection risk
456
- - Consider debridement if indicated
457
- - Document with serial photos & measurements
458
-
459
- ## Clinical Guidelines
460
- {(guideline_context or '')[:500]}...
461
-
462
- *Note: Decision support only; not a diagnosis.*
463
- """.strip()
464
-
465
- # ------------------------ I/O ------------------------
466
- def save_and_commit_image(self, image_pil):
467
- """Save image locally and optionally upload to HuggingFace dataset"""
468
  try:
469
- os.makedirs(self.config.UPLOADS_DIR, exist_ok=True)
470
- filename = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
471
- local_path = os.path.join(self.config.UPLOADS_DIR, filename)
472
- image_pil.convert("RGB").save(local_path)
473
- logging.info(f"✅ Image saved locally: {local_path}")
 
474
 
475
- if self.config.HF_TOKEN and self.config.DATASET_ID:
476
  try:
477
- HfFolder.save_token(self.config.HF_TOKEN)
 
478
  api = HfApi()
479
  api.upload_file(
480
- path_or_fileobj=local_path,
481
  path_in_repo=f"images/{filename}",
482
- repo_id=self.config.DATASET_ID,
483
  repo_type="dataset",
 
484
  commit_message=f"Upload wound image: {filename}",
485
  )
486
  logging.info("✅ Image committed to HF dataset")
487
  except Exception as e:
488
  logging.warning(f"HF upload failed: {e}")
489
 
490
- return local_path
491
  except Exception as e:
492
- logging.error(f"Image saving error: {e}")
493
- return None
494
 
495
- # ------------------------ Pipeline (GPU-safe) ------------------------
496
- # decoration is evaluated at import; only the selected branch executes
497
- @spaces.GPU(enable_queue=True, duration=120) if SPACES_AVAILABLE else (lambda f: f)
498
- def full_analysis_pipeline(self, image, questionnaire_data):
499
- """Complete analysis pipeline. VLM loads here (inside GPU worker) to avoid main-process CUDA init."""
500
  try:
501
- # Try to build a VLM inside the worker. If ZeroGPU fails, we fallback to CPU.
502
- if "medgemma_pipe" not in self.models_cache or self.models_cache["medgemma_pipe"] is None:
503
- vlm_loaded = False
504
- # Prefer a small VLM (Qwen2-VL 2B) for Spaces; if it fails on GPU, retry on CPU.
505
- try:
506
- self.models_cache["medgemma_pipe"] = pipeline(
507
- "image-text-to-text",
508
- model=os.environ.get("SMARTHEAL_VLM", "Qwen/Qwen2-VL-2B-Instruct"),
509
- token=self.config.HF_TOKEN,
510
- device_map="cuda", # we're inside ZeroGPU worker now
511
- torch_dtype="auto",
512
- max_new_tokens=self.config.MAX_NEW_TOKENS,
513
- trust_remote_code=True,
514
- )
515
- vlm_loaded = True
516
- logging.info("✅ VLM loaded on GPU worker")
517
- except Exception as e:
518
- logging.warning(f"GPU VLM failed; falling back to CPU: {e}")
519
- try:
520
- self.models_cache["medgemma_pipe"] = pipeline(
521
- "image-text-to-text",
522
- model=os.environ.get("SMARTHEAL_VLM", "Qwen/Qwen2-VL-2B-Instruct"),
523
- token=self.config.HF_TOKEN,
524
- device_map="cpu",
525
- torch_dtype="auto",
526
- max_new_tokens=self.config.MAX_NEW_TOKENS,
527
- trust_remote_code=True,
528
- )
529
- vlm_loaded = True
530
- logging.info("✅ VLM loaded on CPU")
531
- except Exception as e2:
532
- self.models_cache["medgemma_pipe"] = None
533
- logging.error(f"❌ Could not load any VLM: {e2}")
534
-
535
- # Save image
536
- saved_path = self.save_and_commit_image(image)
537
-
538
- # Visual analysis
539
- visual_results = self.perform_visual_analysis(image)
540
-
541
- # Patient info string
542
  pi = questionnaire_data or {}
543
- patient_info = ", ".join([f"{k}: {v}" for k, v in pi.items() if str(v).strip() != ""])
544
-
545
- # KB query
546
- wound_type = visual_results.get("wound_type", "wound")
547
- moisture = pi.get("moisture", "unknown")
548
- infection = pi.get("infection", "unknown")
549
- diabetic = pi.get("diabetic", "unknown")
550
- query = f"best practices for managing a {wound_type} with moisture level '{moisture}' and signs of infection '{infection}' in a patient who is diabetic '{diabetic}'"
 
 
 
 
 
 
 
 
551
  guideline_context = self.query_guidelines(query)
552
 
553
- # Report
554
- final_report = self.generate_final_report(patient_info, visual_results, guideline_context, image)
555
 
556
  return {
557
  "success": True,
558
  "visual_analysis": visual_results,
559
- "report": final_report,
560
  "saved_image_path": saved_path,
561
- "timestamp": datetime.now().isoformat(),
 
 
562
  }
563
-
564
  except Exception as e:
565
- logging.error(f"Full analysis pipeline error: {e}", exc_info=True)
566
  return {
567
  "success": False,
568
  "error": str(e),
569
- "timestamp": datetime.now().isoformat(),
 
 
 
570
  }
571
 
572
- # ------------------------ Legacy API ------------------------
573
- def analyze_wound(self, image, questionnaire_data):
574
- """Legacy method for backward compatibility"""
575
  try:
576
  if isinstance(image, str):
577
- try:
578
- image = Image.open(image)
579
- logging.info("Converted path to PIL Image")
580
- except Exception as e:
581
- logging.error(f"Error opening image: {e}")
582
- if not isinstance(image, Image.Image):
583
- # file-like?
584
- if hasattr(image, "read"):
585
- try:
586
- if hasattr(image, "seek"):
587
- image.seek(0)
588
- image = Image.open(image)
589
- except Exception as e:
590
- logging.error(f"Error reading file-like image: {e}")
591
- raise ValueError(f"Invalid image format: {type(image)}")
592
-
593
- result = self.full_analysis_pipeline(image, questionnaire_data)
594
-
595
- if result.get("success"):
596
- return {
597
- "timestamp": result["timestamp"],
598
- "summary": f"Analysis completed for {questionnaire_data.get('patient_name', 'patient')}",
599
- "recommendations": result["report"],
600
- "wound_detection": {
601
- "status": "success",
602
- "detections": [result["visual_analysis"]],
603
- "total_wounds": 1,
604
- },
605
- "segmentation_result": {
606
- "status": "success",
607
- "wound_area_percentage": result["visual_analysis"].get("surface_area_cm2", 0),
608
- },
609
- "risk_assessment": self._assess_risk_legacy(questionnaire_data),
610
- "guideline_recommendations": [result["report"][:200] + "..."],
611
- }
612
  else:
613
- return {
614
- "timestamp": result["timestamp"],
615
- "summary": f"Analysis failed: {result.get('error','unknown')}",
616
- "recommendations": "Please consult with a healthcare professional.",
617
- "wound_detection": {"status": "error", "message": result.get("error", "")},
618
- "segmentation_result": {"status": "error", "message": result.get("error", "")},
619
- "risk_assessment": {"risk_score": 0, "risk_level": "Unknown", "risk_factors": []},
620
- "guideline_recommendations": ["Analysis unavailable due to error"],
621
- }
622
 
 
623
  except Exception as e:
624
- logging.error(f"Legacy analyze_wound error: {e}")
625
  return {
626
- "timestamp": datetime.now().isoformat(),
627
- "summary": f"Analysis error: {str(e)}",
628
- "recommendations": "Please consult with a healthcare professional.",
629
- "wound_detection": {"status": "error", "message": str(e)},
630
- "segmentation_result": {"status": "error", "message": str(e)},
631
- "risk_assessment": {"risk_score": 0, "risk_level": "Unknown", "risk_factors": []},
632
- "guideline_recommendations": ["Analysis unavailable due to error"],
633
- }
634
-
635
- def _assess_risk_legacy(self, questionnaire_data):
636
- """Legacy risk assessment for backward compatibility"""
637
- risk_factors = []
638
- risk_score = 0
639
- try:
640
- age = int(questionnaire_data.get("patient_age", 0) or 0)
641
- if age > 65:
642
- risk_factors.append("Advanced age (>65)")
643
- risk_score += 2
644
- elif age > 50:
645
- risk_factors.append("Older adult (50-65)")
646
- risk_score += 1
647
-
648
- duration = str(questionnaire_data.get("wound_duration", "")).lower()
649
- if any(t in duration for t in ["month", "months", "year"]):
650
- risk_factors.append("Chronic wound (>4 weeks)")
651
- risk_score += 3
652
-
653
- pain_level = int(questionnaire_data.get("pain_level", 0) or 0)
654
- if pain_level >= 7:
655
- risk_factors.append("High pain level")
656
- risk_score += 2
657
-
658
- medical_history = str(questionnaire_data.get("medical_history", "")).lower()
659
- if "diabetes" in medical_history:
660
- risk_factors.append("Diabetes mellitus")
661
- risk_score += 3
662
- if "circulation" in medical_history or "vascular" in medical_history:
663
- risk_factors.append("Vascular/circulation issues")
664
- risk_score += 2
665
- if "immune" in medical_history:
666
- risk_factors.append("Immune system compromise")
667
- risk_score += 2
668
-
669
- risk_level = "High" if risk_score >= 7 else ("Moderate" if risk_score >= 4 else "Low")
670
- return {"risk_score": risk_score, "risk_level": risk_level, "risk_factors": risk_factors}
671
- except Exception as e:
672
- logging.error(f"Risk assessment error: {e}")
673
- return {"risk_score": 0, "risk_level": "Unknown", "risk_factors": []}
 
1
+ # smartheal_ai_processor.py
2
+ # Verbose, instrumented version — preserves public class/function names
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 (do NOT globally hint CUDA here) ----
11
+ os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
12
+ LOGLEVEL = os.getenv("LOGLEVEL", "INFO").upper()
13
+ SMARTHEAL_DEBUG = os.getenv("SMARTHEAL_DEBUG", "0") == "1"
14
+
15
  import cv2
16
  import numpy as np
17
  from PIL import Image
18
+ from PIL.ExifTags import TAGS
19
+
20
+ # --- Logging config ---
21
+ logging.basicConfig(
22
+ level=getattr(logging, LOGLEVEL, logging.INFO),
23
+ format="%(asctime)s - %(levelname)s - %(message)s",
24
+ )
25
+
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 decorator (REQUIRED) ---
30
+ from spaces import GPU as _SPACES_GPU
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" # optional
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
46
+ PX_PER_CM_MIN, PX_PER_CM_MAX = 5.0, 1200.0
47
+
48
+ # Segmentation preprocessing knobs
49
+ SEG_EXPECTS_RGB = os.getenv("SEG_EXPECTS_RGB", "1") == "1" # most TF models trained on RGB
50
+ SEG_NORM = os.getenv("SEG_NORM", "0to1") # "0to1" | "imagenet"
51
+ SEG_THRESH = float(os.getenv("SEG_THRESH", "0.5"))
52
+
53
+ models_cache: Dict[str, object] = {}
54
+ knowledge_base_cache: Dict[str, object] = {}
55
+
56
+ # ---------- Utilities to prevent CUDA in main process ----------
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
+ # Prevent Ultralytics from probing CUDA on import
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
+ tf.config.set_visible_devices([], "GPU")
85
+ from tensorflow.keras.models import load_model
86
+ return load_model
87
+
88
+ def _import_hf_cls():
89
+ from transformers import pipeline
90
+ return pipeline
91
+
92
+ def _import_embeddings():
93
+ from langchain_community.embeddings import HuggingFaceEmbeddings
94
+ return HuggingFaceEmbeddings
95
+
96
+ def _import_langchain_pdf():
97
+ from langchain_community.document_loaders import PyPDFLoader
98
+ return PyPDFLoader
99
+
100
+ def _import_langchain_faiss():
101
+ from langchain_community.vectorstores import FAISS
102
+ return FAISS
103
+
104
+ def _import_hf_hub():
105
+ from huggingface_hub import HfApi, HfFolder
106
+ return HfApi, HfFolder
107
+
108
+ # ---------- SmartHeal prompts (system + user prefix) ----------
109
+ SMARTHEAL_SYSTEM_PROMPT = """\
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
+
127
+ Guideline context (snippets you can draw principles from; do not quote at length):
128
+ {guideline_context}
129
 
130
+ Write a structured answer with these headings exactly:
131
+ 1. Clinical Summary (max 4 bullet points)
132
+ 2. Likely Stage/Type (if uncertain, say 'uncertain')
133
+ 3. Treatment Plan (specific dressing choices and frequency based on exudate/infection risk)
134
+ 4. Red Flags (what to escalate and when)
135
+ 5. Follow-up Cadence (days)
136
+ 6. Notes (assumptions/uncertainties)
137
+
138
+ Keep to 220–300 words. Do NOT provide diagnosis. Avoid contraindicated advice.
139
+ """
140
+
141
+ # ---------- VLM (MedGemma replaced with Qwen2-VL) ----------
142
+ @_SPACES_GPU(enable_queue=True)
143
+ def _vlm_infer_gpu(messages, model_id: str, max_new_tokens: int, token: Optional[str]):
144
+ """
145
+ Runs entirely inside a Spaces GPU worker. It's the ONLY place we allow CUDA init.
146
+ """
147
+ from transformers import pipeline
148
+ import torch # Ensure torch is imported here
149
+ pipe = pipeline(
150
+ task="image-text-to-text",
151
+ model=model_id,
152
+ torch_dtype=torch.bfloat16, # Use torch_dtype from the working example
153
+ device_map="auto", # CUDA init happens here, safely in GPU worker
154
+ token=token,
155
+ trust_remote_code=True,
156
+ model_kwargs={"low_cpu_mem_usage": True},
157
+ )
158
+ out = pipe(text=messages, max_new_tokens=max_new_tokens, do_sample=False, temperature=0.2)
159
+ try:
160
+ txt = out[0]["generated_text"][-1].get("content", "")
161
+ except Exception:
162
+ txt = out[0].get("generated_text", "")
163
+ return (txt or "").strip() or "⚠️ Empty response"
164
+
165
+ def generate_medgemma_report( # kept name so callers don't change
166
+ patient_info: str,
167
+ visual_results: Dict,
168
+ guideline_context: str,
169
+ image_pil: Image.Image,
170
+ max_new_tokens: Optional[int] = None,
171
+ ) -> str:
172
+ """
173
+ MedGemma replacement using Qwen/Qwen2-VL-2B-Instruct via image-text-to-text.
174
+ Loads & runs ONLY inside a GPU worker to satisfy Stateless GPU constraints.
175
+ """
176
+ if os.getenv("SMARTHEAL_ENABLE_VLM", "1") != "1":
177
+ return "⚠️ VLM disabled"
178
+
179
+ model_id = os.getenv("SMARTHEAL_VLM_MODEL", "Qwen/Qwen2-VL-2B-Instruct")
180
+ max_new_tokens = max_new_tokens or int(os.getenv("SMARTHEAL_VLM_MAX_TOKENS", "600"))
181
+
182
+ uprompt = SMARTHEAL_USER_PREFIX.format(
183
+ patient_info=patient_info,
184
+ wound_type=visual_results.get("wound_type", "Unknown"),
185
+ length_cm=visual_results.get("length_cm", 0),
186
+ breadth_cm=visual_results.get("breadth_cm", 0),
187
+ area_cm2=visual_results.get("surface_area_cm2", 0),
188
+ det_conf=float(visual_results.get("detection_confidence", 0.0)),
189
+ px_per_cm=visual_results.get("px_per_cm", "?"),
190
+ guideline_context=(guideline_context or "")[:900],
191
+ )
192
+
193
+ messages = [
194
+ {"role": "system", "content": [{"type": "text", "text": SMARTHEAL_SYSTEM_PROMPT}]},
195
+ {"role": "user", "content": [
196
+ {"type": "image", "image": image_pil},
197
+ {"type": "text", "text": uprompt},
198
+ ]},
199
+ ]
200
 
201
+ try:
202
+ # IMPORTANT: do not import transformers or touch CUDA here. Only call the GPU worker.
203
+ return _vlm_infer_gpu(messages, model_id, max_new_tokens, HF_TOKEN)
204
+ except Exception as e:
205
+ logging.error(f"VLM call failed: {e}")
206
+ return "⚠️ VLM error"
207
+
208
+ # ---------- Initialize CPU models ----------
209
+ def load_yolo_model():
210
+ YOLO = _import_ultralytics()
211
+ # Construct model with CUDA masked to avoid auto-selecting cuda:0
212
+ with _no_cuda_env():
213
+ model = YOLO(YOLO_MODEL_PATH)
214
+ return modeldef load_segmentation_model():
215
+ import tensorflow as tf
216
+ load_model = _import_tf_loader()
217
+ return load_model(SEG_MODEL_PATH, compile=False, custom_objects={\'InputLayer\': tf.keras.layers.InputLayer})
218
+
219
+ def load_classification_pipeline():
220
+ pipe = _import_hf_cls()
221
+ return pipe("image-classification", model="Hemg/Wound-classification", token=HF_TOKEN, device="cpu")
222
+
223
+ def load_embedding_model():
224
+ Emb = _import_embeddings()
225
+ return Emb(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={"device": "cpu"})
226
+
227
+ def initialize_cpu_models() -> None:
228
+ if HF_TOKEN:
229
+ try:
230
+ HfApi, HfFolder = _import_hf_hub()
231
+ HfFolder.save_token(HF_TOKEN)
232
+ logging.info("✅ HF token set")
233
+ except Exception as e:
234
+ logging.warning(f"HF token save failed: {e}")
235
 
236
+ if "det" not in models_cache:
237
+ try:
238
+ models_cache["det"] = load_yolo_model()
239
+ logging.info("✅ YOLO loaded (CPU; CUDA masked in main)")
240
+ except Exception as e:
241
+ logging.error(f"YOLO load failed: {e}")
242
 
243
+ if "seg" not in models_cache:
244
+ try:
245
+ if os.path.exists(SEG_MODEL_PATH):
246
+ models_cache["seg"] = load_segmentation_model()
247
+ m = models_cache["seg"]
248
+ ishape = getattr(m, "input_shape", None)
249
+ oshape = getattr(m, "output_shape", None)
250
+ logging.info(f"✅ Segmentation model loaded (CPU) | input_shape={ishape} output_shape={oshape}")
251
+ else:
252
+ models_cache["seg"] = None
253
+ logging.warning("Segmentation model file missing; skipping.")
254
+ except Exception as e:
255
+ models_cache["seg"] = None
256
+ logging.warning(f"Segmentation unavailable: {e}")
257
 
258
+ if "cls" not in models_cache:
259
+ try:
260
+ models_cache["cls"] = load_classification_pipeline()
261
+ logging.info("✅ Classifier loaded (CPU)")
262
+ except Exception as e:
263
+ models_cache["cls"] = None
264
+ logging.warning(f"Classifier unavailable: {e}")
265
 
266
+ if "embedding_model" not in models_cache:
267
+ try:
268
+ models_cache["embedding_model"] = load_embedding_model()
269
+ logging.info("✅ Embeddings loaded (CPU)")
270
+ except Exception as e:
271
+ models_cache["embedding_model"] = None
272
+ logging.warning(f"Embeddings unavailable: {e}")
273
 
274
+ def setup_knowledge_base() -> None:
275
+ if "vector_store" in knowledge_base_cache:
276
+ return
277
+ docs: List = []
278
+ try:
279
+ PyPDFLoader = _import_langchain_pdf()
280
+ for pdf in GUIDELINE_PDFS:
281
+ if os.path.exists(pdf):
282
+ try:
283
+ docs.extend(PyPDFLoader(pdf).load())
284
+ logging.info(f"Loaded PDF: {pdf}")
285
+ except Exception as e:
286
+ logging.warning(f"PDF load failed ({pdf}): {e}")
287
+ except Exception as e:
288
+ logging.warning(f"LangChain PDF loader unavailable: {e}")
289
 
290
+ if docs and models_cache.get("embedding_model"):
291
+ try:
292
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
293
+ FAISS = _import_langchain_faiss()
294
+ chunks = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100).split_documents(docs)
295
+ knowledge_base_cache["vector_store"] = FAISS.from_documents(chunks, models_cache["embedding_model"])
296
+ logging.info(f"✅ Knowledge base ready ({len(chunks)} chunks)")
297
+ except Exception as e:
298
+ knowledge_base_cache["vector_store"] = None
299
+ logging.warning(f"KB build failed: {e}")
300
+ else:
301
+ knowledge_base_cache["vector_store"] = None
302
+ logging.warning("KB disabled (no docs or embeddings).")
303
+
304
+ initialize_cpu_models()
305
+ setup_knowledge_base()
306
+
307
+ # ---------- Calibration helpers ----------
308
+ def _exif_to_dict(pil_img: Image.Image) -> Dict[str, object]:
309
+ out = {}
310
+ try:
311
+ exif = pil_img.getexif()
312
+ if not exif:
313
+ return out
314
+ for k, v in exif.items():
315
+ tag = TAGS.get(k, k)
316
+ out[tag] = v
317
+ except Exception:
318
+ pass
319
+ return out
320
 
321
+ def _to_float(val) -> Optional[float]:
322
+ try:
323
+ if val is None:
324
+ return None
325
+ if isinstance(val, tuple) and len(val) == 2:
326
+ num, den = float(val[0]), float(val[1]) if float(val[1]) != 0 else 1.0
327
+ return num / den
328
+ return float(val)
329
+ except Exception:
330
+ return None
331
 
332
+ def _estimate_sensor_width_mm(f_mm: Optional[float], f35: Optional[float]) -> Optional[float]:
333
+ if f_mm and f35 and f35 > 0:
334
+ return 36.0 * f_mm / f35
335
+ return None
336
+
337
+ def estimate_px_per_cm_from_exif(pil_img: Image.Image, default_px_per_cm: float = DEFAULT_PX_PER_CM) -> Tuple[float, Dict]:
338
+ meta = {"used": "default", "f_mm": None, "f35": None, "sensor_w_mm": None, "distance_m": None}
339
+ try:
340
+ exif = _exif_to_dict(pil_img)
341
+ f_mm = _to_float(exif.get("FocalLength"))
342
+ f35 = _to_float(exif.get("FocalLengthIn35mmFilm") or exif.get("FocalLengthIn35mm"))
343
+ subj_dist_m = _to_float(exif.get("SubjectDistance"))
344
+ sensor_w_mm = _estimate_sensor_width_mm(f_mm, f35)
345
+ meta.update({"f_mm": f_mm, "f35": f35, "sensor_w_mm": sensor_w_mm, "distance_m": subj_dist_m})
346
+
347
+ if f_mm and sensor_w_mm and subj_dist_m and subj_dist_m > 0:
348
+ w_px = pil_img.width
349
+ field_w_mm = sensor_w_mm * (subj_dist_m * 1000.0) / f_mm
350
+ field_w_cm = field_w_mm / 10.0
351
+ px_per_cm = w_px / max(field_w_cm, 1e-6)
352
+ px_per_cm = float(np.clip(px_per_cm, PX_PER_CM_MIN, PX_PER_CM_MAX))
353
+ meta["used"] = "exif"
354
+ return px_per_cm, meta
355
+ return float(default_px_per_cm), meta
356
+ except Exception:
357
+ return float(default_px_per_cm), meta
358
+
359
+ # ---------- Segmentation helpers ----------
360
+ def _imagenet_norm(arr: np.ndarray) -> np.ndarray:
361
+ mean = np.array([123.675, 116.28, 103.53], dtype=np.float32)
362
+ std = np.array([58.395, 57.12, 57.375], dtype=np.float32)
363
+ return (arr.astype(np.float32) - mean) / std
364
+
365
+ def _preprocess_for_seg(bgr_roi: np.ndarray, target_hw: Tuple[int, int]) -> np.ndarray:
366
+ H, W = target_hw
367
+ resized = cv2.resize(bgr_roi, (W, H), interpolation=cv2.INTER_LINEAR)
368
+ if SEG_EXPECTS_RGB:
369
+ resized = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
370
+ if SEG_NORM.lower() == "imagenet":
371
+ x = _imagenet_norm(resized)
372
+ else:
373
+ x = resized.astype(np.float32) / 255.0
374
+ x = np.expand_dims(x, axis=0) # (1,H,W,3)
375
+ return x
376
+
377
+ def _to_prob(pred: np.ndarray) -> np.ndarray:
378
+ p = np.squeeze(pred)
379
+ pmin, pmax = float(p.min()), float(p.max())
380
+ if pmax > 1.0 or pmin < 0.0:
381
+ p = 1.0 / (1.0 + np.exp(-p))
382
+ return p.astype(np.float32)
383
+
384
+ # ---- Adaptive threshold + GrabCut grow ----
385
+ def _adaptive_prob_threshold(p: np.ndarray) -> float:
386
+ """
387
+ Choose a threshold that avoids tiny blobs while not swallowing skin.
388
+ Try Otsu and the 90th percentile, clamp to [0.25, 0.65], pick by area heuristic.
389
+ """
390
+ p01 = np.clip(p.astype(np.float32), 0, 1)
391
+ p255 = (p01 * 255).astype(np.uint8)
392
+
393
+ ret_otsu, _ = cv2.threshold(p255, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
394
+ thr_otsu = float(np.clip(ret_otsu / 255.0, 0.25, 0.65))
395
+ thr_pctl = float(np.clip(np.percentile(p01, 90), 0.25, 0.65))
396
+
397
+ def area_frac(thr: float) -> float:
398
+ return float((p01 >= thr).sum()) / float(p01.size)
399
+
400
+ af_otsu = area_frac(thr_otsu)
401
+ af_pctl = area_frac(thr_pctl)
402
+
403
+ def score(af: float) -> float:
404
+ target_low, target_high = 0.03, 0.10
405
+ if af < target_low: return abs(af - target_low) * 3.0
406
+ if af > target_high: return abs(af - target_high) * 1.5
407
+ return 0.0
408
+
409
+ return thr_otsu if score(af_otsu) <= score(af_pctl) else thr_pctl
410
+
411
+ def _grabcut_refine(bgr: np.ndarray, seed01: np.ndarray, iters: int = 3) -> np.ndarray:
412
+ """Grow from a confident core into low-contrast margins."""
413
+ h, w = bgr.shape[:2]
414
+ gc = np.full((h, w), cv2.GC_PR_BGD, np.uint8)
415
+ k = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
416
+ seed_dil = cv2.dilate(seed01, k, iterations=1)
417
+ gc[seed01.astype(bool)] = cv2.GC_PR_FGD
418
+ gc[seed_dil.astype(bool)] = cv2.GC_FGD
419
+ gc[0, :], gc[-1, :], gc[:, 0], gc[:, 1] = cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD, cv2.GC_BGD
420
+ bgdModel = np.zeros((1, 65), np.float64)
421
+ fgdModel = np.zeros((1, 65), np.float64)
422
+ cv2.grabCut(bgr, gc, None, bgdModel, fgdModel, iters, cv2.GC_INIT_WITH_MASK)
423
+ return np.where((gc == cv2.GC_FGD) | (gc == cv2.GC_PR_FGD), 1, 0).astype(np.uint8)
424
 
425
  def _fill_holes(mask01: np.ndarray) -> np.ndarray:
426
  h, w = mask01.shape[:2]
 
432
  return out.astype(np.uint8)
433
 
434
  def _clean_mask(mask01: np.ndarray) -> np.ndarray:
435
+ """Open → Close → Fill holes → Largest component (no dilation)."""
436
  mask01 = (mask01 > 0).astype(np.uint8)
437
  k3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
438
  k5 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
439
  mask01 = cv2.morphologyEx(mask01, cv2.MORPH_OPEN, k3, iterations=1)
440
  mask01 = cv2.morphologyEx(mask01, cv2.MORPH_CLOSE, k5, iterations=1)
441
  mask01 = _fill_holes(mask01)
442
+ # Keep largest component only
443
+ num, labels, stats, _ = cv2.connectedComponentsWithStats(mask01, 8)
444
+ if num > 1:
445
+ areas = stats[1:, cv2.CC_STAT_AREA]
446
+ if areas.size:
447
+ largest_idx = 1 + int(np.argmax(areas))
448
+ mask01 = (labels == largest_idx).astype(np.uint8)
449
  return (mask01 > 0).astype(np.uint8)
450
 
451
+ # Global last debug dict (per-process)
452
+ _last_seg_debug: Dict[str, object] = {}
453
+
454
+ def segment_wound(image_bgr: np.ndarray, ts: str, out_dir: str) -> Tuple[np.ndarray, Dict[str, object]]:
455
+ """
456
+ TF model → adaptive threshold on prob → GrabCut grow → cleanup.
457
+ Fallback: KMeans-Lab.
458
+ Returns (mask_uint8_0_255, debug_dict)
459
+ """
460
+ debug = {"used": None, "reason": None, "positive_fraction": 0.0,
461
+ "thr": None, "heatmap_path": None, "roi_seen_by_model": None}
462
+
463
+ seg_model = models_cache.get("seg", None)
464
+
465
+ # --- Model path ---
466
+ if seg_model is not None:
467
+ try:
468
+ ishape = getattr(seg_model, "input_shape", None)
469
+ if not ishape or len(ishape) < 4:
470
+ raise ValueError(f"Bad seg input_shape: {ishape}")
471
+ th, tw = int(ishape[1]), int(ishape[2])
472
+
473
+ x = _preprocess_for_seg(image_bgr, (th, tw))
474
+ roi_seen_path = None
475
+ if SMARTHEAL_DEBUG:
476
+ roi_seen_path = os.path.join(out_dir, f"roi_for_seg_{ts}.png")
477
+ cv2.imwrite(roi_seen_path, image_bgr)
478
+
479
+ pred = seg_model.predict(x, verbose=0)
480
+ if isinstance(pred, (list, tuple)): pred = pred[0]
481
+ p = _to_prob(pred)
482
+ p = cv2.resize(p, (image_bgr.shape[1], image_bgr.shape[0]), interpolation=cv2.INTER_LINEAR)
483
+
484
+ heatmap_path = None
485
+ if SMARTHEAL_DEBUG:
486
+ hm = (np.clip(p, 0, 1) * 255).astype(np.uint8)
487
+ heat = cv2.applyColorMap(hm, cv2.COLORMAP_JET)
488
+ heatmap_path = os.path.join(out_dir, f"seg_pred_heatmap_{ts}.png")
489
+ cv2.imwrite(heatmap_path, heat)
490
+
491
+ thr = _adaptive_prob_threshold(p)
492
+ core01 = (p >= thr).astype(np.uint8)
493
+ core_frac = float(core01.sum()) / float(core01.size)
494
+
495
+ if core_frac < 0.005:
496
+ thr2 = max(thr - 0.10, 0.15)
497
+ core01 = (p >= thr2).astype(np.uint8)
498
+ thr = thr2
499
+ core_frac = float(core01.sum()) / float(core01.size)
500
+
501
+ if core01.any():
502
+ gc01 = _grabcut_refine(image_bgr, core01, iters=3)
503
+ mask01 = _clean_mask(gc01)
504
+ else:
505
+ mask01 = np.zeros(core01.shape, np.uint8)
506
+
507
+ pos_frac = float(mask01.sum()) / float(mask01.size)
508
+ logging.info(f"SegModel USED | thr={float(thr):.2f} core_frac={core_frac:.4f} final_frac={pos_frac:.4f}")
509
+
510
+ debug.update({
511
+ "used": "tf_model",
512
+ "reason": "ok",
513
+ "positive_fraction": pos_frac,
514
+ "thr": float(thr),
515
+ "heatmap_path": heatmap_path,
516
+ "roi_seen_by_model": roi_seen_path
517
+ })
518
+ return (mask01 * 255).astype(np.uint8), debug
519
+
520
+ except Exception as e:
521
+ logging.warning(f"⚠️ Segmentation model failed → fallback. Reason: {e}")
522
+ debug.update({"used": "fallback_kmeans", "reason": f"model_failed: {e}"})
523
 
524
+ # --- Fallback: KMeans in Lab (reddest cluster as wound) ---
525
+ Z = image_bgr.reshape((-1, 3)).astype(np.float32)
 
526
  criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
527
  _, labels, centers = cv2.kmeans(Z, 2, None, criteria, 5, cv2.KMEANS_PP_CENTERS)
528
  centers_u8 = centers.astype(np.uint8).reshape(1, 2, 3)
529
  centers_lab = cv2.cvtColor(centers_u8, cv2.COLOR_BGR2LAB)[0]
530
+ wound_idx = int(np.argmax(centers_lab[:, 1])) # maximize a* (red)
531
+ mask01 = (labels.reshape(image_bgr.shape[:2]) == wound_idx).astype(np.uint8)
532
+ mask01 = _clean_mask(mask01)
533
+
534
+ pos_frac = float(mask01.sum()) / float(mask01.size)
535
+ logging.info(f"KMeans USED | final_frac={pos_frac:.4f}")
536
+
537
+ debug.update({
538
+ "used": "fallback_kmeans",
539
+ "reason": debug.get("reason") or "no_model",
540
+ "positive_fraction": pos_frac,
541
+ "thr": None
542
+ })
543
+ return (mask01 * 255).astype(np.uint8), debug
544
+
545
+ # ---------- Measurement + overlay helpers ----------
546
+ def largest_component_mask(binary01: np.ndarray, min_area_px: int = 50) -> np.ndarray:
547
+ num, labels, stats, _ = cv2.connectedComponentsWithStats(binary01.astype(np.uint8), connectivity=8)
548
+ if num <= 1:
549
+ return binary01.astype(np.uint8)
550
+ areas = stats[1:, cv2.CC_STAT_AREA]
551
+ if areas.size == 0 or areas.max() < min_area_px:
552
+ return binary01.astype(np.uint8)
553
+ largest_idx = 1 + int(np.argmax(areas))
554
+ return (labels == largest_idx).astype(np.uint8)
555
+
556
+ def measure_min_area_rect(mask01: np.ndarray, px_per_cm: float) -> Tuple[float, float, Tuple]:
557
+ contours, _ = cv2.findContours(mask01.astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
558
+ if not contours:
559
+ return 0.0, 0.0, (None, None)
560
+ cnt = max(contours, key=cv2.contourArea)
561
+ rect = cv2.minAreaRect(cnt)
562
+ (w_px, h_px) = rect[1]
563
+ length_px, breadth_px = (max(w_px, h_px), min(w_px, h_px))
564
+ length_cm = round(length_px / max(px_per_cm, 1e-6), 2)
565
+ breadth_cm = round(breadth_px / max(px_per_cm, 1e-6), 2)
566
+ box = cv2.boxPoints(rect).astype(int)
567
+ return length_cm, breadth_cm, (box, rect[0])
568
+
569
+ def area_cm2_from_contour(mask01: np.ndarray, px_per_cm: float) -> Tuple[float, Optional[np.ndarray]]:
570
+ """Area from largest polygon (sub-pixel); returns (area_cm2, contour)."""
571
+ m = (mask01 > 0).astype(np.uint8)
572
+ contours, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
573
+ if not contours:
574
+ return 0.0, None
575
+ cnt = max(contours, key=cv2.contourArea)
576
+ poly_area_px2 = float(cv2.contourArea(cnt))
577
+ area_cm2 = round(poly_area_px2 / (max(px_per_cm, 1e-6) ** 2), 2)
578
+ return area_cm2, cnt
579
+
580
+ def clamp_area_with_minrect(cnt: np.ndarray, px_per_cm: float, area_cm2_poly: float) -> float:
581
+ rect = cv2.minAreaRect(cnt)
582
+ (w_px, h_px) = rect[1]
583
+ rect_area_px2 = float(max(w_px, 0.0) * max(h_px, 0.0))
584
+ rect_area_cm2 = rect_area_px2 / (max(px_per_cm, 1e-6) ** 2)
585
+ return round(min(area_cm2_poly, rect_area_cm2 * 1.05), 2)
586
+
587
+ def draw_measurement_overlay(
588
+ base_bgr: np.ndarray,
589
+ mask01: np.ndarray,
590
+ rect_box: np.ndarray,
591
+ length_cm: float,
592
+ breadth_cm: float,
593
+ thickness: int = 2
594
+ ) -> np.ndarray:
595
+ """
596
+ 1) Strong red mask overlay + white contour
597
+ 2) Min-area rectangle
598
+ 3) Double-headed arrows labeled Length/Width
599
+ """
600
+ overlay = base_bgr.copy()
601
+
602
+ # Mask tint
603
+ mask255 = (mask01 * 255).astype(np.uint8)
604
+ mask3 = cv2.merge([mask255, mask255, mask255])
605
+ red = np.zeros_like(overlay); red[:] = (0, 0, 255)
606
+ alpha = 0.55
607
+ tinted = cv2.addWeighted(overlay, 1 - alpha, red, alpha, 0)
608
+ overlay = np.where(mask3 > 0, tinted, overlay)
609
+
610
+ # Contour
611
+ cnts, _ = cv2.findContours(mask255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
612
+ if cnts:
613
+ cv2.drawContours(overlay, cnts, -1, (255, 255, 255), 2)
614
+
615
+ if rect_box is not None:
616
+ cv2.polylines(overlay, [rect_box], True, (255, 255, 255), thickness)
617
+ pts = rect_box.reshape(-1, 2)
618
+
619
+ def midpoint(a, b): return (int((a[0] + b[0]) / 2), int((a[1] + b[1]) / 2))
620
+ e = [np.linalg.norm(pts[i] - pts[(i + 1) % 4]) for i in range(4)]
621
+ long_edge_idx = int(np.argmax(e))
622
+ mids = [midpoint(pts[i], pts[(i + 1) % 4]) for i in range(4)]
623
+ long_pair = (long_edge_idx, (long_edge_idx + 2) % 4)
624
+ short_pair = ((long_edge_idx + 1) % 4, (long_edge_idx + 3) % 4)
625
+
626
+ def draw_double_arrow(img, p1, p2):
627
+ cv2.arrowedLine(img, p1, p2, (0, 0, 0), thickness + 2, tipLength=0.05)
628
+ cv2.arrowedLine(img, p2, p1, (0, 0, 0), thickness + 2, tipLength=0.05)
629
+ cv2.arrowedLine(img, p1, p2, (255, 255, 255), thickness, tipLength=0.05)
630
+ cv2.arrowedLine(img, p2, p1, (255, 255, 255), thickness, tipLength=0.05)
631
+
632
+ def put_label(text, anchor):
633
+ org = (anchor[0] + 6, anchor[1] - 6)
634
+ cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 4, cv2.LINE_AA)
635
+ cv2.putText(overlay, text, org, cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
636
+
637
+ draw_double_arrow(overlay, mids[long_pair[0]], mids[long_pair[1]])
638
+ draw_double_arrow(overlay, mids[short_pair[0]], mids[short_pair[1]])
639
+ put_label(f"Length: {length_cm:.2f} cm", mids[long_pair[0]])
640
+ put_label(f"Width: {breadth_cm:.2f} cm", mids[short_pair[0]])
641
+
642
+ return overlay
643
+
644
+ # ---------- AI PROCESSOR ----------
645
  class AIProcessor:
646
  def __init__(self):
647
+ self.models_cache = models_cache
648
+ self.knowledge_base_cache = knowledge_base_cache
649
+ self.uploads_dir = UPLOADS_DIR
650
+ self.dataset_id = DATASET_ID
651
+ self.hf_token = HF_TOKEN
652
+
653
+ def _ensure_analysis_dir(self) -> str:
654
+ out_dir = os.path.join(self.uploads_dir, "analysis")
655
+ os.makedirs(out_dir, exist_ok=True)
656
+ return out_dir
657
+
658
+ def perform_visual_analysis(self, image_pil: Image.Image) -> Dict:
659
+ """
660
+ YOLO detect → crop ROI → segment_wound(ROI) → clean mask →
661
+ minAreaRect measurement (cm) using EXIF px/cm → save outputs.
662
+ """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
663
  try:
664
+ px_per_cm, exif_meta = estimate_px_per_cm_from_exif(image_pil, DEFAULT_PX_PER_CM)
665
+ # Guardrails for calibration to avoid huge area blow-ups
666
+ px_per_cm = float(np.clip(px_per_cm, 20.0, 350.0))
667
+ if (exif_meta or {}).get("used") != "exif":
668
+ logging.warning(f"Calibration fallback used: px_per_cm={px_per_cm:.2f} (default). Prefer ruler/Aruco for accuracy.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
669
 
 
 
 
 
670
  image_cv = cv2.cvtColor(np.array(image_pil.convert("RGB")), cv2.COLOR_RGB2BGR)
671
 
672
+ # --- Detection ---
673
+ det_model = self.models_cache.get("det")
674
+ if det_model is None:
675
+ raise RuntimeError("YOLO model not loaded")
676
+ # Force CPU inference and avoid CUDA touch
677
+ results = det_model.predict(image_cv, verbose=False, device="cpu")
678
  if (not results) or (not getattr(results[0], "boxes", None)) or (len(results[0].boxes) == 0):
679
+ try:
680
+ import gradio as gr
681
+ raise gr.Error("No wound could be detected.")
682
+ except Exception:
683
+ raise RuntimeError("No wound could be detected.")
684
 
685
+ box = results[0].boxes[0].xyxy[0].cpu().numpy().astype(int)
686
  x1, y1, x2, y2 = [int(v) for v in box]
687
  x1, y1 = max(0, x1), max(0, y1)
688
  x2, y2 = min(image_cv.shape[1], x2), min(image_cv.shape[0], y2)
689
+ roi = image_cv[y1:y2, x1:x2].copy()
690
+ if roi.size == 0:
691
+ try:
692
+ import gradio as gr
693
+ raise gr.Error("Detected ROI is empty.")
694
+ except Exception:
695
+ raise RuntimeError("Detected ROI is empty.")
696
 
697
+ out_dir = self._ensure_analysis_dir()
 
698
  ts = datetime.now().strftime("%Y%m%d_%H%M%S")
699
+
700
+ # --- Segmentation (model-first + KMeans fallback) ---
701
+ mask_u8_255, seg_debug = segment_wound(roi, ts, out_dir)
702
+ mask01 = (mask_u8_255 > 127).astype(np.uint8)
703
+
704
+ if mask01.any():
705
+ mask01 = _clean_mask(mask01)
706
+ logging.debug(f"Mask postproc: px_after={int(mask01.sum())}")
707
+
708
+ # --- Measurement (accurate & conservative) ---
709
+ if mask01.any():
710
+ length_cm, breadth_cm, (box_pts, _) = measure_min_area_rect(mask01, px_per_cm)
711
+ area_poly_cm2, largest_cnt = area_cm2_from_contour(mask01, px_per_cm)
712
+ if largest_cnt is not None:
713
+ surface_area_cm2 = clamp_area_with_minrect(largest_cnt, px_per_cm, area_poly_cm2)
714
+ else:
715
+ surface_area_cm2 = area_poly_cm2
716
+
717
+ anno_roi = draw_measurement_overlay(roi, mask01, box_pts, length_cm, breadth_cm)
718
+ segmentation_empty = False
719
+ else:
720
+ # Fallback if seg failed: use ROI dimensions
721
+ h_px = max(0, y2 - y1); w_px = max(0, x2 - x1)
722
+ length_cm = round(max(h_px, w_px) / px_per_cm, 2)
723
+ breadth_cm = round(min(h_px, w_px) / px_per_cm, 2)
724
+ surface_area_cm2 = round((h_px * w_px) / (px_per_cm ** 2), 2)
725
+ anno_roi = roi.copy()
726
+ cv2.rectangle(anno_roi, (2, 2), (anno_roi.shape[1]-3, anno_roi.shape[0]-3), (0, 0, 255), 3)
727
+ cv2.line(anno_roi, (0, 0), (anno_roi.shape[1]-1, anno_roi.shape[0]-1), (0, 0, 255), 2)
728
+ cv2.line(anno_roi, (anno_roi.shape[1]-1, 0), (0, anno_roi.shape[0]-1), (0, 0, 255), 2)
729
+ box_pts = None
730
+ segmentation_empty = True
731
+
732
+ # --- Save visualizations ---
733
+ original_path = os.path.join(out_dir, f"original_{ts}.png")
734
+ cv2.imwrite(original_path, image_cv)
735
+
736
  det_vis = image_cv.copy()
737
  cv2.rectangle(det_vis, (x1, y1), (x2, y2), (0, 255, 0), 2)
738
+ detection_path = os.path.join(out_dir, f"detection_{ts}.png")
739
+ cv2.imwrite(detection_path, det_vis)
740
+
741
+ roi_mask_path = os.path.join(out_dir, f"roi_mask_{ts}.png")
742
+ cv2.imwrite(roi_mask_path, (mask01 * 255).astype(np.uint8))
743
+
744
+ # ROI overlay (mask tint + contour, without arrows)
745
+ mask255 = (mask01 * 255).astype(np.uint8)
746
+ mask3 = cv2.merge([mask255, mask255, mask255])
747
+ red = np.zeros_like(roi); red[:] = (0, 0, 255)
748
+ alpha = 0.55
749
+ tinted = cv2.addWeighted(roi, 1 - alpha, red, alpha, 0)
750
+ if mask255.any():
751
+ roi_overlay = np.where(mask3 > 0, tinted, roi)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
752
  cnts, _ = cv2.findContours(mask255, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
753
+ cv2.drawContours(roi_overlay, cnts, -1, (255, 255, 255), 2)
754
+ else:
755
+ roi_overlay = anno_roi
756
+
 
 
 
 
 
757
  seg_full = image_cv.copy()
758
+ seg_full[y1:y2, x1:x2] = roi_overlay
759
+ segmentation_path = os.path.join(out_dir, f"segmentation_{ts}.png")
760
+ cv2.imwrite(segmentation_path, seg_full)
761
+
762
+ segmentation_roi_path = os.path.join(out_dir, f"segmentation_roi_{ts}.png")
763
+ cv2.imwrite(segmentation_roi_path, roi_overlay)
764
+
765
+ # Annotated (mask + arrows + labels) in full-frame
766
+ anno_full = image_cv.copy()
767
+ anno_full[y1:y2, x1:x2] = anno_roi
768
+ annotated_seg_path = os.path.join(out_dir, f"segmentation_annotated_{ts}.png")
769
+ cv2.imwrite(annotated_seg_path, anno_full)
770
 
771
+ # --- Optional classification ---
772
  wound_type = "Unknown"
773
+ cls_pipe = self.models_cache.get("cls")
774
+ if cls_pipe is not None:
775
  try:
776
+ preds = cls_pipe(Image.fromarray(cv2.cvtColor(roi, cv2.COLOR_BGR2RGB)))
 
777
  if preds:
778
  wound_type = max(preds, key=lambda x: x.get("score", 0)).get("label", "Unknown")
779
  except Exception as e:
780
+ logging.warning(f"Classification failed: {e}")
781
+
782
+ # Log end-of-seg summary
783
+ seg_summary = {
784
+ "seg_used": seg_debug.get("used"),
785
+ "seg_reason": seg_debug.get("reason"),
786
+ "positive_fraction": round(float(seg_debug.get("positive_fraction", 0.0)), 6),
787
+ "threshold": seg_debug.get("thr"),
788
+ "segmentation_empty": segmentation_empty,
789
+ "exif_px_per_cm": round(px_per_cm, 3),
790
+ }
791
+ _log_kv("SEG_SUMMARY", seg_summary)
792
 
793
  return {
794
  "wound_type": wound_type,
795
+ "length_cm": length_cm,
796
+ "breadth_cm": breadth_cm,
797
+ "surface_area_cm2": surface_area_cm2,
798
+ "px_per_cm": round(px_per_cm, 2),
799
+ "calibration_meta": exif_meta,
800
+ "detection_confidence": float(results[0].boxes.conf[0].cpu().item())
801
+ if getattr(results[0].boxes, "conf", None) is not None else 0.0,
802
+ "detection_image_path": detection_path,
803
+ "segmentation_image_path": annotated_seg_path,
804
+ "segmentation_annotated_path": annotated_seg_path,
805
+ "segmentation_roi_path": segmentation_roi_path,
806
+ "roi_mask_path": roi_mask_path,
807
+ "segmentation_empty": segmentation_empty,
808
+ "segmentation_debug": seg_debug,
809
+ "original_image_path": original_path,
810
  }
 
811
  except Exception as e:
812
+ logging.error(f"Visual analysis failed: {e}", exc_info=True)
813
+ raise
814
 
815
+ # ---------- Knowledge base + reporting ----------
816
+ def query_guidelines(self, query: str) -> str:
 
817
  try:
818
+ vs = self.knowledge_base_cache.get("vector_store")
819
+ if not vs:
820
+ return "Knowledge base is not available."
821
+ retriever = vs.as_retriever(search_kwargs={"k": 5})
822
+ # Modern API (avoid get_relevant_documents deprecation)
823
+ docs = retriever.invoke(query)
824
+ lines: List[str] = []
 
 
 
 
 
 
 
 
825
  for d in docs:
826
+ src = (d.metadata or {}).get("source", "N/A")
827
+ txt = (d.page_content or "")[:300]
828
+ lines.append(f"Source: {src}\nContent: {txt}...")
829
+ return "\n\n".join(lines) if lines else "No relevant guideline snippets found."
 
 
830
  except Exception as e:
831
+ logging.warning(f"Guidelines query failed: {e}")
832
+ return f"Guidelines query failed: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
833
 
834
+ def _generate_fallback_report(self, patient_info: str, visual_results: Dict, guideline_context: str) -> str:
835
+ return f"""# 🩺 SmartHeal AI - Comprehensive Wound Analysis Report
836
 
837
+ ## 📋 Patient Information
838
  {patient_info}
839
 
840
+ ## 🔍 Visual Analysis Results
841
+ - **Wound Type**: {visual_results.get('wound_type', 'Unknown')}
842
+ - **Dimensions**: {visual_results.get('length_cm', 0)} cm × {visual_results.get('breadth_cm', 0)} cm
843
+ - **Surface Area**: {visual_results.get('surface_area_cm2', 0)} cm²
844
+ - **Detection Confidence**: {visual_results.get('detection_confidence', 0):.1%}
845
+ - **Calibration**: {visual_results.get('px_per_cm','?')} px/cm ({(visual_results.get('calibration_meta') or {}).get('used','default')})
846
+
847
+ ## 📊 Analysis Images
848
+ - **Original**: {visual_results.get('original_image_path', 'N/A')}
849
+ - **Detection**: {visual_results.get('detection_image_path', 'N/A')}
850
+ - **Segmentation**: {visual_results.get('segmentation_image_path', 'N/A')}
851
+ - **Annotated**: {visual_results.get('segmentation_annotated_path', 'N/A')}
852
+
853
+ ## 🎯 Clinical Summary
854
+ Automated analysis provides quantitative measurements; verify via clinical examination.
855
+
856
+ ## 💊 Recommendations
857
+ - Cleanse wound gently; select dressing per exudate/infection risk
858
+ - Debride necrotic tissue if indicated (clinical decision)
859
+ - Document with serial photos and measurements
860
+
861
+ ## 📅 Monitoring
862
+ - Daily in week 1, then every 2–3 days (or as indicated)
863
+ - Weekly progress review
864
+
865
+ ## 📚 Guideline Context
866
+ {(guideline_context or '')[:800]}{"..." if guideline_context and len(guideline_context) > 800 else ''}
867
+
868
+ **Disclaimer:** Automated, for decision support only. Verify clinically.
869
+ """
870
+
871
+ def generate_final_report(
872
+ self,
873
+ patient_info: str,
874
+ visual_results: Dict,
875
+ guideline_context: str,
876
+ image_pil: Image.Image,
877
+ max_new_tokens: Optional[int] = None,
878
+ ) -> str:
879
+ try:
880
+ report = generate_medgemma_report(
881
+ patient_info, visual_results, guideline_context, image_pil, max_new_tokens
882
  )
883
+ if report and report.strip() and not report.startswith(("⚠️", "❌")):
884
+ return report
885
+ logging.warning("VLM unavailable/invalid; using fallback.")
 
 
 
 
 
 
 
886
  return self._generate_fallback_report(patient_info, visual_results, guideline_context)
887
  except Exception as e:
888
+ logging.error(f"Report generation failed: {e}")
889
  return self._generate_fallback_report(patient_info, visual_results, guideline_context)
890
 
891
+ def save_and_commit_image(self, image_pil: Image.Image) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892
  try:
893
+ os.makedirs(self.uploads_dir, exist_ok=True)
894
+ ts = datetime.now().strftime("%Y%m%d_%H%M%S")
895
+ filename = f"{ts}.png"
896
+ path = os.path.join(self.uploads_dir, filename)
897
+ image_pil.convert("RGB").save(path)
898
+ logging.info(f"✅ Image saved locally: {path}")
899
 
900
+ if HF_TOKEN and DATASET_ID:
901
  try:
902
+ HfApi, HfFolder = _import_hf_hub()
903
+ HfFolder.save_token(HF_TOKEN)
904
  api = HfApi()
905
  api.upload_file(
906
+ path_or_fileobj=path,
907
  path_in_repo=f"images/{filename}",
908
+ repo_id=DATASET_ID,
909
  repo_type="dataset",
910
+ token=HF_TOKEN,
911
  commit_message=f"Upload wound image: {filename}",
912
  )
913
  logging.info("✅ Image committed to HF dataset")
914
  except Exception as e:
915
  logging.warning(f"HF upload failed: {e}")
916
 
917
+ return path
918
  except Exception as e:
919
+ logging.error(f"Failed to save/commit image: {e}")
920
+ return ""
921
 
922
+ def full_analysis_pipeline(self, image_pil: Image.Image, questionnaire_data: Dict) -> Dict:
 
 
 
 
923
  try:
924
+ saved_path = self.save_and_commit_image(image_pil)
925
+ visual_results = self.perform_visual_analysis(image_pil)
926
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
927
  pi = questionnaire_data or {}
928
+ patient_info = (
929
+ f"Age: {pi.get('age','N/A')}, "
930
+ f"Diabetic: {pi.get('diabetic','N/A')}, "
931
+ f"Allergies: {pi.get('allergies','N/A')}, "
932
+ f"Date of Wound: {pi.get('date_of_injury','N/A')}, "
933
+ f"Professional Care: {pi.get('professional_care','N/A')}, "
934
+ f"Oozing/Bleeding: {pi.get('oozing_bleeding','N/A')}, "
935
+ f"Infection: {pi.get('infection','N/A')}, "
936
+ f"Moisture: {pi.get('moisture','N/A')}"
937
+ )
938
+
939
+ query = (
940
+ f"best practices for managing a {visual_results.get('wound_type','Unknown')} "
941
+ f"with moisture '{pi.get('moisture','unknown')}' and infection '{pi.get('infection','unknown')}' "
942
+ f"in a diabetic status '{pi.get('diabetic','unknown')}'"
943
+ )
944
  guideline_context = self.query_guidelines(query)
945
 
946
+ report = self.generate_final_report(patient_info, visual_results, guideline_context, image_pil)
 
947
 
948
  return {
949
  "success": True,
950
  "visual_analysis": visual_results,
951
+ "report": report,
952
  "saved_image_path": saved_path,
953
+ "guideline_context": (guideline_context or "")[:500] + (
954
+ "..." if guideline_context and len(guideline_context) > 500 else ""
955
+ ),
956
  }
 
957
  except Exception as e:
958
+ logging.error(f"Pipeline error: {e}")
959
  return {
960
  "success": False,
961
  "error": str(e),
962
+ "visual_analysis": {},
963
+ "report": f"Analysis failed: {str(e)}",
964
+ "saved_image_path": None,
965
+ "guideline_context": "",
966
  }
967
 
968
+ def analyze_wound(self, image, questionnaire_data: Dict) -> Dict:
 
 
969
  try:
970
  if isinstance(image, str):
971
+ if not os.path.exists(image):
972
+ raise ValueError(f"Image file not found: {image}")
973
+ image_pil = Image.open(image)
974
+ elif isinstance(image, Image.Image):
975
+ image_pil = image
976
+ elif isinstance(image, np.ndarray):
977
+ image_pil = Image.fromarray(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
978
  else:
979
+ raise ValueError(f"Unsupported image type: {type(image)}")
 
 
 
 
 
 
 
 
980
 
981
+ return self.full_analysis_pipeline(image_pil, questionnaire_data or {})
982
  except Exception as e:
983
+ logging.error(f"Wound analysis error: {e}")
984
  return {
985
+ "success": False,
986
+ "error": str(e),
987
+ "visual_analysis": {},
988
+ "report": f"Analysis initialization failed: {str(e)}",
989
+ "saved_image_path": None,
990
+ "guideline_context": "",
991
+ }