diff --git a/server.py b/server.py index c195ee8..57caed9 100644 --- a/server.py +++ b/server.py @@ -27,6 +27,8 @@ CLASS_NAMES = ['closed', 'open'] # From training, sorted alphabetically POLL_INTERVAL_SECONDS = 10 REQUEST_TIMEOUT_SECONDS = 5 UNSURE_CONFIDENCE_THRESHOLD = 0.97 +PREDICTION_HISTORY = [] +PREDICTION_HISTORY_MAX_LENGTH = 3 # --- Model Inference --- def get_prediction(model, image_bytes, device): @@ -74,6 +76,16 @@ async def monitor_garage_door(app): prediction, confidence = result logging.debug(f"Garage door status: {prediction} (confidence: {confidence:.4f})") + # Update prediction history + if confidence >= UNSURE_CONFIDENCE_THRESHOLD: + PREDICTION_HISTORY.append(prediction) + else: + PREDICTION_HISTORY.append('unknown') + + # Trim history if it's too long + if len(PREDICTION_HISTORY) > PREDICTION_HISTORY_MAX_LENGTH: + PREDICTION_HISTORY.pop(0) + if confidence < UNSURE_CONFIDENCE_THRESHOLD: # Sanitize timestamp for use in filename timestamp = datetime.now().isoformat().replace(':', '-')