Commit Β·
0406f85
1
Parent(s): 4742baa
Fix ultralytics model loading with CPU device and error handling
Browse files- backend/inference.py +108 -46
backend/inference.py
CHANGED
|
@@ -8,9 +8,13 @@ from ultralytics import YOLO
|
|
| 8 |
|
| 9 |
# MODEL LOAD (Safe Backend Path)
|
| 10 |
AW_MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "AW_yolo.pt")
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# CONFIGURABLE PARAMETERS
|
| 16 |
MIN_AREA = 150 # minimum contour area (px)
|
|
@@ -30,54 +34,87 @@ def infer_aw_contour(frame, conf_threshold=DEFAULT_CONF):
|
|
| 30 |
"frame_height": 0
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
overlay = frame.copy()
|
| 41 |
contours_list = []
|
| 42 |
detection_count = 0
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
return {
|
| 83 |
"overlay": overlay,
|
|
@@ -96,10 +133,14 @@ from ultralytics import YOLO
|
|
| 96 |
from collections import deque
|
| 97 |
|
| 98 |
cervix_MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "cervix_yolo.pt")
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Stability buffer for video
|
| 105 |
detect_history = deque(maxlen=10)
|
|
@@ -142,14 +183,35 @@ def analyze_frame(frame, conf_threshold=0.3):
|
|
| 142 |
"quality_percent": 0
|
| 143 |
}
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
r = results[0]
|
| 155 |
|
|
|
|
| 8 |
|
| 9 |
# MODEL LOAD (Safe Backend Path)
|
| 10 |
AW_MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "AW_yolo.pt")
|
| 11 |
+
try:
|
| 12 |
+
aw_model = YOLO(AW_MODEL_PATH)
|
| 13 |
+
aw_model.to('cpu') # Force CPU device
|
| 14 |
+
print("β
Acetowhite model loaded from:", AW_MODEL_PATH)
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"β Error loading Acetowhite model: {e}")
|
| 17 |
+
aw_model = None
|
| 18 |
|
| 19 |
# CONFIGURABLE PARAMETERS
|
| 20 |
MIN_AREA = 150 # minimum contour area (px)
|
|
|
|
| 34 |
"frame_height": 0
|
| 35 |
}
|
| 36 |
|
| 37 |
+
if aw_model is None:
|
| 38 |
+
print("β Acetowhite model not loaded")
|
| 39 |
+
return {
|
| 40 |
+
"overlay": None,
|
| 41 |
+
"contours": [],
|
| 42 |
+
"detections": 0,
|
| 43 |
+
"frame_width": frame.shape[1],
|
| 44 |
+
"frame_height": frame.shape[0]
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
print(f"π Running YOLO prediction on frame shape: {frame.shape}")
|
| 49 |
+
results = aw_model.predict(
|
| 50 |
+
frame,
|
| 51 |
+
conf=conf_threshold,
|
| 52 |
+
imgsz=IMG_SIZE,
|
| 53 |
+
verbose=False,
|
| 54 |
+
device='cpu'
|
| 55 |
+
)[0]
|
| 56 |
+
print(f"β
YOLO prediction complete")
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"β YOLO prediction error: {e}")
|
| 59 |
+
import traceback
|
| 60 |
+
traceback.print_exc()
|
| 61 |
+
return {
|
| 62 |
+
"overlay": None,
|
| 63 |
+
"contours": [],
|
| 64 |
+
"detections": 0,
|
| 65 |
+
"frame_width": frame.shape[1],
|
| 66 |
+
"frame_height": frame.shape[0]
|
| 67 |
+
}
|
| 68 |
|
| 69 |
overlay = frame.copy()
|
| 70 |
contours_list = []
|
| 71 |
detection_count = 0
|
| 72 |
|
| 73 |
+
try:
|
| 74 |
+
if results.masks is not None and len(results.masks.xy) > 0:
|
| 75 |
+
print(f"β
Found masks: {len(results.masks.xy)}")
|
| 76 |
|
| 77 |
+
for idx, polygon in enumerate(results.masks.xy):
|
| 78 |
|
| 79 |
+
confidence = float(results.boxes.conf[idx])
|
| 80 |
|
| 81 |
+
# Skip low-confidence masks (extra safety layer)
|
| 82 |
+
if confidence < conf_threshold:
|
| 83 |
+
continue
|
| 84 |
|
| 85 |
+
contour = polygon.astype(np.int32)
|
| 86 |
|
| 87 |
+
area = cv2.contourArea(contour)
|
| 88 |
|
| 89 |
+
if area < MIN_AREA:
|
| 90 |
+
continue
|
| 91 |
|
| 92 |
+
# Optional smoothing
|
| 93 |
+
epsilon = SMOOTHING_EPSILON * cv2.arcLength(contour, True)
|
| 94 |
+
contour = cv2.approxPolyDP(contour, epsilon, True)
|
| 95 |
|
| 96 |
+
# Draw clean contour
|
| 97 |
+
cv2.polylines(
|
| 98 |
+
overlay,
|
| 99 |
+
[contour],
|
| 100 |
+
isClosed=True,
|
| 101 |
+
color=(0, 255, 0),
|
| 102 |
+
thickness=2
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
contours_list.append({
|
| 106 |
+
"points": contour.tolist(),
|
| 107 |
+
"area": float(area),
|
| 108 |
+
"confidence": round(confidence, 3)
|
| 109 |
+
})
|
| 110 |
|
| 111 |
+
detection_count += 1
|
| 112 |
+
else:
|
| 113 |
+
print("βΉοΈ No masks found in results")
|
| 114 |
+
except Exception as e:
|
| 115 |
+
print(f"β Error processing masks: {e}")
|
| 116 |
+
import traceback
|
| 117 |
+
traceback.print_exc()
|
| 118 |
|
| 119 |
return {
|
| 120 |
"overlay": overlay,
|
|
|
|
| 133 |
from collections import deque
|
| 134 |
|
| 135 |
cervix_MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "cervix_yolo.pt")
|
| 136 |
+
try:
|
| 137 |
+
cervix_model = YOLO(cervix_MODEL_PATH)
|
| 138 |
+
cervix_model.to('cpu') # Force CPU device
|
| 139 |
+
print(f"β
Cervix model loaded from: {cervix_MODEL_PATH}")
|
| 140 |
+
print(f"β
Classes: {cervix_model.model.names}")
|
| 141 |
+
except Exception as e:
|
| 142 |
+
print(f"β Error loading Cervix model: {e}")
|
| 143 |
+
cervix_model = None
|
| 144 |
|
| 145 |
# Stability buffer for video
|
| 146 |
detect_history = deque(maxlen=10)
|
|
|
|
| 183 |
"quality_percent": 0
|
| 184 |
}
|
| 185 |
|
| 186 |
+
if cervix_model is None:
|
| 187 |
+
print("β Cervix model not loaded")
|
| 188 |
+
return {
|
| 189 |
+
"detected": False,
|
| 190 |
+
"detection_confidence": 0.0,
|
| 191 |
+
"quality_score": 0.0,
|
| 192 |
+
"quality_percent": 0
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 196 |
|
| 197 |
+
try:
|
| 198 |
+
results = cervix_model.predict(
|
| 199 |
+
frame,
|
| 200 |
+
conf=conf_threshold,
|
| 201 |
+
imgsz=640,
|
| 202 |
+
verbose=False,
|
| 203 |
+
device='cpu'
|
| 204 |
+
)
|
| 205 |
+
except Exception as e:
|
| 206 |
+
print(f"β Cervix model prediction error: {e}")
|
| 207 |
+
import traceback
|
| 208 |
+
traceback.print_exc()
|
| 209 |
+
return {
|
| 210 |
+
"detected": False,
|
| 211 |
+
"detection_confidence": 0.0,
|
| 212 |
+
"quality_score": 0.0,
|
| 213 |
+
"quality_percent": 0
|
| 214 |
+
}
|
| 215 |
|
| 216 |
r = results[0]
|
| 217 |
|