diff --git a/data_sources/crowd_analyzer.py b/data_sources/crowd_analyzer.py index 7dd355c..c69e3f8 100644 --- a/data_sources/crowd_analyzer.py +++ b/data_sources/crowd_analyzer.py @@ -1,7 +1,7 @@ -"""Crowd density estimator using YOLOv8 object detection. +"""Crowd density estimator using YOLOv11 object detection. Analyses webcam snapshot images from ski stations to estimate crowd levels. -Uses the YOLOv8 nano model (``yolov8n.pt``) from the ``ultralytics`` package +Uses the YOLOv11 medium model (``yolov11m.pt``) from the ``ultralytics`` package to detect people, cars, buses and trucks. Detected object counts are mapped to three crowd density levels: @@ -36,7 +36,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent CONFIG_PATH = BASE_DIR / "config" / "stations.yaml" # YOLO model weights file – downloaded automatically on first use. -YOLO_MODEL_WEIGHTS = "yolov8n.pt" +YOLO_MODEL_WEIGHTS = "yolov11m.pt" # COCO class names that count towards crowd estimation. VEHICLE_CLASSES = {"car", "bus", "truck", "motorcycle"} @@ -66,8 +66,8 @@ class CrowdAnalyzer: Path to the stations YAML configuration file. Defaults to ``config/stations.yaml`` relative to the project root. model_weights: - YOLOv8 weights identifier passed to ``ultralytics.YOLO()``. - Defaults to ``"yolov8n.pt"`` (nano model). + YOLOv11 weights identifier passed to ``ultralytics.YOLO()``. + Defaults to ``"yolov11m.pt"`` (medium model). store: SQLite store where analysis results are written. """ @@ -101,10 +101,6 @@ class CrowdAnalyzer: dict Keys: - * ``cars_detected`` – number of cars detected - * ``people_detected`` – number of people detected - * ``buses_detected`` – number of buses detected - * ``total_vehicles`` – cars + buses + trucks combined * ``crowd_density`` – ``"low"``, ``"medium"``, or ``"high"`` * ``confidence_scores`` – list of per-detection confidence values * ``timestamp`` – ISO-8601 timestamp of when analysis ran @@ -153,10 +149,6 @@ class CrowdAnalyzer: total_vehicles = cars + buses + trucks return { - "cars_detected": cars, - "people_detected": people, - "buses_detected": buses, - "total_vehicles": total_vehicles, "crowd_density": self._classify_density(cars, people), "confidence_scores": confidence_scores, "timestamp": datetime.now().isoformat(timespec="seconds"), @@ -321,10 +313,6 @@ class CrowdAnalyzer: return { "station_id": station_id, "image_path": image_path, - "cars_detected": 0, - "people_detected": 0, - "buses_detected": 0, - "total_vehicles": 0, "crowd_density": "unknown", "confidence_scores": [], "timestamp": datetime.now().isoformat(timespec="seconds"), diff --git a/pipeline/feature_engineering.py b/pipeline/feature_engineering.py index ca53174..375d6cf 100644 --- a/pipeline/feature_engineering.py +++ b/pipeline/feature_engineering.py @@ -24,6 +24,7 @@ _SPANISH_HOLIDAYS: set[tuple[int, int]] = { (1, 1), # New Year's Day (1, 6), # Epiphany (5, 1), # Labour Day + (7, 25), # Santiago Apóstol (National + Día de Galicia) (8, 15), # Assumption of Mary (10, 12), # National Day of Spain (11, 1), # All Saints' Day @@ -32,6 +33,8 @@ _SPANISH_HOLIDAYS: set[tuple[int, int]] = { (12, 25), # Christmas Day # Asturian regional (9, 8), # Day of Asturias + # Castilla y León regional + (4, 23), # Día de Castilla y León } # Approximate Good Friday dates for years 2018-2035 (moveable feast). @@ -57,6 +60,16 @@ _GOOD_FRIDAY: dict[int, tuple[int, int]] = { 2035: (3, 23), } +# Easter Monday dates for years 2018-2035 (Good Friday + 3 days). +# Regional holiday in Castilla y León and Galicia. +_EASTER_MONDAY: dict[int, tuple[int, int]] = { + 2018: (4, 2), 2019: (4, 22), 2020: (4, 13), 2021: (4, 5), + 2022: (4, 18), 2023: (4, 10), 2024: (4, 1), 2025: (4, 21), + 2026: (4, 6), 2027: (3, 29), 2028: (4, 17), 2029: (4, 2), + 2030: (4, 22), 2031: (4, 14), 2032: (3, 29), 2033: (4, 18), + 2034: (4, 10), 2035: (3, 26), +} + def _build_holiday_set(years: Optional[list[int]] = None) -> set[pd.Timestamp]: """Return a set of Timestamps for every known holiday across the given years. @@ -81,6 +94,10 @@ def _build_holiday_set(years: Optional[list[int]] = None) -> set[pd.Timestamp]: if gf: holidays.add(pd.Timestamp(year=year, month=gf[0], day=gf[1])) + em = _EASTER_MONDAY.get(year) + if em: + holidays.add(pd.Timestamp(year=year, month=em[0], day=em[1])) + return holidays diff --git a/storage/sqlite_store.py b/storage/sqlite_store.py index 3e8c0d7..8493409 100644 --- a/storage/sqlite_store.py +++ b/storage/sqlite_store.py @@ -318,24 +318,16 @@ class SQLiteStore: analysis_date, station_id, image_path, - cars_detected, - people_detected, - buses_detected, - total_vehicles, crowd_density, confidence_scores_json ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?) """, ( analyzed_at, analysis_date, result.get("station_id"), result.get("image_path"), - int(result.get("cars_detected", 0)), - int(result.get("people_detected", 0)), - int(result.get("buses_detected", 0)), - int(result.get("total_vehicles", 0)), result.get("crowd_density"), json.dumps(result.get("confidence_scores") or [], ensure_ascii=False), ), @@ -370,8 +362,7 @@ class SQLiteStore: query = f""" SELECT analyzed_at, analysis_date, station_id, image_path, - cars_detected, people_detected, buses_detected, - total_vehicles, crowd_density, confidence_scores_json + crowd_density, confidence_scores_json FROM crowd_analysis {where} ORDER BY analyzed_at ASC, id ASC @@ -645,10 +636,6 @@ class SQLiteStore: analysis_date TEXT NOT NULL, station_id TEXT NOT NULL, image_path TEXT, - cars_detected INTEGER NOT NULL, - people_detected INTEGER NOT NULL, - buses_detected INTEGER NOT NULL, - total_vehicles INTEGER NOT NULL, crowd_density TEXT NOT NULL, confidence_scores_json TEXT NOT NULL ); diff --git a/tests/test_crowd_analyzer.py b/tests/test_crowd_analyzer.py index 759a1d4..9b33eb6 100644 --- a/tests/test_crowd_analyzer.py +++ b/tests/test_crowd_analyzer.py @@ -274,31 +274,6 @@ class TestAnalyzeImage: return analyzer.analyze_image(str(img_path)) - def test_counts_cars_correctly(self, tmp_path): - """Three cars in detections must produce cars_detected=3.""" - result = self._run(tmp_path, ["car", "car", "car"], [0.9, 0.85, 0.8]) - assert result["cars_detected"] == 3 - - def test_counts_people_correctly(self, tmp_path): - """Two people detections must produce people_detected=2.""" - result = self._run(tmp_path, ["person", "person"], [0.95, 0.88]) - assert result["people_detected"] == 2 - - def test_counts_buses_correctly(self, tmp_path): - """One bus detection must produce buses_detected=1.""" - result = self._run(tmp_path, ["bus"], [0.7]) - assert result["buses_detected"] == 1 - - def test_total_vehicles_excludes_people(self, tmp_path): - """total_vehicles must count cars + buses + trucks, not people.""" - result = self._run( - tmp_path, - ["car", "person", "bus", "truck"], - [0.9, 0.9, 0.8, 0.75], - ) - assert result["total_vehicles"] == 3 # car + bus + truck - assert result["people_detected"] == 1 - def test_confidence_scores_are_included(self, tmp_path): """All detection confidence scores must be present in the result.""" result = self._run(tmp_path, ["car", "person"], [0.91, 0.82]) @@ -308,15 +283,7 @@ class TestAnalyzeImage: def test_result_has_required_keys(self, tmp_path): """analyze_image must return all documented keys.""" result = self._run(tmp_path, [], []) - required = { - "cars_detected", - "people_detected", - "buses_detected", - "total_vehicles", - "crowd_density", - "confidence_scores", - "timestamp", - } + required = {"crowd_density", "confidence_scores", "timestamp"} assert required.issubset(result.keys()) def test_crowd_density_is_valid_string(self, tmp_path): @@ -455,10 +422,6 @@ class TestAnalyzeAllStations: return { "station_id": station_id, "crowd_density": "low", - "cars_detected": 0, - "people_detected": 0, - "buses_detected": 0, - "total_vehicles": 0, "confidence_scores": [], "timestamp": "2026-01-01T12:00:00", "image_path": None, diff --git a/tests/test_sqlite_store.py b/tests/test_sqlite_store.py index 15f0eb6..8c39eac 100644 --- a/tests/test_sqlite_store.py +++ b/tests/test_sqlite_store.py @@ -107,10 +107,6 @@ def test_crowd_analysis_roundtrip(tmp_path): "timestamp": "2026-03-13T11:00:00", "station_id": "sanisidro", "image_path": "/tmp/frame.jpg", - "cars_detected": 5, - "people_detected": 2, - "buses_detected": 0, - "total_vehicles": 5, "crowd_density": "low", "confidence_scores": [0.91, 0.88], } @@ -120,10 +116,6 @@ def test_crowd_analysis_roundtrip(tmp_path): "timestamp": "2026-03-13T12:00:00", "station_id": "sanisidro", "image_path": "/tmp/frame2.jpg", - "cars_detected": 25, - "people_detected": 4, - "buses_detected": 1, - "total_vehicles": 26, "crowd_density": "medium", "confidence_scores": [0.77], }