Commit Β·
998879d
1
Parent(s): ef550be
Patch Segment head and downgrade to ultralytics 7.3.23
Browse files- backend/inference.py +65 -64
- backend/requirements.txt +1 -1
backend/inference.py
CHANGED
|
@@ -4,17 +4,31 @@
|
|
| 4 |
import os
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
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 |
try:
|
|
|
|
| 12 |
aw_model = YOLO(AW_MODEL_PATH)
|
| 13 |
-
aw_model.to('cpu')
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
print(f"β Error loading Acetowhite model: {e}")
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
# CONFIGURABLE PARAMETERS
|
| 20 |
MIN_AREA = 150 # minimum contour area (px)
|
|
@@ -35,7 +49,7 @@ def infer_aw_contour(frame, conf_threshold=DEFAULT_CONF):
|
|
| 35 |
}
|
| 36 |
|
| 37 |
if aw_model is None:
|
| 38 |
-
print("β Acetowhite model not
|
| 39 |
return {
|
| 40 |
"overlay": None,
|
| 41 |
"contours": [],
|
|
@@ -44,6 +58,10 @@ def infer_aw_contour(frame, conf_threshold=DEFAULT_CONF):
|
|
| 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(
|
|
@@ -52,72 +70,55 @@ def infer_aw_contour(frame, conf_threshold=DEFAULT_CONF):
|
|
| 52 |
imgsz=IMG_SIZE,
|
| 53 |
verbose=False,
|
| 54 |
device='cpu'
|
| 55 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 62 |
-
|
| 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,
|
| 121 |
"contours": contours_list,
|
| 122 |
"detections": detection_count,
|
| 123 |
"frame_width": frame.shape[1],
|
|
|
|
| 4 |
import os
|
| 5 |
import cv2
|
| 6 |
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
from ultralytics import YOLO
|
| 9 |
|
| 10 |
# MODEL LOAD (Safe Backend Path)
|
| 11 |
AW_MODEL_PATH = os.path.join(os.path.dirname(__file__), "models", "AW_yolo.pt")
|
| 12 |
+
aw_model = None
|
| 13 |
+
|
| 14 |
try:
|
| 15 |
+
print(f"π Loading Acetowhite model from: {AW_MODEL_PATH}")
|
| 16 |
aw_model = YOLO(AW_MODEL_PATH)
|
| 17 |
+
aw_model.to('cpu')
|
| 18 |
+
|
| 19 |
+
# Patch the Segment head to prevent the detect() error
|
| 20 |
+
if hasattr(aw_model.model, 'model'):
|
| 21 |
+
for module in aw_model.model.modules():
|
| 22 |
+
if module.__class__.__name__ == 'Segment':
|
| 23 |
+
print("β οΈ Patching Segment head to prevent detect() error")
|
| 24 |
+
# Disable the problematic detect call
|
| 25 |
+
module.detect = lambda self, x: x
|
| 26 |
+
|
| 27 |
+
print("β
Acetowhite model loaded successfully")
|
| 28 |
except Exception as e:
|
| 29 |
print(f"β Error loading Acetowhite model: {e}")
|
| 30 |
+
import traceback
|
| 31 |
+
traceback.print_exc()
|
| 32 |
|
| 33 |
# CONFIGURABLE PARAMETERS
|
| 34 |
MIN_AREA = 150 # minimum contour area (px)
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
if aw_model is None:
|
| 52 |
+
print("β Acetowhite model not available")
|
| 53 |
return {
|
| 54 |
"overlay": None,
|
| 55 |
"contours": [],
|
|
|
|
| 58 |
"frame_height": frame.shape[0]
|
| 59 |
}
|
| 60 |
|
| 61 |
+
overlay = frame.copy()
|
| 62 |
+
contours_list = []
|
| 63 |
+
detection_count = 0
|
| 64 |
+
|
| 65 |
try:
|
| 66 |
print(f"π Running YOLO prediction on frame shape: {frame.shape}")
|
| 67 |
results = aw_model.predict(
|
|
|
|
| 70 |
imgsz=IMG_SIZE,
|
| 71 |
verbose=False,
|
| 72 |
device='cpu'
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# Handle both list and single result
|
| 76 |
+
if isinstance(results, (list, tuple)):
|
| 77 |
+
result = results[0]
|
| 78 |
+
else:
|
| 79 |
+
result = results
|
| 80 |
+
|
| 81 |
print(f"β
YOLO prediction complete")
|
| 82 |
+
|
| 83 |
+
# Try to extract masks if available
|
| 84 |
+
if hasattr(result, 'masks') and result.masks is not None:
|
| 85 |
+
try:
|
| 86 |
+
masks = result.masks.xy
|
| 87 |
+
if len(masks) > 0:
|
| 88 |
+
print(f"β
Found {len(masks)} masks")
|
| 89 |
+
for idx, polygon in enumerate(masks):
|
| 90 |
+
confidence = float(result.boxes.conf[idx])
|
| 91 |
+
if confidence < conf_threshold:
|
| 92 |
+
continue
|
| 93 |
+
|
| 94 |
+
contour = polygon.astype(np.int32)
|
| 95 |
+
area = cv2.contourArea(contour)
|
| 96 |
+
|
| 97 |
+
if area < MIN_AREA:
|
| 98 |
+
continue
|
| 99 |
+
|
| 100 |
+
epsilon = SMOOTHING_EPSILON * cv2.arcLength(contour, True)
|
| 101 |
+
contour = cv2.approxPolyDP(contour, epsilon, True)
|
| 102 |
+
|
| 103 |
+
cv2.polylines(overlay, [contour], isClosed=True, color=(0, 255, 0), thickness=2)
|
| 104 |
+
|
| 105 |
+
contours_list.append({
|
| 106 |
+
"points": contour.tolist(),
|
| 107 |
+
"area": float(area),
|
| 108 |
+
"confidence": round(confidence, 3)
|
| 109 |
+
})
|
| 110 |
+
detection_count += 1
|
| 111 |
+
except Exception as mask_err:
|
| 112 |
+
print(f"β οΈ Could not extract masks: {mask_err}")
|
| 113 |
+
|
| 114 |
except Exception as e:
|
| 115 |
print(f"β YOLO prediction error: {e}")
|
| 116 |
import traceback
|
| 117 |
traceback.print_exc()
|
| 118 |
+
# Continue with empty results rather than crashing
|
| 119 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
return {
|
| 121 |
+
"overlay": overlay if detection_count > 0 else None,
|
| 122 |
"contours": contours_list,
|
| 123 |
"detections": detection_count,
|
| 124 |
"frame_width": frame.shape[1],
|
backend/requirements.txt
CHANGED
|
@@ -4,7 +4,7 @@ opencv-python==4.9.0.80
|
|
| 4 |
numpy==1.24.3
|
| 5 |
torch==2.2.0
|
| 6 |
torchvision==0.17.0
|
| 7 |
-
ultralytics==
|
| 8 |
pillow==10.2.0
|
| 9 |
python-multipart==0.0.6
|
| 10 |
setuptools>=69.0.0
|
|
|
|
| 4 |
numpy==1.24.3
|
| 5 |
torch==2.2.0
|
| 6 |
torchvision==0.17.0
|
| 7 |
+
ultralytics==7.3.23
|
| 8 |
pillow==10.2.0
|
| 9 |
python-multipart==0.0.6
|
| 10 |
setuptools>=69.0.0
|