The DetectionEngine is module M2 of the Anaquel Inteligente 3B pipeline. It loads a fine-tuned YOLOv8-seg model fromDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/elzackarias/Hackaton3B-Reto1/llms.txt
Use this file to discover all available pages before exploring further.
models/best3.pt, runs per-frame inference on BGR images from the camera capture module, counts visible units for each of the 7 product SKUs, and computes frame-over-frame diffs to emit DetectionEvent objects consumed by the InventoryEngine (M3).
CLASS_NAMES Mapping
The model recognises 7 product classes. Each YOLO class ID maps to an internalsku_id and a human-readable display name.
| Class ID | sku_id | Display Name |
|---|---|---|
0 | agua_burst | Agua Natural Burst 1500 ml |
1 | burst_energetica_roja | Bebida Energetica Red Burst 473 ml |
2 | burst_energy | Bebida Energetica Original Burst Energy 600 ml |
3 | nachos_naturasol | Nachos Con Sal Naturasol 200 gr |
4 | nebraska_mango | Bebida Mango-Durazno Nebraska 460 ml |
5 | sisi_cola | Refresco Cola Sin Azucar Sisi 355 ml |
6 | sun_paradise_naranja | Bebida Naranja Sun Paradise 900 ml |
detection_engine.py as CLASS_NAMES and must stay in sync with classes.txt and the dataset YAML used during training.
DetectionEngine Constructor
Path to the trained YOLOv8-seg weights file relative to the project root.
Minimum confidence threshold passed to
model.predict(). Keep low because
the anti-flicker mechanism (3-frame consistency) filters false positives
downstream.When
True, every processed frame is annotated with bounding boxes and
saved as a JPEG to backend/debug_frames/. Useful for tuning and
troubleshooting in non-production environments.Input image size passed to YOLO inference. Must match the size used during
training.
On initialisation the engine performs 3 dummy warm-up inferences on a
black 640×640 frame so that the first real inference does not incur
JIT-compilation latency:
detect(frame) Method
predict on a single BGR frame and returns a DetectionResult.
Run inference
Calls
self.model.predict(frame, conf=self.conf, imgsz=self.imgsz, verbose=False) and iterates over the returned bounding boxes.Count per-SKU detections
For every detected box, looks up the class ID in
CLASS_NAMES and increments the corresponding counts[sku_id] counter. The initial counts dict is pre-populated with 0 for all 7 SKUs.Assign stock_level
A second pass over raw detections computes
pct = count / STOCK_INITIAL (where STOCK_INITIAL = 8) and assigns a stock level string to each SlotDetection:| Condition | stock_level |
|---|---|
pct > 0.50 | "ok" |
pct > 0.25 | "warning" |
pct ≤ 0.25 | "critical" |
compare(prev, curr) Method
DetectionResult snapshots and emits DetectionEvent objects only when a count change is confirmed to be genuine. Two mechanisms guard against noise:
Anti-Flicker (Consistency Window)
Raw per-SKU count differences are appended to_diff_history[sku_id]. An event is only emitted once consistency_frames = 3 consecutive frames all carry the same-sign difference:
[-1, +1, -1]) the diff is treated as noise and no event fires. The history for that SKU is only cleared after a confirmed event or when a zero-diff frame resets it.
Per-Slot Cooldown
Even after the consistency check passes, the engine enforces a per-slot cooldown (defaultcooldown_seconds = 3.0). The _cooldown dict stores the time.time() of the last emitted event per slot_id:
Returned DetectionEvent
Each confirmed event carries:
UUID v4 string, unique per event.
EventType.RETIRO (count went down) or EventType.DEVOLUCION (count went up).Identifies the affected product.
Shelf slot (= YOLO class ID + 1, range 1–7).
Average confidence score across all detections of that SKU in the current frame.
Unit counts from the previous and current
DetectionResult.Model Training
The YOLOv8-seg model was trained usingtrain_model.py with the following configuration:
Multiple training runs are stored under
runs/anaquel_3b*/weights/. The
final production weights were copied to models/best3.pt. An intermediate
checkpoint is also available at models/best2.pt.