Spaces:
Paused
Paused
fixed new model weights loading
Browse files- perception_roi_server.py +20 -1
perception_roi_server.py
CHANGED
|
@@ -34,6 +34,7 @@ from fastapi.responses import FileResponse, StreamingResponse, JSONResponse
|
|
| 34 |
from ultralytics import YOLO
|
| 35 |
|
| 36 |
DEFAULT_WEIGHTS = os.environ.get("YOLO_WEIGHTS", "yolov8s.pt")
|
|
|
|
| 37 |
DEFAULT_CONF = float(os.environ.get("YOLO_CONF", "0.25"))
|
| 38 |
DEFAULT_DEVICE = os.environ.get("YOLO_DEVICE", "auto")
|
| 39 |
FAST_DETECT_SCALE = float(os.environ.get("FAST_DETECT_SCALE", "0.65"))
|
|
@@ -58,8 +59,26 @@ def root():
|
|
| 58 |
_model_lock = threading.Lock()
|
| 59 |
_models: Dict[str, YOLO] = {}
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def get_model(weights: str) -> YOLO:
|
| 62 |
-
key =
|
| 63 |
with _model_lock:
|
| 64 |
if key not in _models:
|
| 65 |
_models[key] = YOLO(key)
|
|
|
|
| 34 |
from ultralytics import YOLO
|
| 35 |
|
| 36 |
DEFAULT_WEIGHTS = os.environ.get("YOLO_WEIGHTS", "yolov8s.pt")
|
| 37 |
+
WEIGHTS_DIR = os.environ.get("WEIGHTS_DIR", "")
|
| 38 |
DEFAULT_CONF = float(os.environ.get("YOLO_CONF", "0.25"))
|
| 39 |
DEFAULT_DEVICE = os.environ.get("YOLO_DEVICE", "auto")
|
| 40 |
FAST_DETECT_SCALE = float(os.environ.get("FAST_DETECT_SCALE", "0.65"))
|
|
|
|
| 59 |
_model_lock = threading.Lock()
|
| 60 |
_models: Dict[str, YOLO] = {}
|
| 61 |
|
| 62 |
+
def _resolve_weights_path(weights: str) -> str:
|
| 63 |
+
if not weights:
|
| 64 |
+
return DEFAULT_WEIGHTS
|
| 65 |
+
w = str(weights)
|
| 66 |
+
if os.path.isabs(w) or os.path.exists(w):
|
| 67 |
+
return w
|
| 68 |
+
# Try configured weights dir first
|
| 69 |
+
if WEIGHTS_DIR:
|
| 70 |
+
cand = os.path.join(WEIGHTS_DIR, w)
|
| 71 |
+
if os.path.exists(cand):
|
| 72 |
+
return cand
|
| 73 |
+
# Try common local directories
|
| 74 |
+
for base in (os.getcwd(), os.path.dirname(__file__), DATA_DIR, "/app", "/workspace", "/data"):
|
| 75 |
+
cand = os.path.join(base, w)
|
| 76 |
+
if os.path.exists(cand):
|
| 77 |
+
return cand
|
| 78 |
+
return w
|
| 79 |
+
|
| 80 |
def get_model(weights: str) -> YOLO:
|
| 81 |
+
key = _resolve_weights_path(weights or DEFAULT_WEIGHTS)
|
| 82 |
with _model_lock:
|
| 83 |
if key not in _models:
|
| 84 |
_models[key] = YOLO(key)
|