foodrecognitionapi / test_improvements.py
har1zarD
test
8c64cb8
#!/usr/bin/env python3
"""
Test script to validate the food recognition improvements.
Tests the smart override system and validation rules.
"""
import sys
import os
sys.path.append(os.path.dirname(__file__))
# Import the constants from the main app
from app import (
SMART_FOOD_OVERRIDES,
FOOD_MODELS,
MIN_CONFIDENCE_THRESHOLD,
MIN_ALTERNATIVE_CONFIDENCE,
COMPREHENSIVE_FOOD_CATEGORIES,
BALKAN_TO_FOOD101_MAPPING
)
def test_smart_overrides():
"""Test the smart override system."""
print("πŸ§ͺ Testing Smart Override System")
print("=" * 50)
# Test 1: Fried Food overrides
if "Fried Food" in SMART_FOOD_OVERRIDES:
fried_overrides = SMART_FOOD_OVERRIDES["Fried Food"]
print(f"βœ… Fried Food overrides: {len(fried_overrides)} items")
# Check key items
if "fish_and_chips" in fried_overrides:
print(f" 🐟 Fish and Chips: {fried_overrides['fish_and_chips']}")
if "pancakes" in fried_overrides:
print(f" πŸ₯ž Pancakes: {fried_overrides['pancakes']}")
# Test 2: Model priority changes
print(f"\nπŸ“Š Model Priorities:")
for model_key, config in FOOD_MODELS.items():
priority = config["priority"]
desc = config["description"]
print(f" {priority}. {model_key}: {desc}")
print(f"\nβš™οΈ Confidence Thresholds:")
print(f" Minimum: {MIN_CONFIDENCE_THRESHOLD:.1%}")
print(f" Alternative: {MIN_ALTERNATIVE_CONFIDENCE:.1%}")
# Test model count
active_models = len(FOOD_MODELS)
print(f"\n🎯 Active Models: {active_models}")
if active_models == 1:
print(" βœ… PERFECT: Only Food-101 enabled (no generic models)")
else:
print(" ⚠️ WARNING: Multiple models active")
return True
def test_food_categories():
"""Test comprehensive food categories."""
print(f"\n🍽️ Food Categories: {len(COMPREHENSIVE_FOOD_CATEGORIES)} items")
# Test key foods
key_foods = ["pancakes", "fish_and_chips", "american_pancakes", "waffles"]
for food in key_foods:
if food in COMPREHENSIVE_FOOD_CATEGORIES:
print(f" βœ… {food} - Found")
else:
print(f" ❌ {food} - Missing")
# Test Balkan mapping
print(f"\nπŸ‡§πŸ‡¦ Balkan Food Mapping:")
for balkan_food, food101_equiv in BALKAN_TO_FOOD101_MAPPING.items():
print(f" {balkan_food} β†’ {food101_equiv}")
def test_validation_rules():
"""Test validation rules logic."""
print(f"\nπŸ” Testing Validation Rules")
# Simulate predictions that should be caught
test_cases = [
{"label": "pancakes dessert", "expected": "FAIL", "reason": "Breakfast as dessert"},
{"label": "fish_and_chips", "expected": "PASS", "reason": "Correct classification"},
{"label": "american pancakes", "expected": "PASS", "reason": "Correct breakfast"},
{"label": "fried food", "expected": "PENALTY", "reason": "Too generic"}
]
for case in test_cases:
label = case["label"].lower().replace("_", " ")
# Rule 1: Breakfast items classified as dessert
is_breakfast_dessert = (
any(term in label for term in ['pancake', 'waffle', 'french_toast']) and
any(term in label for term in ['dessert', 'cake', 'sweet'])
)
# Rule 2: Generic high confidence items
is_generic = label in ['food', 'meal', 'dish', 'fried food', 'dessert']
if is_breakfast_dessert:
result = "FAIL (Breakfast as dessert)"
elif is_generic:
result = "PENALTY (Too generic)"
else:
result = "PASS"
status = "βœ…" if case["expected"] in result else "❌"
print(f" {status} {case['label']}: {result}")
if __name__ == "__main__":
print("πŸš€ Testing Food Recognition Improvements")
print("=" * 60)
try:
test_smart_overrides()
test_food_categories()
test_validation_rules()
print("\n" + "=" * 60)
print("βœ… ALL TESTS COMPLETED - Model improvements validated!")
print("\nπŸ“ Key Improvements:")
print(" β€’ Food-101 model prioritized for specific dishes")
print(" β€’ Smart overrides for fried food β†’ specific dishes")
print(" β€’ Validation rules prevent breakfast β†’ dessert mistakes")
print(" β€’ Enhanced confidence boosting for target foods")
print(" β€’ Stricter confidence thresholds for better quality")
except Exception as e:
print(f"❌ Test failed: {e}")
sys.exit(1)